Multiple video source support

Updated at:

A video source is the fundamental data object in AliPlayerKit. It defines various source types and supports three playback methods—VidAuth, VidSts, and URL—to unify the configuration and management of video resources.

Key concepts

What is a video source?

A video source specifies the origin of a video and the authorization method required for playback.

AliPlayerKit uses the VideoSource class to encapsulate three types of video sources:

Video source type

Authorization method

Security level

Use case

VidAuth

VID + PlayAuth

Medium

Recommended for most production environments.

VidSts

VID + STS temporary credential

High

High-security scenarios

URL

Direct URL

Low

Public resources, testing, and demos

How to choose a video source type

Requirement

Recommended type

Description

Requires authorization with standard security needs

VidAuth (Recommended)

Simple authorization mechanism, easy to integrate.

Requires high security and temporary access credentials

VidSts

Uses temporary credentials and supports fine-grained access control.

Public video resources

URL

No authorization needed, simple to use.

Design features

  • Immutable object: All properties of a VideoSource object are read-only and cannot be modified after creation.

  • Factory method creation: The init and new methods are marked as NS_UNAVAILABLE. Use the provided class factory methods to create instances.

  • Parameter validation: Factory methods validate that required parameters are not nil or empty strings. If validation fails, they return nil.

  • Validity check: The isValid method lets you perform a secondary check before configuring the player.

  • Unique identifier: The uniqueId property is automatically derived based on the source type. It returns the vid for VidAuth and VidSts sources, and the url for URL sources. The player uses this ID to reuse instances during lifecycle management.

Video source types

VidAuth mode (Recommended)

This mode authorizes playback by using a video ID (VID) and a PlayAuth. Its simple authorization and easy integration make it the recommended choice for most production environments.

Use cases:

  • Video resources that require authorization.

  • Video playback in most production environments.

  • Scenarios that require a simple and secure authorization method.

Parameters:

Parameter

Type

Required

Description

vid

NSString *

Yes

The unique identifier for the video. Cannot be nil or an empty string.

playAuth

NSString *

Yes

The playback authorization code. Obtain this from your server. Cannot be nil or an empty string.

Example:

VideoSource *videoSource =
    [VideoSource vidAuthSourceWithVid:@"your_video_id"
                             playAuth:@"your_play_auth"];

AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
model.sceneType = SceneTypeVOD;
model.autoPlay = YES;

VidSts mode

This mode uses a video ID (VID) and an Alibaba Cloud Security Token Service (STS) temporary credential to authorize playback. It offers a higher level of security and access control.

Use cases:

  • Scenarios that require temporary access credentials.

  • Video resources with high security requirements.

  • Scenarios that require fine-grained access control.

Parameters:

Parameter

Type

Required

Description

vid

NSString *

Yes

The unique identifier for the video. Cannot be nil or an empty string.

accessKeyId

NSString *

Yes

The STS access key ID. Cannot be nil or an empty string.

accessKeySecret

NSString *

Yes

The STS access key secret. Cannot be nil or an empty string.

securityToken

NSString *

Yes

The STS security token. Cannot be nil or an empty string.

region

NSString *

No

The region information, such as @"cn-shanghai". The default region is used for nil values.

Example:

VideoSource *videoSource =
    [VideoSource vidStsSourceWithVid:@"your_video_id"
                         accessKeyId:@"your_access_key_id"
                     accessKeySecret:@"your_access_key_secret"
                       securityToken:@"your_security_token"
                              region:@"cn-shanghai"];

AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
model.sceneType = SceneTypeVOD;
model.autoPlay = YES;

URL mode

This mode plays a video directly from a URL, making it suitable for publicly accessible video resources.

Use cases:

  • Public video resources that do not require authorization.

  • Testing and demo scenarios.

  • Playback of a live stream.

Parameters:

Parameter

Type

Required

Description

url

NSString *

Yes

The URL of the video. Cannot be nil or an empty string.

Example:

VideoSource *videoSource =
    [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];

AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];

Comparison of video source types

Feature

VidAuth

VidSts

URL

Authorization method

PlayAuth

STS temporary credential

No authorization required

Required parameters

vid, playAuth

vid, accessKeyId, accessKeySecret, securityToken

