All Products
Search
Document Center

Secure download

Last Updated: Sep 06, 2019

Overview

ApsaraVideo Player SDK allows you to download videos from ApsaraVideo for VOD by using the STS token and playAuth. ApsaraVideo for VOD supports two download modes: normal download and secure download. You can set the download mode in the console.
When you select the normal download mode, downloaded videos are not encrypted by Alibaba Cloud, even though the videos have been encrypted by ApsaraVideo for VOD. That means you can use third-party players to play the downloaded videos.
When you select the secure download mode, downloaded videos are encrypted by Alibaba Cloud, even though the videos have not been encrypted by ApsaraVideo for VOD. That means you cannot use third-party players other than ApsaraVideo Player to play the downloaded videos. In the secure download mode, a downloaded video is saved locally as an encrypted video. It can only be played by the application with the bundle ID or keystore specified in the console. This mode protects video copyrights and is available for ApsaraVideo for VOD users only.

Key implementation on Android

The process for video download is as follows:

  1. Create and set a download object. 2. Set listeners. 3. Prepare the download source. 4. Select download tasks. 5. Update the download source information and start download. 6. Release the download object after the download succeeds or fails.

1. Create and set a download object

Use the AliDownloaderFactory class to create a download object. The sample code is as follows:

  1. AliMediaDownloader mAliDownloader = null;
  2. ......
  3. // Create a download object.
  4. mAliDownloader = AliDownloaderFactory.create(getApplicationContext());
  5. // Set the path for storing downloaded files.
  6. mAliDownloader.setSaveDir("The path of the folder for storing downloaded files");

To download transcoded videos that are encrypted by Alibaba Cloud, you need to configure a security file for encryption verification, that is, encryptedApp.dat. For more information about how to configure this file, see Obtain the security file and the video for obtaining the security file. We recommend that you configure the security file in your application, and you only need to do that once.

  1. PrivateService.initService(getApplicationContext(), "The local path of the encryptedApp.dat file");

2. Set listeners

The download object provides multiple listeners. Use the following code to set listeners:

  1. mAliDownloader.setOnPreparedListener(new AliMediaDownloader.OnPreparedListener() {
  2. @Override
  3. public void onPrepared(MediaInfo mediaInfo) {
  4. // Indicate that the preparation of download tasks succeeds.
  5. }
  6. });
  7. mAliDownloader.setOnProgressListener(new AliMediaDownloader.OnProgressListener() {
  8. @Override
  9. public void onDownloadingProgress(int percent) {
  10. // Specify the percentage of the download progress.
  11. }
  12. @Override
  13. public void onProcessingProgress(int percent) {
  14. // Specify the percentage of the processing progress.
  15. }
  16. });
  17. mAliDownloader.setOnErrorListener(new AliMediaDownloader.OnErrorListener() {
  18. @Override
  19. public void onError(ErrorInfo errorInfo) {
  20. // Indicate that a download error occurs.
  21. }
  22. });
  23. mAliDownloader.setOnCompletionListener(new AliMediaDownloader.OnCompletionListener() {
  24. @Override
  25. public void onCompletion() {
  26. // Indicate that the download succeeds.
  27. }
  28. });

Callbacks for download progress and processing progress depend on different data sources. The callback for download progress requires online data before the download is completed. However, the callback for processing progress does not require online data.

3. Prepare the download source

Call the prepare method to prepare the download source. ApsaraVideo Player SDK for Android supports two download sources: VidSts and VidAuth. The following code uses VidSts as an example:

  1. // Create the VidSts object.
  2. VidSts aliyunVidSts = new VidSts();
  3. aliyunVidSts.setVid(VID);
  4. aliyunVidSts.setAccessKeyId(Temporary AccessKey ID);
  5. aliyunVidSts.setAccessKeySecret(Temporary AccessKey secret);
  6. aliyunVidSts.setSecurityToken(Security token);
  7. aliyunVidSts.setRegion(Access region);
  8. // Prepare the download source.
  9. mAliDownloader.prepare(aliyunVidSts)

4. Select download tasks

When the download source preparation is completed, the OnPreparedListener callback is fired. Select a video track for download:

  1. public void onPrepared(MediaInfo mediaInfo) {
  2. // Indicate that the preparation of download tasks succeeds.
  3. List<TrackInfo> trackInfos = mediaInfo.getTrackInfos();
  4. // Download the information of tracks, for example, the first track.
  5. mAliDownloader.selectItem(trackInfos.get(0).getIndex());
  6. }

