This topic describes how to use the ApsaraVideo Player SDK for iOS to implement RTS.
Prerequisites
CocoaPods is installed in your environment.
Integrate the SDK
Add the ApsaraVideo Player SDK dependency using CocoaPods.
Open a terminal window.
Navigate to your project directory and create a Podfile.
pod init
Edit the Podfile to add the latest dependencies.
player_sdk_version = 'x.x.x' # We recommend that you use the latest version. rts_sdk_version = '7.10.0' # Independent version number. The latest version is used. # ApsaraVideo Player SDK pod 'AliPlayerSDK_iOS' , player_sdk_version # The bridge layer (AlivcArtc) between the player and the RTS component. The version number must be the same as the player's version number. This must be integrated with the RTS component. pod 'AliPlayerSDK_iOS_ARTC' , player_sdk_version # RTS component pod 'RtsSDK' , rts_sdk_versionImportantThe version number of the bridge layer (AlivcArtc), which connects the player and the Real-Time Streaming (RTS) component, must match the player's version number. You must integrate the bridge layer with the RTS component.
For more information about player versions, see SDK download. We recommend that you use the latest version. ApsaraVideo Player V7.5.0 or later is recommended, and the minimum supported version is V5.4.5.0.
For more information about common issues, see FAQ about ApsaraVideo Player for iOS.
Install the SDK. After the command runs, an .xcworkspace file is generated, which indicates that the SDK is integrated.
pod install
Use the ApsaraVideo Player SDK API
Call the ApsaraVideo Player SDK API to implement the RTS feature. For more information about the features of the ApsaraVideo Player SDK, see Advanced features and API reference.
The following code provides an example. For more information, see the RTS playback module in the API-Example project. This project is an Objective-C demo for the ApsaraVideo Player SDK for iOS and helps developers quickly learn how to integrate the core features of the SDK.
When you use ApsaraVideo Player for RTS stream pulling, you cannot call pause to pause the live stream. Instead, you can call stop to stop the playback and then call prepare to restart it.
The seek (drag) operation is not supported.
Create a player
Create an AliPlayer instance.
You can create an AliPlayer instance.
// Create a player instance. AliPlayer mAliPlayer = [[AliPlayer alloc] init]; // Create a view container for the playback video and set the rendering view for the player. UIView playerView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:playerView]; mAliPlayer.playerView = playerView; [mAliPlayer setTraceID:traceId];NoteFeatures provided by the player, such as Playback quality monitoring (which lets you view data about the player's overall playback quality), Single-point tracing (which lets you locate specific users or devices, analyze their playback behavior, and quickly identify issues such as abnormal playback), and Video playback statistics, rely on the instrumentation log reporting feature.
When you create a player, the available features vary based on the
setTraceIDparameter setting:If you do not pass the
setTraceIDparameter (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 issue tracking feature.If you pass a traceId to the
setTraceIDparameter: You must define the value of traceId. It must be a unique identifier for your user or the user's device, such as your business's user ID or a device ID such as International Mobile Equipment Identity (IMEI) or identifier for advertisers (IDFA). After you pass the traceId, the instrumentation log reporting feature is enabled. You can use the playback quality monitoring, single-point issue tracking, and video playback statistics features.If you set the
setTraceIDparameter toDisableAnalytics: The instrumentation log reporting feature is disabled. You cannot use the playback quality monitoring, single-point issue tracking, or video playback statistics features.
Set the playback source.
The player supports four types of playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource is used for direct URL playback. To use the RTS service, you must set the URL protocol to
artc://.AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:"artc://<streaming URL>"]; [mAliPlayer setUrlSource:urlSource];NoteFor more information about how to set a playback source, see Basic features.
You can configure playback parameters.
You can configure playback parameters to improve the performance of RTS.
NoteApsaraVideo Player SDK V6.3.0 and later support automatic optimal configuration for low latency. If the playback URL starts with "artc://" and the user has not manually modified the
MaxDelayTime,HighBufferDuration, orStartBufferDurationvalues ofAVPConfig, the SDK automatically uses the values 1000, 10, and 10 for playback, respectively.To customize the controls, see the following example:
// 1. Get and modify the configuration. AVPConfig *config = mAliPlayer.getConfig; if ([playUrl hasPrefix:@"artc://"]) { // The maximum latency 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 { // Use the default configuration for config or customize it. } // 2. Apply the configuration. [mAliPlayer setConfig:config];Start the playback.
// Prepare for playback. [mAliPlayer prepare]; // After prepare, you can synchronously call the start operation. The playback starts automatically after the onPrepared callback is complete. [mAliPlayer start];
Control playback
The iOS Player SDK supports common operations, such as starting playback and seeking to a specified time.
Start playback
You can start video playback using the start API. The following code provides an example:
[mAliPlayer start];Stop playback
You can stop video playback using the stop API. The following code provides an example:
[mAliPlayer stop];Destroy the player
You can destroy the player instance synchronously or asynchronously. The following code provides an example:
// Synchronous destroy. The stop API is automatically called internally.
[mAliPlayer destroy];
// Asynchronous destroy. The stop API is automatically called internally.
[mAliPlayer destroyAsync];When you call the synchronous destroy API, the system returns a response only after the player resources are completely released. If high interface response speed is required, we recommend that you use the asynchronous destroy API and note the following points:
Do not perform any other operations on the player object during the asynchronous destroy process.
You do not need to manually stop the player before you call the asynchronous destroy API because the process already includes an asynchronous stop procedure.
Auxiliary features
Enable or disable logs
// Enable logs. [mAliPlayer setEnableLog:YES]; [mAliPlayer setLogCallbackInfo:LOG_LEVEL_TRACE callbackBlock:nil]; // Disable logs. [mAliPlayer setEnableLog:NO]; [mAliPlayer setLogCallbackInfo:LOG_LEVEL_NONE callbackBlock:nil];Live stream fallback
NotePlayback failure fallback applies only to timeout scenarios. In a playback timeout scenario, if a fallback solution is configured, the system performs a fallback playback. Otherwise, the onError callback of the player is triggered. Exceptions such as 404, 403, or streamer disconnection do not trigger a fallback.
Automatic RTS fallback uses the same domain name to fall back from RTS to FLV. If your RTS and FLV domain names are different, you must configure a custom RTS fallback solution to specify the target FLV domain name.
Automatic RTS fallback (enabled by default)
When you play a video from an RTS URL, if custom RTS fallback is not configured and RTS stream pulling fails, the system automatically falls back to the default FLV URL that corresponds to the RTS URL for playback. The following code provides an example:
// 1: enabled. 0: disabled. By default, this feature is enabled. [AliPlayerGlobalSettings setOption:ALLOW_PRE_RENDER valueInt:1];Custom RTS fallback
When you play a video from an RTS URL, you can set a fallback URL, such as an HLS or FLV URL. If RTS stream pulling fails, the system automatically falls back to this URL for playback.
// Set the fallback source downgradeUrl. AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:downgradeUrl]; // Optional. Configure other items for config. AVPConfig *config = [mAliPlayer getConfig]; // Set the fallback URL. [mAliPlayer enableDowngrade:urlSource config:config];
Obtain the TraceID
Each low-latency playback has a traceId that can be used for troubleshooting. You can obtain the traceId through the player's event callback.
// Listen for the onPlayerEvent callback of the player 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; } }