All Products
Search
Document Center

ApsaraVideo Live:Implement screen sharing on different devices

Last Updated:Feb 06, 2025

Alibaba Cloud implements screen sharing on different devices that run Windows, iOS, and Android based on ApsaraVideo Real-time Communication (ARTC) SDK. This topic describes how to use ARTC SDK to implement screen sharing with a small amount of code.

Solution overview

In some cases, streamers may want to share their screens during live streaming. ARTC SDK provides best practices for screen sharing. Developers can call related methods of ARTC SDK to implement screen sharing. This requires only a small amount of code. ARTC SDK supports Windows, iOS, and Android devices.

The following sections describe how to use ARTC SDK to implement screen sharing on Windows, iOS, and Android.

I. Windows

1. Screen sharing

1.1 Start screen sharing

/*
* Share a part of the screen.
*/
/*
* Share a part of the screen.
*/
      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 Stop screen sharing

mpEngine->StopScreenShare();

2. Multi-window sharing

2.1 Enumerate all shareable windows or desktops

mScreenShareType = AliEngineScreenShareDesktop ; 
mScreenShareType = AliEngineScreenShareWindow ; 

/*
 mSourceId The ID that was previously selected. The following code populates a ComboBox list.
*/

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

        /*
        * If you do not want to share the window of the application itself, use sourceIsSelf to distinguish it from others.
        */
        info.sourceIsSelf 
        CString s = AliStringToCString(info.sourceName);
        lb->AddString(s);
        if (mSourceId == info.sourceId) {
          lb->SetCurSel(i);
        }
      }
      
      /*
      * By default, the first window or desktop is selected. The ID and name of the window or desktop are recorded.
      */
      if (mSourceId.isEmpty() && lb->GetCount() > 0)
      {
        lb->SetCurSel(0);
        AliEngineScreenSourcInfo info = mCurrentSourceList->GetSourceInfo(0);
        mSourceId = info.sourceId;
        mSourceTitle = info.sourceName;
      }
  }

2.2 Share a corresponding window or desktop

msSourceId is the ID of the shared window or desktop, which can be obtained from the preceding enumeration method.
screenShareSource indicates the configuration.

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

/*
* Share a desktop.
*/
mpEngine->StartScreenShareByDesktopId(atol(msSourceId.c_str()), screenShareSource);

/*
* Share a window.
*/
mpEngine->StartScreenShareByWindowId(atol(msSourceId.c_str()), screenShareSource);

/*
* Obtain the display scope of the shared window or desktop.
*/
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 Stop sharing a corresponding window or desktop

mpEngine->StopScreenShare();

II. iOS

1. Access method

  1. To implement screen sharing on iOS, you must create an App Extension based on the original host app and use AliScreenShare.framework in the App Extension.

  2. Embed the newly created App Extension in the host app and configure the same App Groups for the App Extension. If no App Groups are configured in the host app, add the App Groups capability in the profile of the host app.

  3. The host app and App Extension must have the same prefix for their bundle IDs and be signed with the same development certificate.

2. Share a window or audio

    AliRtcScreenShareMode mode = AliRtcScreenShareNone;

 /*
 * Specify whether to share the audio based on your business requirements.
 */
    if (ctrl.shareVideo) {
      mode |= AliRtcScreenShareVideo;
    }
    
    if (ctrl.shareAudioApp) {
      mode |= AliRtcScreenShareAudioApp;
    }
    
    [strongSelf.engine startScreenShare:kAppGroup mode:mode];
    [strongSelf.engine setAudioShareAppVolume:ctrl.appPublishVolume];

3. Stop sharing a window or audio

[strongSelf.engine stopScreenShare];

4. Change settings

/* Call relevant methods and change the parameter values. */
if (ctrl.enablePublishScreenTrackConfig) {
   [strongSelf.engine setPublishVideoStreamAttribute:AliRtcVideoTrackScreen attr:@"range=mpu"];
} else {
    [strongSelf.engine setPublishVideoStreamAttribute:AliRtcVideoTrackScreen attr:@""];
}

/* Republish the screen track. You can perform this operation only after you stop stream ingest. */
[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];
            

III. Android

1. Share a window or audio

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. Stop sharing a window or audio

pubScreenCaptureResult = mAliRtcEngine.stopScreenShare();