All Products
Search
Document Center

ApsaraVideo Live:Best practices for RTS playback

Last Updated:Mar 01, 2024

If your browser does not support WebRTC or stream pulling over RTS fails, you can play the stream over a degraded protocol such as HTTP Live Streaming (HLS) or HTTP Flash Video (HTTP-FLV). This topic describes the scenarios in which you can use a degraded protocol rather than RTS to play streams when you use your own player that is integrated with Web RTS SDK or use ApsaraVideo Player.

Overview

Scenarios in which you can consider using a degraded protocol rather than RTS to play streams:

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

    Note

    Web RTS SDK depends on the browser's support for WebRTC. Common browsers on the market have been tested in terms of their compatibility with the SDK. For more information, see Supported browser versions. You can check whether your browser meets the requirements by using the isSupport function provided by Web RTS SDK. If your browser supports WebRTC but is not in the list, you can perform a full test and skip the isSupport check for the browser.

  • 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 section describes how to use a degraded protocol rather than RTS to play streams in the preceding scenarios when you use your own player that is integrated with Web RTS SDK or use 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 is integrated with Web RTS SDK and can be used to play RTS streams. ApsaraVideo Player has a built-in logic for automatic protocol degradation. You need to only provide a URL based on the degraded protocol to trigger this logic.

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?

You cannot use Web RTS SDK in native WeChat mini programs because WeChat mini programs cannot parse the RTS signaling protocol. However, you can embed web pages that are integrated with the SDK into the WebView of WeChat mini programs.