This topic describes how to integrate Real-Time Streaming (RTS) SDK for a mobile client with ApsaraVideo Player.

Background information

If you use ApsaraVideo Player, the integration is simple. This is because the plug-in that is used to integrate RTS SDK with ApsaraVideo Player is built in ApsaraVideo Player SDK. You need only to configure dependencies before you can use the Advanced Real-Time Communication (ARTC) protocol in the same way as the Real-Time Messaging Protocol (RTMP).

Integrate RTS SDK for Android with ApsaraVideo Player

  1. Integrate RTS SDK as a plug-in into ApsaraVideo Player SDK.

    Add the AliyunARTC.aar dependency of ApsaraVideo Player SDK and the RtsSDK.aar dependency to your application project. Both Maven and local dependencies are supported. After you add the AliyunARTC.aar dependency to your project, ApsaraVideo Player SDK loads RTS SDK as a plug-in.

    • Maven dependencies
      Name Version Code
      AliyunARTC 5.3.0p implementation 'com.aliyun.sdk.android:AlivcArtc:5.3.0p'
      RtsSDK 1.6.1 implementation 'com.aliyun.rts.android:RtsSDK:1.6.1'
    • Local dependencies

      For information about how to obtain local dependencies, see SDK download.

  2. Integrate ApsaraVideo Player SDK and RTS SDK into a project.

    Integrate ApsaraVideo Player SDK and RTS SDK into a project by using Maven or local dependencies.

    • Maven dependencies
      Name Version Code
      AliyunPlayer 5.3.2 implementation 'com.aliyun.sdk.android:AliyunPlayer:5.3.2-full'
    • Local dependencies

      For information about how to obtain the ZIP package provided by ApsaraVideo Player, see Release notes of ApsaraVideo Player SDK for Android. For information about how to integrate ApsaraVideo Player SDK, see Integration.

    1. Create a project, as shown in the following figure. Maven dependencies are used as an example.
      01
    2. Add the following code to the build.gradle file in the root directory:
      b
      // The URL of the Maven repository for MPChart. If you do not need MPChart, you can delete this line of code.
      maven { url 'https://jitpack.io' }
      // The URL of the Maven repository for ApsaraVideo Player SDK and RTS SDK. If you use ijkplayer, you can delete this line of code.
      maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases' }
      ext {
          compileSdkVersion = 25
          buildToolsVersion = "25.0.3"
          minSdkVersion = 21
          targetSdkVersion = 25
      
          versionCode = 800800
          versionName = "0.8.8"
          appVersionCode = 1
          appVersionName = "1.0"
      
          externalRecyclerView = 'com.android.support:recyclerview-v7:25.4.0'
          externalMPAndroidChart = 'com.github.PhilJay:MPAndroidChart:v3.1.0'
          externalSupportAppCompat = 'com.android.support:appcompat-v7:25.4.0'
      
          // The player for ARTC-based live streaming.
          externalAliyunARTCNet = 'com.aliyun.rts.android:RtsSDK:1.6.0'
          externalAliyunARTC = 'com.aliyun.sdk.android:AliyunArtc:5.3.2'
          externalAliyunPlayer = 'com.aliyun.sdk.android:AliyunPlayer:5.3.2-full'
      
          //required, enough for most devices.
          //implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
          //implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
      }
  3. Call the API of ApsaraVideo Player to use the RTS feature.
    1. Create a player.

      Use the AliPlayerFactory class to create a player. You can create two types of players: AliPlayer and AliListPlayer. If you want to play only one stream, use AliPlayer. You can run the following code to create an AliPlayer:

      AliPlayer aliyunVodPlayer;
      .....
      aliyunVodPlayer = AliPlayerFactory.createAliPlayer(getApplicationContext());
    2. Configure player listeners.

      ApsaraVideo Player provides a variety of listeners, such as onPrepared and onCompletion. You can run the following code to configure listeners:

      aliyunVodPlayer.setOnCompletionListener(new IPlayer.OnCompletionListener() {
          @Override
          public void onCompletion() {
              // Listens to the completion of playback.
          }
      });
      aliyunVodPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
          @Override
          public void onError(ErrorInfo errorInfo) {
              // Listens to the occurrence of errors.
          }
      });
      aliyunVodPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
          @Override
          public void onPrepared() {
              // Listens to successful preparation.
          }
      });
      aliyunVodPlayer.setOnVideoSizeChangedListener(new IPlayer.OnVideoSizeChangedListener() {
          @Override
          public void onVideoSizeChanged(int width, int height) {
              // Listens to the changes of the video resolution.
          }
      });
      aliyunVodPlayer.setOnRenderingStartListener(new IPlayer.OnRenderingStartListener() {
          @Override
          public void onRenderingStart() {
              // Listens to the display of the first frame.
          }
      });
      aliyunVodPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
          @Override
          public void onInfo(int type, long extra) {
              // Listens to other events and information, such as the start of loop playback, the buffer position, the playback position, and the start of autoplay.
          }
      });
      aliyunVodPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
          @Override
          public void onLoadingBegin() {
              // Listens to the start of buffering.
          }
          @Override
          public void onLoadingProgress(int percent, float kbps) {
              // Listens to the buffering progress.
          }
          @Override
          public void onLoadingEnd() {
              // Listens to the completion of buffering.
          }
      });
      aliyunVodPlayer.setOnSeekCompleteListener(new IPlayer.OnSeekCompleteListener() {
          @Override
          public void onSeekComplete() {
              // Listens to the completion of seeking.
          }
      });
      aliyunVodPlayer.setOnSubtitleDisplayListener(new IPlayer.OnSubtitleDisplayListener() {
          @Override
          public void onSubtitleShow(long id, String data) {
              // Listens to the display of subtitles.
          }
          @Override
          public void onSubtitleHide(long id) {
              // Listens to the hiding of subtitles.
          }
      });
      aliyunVodPlayer.setOnTrackChangedListener(new IPlayer.OnTrackChangedListener() {
          @Override
          public void onChangedSuccess(TrackInfo trackInfo) {
              // Listens to the success of switching between audio and video streams or between resolutions.
          }
          @Override
          public void onChangedFail(TrackInfo trackInfo, ErrorInfo errorInfo) {
              // Listens to the failure of switching between audio and video streams or between resolutions.
          }
      });
      aliyunVodPlayer.setOnStateChangedListener(new IPlayer.OnStateChangedListener() {
          @Override
          public void onStateChanged(int newState) {
              // Listens to the change of the player status.
          }
      });
      aliyunVodPlayer.setOnSnapShotListener(new IPlayer.OnSnapShotListener() {
          @Override
          public void onSnapShot(Bitmap bm, int with, int height) {
              // Listens to the capture of snapshots.
          }
      });
    3. Set the playback source and prepare for playback.

      ApsaraVideo Player supports four types of playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource is used for URL-based playback. If you use UrlSource, you can prefix URLs with artc:// to use the RTS feature.

      UrlSource urlSource = new UrlSource();
      urlSource.setUri("artc://xxxx");
      aliyunVodPlayer.setDataSource(urlSource);
    4. Configure the UI view.

      If the playback source contains video images, configure the UI view to display the video images. SurfaceView and TextureView are supported. In the following code, SurfaceView is used as an example:

      surfaceView = (SurfaceView) findViewById(R.id.playview);
      surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              aliyunVodPlayer.setDisplay(holder);
          }
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
              aliyunVodPlayer.redraw();
          }
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
              aliyunVodPlayer.setDisplay(null);
          }
      });
    5. Configure playback control.

      You can create playback control buttons and associate click events with playback control methods to implement playback control. The basic control features include play, stop, pause, and seek. The seek feature is valid only for ApsaraVideo VOD. If you pause a live stream, the live stream stops at the current position. After you resume the playback of the live stream, the live stream starts from that position. Sample code:

      // Prepare for the playback. You can specify the start() method in the callback so that the playback starts upon the completion of preparation.
      aliyunVodPlayer.prepare();
      // Start the playback.
      aliyunVodPlayer.start();
      // Pause the playback.
      aliyunVodPlayer.pause();
      // Stop the playback.
      aliyunVodPlayer.stop();
      // Jump to an inaccurate position.
      aliyunVodPlayer.seekTo(long position);
      // Reset the player.
      aliyunVodPlayer.reset();
      // Release the player. The player cannot be used after it is released.
      aliyunVodPlayer.release();

      For more information about the features of ApsaraVideo Player SDK, see API operations and Implementation.

    6. Set parameters.

      We recommend that you set the following parameters to take full advantage of RTS:

      // Query the configurations.
      PlayerConfig config = mAliyunVodPlayer.getConfig();
      // Set the maximum latency to 1 second. The ARTC protocol controls the latency.
      config.mMaxDelayTime = 1000;
      // Set the buffer period to 10 milliseconds. The ARTC protocol controls the buffer period. 
      config.mHighBufferDuration = 10;
      config.mStartBufferDuration = 10;
      ....// Other settings.
      // Load the settings for the player.
      mAliyunVodPlayer.setConfig(config);
    7. Load the RtsSDK library.

      Add System.loadLibrary("RtsSDK"); to the required activity.

      static {
          System.loadLibrary("RtsSDK");
      }

