This topic describes how to implement RTS using ApsaraVideo Player SDK for iOS.
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_versionImportantThe 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
You can call the APIs of the ApsaraVideo Player SDK to enable the RTS feature. For more features of the SDK, see Advanced features and API operations.
The following sample code provides an example. For detailed code, see the API-Example RTS Ultra-Low-Latency Streaming Playback module. This project is an iOS sample project for the ApsaraVideo Player SDK written in Objective-C, which helps developers quickly integrate the core SDK features.
When implementing 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 playback.
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 the Playback Quality Monitoring (view data related to overall playback quality), Single-Point Tracing (locate specific users or devices, analyze their playback behavior, and quickly identify playback abnormalities), and Video Playback Statistics features—all of which depend on the instrumentation log reporting feature.
The capabilities available after you create a player depend on how you configure the
setTraceIDparameter, as follows:If the
setTraceIDparameter is omitted (default), the instrumentation log reporting feature is enabled. You can use the playback quality monitoring and video playback statistics features, but you cannot use the single-point tracing feature.setTraceIDparameter provided with a trace ID: You define the value of the trace ID, which must be a unique identifier for your user or user device, such as your business's user ID or device IDs like IMEI or IDFA. After you provide a trace ID, the instrumentation log reporting feature is enabled, and you can subsequently use playback quality monitoring, single-point tracing, and video playback statistics features.setTraceIDparameter set toDisableAnalytics: This disables instrumentation log reporting. Subsequently, you cannot use playback quality monitoring, single-point tracing, or video playback statistics.
Set the playback source.
The player supports four playback sources: VidSts, VidAuth, VidMps, and UrlSource. Of these, UrlSource enables direct URL playback and requires the URL to be set to the
artc://protocol to use the RTS service.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 enhance the RTS effect.
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 starting playback and seeking to a specific time point.
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 dispose of the player synchronously or asynchronously. For example:
// 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 disposal returns only after all player resources are fully released. For faster UI response, use asynchronous disposal. Keep in mind:
Avoid any operations on the player object during asynchronous disposal.
You do not need to manually stop playback before calling asynchronous disposal, because the process includes an asynchronous 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
NoteDowngrade on playback failure 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 )
When playing an RTS streaming URL and no custom downgrade is configured, the player automatically downgrades to the default FLV URL associated with the RTS URL if 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
When playing an RTS streaming URL, specify a downgrade URL such as an HLS or FLV URL. If RTS stream pulling fails, the player automatically plays from 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 generates a trace ID, which you can use for troubleshooting. Retrieve the trace ID 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; } }