iOS Bitmovin player example
Please visit our Github to find an example with guide in Readme on how to play DRM protected content in Bitmovin player for iOS apps.
Github example respository link
Bitmovin Player with Cloud DRM in Swift
That sets up and displays a video player using the BitmovinPlayer framework. Here's a detailed description of what the code does:
Prerequisites
XCode installed
Knowledge of Swift programming language
Obtain the Bitmovin Player SDK and license key from the Bitmovin dashboard (https://dashboard.bitmovin.com/player/licenses) and follow their instructions for integration.
Access to Cloud DRM (via Cloud Video Kit web console) to obtain:
videoUrl- The url to the HLS manifest of the video
certificateUrl- Certificate server url
x-drm-brandGuid- Tenant ID in the Cloud Video Kit
x-drm-userToken- A token that allows you to issue a license to play drm material.
More information about the structure of the token and how to generate it can be found here
{
"exp": 1893456000,
"drmTokenInfo": {
"exp": "2030-01-01T00:00:00+00:00",
"kid": ["c7962562-1517-44c2-8937-a1f2c6b849fc"],
"p": {
"pers": false
}
}
}
Bitmovin configuration
- Obtain the Bitmovin Player SDK and license key from the Bitmovin website (https://bitmovin.com) and follow their instructions for integration.
- Allow-list your package names for your player license: Log in to https://bitmovin.com/dashboard, navigate to
Player → Licenses, and add the following package names of the sample applications as allowed domains:com.insys.player.bitmovin.drm.swiftui.drmplaybackswiftui(for SwiftUI)com.insys.player.bitmovin.drm.uikit.drmplaybackuikit(for UIKit) This step ensures that your license key is valid for your specific package names.
- You can add your Bitmovin player license key to the
Info.plistfile asBitmovinPlayerLicenseKey. Another option is to set the license key by using the PlayerConfig.key property when creating a Player instance.
Getting Started
- The ContentView struct conforms to the View protocol in SwiftUI, indicating that it represents a view in the app's user interface.
- Inside ContentView, we have the following properties:
player: Player represents the video player object.playerViewConfig: PlayerViewConfig holds the configuration settings for the player's view.
- The sourceConfig property is a computed property of type
SourceConfig.- At the beginning of the code block, two URL variables are defined. The first one,
videoUrl, represents the URL of the video to be played. The second variable,certificateUrl, holds the URL of the FairPlay Streaming certificate. - Then, a check is performed to ensure that the values of
videoUrlandcertificateUrlare valid. If either of these values is not provided (i.e., they are empty), the application will be halted, and an error will be displayed. - Next, the configuration objects for the
FairPlayplayer are created. AFairplayConfigobject named fpsConfig is instantiated, representing the FairPlay Streaming configuration. - The prepareMessage block within the fpsConfig object contains a function that is called to prepare the
Server Playback Context (SPC)data. In this case, the function returns the original SPC data without any modifications. - The prepareCertificate block within the fpsConfig object contains a function that is called to prepare the certificate data. In this example, the function returns the original certificate data without any modifications.
- Then, the license request headers are set in the fpsConfig.licenseRequestHeaders dictionary. In this case, two values are set:
x-drm-brandGuidandx-drm-usertoken. These headers are required to pass information about thebrandGuidanduserTokenduring the DRM license request. - Finally, a
SourceConfigobject is created. This object represents the configuration for the video source. It includes the video URL, the type of video (in this case,.hls), and the DRM configuration (fpsConfig) for FairPlay Streaming.
- At the beginning of the code block, two URL variables are defined. The first one,
- Inside the
init()method of the ContentView class, the video player configuration and the player view configuration are initialized. Here's a more detailed explanation of what happens inside this method:- A
PlayerConfigobject named playerConfig is created.PlayerConfigis responsible for configuring the video player. - Then, the
create(playerConfig:)method is called on thePlayerFactoryclass, passing the playerConfig object as an argument. This method creates an instance of the video player based on the provided configuration and assigns it to the player variable in the ContentView class. - Next, the playerViewConfig variable of type
PlayerViewConfigis initialized. This configuration holds the settings related to the appearance and behavior of the video player view.
- A
- The body property defines the structure of the view. It consists of a VStack that contains a ZStack. The ZStack is used to layer the video player view on top of a black background color represented by the Color.black. The actual video player view is created using the SwiftUIPlayerView and configured with the player and playerViewConfig properties.
- The onAppear modifier is applied to the VStack and specifies an action to be performed when the view appears. In this case, it loads the sourceConfig into the player using the
player.load(sourceConfig:)method. This action ensures that the video specified in the sourceConfig is loaded and ready to be played when the view appears.