Screen sharing enables a host of an interactive live streaming broadcast or a user in a video call to display what is on their screen to other users in the channel.
This technology has many advantages for communicating information such as the following:
macOS assigns a unique Display ID (CGDirectDisplayID
) for each screen and a unique Window ID (CGWindowID
) for each window. Call Apple native APIs and Agora APIs to share the specified window or screen. The following diagram shows the data transfer of screen sharing on macOS:
Before proceeding, ensure that you have implemented the basic real-time engagement function. See Start a Call or Start Interactive Live Streaming.
This section shows you how to implement screen sharing. Add the following sample code to the ViewController.swift
file.
Call the Apple native method and get the Display ID of the screen to be shared or the Window ID of the window to be shared.
// swift
// Get Display ID
let screenId = screen.deviceDescription[NSDeviceDescriptionKey(rawValue: "NSScreenNumber")]
// Get Window ID
let windowDicCFArray = CGWindowListCopyWindowInfo([.optionAll, .excludeDesktopElements], 0)
If you need to share the specified screen, add the following code to your project:
// swift
let resolution = self.selectedResolution
let fps = self.selectedFps
let screen = selectedScreen
let params = AgoraScreenCaptureParameters()
params.dimensions = resolution.size()
params.frameRate = fps
params.bitrate = 1000
agoraKit.startScreenCapture(byDisplayId: UInt32(screen.id), regionRect: .zero, captureParams: params)
If you need to share the specified window, add the following code to your project:
// swift
let resolution = self.selectedResolution
let fps = self.selectedFps
let window = selectedWindow
let params = AgoraScreenCaptureParameters()
params.dimensions = resolution.size()
params.frameRate = fps
params.bitrate = 1000
agoraKit.startScreenCapture(byWindowId: UInt32(window.id), regionRect: .zero, captureParams: params)
If you only need to publish the screen-sharing stream, add the following code to your project:
// swift
option.publishCameraTrack = .of(false)
option.publishScreenTrack = .of(true)
agoraKit.joinChannel(byToken: KeyCenter.Token, channelId: channel, uid: 0, mediaOptions: option)
If you need to publish the screen-sharing stream and the video stream captured by the local camera, add the following code to your project:
// swift
// Join the channel and publish the video stream captured by the local camera
option.publishCameraTrack = .of(true)
option.publishScreenTrack = .of(false)
agoraKit.joinChannel(byToken: KeyCenter.Token, channelId: channel, uid: 0, mediaOptions: option)
// Join the same channel and publish the screen-sharing stream
let connection2 = AgoraRtcConnection()
connection2.channelId = channel
connection2.localUid = 0
let mediaOptions = AgoraRtcChannelMediaOptions()
mediaOptions.publishCameraTrack = .of(false)
mediaOptions.publishScreenTrack = .of(true)
agoraKit.joinChannelEx(byToken: nil, connection: connection2, delegate: channel, mediaOptions: mediaOptions)
Changing AgoraScreenCaptureParameters
can affect your communication usage charges. If you set the dimensions
parameter as default, Agora uses 1920 x 1080 to calculate your usage charges.
This section provides reference information you may need when sharing the screen.
Agora provides an open-source sample project that implements sharing the screen on GitHub. You can try the sample project and view the source code.