Integrate RTS SDK for iOS with ApsaraVideo Player

  1. Integrate RTS SDK as a plug-in into ApsaraVideo Player SDK.

    In addition to ApsaraVideo Player SDK, your application must also depend on RTS SDK and AliPlayerSDK_iOS_ARTC. ApsaraVideo Player SDK automatically loads RTS SDK as a plug-in.

    • Pod dependencies
      Name Version Code
      AliPlayerSDK_iOS_ARTC 5.3.0p pod 'AliPlayerSDK_iOS_ARTC','5.3.0p'
      RtsSDK 1.6.1 pod 'RtsSDK','1.6.1'
    • Local dependencies

      For information about how to obtain the required frameworks, see SDK download.

  2. Integrate ApsaraVideo Player SDK and RTS SDK into a project.
    • Pod dependencies
      Name Version Code
      AliPlayerSDK_iOS 5.3.2 pod 'AliPlayerSDK_iOS','5.3.2'
      If you use pod dependencies, open a command-line interface (CLI) on a terminal. Go to the root directory of your project and run the pod init command. In the Podfile that is generated, enter the following code. If you need to use a specific version, specify the version in the code. Run the pod install command.
       pod 'RtsSDK'
       pod 'AliPlayerSDK_iOS'
       pod 'AliPlayerSDK_iOS_ARTC'
       ```
    • Local dependencies

      For information about how to obtain the ZIP package provided by ApsaraVideo Player, see Release notes of ApsaraVideo Player SDK for iOS. For information about how to integrate ApsaraVideo Player SDK, see Integration.

      If you use local dependencies, drag the library files in the following figure to your project. Then, select Embed & Sign for frameworks on the General tab of your project. 001
  3. Call the API of ApsaraVideo Player.
    1. Initialize AliPlayer.
      - (AliPlayer *)aliPlayer{
          if (!_aliPlayer) {
              _aliPlayer = [[AliPlayer alloc] init];
              _aliPlayer.scalingMode =  AVP_SCALINGMODE_SCALEASPECTFIT;
              _aliPlayer.rate = 1;
          // To use AVPDelegate, add the following line of code:
              _aliPlayer.delegate = self;
          // Set the UI view and apply the UI view to AliPlayer.
              _aliPlayer.playerView = self.basePlayerView.playerView;
              _aliPlayer.autoPlay = YES;
          }
          return _aliPlayer;
      }
    2. Configure playback URLs.
      AVPUrlSource *source = [[AVPUrlSource alloc] urlWithString:_url];
      [self.aliPlayer setUrlSource:source];
    3. Set the player parameters. If you use ApsaraVideo Player to play ARTC-based live streams, we recommend that you use the following settings.
      Note You must set player parameters before you call the prepare method.
      AVPConfig *config = self.aliPlayer.getConfig;
      // The maximum latency of live streaming.
      [config setMaxDelayTime:1000];
      // The buffer period.
      [config setHighBufferDuration:10];
      // The maximum startup loading duration.
      [config setStartBufferDuration:10];
      // The number of retries.
      [config setNetworkRetryCount:2];
      // The retry interval.  
      [config setNetworkTimeout:15000];
       [_aliPlayer setConfig:config];
      // By default, hardware decoding is enabled. If hardware decoding fails during the preparation of the player, hardware decoding is switched to software decoding.
       _aliPlayer.enableHardwareDecoder = YES;
    4. Enable logging.
      [AliPlayer setEnableLog:YES];
      [AliPlayer setLogCallbackInfo:LOG_LEVEL_DEBUG callbackBlock:nil];
    5. Configure playback control.
      [self.aliPlayer prepare];
      [self.aliPlayer stop];
      [self.aliPlayer destroy];
      [self.aliPlayer reload];