更新时间:2024-07-30 GMT+08:00

屏幕共享集成

屏幕共享:共享自己的手机屏幕的实时界面给其它用户,更加高效的与其它用户沟通。

共享Bundle Id相关说明

ScreenShareExtension插件bundle Id格式:

主App bundle Id + ScreenShareExtension

ScreenShareExtensionSetupUI插件bundle Id格式:

主App bundle Id + ScreenShareExtensionSetupUI

例:
主App bundle Id:com.xx.xx
ScreenShareExtension插件bundIe Id:com.xx.xx.ScreenShareExtension
ScreenShareExtensionSetupUI插件bundIe Id:com.xx.xx.ScreenShareExtensionSetupUI

iOS端屏幕共享需要添加iOS Extension并实现相关代码,以下步骤用于说明如何集成SDK进行屏幕共享Extension的开发。

由于相关特性对系统能力的依赖,因此需要iOS12及以上版本才能支持屏幕共享功能。详情信息参见“官方文档”。

  1. 创建新的Extension Target,选择File->New->Target->Broadcast Upload Extension,创建Extension Target。

    图1 添加选择Target
    图2 选择Broadcast Upload Extension,点击“Next”创建
    图3 添加Broadcast Upload Extension后效果(ScreenShareExtension是创建时的名称)

  2. 设置Extension的支持系统版本号12.0。

    图4 Extension设置
    图5 ExtensionUI设置

  3. 给主工程和Extension添加App Groups,并设置为在开发准备中申请到的groupId。

    图6 默认没有App Groups
    图7 添加App Groups,选择TARGET->Signing&Capabilities->Capability
    图8 选中App Groups,

    按照上面两个图片,同理分别为ScreenShareExtension和ScreenShareExtensionSetupUI添加App Groups

    图9 App Groups添加完成效果

  4. 分别给entitlements文件中添加App Groups ID(申请地址 此groupId在初始化的时候也会用到)

    图10 应用程序添加ID效果
    图11 ScreenShareExtension添加ID效果
    图12 ScreenShareExtensionSetupUI添加ID效果

  5. 给ScreenShareExtension 添加HWMExtension.framework,添加配置TARGETS—>ScreenShareExtension—>Build Settings->Other Linker Flags 中添加-lc++

    图13 选择ScreenShareExtension->Add Files to
    图14 选择HWMExtension.framework添加到Extension
    图15 添加完成的效果

  6. 配置屏幕共享代码,Extensions 的SampleHandler.m文件中添加代码如下。

    图16 SampleHandler文件位置
    文件内容修改groupId后可以直接使用。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    #import "SampleHandler.h"
    #import <HWMExtension/HWMExtension.h>
     
     @interface SampleHandler()<ScreenShareManagerDelegate>
     @property (strong, nonatomic) HWMExensionDataManager *screenShareManager;
     @end
     
     @implementation SampleHandler
     
     - (instancetype)init {
         if (self = [super init]) {
    //         [HWMExtensionAppGroup setAppGroup:@"group.com.huawei.xxx"];// 这里替换成自己申请的appgroup
            self.screenShareManager = [[HWMExensionDataManager alloc] init];
            self.screenShareManager.delegate = self;
         }
         return self;
     }
     
    - (void)dealloc {
        self.screenShareManager = nil;
    }
     
    - (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
        // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
        [self.screenShareManager broadcastStartedWithSetupInfo:setupInfo];
    }
     
    - (void)broadcastPaused {
        // User has requested to pause the broadcast. Samples will stop being delivered.
        [self.screenShareManager broadcastPaused];
    }
     
    - (void)broadcastResumed {
        // User has requested to resume the broadcast. Samples delivery will resume.
        [self.screenShareManager broadcastResumed];
    }
     
    - (void)broadcastFinished {
        // User has requested to finish the broadcast.
        [self.screenShareManager broadcastFinished];
    }
     
    - (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
        switch (sampleBufferType) {
            case RPSampleBufferTypeVideo:
                // Handle video sample buffer
                [self.screenShareManager processSampleBuffer:sampleBuffer withType:RPSampleBufferTypeVideo];  
                break;
            case RPSampleBufferTypeAudioApp:
                // Handle audio sample buffer for app audio
                // 音频共享相关
                if ([self currentIsAudioShare]) {
                    [self.screenShareManager processSampleBuffer:sampleBuffer withType:RPSampleBufferTypeAudioApp];
                }
                break;
            case RPSampleBufferTypeAudioMic:
                // Handle audio sample buffer for mic audio
                break;
                
            default:
                break;
        }
    }
     
    - (void)screenShareManagerFinishBroadcastWithError:(NSError *)error {
        [self.screenShareManager broadcastFinished];
        [self finishBroadcastWithError: error];
    }
    - (BOOL)currentIsAudioShare {
        NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:[HWMExtensionAppGroup getAppGroup]];
        NSString *audioShareType = [userDefault objectForKey:kHWMExtensionAudioShareKey];
        if ([audioShareType isEqualToString:kHWMExtensionAudioShareValue]) {
            return YES;
        }
        return NO;
    }
     @end