All Products
Search
Document Center

ApsaraVideo Live:Time shifting

Last Updated:Aug 14, 2024

Time shifting allows viewers to watch a live stream from the beginning when the live stream is still in progress. This topic describes how time shifting works and how to send requests to use the feature.

How time shifting works

Time shifting is based on the HTTP Live Streaming (HLS) protocol. HLS breaks down a live stream into TS segments and generates an M3U8 index file to record the order of the TS segments in real time. When you request the live stream, the index file that contains the URLs of the TS segments is returned. Generally, TS segments and the corresponding URLs are not stored. Therefore, you cannot rewind a live stream to view previously streamed content. To resolve this issue, you can enable time shifting to store TS segments in an Object Storage Service (OSS) bucket and the corresponding URLs in a dataset for up to 30 days. For information about how to specify the retention period, see Configure time shifting.

Usage notes

The feature allows up to 100,000 viewers to watch a live stream at the same time. If you want to raise the upper limit, submit a ticket. For more information, see Contact us.

Enable time shifting

To enable time shifting, use one of the following methods:

Method 1: Enable time shifting in the ApsaraVideo Live console. For more information, see Configure time shifting.

Method 2: Call the OpenLiveShift operation.

The following sample code provides an example on how to call the API operation by using the server SDK for Java:

 
// Replace the placeholder values with actual values.
DefaultProfile profile = DefaultProfile.getProfile("<regionId>", "<ALIBABA_CLOUD_ACCESS_KEY_ID>", "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>");
IAcsClient client = new DefaultAcsClient(profile);
OpenLiveShiftRequest openLiveShiftRequest = new OpenLiveShiftRequest();
openLiveShiftRequest.setDomainName("<DomainName>");
openLiveShiftRequest.setAppName("<AppName>");
openLiveShiftRequest.setStreamName("<StreamName>");
// The retention period. Unit: day. Valid values: 1 to 30. Default value: 7.
openLiveShiftRequest.setVision("<5>");
// Specifies whether to ignore the time-shifted data of transcoded streams. Valid values: true and false. Default value: true.
openLiveShiftRequest.setIgnoreTranscode("<true>");
//
// Code for other features.
//

try {
    OpenLiveShiftResponse openLiveShiftResponse = client.getAcsResponse(openLiveShiftRequest);
    System.out.println(new Gson().toJson(openLiveShiftResponse));
    // todo something.
} catch (ServerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClientException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Note
  • If you set the AppName parameter to an asterisk (*), time shifting is enabled for all live streams under the specified domain name.

  • If you set the StreamName parameter to an asterisk (*), time shifting is enabled for all live streams of the specified application.

  • You can call the DescribeLiveShiftConfigs operation to query the time shifting configuration for a domain name.

  • For information about the server SDK for Java, see Use the server SDK for Java.

Send a request for time-shifted viewing

To construct a request for time-shifted viewing, add the required parameters to the HLS URL of a live stream. The required parameters are in the following format: lhs_{type}_{format}_{unit}_{zone}. Sample request:

http://example.com/AppName/StreamName.m3u8?aliyunols=on&lhs_start_human_s_8=20171024160220&lhs_end_human_s_8=20171024160420
Note

You must set the aliyunols parameter to on.

The following table describes the required parameters.

Parameter

Description

type

The type of time. Valid values:

  • start: the start time of the live stream content that you want to view.

  • end: the end time of the live stream content that you want to view.

  • vodend: the end time of the video-on-demand (VOD) content.

    Note

    If you set this parameter to vodend, the video is played in VOD mode. In this case, all TS segments generated within the specified period are returned, including the endlist tag.

  • offset: the amount of time by which you want to rewind the live stream.

format

The time format. Valid values:

  • unix: UNIX timestamp.

  • human: human-readable time. Format: yyyyMMddHHmmss. Example: 20170809230130.

unit

The unit of time. Valid values:

  • s: seconds.

  • ms: milliseconds.

  • us: microseconds.

  • ns: nanoseconds.

Note

You can query the valid time range and available time-shifted data for a specific domain name in the ApsaraVideo Live console. For more information, see Usage statistics. You can also send a request to obtain the statistics. For more information, see the next section.

zone

The time zone. Valid values: 0 to 9. A value of 0 specifies the UTC time zone. A value of 8 specifies the UTC+8 time zone.

Note

If the format parameter is set to unix, you must set the zone parameter to 0.

Examples:

  • lhs_start_human_s_8=20170809200010

  • lhs_start_unix_s_0=1502280113

  • lhs_end_human_s_8=20170809200010

  • lhs_vodend_unix_s_0=1502280113

  • lhs_offset_unix_ms_0=1800000: specifies to rewind the live stream by 30 minutes.

Query the valid time range for time-shifted viewing

You can send a request to query the valid time range for time-shifted viewing. The request is constructed by adding the required parameters to a request URL in the following format: http://{domain}/openapi/timeline/query. Examples:

// An unsigned URL
http://example.com/openapi/timeline/query?aliyunols=on&app=bbb&stream=cc&format=ts&lhs_end_unix_s_0=1513334270&lhs_start_unix_s_0=1513334255
// A signed URL
http://example.com/openapi/timeline/query?auth_key=1606204918-0-0-3e898a5f491fedb113d17aad7c091dde&aliyunols=on&app=zzb-test&stream=zzb-shiyi&format=ts&lhs_start_unix_s_0=1606200900&lhs_end_unix_s_0=1606202520

Item

Description

Request method

GET

URL

The request URL. Format: http://{domain}/openapi/timeline/query. The domain field specifies the streaming domain.

Required parameters

  • aliyunols: Set this parameter to on.

  • app: the name of the application.

  • stream: the name of the live stream.

  • format: the format of the time-shifted data that you want to query. Valid values: ts and flv.

  • lhs_start_unix_s_0: the start time in UNIX timestamp format. Unit: seconds.

  • lhs_end_unix_s_0: the end time in UNIX timestamp format. Unit: seconds.

Note

If URL signing is enabled in Alibaba Cloud CDN, you must add the auth_key parameter to the URL. You can generate a signed URL in the ApsaraVideo Live console or by using an authentication algorithm. For more information, see Configure URL signing and URL signing examples.

Sample response:

{
  "retCode": 0,
  "description": "success",
  "content": {
    "current": 1514269063,
    "timeline": [
      {
        "start": 1514269054,
        "end": 1514269058
      },
      {
        "start": 1514269058,
        "end": 1514269062
      }
    ]
  }
}

Parameter

Description

current

The current system time. The player aligns the time with the value of this parameter.

timeline

The valid time range for time-shifted viewing.

start

The start time of an available segment for time-shifted viewing in UNIX timestamp format. Unit: seconds.

end

The end time of an available segment for time-shifted viewing in UNIX timestamp format. Unit: seconds.