url

Optional parameters

—

region

—

Security level

Medium

High

Low

Use case

Most production environments

High-security scenarios

Public resources / live streams

Recommendation level

Recommended

For special requirements

Testing / Demos

Source of uniqueId

vid

vid

url

Usage

Basic usage: Create a source

Create a video source by using the VideoSource factory class method:

// VidAuth (Recommended)
VideoSource *authSource =
    [VideoSource vidAuthSourceWithVid:@"your_video_id"
                             playAuth:@"your_play_auth"];

// VidSts
VideoSource *stsSource =
    [VideoSource vidStsSourceWithVid:@"your_video_id"
                         accessKeyId:@"your_access_key_id"
                     accessKeySecret:@"your_access_key_secret"
                       securityToken:@"your_security_token"
                              region:@"cn-shanghai"];

// URL
VideoSource *urlSource =
    [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];
Note

If a required parameter is nil or an empty string, the factory method returns nil. The caller should check the return value.

Basic usage: Validate the source

Before you configure the player, call isValid to validate the video source configuration:

VideoSource *source =
    [VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];

if (source && [source isValid]) {
    AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
    [self.controller configure:model];
} else {
    NSLog(@"Invalid video source configuration: %@", source);
}

Validation rules for isValid:

Type

Validation condition

VidAuth

vid.length > 0 && playAuth.length > 0

VidSts

vid.length > 0 && accessKeyId.length > 0 && accessKeySecret.length > 0 && securityToken.length > 0

URL

url.length > 0

Note

The VidSts region is an optional parameter and is not included in the isValid validation.

Basic usage: End-to-end playback flow

@interface PlayerViewController ()
@property (nonatomic, strong) AliPlayerView *playerView;
@property (nonatomic, strong) AliPlayerController *playerController;
@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1. Create a video source. We recommend fetching the PlayAuth from your server.
    VideoSource *videoSource =
        [VideoSource vidAuthSourceWithVid:@"your_video_id"
                                 playAuth:@"server_returned_play_auth"];

    // 2. Validate the video source.
    if (!videoSource || ![videoSource isValid]) {
        NSLog(@"Invalid video source configuration: %@", videoSource);
        return;
    }

    // 3. Create the player model.
    AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
    model.sceneType = SceneTypeVOD;
    model.coverUrl = @"https://example.com/cover.jpg";
    model.autoPlay = YES;

    // 4. Create and configure the controller.
    self.playerController = [[AliPlayerController alloc] init];
    [self.playerController configure:model];

    // 5. Attach the controller to the player view.
    [self.playerView attach:self.playerController];
}

- (void)dealloc {
    [_playerView detach];
    [_playerController destroy];
}

@end

Advanced usage: Switch video sources

To switch between different video sources on the same controller, simply use a new AliPlayerModel to recall configure::

- (void)switchToNewVideo:(NSString *)newVid playAuth:(NSString *)newPlayAuth {
    VideoSource *newSource =
        [VideoSource vidAuthSourceWithVid:newVid playAuth:newPlayAuth];

    AliPlayerModel *newModel = [[AliPlayerModel alloc] initWithVideoSource:newSource];
    newModel.videoTitle = @"New Video";

    // Re-configure the same controller.
    [self.playerController configure:newModel];
}

Best practices

Security recommendations

Recommendation

Description

Do not hard-code sensitive information

Avoid hard-coding playAuth, accessKeySecret, etc. in client-side code.

Fetch credentials from your server

Obtain authorization information from a server-side endpoint and pass it to the client.

Refresh credentials regularly

VidSts tokens have a limited lifetime and must be refreshed.

Use HTTPS

Ensure data is transmitted securely.

// Recommended: Fetch credentials from a server.
- (void)playVideoWithId:(NSString *)videoId {
    [self.apiService fetchPlayAuthForVideoId:videoId
                                  completion:^(NSString *playAuth, NSError *error) {
        if (error) {
            NSLog(@"Failed to get PlayAuth: %@", error);
            return;
        }

        VideoSource *source =
            [VideoSource vidAuthSourceWithVid:videoId playAuth:playAuth];
        AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
        [self.playerController configure:model];
    }];
}

Common mistakes

