Best practices for RTS playback

更新时间:
复制 MD 格式

If your browser does not support WebRTC or stream pulling over Real-Time Streaming (RTS) fails, you can fall back to HTTP Live Streaming (HLS) or HTTP Flash Video (HTTP-FLV) for playback. This topic covers fallback scenarios and implementation for both custom players integrated with Web RTS SDK and ApsaraVideo Player.

Overview

RTS playback may fall back to a degraded protocol in the following scenarios:

  • Your browser does not support WebRTC or H.264.

    Note

    Web RTS SDK depends on the browser's support for WebRTC. For a list of tested browsers, see Supported browser versions. Use the isSupport function provided by Web RTS SDK to check browser compatibility. If your browser supports WebRTC but is not in the list, perform a full test and skip the isSupport check.

  • Signaling requests fail due to an invalid source URL, invalid HTTPS configuration, or invalid RTS configuration.

  • Playback startup times out or the stream is interrupted during playback.

The following sections describe how to implement protocol fallback for a custom player integrated with Web RTS SDK and for ApsaraVideo Player.

Use your own player that is integrated with Web RTS SDK

Note
 /**
   * Scenarios in which you can use a degraded protocol:
   * 1. The browser is not supported. Configure isSupport().catch to use a degraded protocol.
   * 2. A reconnection failure occurs when connection is being established or during playback. In this case, the 10410 error code is returned in the onError event.
   **/

  var pullStreamUrl = 'The RTS source URL.';
  var fallbackUrl = 'The URL based on a degraded protocol, such as HLS.';

  // Initialize the SDK.
  var aliRts = AliRTS.createClient();
	
	aliRts.on('onError', (error) => {
    console.log(error.errorCode, error.message); // The error code and error message.
    // Determine whether to use a degraded protocol.
    switch(error.errorCode){
      case 10410: // Reconnection for stream pulling (subscription) failed.
        fallback(); // Use a degraded protocol.
        break;
      default:
    }
  });

  // Check whether the browser is supported.
  // You can skip the isSupport check and directly execute subscribeRts to pull the steam if the browser is not in the list that is described in the following topic: ApsaraVideo Live > User Guide > Real-Time Streaming > Web RTS SDK > Web RTS SDK for stream pulling > Integrate Web RTS SDK with other players > SDK overview. This operation may cause risks. Perform a full test before you perform this operation.
  aliRts.isSupport({ isReceiveVideo: true }).then(subscribeRts).catch(fallback)

  function subscribeRts() {
    aliRts.subscribe(pullStreamUrl, {
      // mediaTimeout: 6000  // Specify the timeout period.
      // retryTimes: 5,      // Specify the number of reconnection attempts. Default value: 5.
  		// retryInterval: 2000,// Specify the interval between reconnection attempts. Default value: 2000. Unit: milliseconds.
    }).then((remoteStream) => {
      remoteStream.play(mediaElement);
    }).catch(() => {})
  }

  // Use a degraded protocol for playback.
  function fallback() {
    // The following code provides an example. You can choose an appropriate player based on the protocol that you use.
    hlsPlayer.play(fallbackUrl)
  }

Use ApsaraVideo Player

ApsaraVideo Player has built-in Web RTS SDK integration and supports automatic protocol fallback. You only need to provide a fallback URL to enable this feature.

Note

  /**
   * By default, ApsaraVideo Player plays the RTS stream from the source URL. If the playback fails, ApsaraVideo Player automatically plays the stream from the URL provided by rtsFallbackSource, such as an HLS URL. 
   * Scenarios in which a degraded protocol may be used:
   * 1. The browser does not support RTS. ApsaraVideo Player uses a degraded protocol to play the stream.
   * 2. Signaling requests fail due to an invalid source URL, invalid HTTPS configuration, or invalid RTS configuration. ApsaraVideo Player uses a degraded protocol to play the stream.
   * 3. Playback startup times out or the stream is interrupted during playback. ApsaraVideo Player retries playback based on the custom policy. If the retry fails, ApsaraVideo Player uses a degraded protocol to play the stream.
   **/

  var options = {
    "id": "player-con",
    "source": "The RTS playback URL.",
    "rtsFallbackSource": "The URL based on a degraded protocol, such as HLS.",
    "width": "100%",
    "height": "500px",
    "autoplay": true,
    "isLive": true,
    "playsinline": true,
    "skipRtsSupportCheck": false, // You can set the value to true to skip the check and forcibly use RTS if the browser is not in the list that is described in the following topic: ApsaraVideo Live > User Guide > Real-Time Streaming > Web RTS SDK > Web RTS SDK for stream pulling > Integrate Web RTS SDK with other players > SDK overview. This operation may cause risks. Perform a full test before you perform this operation.

    /**
     * If RTS stream pulling times out, a retry occurs by default.
     * The following parameters are used to control the retry policy before a degraded protocol is used. For example, if the stream cannot be pulled after 3,000 milliseconds, a retry occurs. If the stream still cannot be pulled after another 3,000 milliseconds, a degraded protocol is used. The total waiting period is 6,000 milliseconds.
     **/
    // The period of time after which a retry occurs if the RTS stream fails to be pulled. Default value: 3000. Unit: milliseconds.
    // rtsLoadDataTimeout: 2000,

    // The number of retries for failed RTS stream pulling. Default value: 5. We recommend that you set this parameter to 1. This way, the total waiting period can be reduced.
    liveRetry: 1,
  };

  var player = new Aliplayer(options, function () {/* player ready */});

  // The event that is triggered when a degraded protocol is used.
  player.on('rtsFallback', function(event) {
    // event.paramData.reason The reason for degradation.
    // event.paramData.fallbackUrl The URL based on a degraded protocol.
  })

Can I use Web RTS SDK in WeChat mini programs?

Web RTS SDK cannot run in native WeChat mini programs because they do not support the RTS signaling protocol. As a workaround, embed SDK-integrated web pages in the WebView of WeChat mini programs.