4

I want to play stream through my delegate to apply my client certificate there. But no one event handled in delegate. When I turn off client certificate on the streaming server side, playback works fine. Otherwise - 400 Bad request (Client do not send client certificate). I try to send certificate in authentication challenge in delegate, but this event not handled.

(tvOS 10 with swift 3.)

Here it is code that initialize playback:

let movieAsset:AVURLAsset = AVURLAsset(url: plurl!, options: nil)
let plCont:PlayerViewDelegate = PlayerViewDelegate()

movieAsset.resourceLoader.setDelegate(plCont, queue: DispatchQueue.main)

let playerItem:AVPlayerItem = AVPlayerItem(asset: movieAsset)

playerController.player = AVPlayer(playerItem: playerItem)
playerController.player?.play()

Here it is code that describes delegate:

    class PlayerViewDelegate: NSObject, AVAssetResourceLoaderDelegate {

        func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool{
            return true
        }

        func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForRenewalOfRequestedResource renewalRequest: AVAssetResourceRenewalRequest) -> Bool{
            return true
        }

        func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel loadingRequest: AVAssetResourceLoadingRequest){

        }

        func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel authenticationChallenge: URLAuthenticationChallenge){
            print("PLAY CHALLENGE 2")
        }

        func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForResponseTo authenticationChallenge: URLAuthenticationChallenge) -> Bool {
            print("PLAY CHALLENGE")
            return true
        }
}

"PLAY CHALLENGE" (and all others events) are never occurred.

What I do wrong?

UPDATED!

I saw this topic Delegates methods not invokes without custom_scheme.

When I change my scheme to custom one: https -> ss_https, only one method was invoked:

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool

But I need to change scheme back to original, because the other events are still not handled. All examples in internet written in objective-c and just change scheme with request.scheme = "https". But in swift 3 all important objects are immutable! So I cannot change request, url, scheme, etc in the resourceLoader event.

Sergey Shoshin
  • 475
  • 4
  • 17
  • 1
    Are you using HTTP/HTTPs? Your delegate will never be given an opportunity to handle a loading request if the AVURLAsset’s url uses an http or https url scheme. In order to get the resource loader to use your delegate, you must initialize the AVURLAsset using a url that has a custom scheme. Checkout the URL Manipulation section in http://blog.jaredsinclair.com/post/149892449150/implementing-avassetresourceloaderdelegate-a – Chris Oct 13 '17 at 09:53
  • Possible duplicate: https://stackoverflow.com/questions/26649865/avassetresourceloaderdelegate-methods-not-working-on-device – Chris Oct 13 '17 at 09:55
  • Yes, i saw this post. But I still have a question "how to send client certificate to the server". I'll update the question. – Sergey Shoshin Oct 13 '17 at 10:56
  • @Chris, now I cannot change my url scheme back to original as described in many comments of questions similar to posted by you. – Sergey Shoshin Oct 13 '17 at 11:07
  • 1
    Progress! I am glad your delegates are now being called. `AVAssetResourceLoadingRequest` object contains your original request and all the information you need to rebuild a new request. Using the original request get the URL and remove/replace the scheme. – Chris Oct 13 '17 at 13:44
  • You'll be creating a new URLRequest not altering the original. – Chris Oct 13 '17 at 13:52

0 Answers0