Mistake 1: Not checking the return value of a factory method

// ✗ Incorrect: The factory method can return nil, but its return value is not checked.
VideoSource *source = [VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source]; // `source` can be nil.

// ✓ Correct: Check the return value.
VideoSource *source = [VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];
if (source && [source isValid]) {
    AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
    [self.playerController configure:model];
}

Mistake 2: Hard-coding sensitive information on the client

// ✗ Incorrect: `playAuth` is hard-coded, which is a security risk.
VideoSource *source =
    [VideoSource vidAuthSourceWithVid:@"video_id"
                             playAuth:@"hardcoded_play_auth_value"];

// ✓ Correct: Fetch from your server.
NSString *playAuth = [self fetchPlayAuthFromServer:videoId];
VideoSource *source =
    [VideoSource vidAuthSourceWithVid:videoId playAuth:playAuth];

Mistake 3: Not handling expired STS security tokens

// ✗ Incorrect: Using an expired token.
VideoSource *source = [VideoSource vidStsSourceWithVid:vid
                                           accessKeyId:accessKeyId
                                       accessKeySecret:accessKeySecret
                                         securityToken:expiredToken
                                                region:region];

// ✓ Correct: Check the token and refresh it if needed.
if ([self isTokenExpired:securityToken]) {
    securityToken = [self refreshSecurityToken];
}
VideoSource *source = [VideoSource vidStsSourceWithVid:vid
                                           accessKeyId:accessKeyId
                                       accessKeySecret:accessKeySecret
                                         securityToken:securityToken
                                                region:region];

Mistake 4: Creating an instance with init

// ✗ Compile error: `init` and `new` are marked as NS_UNAVAILABLE.
VideoSource *source = [[VideoSource alloc] init];

// ✓ Correct: Use a class factory method.
VideoSource *source = [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];

API reference

VideoSourceType enum

Enum value

Value

Description

VideoSourceTypeVidAuth

0

Video source using a VID and PlayAuth. (Recommended)

VideoSourceTypeVidSts

1

Video source using a VID and an STS temporary credential.

VideoSourceTypeUrl

2

Video source using a direct URL.

VideoSource properties

Property

Type

Description

sourceType

VideoSourceType

Video source type (readonly)

uniqueId

NSString *

Unique identifier: VidAuth/VidSts returns vid, and URL returns url (readonly, nullable).

url

NSString *

The URL of the video, which is valid only for the URL type (readonly, nullable)

vid

NSString *

The unique identifier for the video (readonly, nullable). This parameter is valid for VidAuth and VidSts types.

playAuth

NSString *

The playback authorization code (readonly, nullable), valid only for the VidAuth type.

accessKeyId

NSString *

The STS Access Key ID is valid only for the VidSts type (readonly, nullable).

accessKeySecret

NSString *

STS access key secret, valid only for the VidSts type (readonly, nullable)

securityToken

NSString *

The STS security token (readonly, nullable) is valid only for the VidSts type.

region

NSString *

Region information, valid only for the VidSts type (readonly, nullable)

VideoSource methods

Method

Return type

Description

+vidAuthSourceWithVid:playAuth:

instancetype

Creates a VidAuth source (returns nil if parameters are invalid)

+vidStsSourceWithVid:accessKeyId:accessKeySecret:securityToken:region:

instancetype

Create a VidSts source (returns nil if parameters are invalid. region can be nil)

+urlSourceWithUrl:

instancetype

Create a URL source (returns nil if the parameters are invalid)

-isValid

BOOL

Validates the video source's configuration.

Note

+new and -init are marked as NS_UNAVAILABLE, so you must create instances using a factory class method.

Related files

File

Description

PlayerKit/Source/Data/VideoSource.h

Defines the VideoSource class and the VideoSourceType enum.

PlayerKit/Source/Data/VideoSource.m

Implements VideoSource, including factory methods, isValid, and uniqueId derivation.

PlayerKit/Source/AliPlayerModel.h

Playback data model (receives a video source through initWithVideoSource:)

For complete API details, see the API reference.

Example

The PlayerKitUsages/UsageVideoSource module provides a complete example of how to use video sources, covering the creation, parameter validation, and playback processes for three types of video sources.