5. Update the download source information and start download

After you select download tasks and download the track information, you can start the download. We recommend that you update the download source information before the download in case of VidSts or VidAuth expiration.

  1. // Update the download source information.
  2. mAliDownloader.updateSource(mVidSts);
  3. // Start the download.
  4. mAliDownloader.start();

6. Release the download object after the download succeeds or fails

After the download succeeds, call the release method to release the download object. The download object is stopped when the onCompletion callback is fired, or is released when the onError callback is fired:

  1. mAliDownloader.stop();
  2. mAliDownloader.release();

To redownload a file, you need to create a new download object.

Key implementation on iOS

The process for video download is as follows:

  1. Create and set a download object. 2. Set listeners. 3. Prepare the download source. 4. Select download tasks. 5. Update the download source information and start download. 6. Release the download object after the download succeeds or fails.

1. Create and set a download object

Create a download object. The sample code is as follows:

  1. AliMediaDownloader *downloader = [[AliMediaDownloader alloc] init];
  2. [downloader setSaveDirectory:self.downLoadPath];
  3. [downloader setDelegate:self];

To download transcoded videos that are encrypted by Alibaba Cloud, you need to configure a security file for encryption verification, that is, encryptedApp.dat. For more information about how to configure this file, see Obtain the security file and the video for obtaining the security file. We recommend that you configure the security file in your application, and you only need to do that once.

  1. NSString *encrptyFilePath = [[NSBundle mainBundle] pathForResource:@"encryptedApp" ofType:@"dat"];
  2. [AliPrivateService initKey:encrptyFilePath];

2. Set listeners

The download object provides multiple listeners. Use the following code to set listeners:

  1. -(void)onPrepared:(AliMediaDownloader *)downloader mediaInfo:(AVPMediaInfo *)info {
  2. // Indicate that the preparation of download tasks succeeds.
  3. }
  4. -(void)onError:(AliMediaDownloader *)downloader errorModel:(AVPErrorModel *)errorModel {
  5. // Indicate that a download error occurs.
  6. }
  7. -(void)onDownloadingProgress:(AliMediaDownloader *)downloader percentage:(int)percent {
  8. // Specify the percentage of the download progress.
  9. }
  10. -(void)onProcessingProgress:(AliMediaDownloader *)downloader percentage:(int)percent {
  11. // Specify the percentage of the processing progress.
  12. }
  13. -(void)onCompletion:(AliMediaDownloader *)downloader {
  14. // Indicate that the download succeeds.
  15. }

Callbacks for download progress and processing progress depend on different data sources. The callback for download progress requires online data before the download is completed. However, the callback for processing progress does not require online data.

3. Prepare the download source

Call the prepare method to prepare the download source. ApsaraVideo Player SDK for iOS supports two download sources: AVPVidStsSource and AVPVidAuthSource. The following code uses AVPVidStsSource as an example:

  1. // Create the AVPVidStsSource object.
  2. AVPVidStsSource* stsSource = [[AVPVidStsSource alloc] init];
  3. stsSource.vid = source.vid;// The video ID.
  4. stsSource.region = DEFAULT_SERVER.region;// The access region.
  5. stsSource.securityToken = DEFAULT_SERVER.securityToken;// The security token.
  6. stsSource.accessKeySecret = DEFAULT_SERVER.accessKeySecret;// The temporary AccessKey secret.
  7. stsSource.accessKeyId = DEFAULT_SERVER.accessKeyId;// The temporary AccessKey ID.
  8. // Prepare the download source.
  9. [downloader prepareWithVid:stsSource];

4. Select download tasks

When the download source preparation is completed, the onPrepared callback is fired. Select a video track for download:

  1. -(void)onPrepared:(AliMediaDownloader *)downloader mediaInfo:(AVPMediaInfo *)info {
  2. NSArray<AVPTrackInfo*>* tracks = info.tracks;
  3. // Download the information of tracks, for example, the first track.
  4. [downloader selectTrack:[tracks objectAtIndex:0].trackIndex];
  5. }

5. Update the download source information and start download

After you select download tasks and download the track information, you can start the download. We recommend that you update the download source information before the download in case of VidSts or VidAuth expiration.

  1. // Update the download source information.
  2. [downloader updateWithVid:vidSource]
  3. // Start the download.
  4. [downloader start];

6. Release the download object after the download succeeds or fails

After the download succeeds, call the destroy method to release the download object.

  1. [self.downloader destroy];
  2. self.downloader = nil;

To redownload a file, you need to create a new download object.