# Multi-key HLS on iOS

There are multiple DRM key systems on the market to choose from. The most popular ones are Google Widevine, Microsoft PlayReady and Apple FairPlay. Under the hood, all these systems encrypt the media samples using the AES-128 encryption algorithm. AES-128 is a standardized block-cipher that allows for multiple ways of choosing the blocks. Widevine and PlayReady both use `CTR` and FairPlay uses `CBC`. Therefore, it was possible to share segments between Widevine and PlayReady protected streams, but not with FairPlay protected stream. However, support has been added for Widevine and PlayReady with AES-128 in `CBC` mode. Hence, it is now possible to create HLS streams compatible with all these key systems. Where you previously had to encode the stream twice, this is no longer needed.

A multi-key HLS stream will play on all iOS and tvOS platforms, using the FairPlay key from the playlist.

Head to our page on [DRM](https://docs-preview.optiview.dolby.com/pr-860/theoplayer/how-to-guides/ios/drm/introduction.md) for more general information.

## The HLS playlist

A multi-key HLS playlist could look like this:

```text
#EXTM3U
#EXT-X-TARGETDURATION:6
#EXT-X-VERSION:5
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-KEY:METHOD=SAMPLE-AES,KEYFORMAT="urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed",KEYFORMATVERSIONS="1",URI="WIDEVINE_INIT_DATA_URL"
#EXT-X-KEY:METHOD=SAMPLE-AES,KEYFORMAT="com.microsoft.playready",KEYFORMATVERSIONS="1",URI="PLAYREADY_INIT_DATA_URL"
#EXT-X-KEY:METHOD=SAMPLE-AES,KEYFORMAT="com.apple.streamingkeydelivery",KEYFORMATVERSIONS="1",URI="FAIRPLAY_INIT_DATA_URL"
#EXT-X-MAP:URI="init.mp4"
#EXTINF:6,
1.mp4
#EXTINF:6,
2.mp4
#EXTINF:6,
3.mp4
#EXTINF:6,
4.mp4
#EXTINF:6,
5.mp4
#EXT-X-ENDLIST
```

There is an `#EXT-X-KEY` tag for all the key systems which all have method `SAMPLE-AES`. The `KEYFORMAT` specifies the key system:

* `urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed` for Widevine
* `com.microsoft.playready` for PlayReady
* `com.apple.streamingkeydelivery` for FairPlay

The `URI` is either a data URI containing the initialization data, or a URI pointing to the initialization data. For more information, please check out the specifications for [Widevine](https://www.academia.edu/36030972/Widevine_DRM_for_HLS), [PlayReady](https://docs.microsoft.com/en-us/playready/packaging/mp4-based-formats-supported-by-playready-clients?tabs=case4) and [FairPlay](https://developer.apple.com/streaming/fps/).

## Configuring THEOplayer

The iOS and tvOS SDKs play the FairPlay key from the playlist, so only the FairPlay key system has to be configured:

```swift
let drmConfiguration = FairPlayDRMConfiguration(
    licenseAcquisitionURL: "https://fairplay-server.com/license",
    certificateURL: "https://fairplay-server.com/certificate"
)
let typedSource = TypedSource(src: "https://yourdomain.com/playlist.m3u8", type: "application/x-mpegurl", drm: drmConfiguration)
let sourceDescription = SourceDescription(source: typedSource)
theoplayer.source = sourceDescription
```

The `#EXT-X-KEY` tags of the other key systems are ignored.
