قالب وردپرس درنا توس
Home / IOS Development / ios – Can AVContentKeySession makeStreamingContentKeyRequestDataForApp be safely synchronized?

ios – Can AVContentKeySession makeStreamingContentKeyRequestDataForApp be safely synchronized?



I have an app that streams audio protected by FairPlay DRM. It was originally shipped with AVAssetResourceLoaderDelegate to deliver Fairplay keys, but now I update it to use AVContentKeySession for iOS 11.2 and later. Page Note: If you are trying to do this and are frustrated with missing documentation, try the "FairPlay Streaming Server SDK (4.2.0)" link here.

Each of my audio products is broken into many tracks. When I open an audio product, I queue up more than one track, via AVQueuePlayer . Each of these tracks generates a request for a Fairplay key. In case when there is no persistent key already downloaded, each of these requests goes to the key server, downloads a key, generates a persistent key, and stores it. Each track has the same key, so they all end up with the same persistent key data, and each one overwrites the last one in the end.

Because the cost of my key servers depends on the number of key requests I make, I just want the first request that actually hits the key server, and subsequent requests use the persistent key. But the method used to make the SPC data pass up to the key server, makeStreamingContentKeyRequestDataForApp uses an async completion block. The corresponding method on AVAssetResourceLoadingRequest is synchronous.

My question: is it safe to force this call to be synchronous with a semaphore? Like this:

  - (void) handleOnlineRequest: (AVContentKeyRequest *) req
NSData * appCert = [self _getAppCertData];
NSData * assetId = [self _kidFromRequest:req];
dispatch_semaphore_t sema = dispatch_semaphore_create (0);
[req makeStreamingContentKeyRequestDataForApp:appCert
                              contentIdentifier:assetId
                                        options:nil
                                        completion:^
    NSData *contentKeyRequestData, NSError *error)
    {
      //request key data and respond to request

      dispatch_semaphore_signal(sema);
    }];
dispatch_semaphore_wait (sema, DISPATCH_TIME_FOREVER);
dispatch_release (semaphore);

The effect is particularly pronounced when you download audio that has not been streamed before. Audio download speeds using AVAssetDownloadTask are very slow, so I start many at once, and each one generates a key request.


Source link