在不同设备上实现屏幕共享

阿里云基于ARTC SDK在不同设备(Windows、iOSAndroid)上实现了屏幕共享的相关功能,本篇文档详细介绍了如何使用ARTC SDK通过少量的代码实现屏幕共享。

方案概览

主播在视频直播时可能会有屏幕共享的需求,阿里云ARTC SDK提供了屏幕分享的最佳实践,开发者可以调用ARTC SDK的相关API实现屏幕共享。您可以通过少量的代码实现屏幕分享,现在ARTC SDK支持Windows、iOSAndroid设备。

接下来我们通过Windows、iOSAndroid的顺序详细的介绍如何使用ARTC SDK快速实现屏幕共享。

一、Windows

1.屏幕共享

1.1 打开屏幕共享

/*
* 共享屏幕局部
*/
/*
* 共享屏幕局部
*/
      RECT rc;
      ::GetWindowRect(::GetDesktopWindow(), &rc);

      AliEngineScreenShareRegion screenRegion;
      screenRegion.originX = (float)rc.left;
      screenRegion.originY = (float)rc.top;
      screenRegion.width = (float)(rc.right - rc.left);
      screenRegion.height = (float)(rc.bottom - rc.top);

      AliEngineScreenShareConfig config;

      config.isShareByRegion = false;
      config.shareRegion = screenRegion;
      
      mpEngine->StartScreenShareByScreenRegion(screenRegion, config);

1.2 停止屏幕共享

mpEngine->StopScreenShare();

2.多窗口共享

2.1 枚举所有可共享的窗口或者桌面

mScreenShareType = AliEngineScreenShareDesktop ; 
mScreenShareType = AliEngineScreenShareWindow ; 

/*
 mSourceId 上次选中的id,下面代码是填充一个comboBox列表
*/

mCurrentSourceList = mEngine->GetScreenShareSourceInfo(mScreenShareType);
  if (nullptr != mCurrentSourceList)
  {
      for (size_t i = 0; i < mCurrentSourceList->GetCount(); i++) {
        AliEngineScreenSourcInfo info = mCurrentSourceList->GetSourceInfo(i);

        /*
        * 如果不希望共享APP自身窗口,可以通过sourceIsSelf来区分
        */
        info.sourceIsSelf 
        CString s = AliStringToCString(info.sourceName);
        lb->AddString(s);
        if (mSourceId == info.sourceId) {
          lb->SetCurSel(i);
        }
      }
      
      /*
      * 默认选择第一个窗口或者桌面,记录ID和窗口名
      */
      if (mSourceId.isEmpty() && lb->GetCount() > 0)
      {
        lb->SetCurSel(0);
        AliEngineScreenSourcInfo info = mCurrentSourceList->GetSourceInfo(0);
        mSourceId = info.sourceId;
        mSourceTitle = info.sourceName;
      }
  }

2.2 共享对应的窗口或者桌面

msSourceId是共享桌面的ID,可以通过上文的枚举接口获取;
screenShareSource是配置;

AliEngineScreenShareRegion  shareRegion ;
shareRegion.originX = 0.f;
shareRegion.originY = 0.f;
shareRegion.width = 640.f;
shareRegion.height = 480.f;
            
screenShareSource.isShareByRegion = true ;
screenShareSource.shareRegion =  shareRegion;

/*
* 共享桌面
*/
mpEngine->StartScreenShareByDesktopId(atol(msSourceId.c_str()), screenShareSource);

/*
* 共享窗口
*/
mpEngine->StartScreenShareByWindowId(atol(msSourceId.c_str()), screenShareSource);

