Implement RTS ultra-low-latency live streaming on iOS by integrating ApsaraVideo Player SDK.
Prerequisite
CocoaPods is installed in your development environment.
Integrate the SDK
Add the ApsaraVideo Player SDK dependencies using the pod method.
-
Open the terminal window.
-
Navigate to your project directory and create a Podfile.
pod init
-
Edit the Podfile to add the dependencies for the latest version.
player_sdk_version = 'x.x.x' # We recommend using the latest version rts_sdk_version = '7.12.0' # The version number is independent of the Player SDK. V7.12.0 is the latest. # ApsaraVideo Player SDK pod 'AliPlayerSDK_iOS' , player_sdk_version # A bridging layer between the SDK and RTS component (AlivcArtc). Its version must be the same as the SDK version. It must be integrated together with the RTS component. pod 'AliPlayerSDK_iOS_ARTC' , player_sdk_version # The RTS component. pod 'RtsSDK' , rts_sdk_versionImportant-
The bridging layer between the player and the RTS component (AlivcArtc) must use the same version number as the player SDK. You must integrate it together with the RTS component.
-
For player versions, see SDK download. We recommend using the latest version. ApsaraVideo Player requires the latest version, with a minimum supported version of V5.4.5.0.
-
For frequently asked questions, see iOS Player FAQ.
-
-
Install the SDK. After you run the command, a *.xcworkspace file is generated, confirming that the SDK integration is complete.
pod install
Using ApsaraVideo Player SDK interfaces
Call the ApsaraVideo Player SDK APIs to enable RTS. For more SDK features, see Advanced features and API operations.
-
The following sample code is for reference only. For the complete implementation, see the API-Example RTS Ultra-Low-Latency Streaming Playback module, an Objective-C sample project that demonstrates core ApsaraVideo Player SDK features.
-
When using RTS stream pulling with ApsaraVideo Player, do not call pause to pause a live stream. Instead, call stop to stop playback, then call prepare to resume.
-
Seeking (dragging) is not supported.
Create a player
-
Create a player.
Create an AliPlayer instance.
// Create a player instance AliPlayer mAliPlayer = [[AliPlayer alloc] init]; // Create a view container for displaying the playback content and set the player's rendering view UIView playerView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:playerView]; mAliPlayer.playerView = playerView; [mAliPlayer setTraceID:traceId];NoteThe player provides Playback Quality Monitoring, Single-Point Tracing, and Video Playback Statistics features, all of which rely on instrumentation log reporting.
The available capabilities depend on how you configure the
setTraceIDparameter:-
If
setTraceIDis omitted (default), instrumentation log reporting is enabled. You can use playback quality monitoring and video playback statistics, but not single-point tracing. -
setTraceIDset to a trace ID: The trace ID must be a unique identifier for your user or device, such as a user ID or device ID (IMEI or IDFA). With a trace ID configured, instrumentation log reporting is enabled, and you can use playback quality monitoring, single-point tracing, and video playback statistics. -
setTraceIDset toDisableAnalytics: Disables instrumentation log reporting. Playback quality monitoring, single-point tracing, and video playback statistics are unavailable.
-
-
Set the playback source.
The player supports four playback sources: VidSts, VidAuth, VidMps, and UrlSource. To use RTS, set UrlSource with a URL that uses the
artc://protocol.AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:"artc://<streaming URL>"]; [mAliPlayer setUrlSource:urlSource];NoteFor more information about setting the playback source, see Basic Features.
-
Configure playback parameters.
Configure playback parameters to optimize the RTS experience.
NoteStarting with version 6.3.0, the player SDK supports automatic optimal low-latency configuration. When the playback URL starts with
artc://and you have not manually modified theMaxDelayTime,HighBufferDuration, orStartBufferDurationvalues inAVPConfig, the SDK automatically uses the values 1000, 10, and 10 for actual playback.To customize these parameters, use the following example:
//1. Obtain and modify the configuration. AVPConfig *config = mAliPlayer.getConfig; if ([playUrl hasPrefix:@"artc://"]) { // The maximum latency. Unit: milliseconds. In this example, set the value to 1000, which is 1 second. [config setMaxDelayTime:1000]; // The buffer duration for playback startup. [config setStartBufferDuration:10]; // The buffer duration for stuttering recovery. [config setHighBufferDuration:10]; } else { // You can use default configuration or specify custom values. } //2. Apply the configuration. [mAliPlayer setConfig:config]; -
Start playback.
// Prepare for playback [mAliPlayer prepare]; // After prepare, you can synchronously call the start operation. Playback automatically starts when the onPrepared callback completes. [mAliPlayer start];
Control playback
ApsaraVideo Player SDK supports common playback controls such as start, stop, and seek.
-
Start playback
Use the start method to start video playback.
[mAliPlayer start];
-
Stop playback
Use the stop method to stop video playback. The following is an example:
[mAliPlayer stop];
-
Destroy the player
You can destroy the player synchronously or asynchronously:
// Synchronously destroy the player. The system automatically calls the stop method.
[mAliPlayer destroy];
// Asynchronously destroy the player. The system automatically calls the stop method.
[mAliPlayer destroyAsync];
Synchronous destruction blocks until all player resources are released. For faster UI response, use asynchronous destruction. Note the following:
-
Do not perform any operations on the player object during asynchronous destruction.
-
You do not need to manually stop playback before asynchronous destruction, because the process includes an automatic stop step.
Use auxiliary features
-
Log Switch
// Enable logging. [mAliPlayer setEnableLog:YES]; [mAliPlayer setLogCallbackInfo:LOG_LEVEL_TRACE callbackBlock:nil]; // Disable logging. [mAliPlayer setEnableLog:NO]; [mAliPlayer setLogCallbackInfo:LOG_LEVEL_NONE callbackBlock:nil]; -
Live Streaming Fallback
Note-
Downgrade applies only to timeout scenarios. If a timeout occurs and you have configured either downgrade option below, the player downgrades automatically. Otherwise, it triggers the onError callback. Errors such as HTTP 404, 403, or streamer disconnection do not trigger downgrade.
-
Automatic RTS downgrade downgrades from RTS to FLV using the same domain. If your RTS and FLV domains differ, configure Custom RTS downgrade to specify the target FLV domain.
-
Automatic RTS downgrade ( enabled by default )
If no custom downgrade is configured, the player automatically falls back to the default FLV URL associated with the RTS URL when RTS stream pulling fails. For example:
// 1 indicates on, and 0 indicates off. The default value is on. [AliPlayerGlobalSettings setOption:ALLOW_PRE_RENDER valueInt:1]; -
Custom RTS downgrade
Specify a fallback URL (HLS or FLV) for RTS playback. If RTS stream pulling fails, the player automatically switches to the specified URL.
// Specify the downgrade source URL. AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:downgradeUrl]; // Optional. Configure other settings. AVPConfig *config = [mAliPlayer getConfig]; // Specify the degraded streaming URL. [mAliPlayer enableDowngrade:urlSource config:config];
-
-
Obtain the Trace ID
Each low-latency playback session generates a trace ID for troubleshooting. Retrieve it from the player event callback.
// Listen to the onPlayerEvent callback and parse the DemuxerTraceID message. - (void)onPlayerEvent:(AliPlayer*)mAliPlayer eventWithString:(AVPEventWithString)eventWithString description:(NSString *)description { switch (eventWithString) { case EVENT_PLAYER_DEMUXER_TRACE_ID: { NSString *traceId = description; } break; default: break; } }