private void HandleStreamingContentKeyRequest(AVContentKeyRequest keyRequest)
        {
            var contentKeyIdentifierString = keyRequest.Identifier.ToString();

            if (string.IsNullOrEmpty(contentKeyIdentifierString))
            {
                Debug.WriteLine("Failed to retrieve the assetID from the keyRequest!");
                return;
            }

            var contentKeyIdentifierUrl = new NSUrl(contentKeyIdentifierString);
            var assetIdString           = contentKeyIdentifierUrl.Host;
            var assetIdData             = NSData.FromString(assetIdString, NSStringEncoding.UTF8);

            Action provideOnlineKey = () =>
            {
                var applicationCertificate = RequestApplicationCertificate();

                try
                {
                    var keys    = new[] { new NSString(AVContentKeyRequest.ProtocolVersions) };
                    var numbers = new NSMutableArray <NSNumber>();
                    numbers.Add(new NSNumber(1));
                    var objects = new NSObject[] { numbers };
                    var options = new NSDictionary <NSString, NSObject>(keys, objects);
                    keyRequest.MakeStreamingContentKeyRequestData(applicationCertificate, assetIdData, options, async(data, error) => {
                        var ckcData = await this.RequestContentKeyFromKeySecurityModule(data, assetIdString);

                        if (ckcData == null)
                        {
                            return;
                        }

                        var keyResponse = AVContentKeyResponse.Create(ckcData);

                        keyRequest.Process(keyResponse);
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Failed to make streaming content key request data: {ex.Message}");
                }
            };

            /*
             * When you receive an AVContentKeyRequest via -contentKeySession:didProvideContentKeyRequest:
             * and you want the resulting key response to produce a key that can persist across multiple
             * playback sessions, you must invoke -respondByRequestingPersistableContentKeyRequest on that
             * AVContentKeyRequest in order to signal that you want to process an AVPersistableContentKeyRequest
             * instead. If the underlying protocol supports persistable content keys, in response your
             * delegate will receive an AVPersistableContentKeyRequest via -contentKeySession:didProvidePersistableContentKeyRequest:.
             */
            if (ShouldRequestPersistableContentKey(assetIdString) || PersistableContentKeyExistsOnDisk(assetIdString))
            {
                try
                {
                    NSError error;
                    keyRequest.RespondByRequestingPersistableContentKeyRequest(out error);

                    if (error != null)
                    {
                        throw new Exception($"Error requesting persistable content key: {error.ToString()}");
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);

                    /*
                     * This case will occur when the client gets a key loading request from an AirPlay Session.
                     * You should answer the key request using an online key from your key server.
                     */
                    provideOnlineKey();
                }

                return;
            }

            try
            {
                provideOnlineKey();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
 public override void DidProvideContentKeyRequest(AVContentKeySession session, AVContentKeyRequest keyRequest)
 {
     HandleStreamingContentKeyRequest(keyRequest);
 }