Multi-source HLS streaming with AVURLAssetHTTPCookiesKey

Sep 22, 2014


New in iOS 8 is the addition of AVURLAssetHTTPCookiesKey. As the name implies and documentation explains, this allows you to specify a set of HTTP cookies to be applied to the internal requests. This is useful if your stream pulls from multiple domains.

In HLS, many HTTP requests (e.g., media, crypt key, variant index) might be issued to different paths or hosts.

Here's an example of how you might implement this if your segments, crypt, and playlists are on different domains:

NSArray *segments = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:segmentDomainURL];
NSArray *crypt    = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:cryptDomainURL];

NSArray *cookies  = [segments arrayByAddingObjectsFromArray:crypt];

NSDictionary *opt = @{ 
                        AVURLAssetReferenceRestrictionsKey: @(AVAssetReferenceRestrictionForbidNone),
                                  AVURLAssetHTTPCookiesKey: cookies
                    };

AVAsset *asset = [AVURLAsset URLAssetWithURL:masterPlaylistURL
                                     options:opt];

The documentation doesn't explicitly state it (yet), but the matching value for AVURLAssetHTTPCookiesKey must be an NSArray of NSHTTPCookie objects.

Unfortunately, in my limited testing it appears that these additional cookies do not get passed to an Apple TV when switching to AirPlay! I was only able to get a multi-domain stream to play on my device.


        

Chris Baltzer