/*
* 可以显示获取共享窗口的显示范围
*/
AliEngineScreenShareRegion region;
		mEngine->GetDesktopRegion(mSourceId, mSourceTitle, region);
		CString resolutionMsg;
		resolutionMsg.Format(_T("Resolution: %d x %d"), region.width, region.height);
		((CStatic*)GetDlgItem(IDC_STATIC_SCREEN_SOURCE_RESOLUTION))->SetWindowTextW(resolutionMsg);
		((CEdit*)GetDlgItem(IDC_EDIT_SHARE_REGION_X))->SetWindowTextW(std::to_wstring(mScreenShareSource.shareRegion.originX).c_str());
		((CEdit*)GetDlgItem(IDC_EDIT_SHARE_REGION_Y))->SetWindowTextW(std::to_wstring(mScreenShareSource.shareRegion.originY).c_str());
		((CEdit*)GetDlgItem(IDC_EDIT_SHARE_REGION_X2))->SetWindowTextW(std::to_wstring(mScreenShareSource.shareRegion.width).c_str());
		((CEdit*)GetDlgItem(IDC_EDIT_SHARE_REGION_Y2))->SetWindowTextW(std::to_wstring(mScreenShareSource.shareRegion.height).c_str());
	}
  

2.3 停止共享对应的窗口或者桌面

mpEngine->StopScreenShare();

二、iOS

1.接入方式

  1. iOS接入屏幕共享需要在原宿主 App 的基础上新建 App Extension, 并在 App Extension 中依赖 AliScreenShare.framework。

  2. 在宿主 APP 当中 embed 新建的 App Extension, 并且设置相同的 App Groups, 如果在原宿主 App 当中没有设置 App Groups, 需要在宿主 App 的 profile 当中新建 App Groups 选项。

  3. 宿主 App 和 App Extension 需要保持相同的 Bundle Id 前缀, 并使用同样的开发证书进行签名。

2.共享窗口/系统音频

    AliRtcScreenShareMode mode = AliRtcScreenShareNone;

 /*
 * 根据需要选择性设置是否共享音频
 */
    if (ctrl.shareVideo) {
      mode |= AliRtcScreenShareVideo;
    }
    
    if (ctrl.shareAudioApp) {
      mode |= AliRtcScreenShareAudioApp;
    }
    
    [strongSelf.engine startScreenShare:kAppGroup mode:mode];
    [strongSelf.engine setAudioShareAppVolume:ctrl.appPublishVolume];

3.停止共享窗口和音频

[strongSelf.engine stopScreenShare];

4.改变设置

/* 值改变,调用相关接口 */
if (ctrl.enablePublishScreenTrackConfig) {
   [strongSelf.engine setPublishVideoStreamAttribute:AliRtcVideoTrackScreen attr:@"range=mpu"];
} else {
    [strongSelf.engine setPublishVideoStreamAttribute:AliRtcVideoTrackScreen attr:@""];
}

/* 停止推流,才能重新repub屏幕流 */
[strongSelf.engine stopScreenShare];

AliRtcScreenShareMode mode = AliRtcScreenShareNone;

if (ctrl.shareVideo) {
   mode |= AliRtcScreenShareVideo;
}

if (ctrl.shareAudioApp) {
   mode |= AliRtcScreenShareAudioApp;
}

[strongSelf.engine startScreenShare:kAppGroup mode:mode];
[strongSelf.engine setAudioShareAppVolume:ctrl.appPublishVolume];
            

三、Android

1.共享窗口/系统音频

if (config.permitScreen && config.permitAudioShare) {
    mScreenShareMode = AliRtcEngine.AliRtcScreenShareMode.AliRtcScreenShareAllMode;
} else if (config.permitScreen) {
    mScreenShareMode = AliRtcEngine.AliRtcScreenShareMode.AliRtcScreenShareOnlyVideoMode;
} else if (config.permitAudioShare) {
    mScreenShareMode = AliRtcEngine.AliRtcScreenShareMode.AliRtcScreenShareOnlyAudioMode;
}
      
mAliRtcEngine.startScreenShare(null, mScreenShareMode);

2.停止共享窗口和音频

pubScreenCaptureResult = mAliRtcEngine.stopScreenShare();