如何让AVPlayer检索SSL保护的播放列表?

Nai*_*ler 8 ssl ipad http-live-streaming ios avplayer

我们正在开发一个HTTP流媒体iOS应用程序,要求我们从安全站点接收播放列表.此站点要求我们使用自签名SSL证书进行身份验证.

在我们将NSURLConnection与委托一起使用以响应授权质询之前,我们从.p12文件中读取凭据.

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    [[challenge sender] useCredential:  self.credentials forAuthenticationChallenge:challenge];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

通过初始连接到我们获取.m3u8播放列表的URL,我们可以使用AVPlayer播放播放列表.问题是这种方法只适用于模拟器.

注意:我们可以使用设备上的NSURLConnection下载播放列表.这必然意味着AVPlayer不能继续使用在此初始连接期间建立的信任.

我们还尝试将凭据添加到[NSURLCredentialStorage sharedCredentialStorage],但没有任何运气.

下面是我们的霰弹枪方法:

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:host
                                         port:443
                                         protocol:@"https"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodClientCertificate];



[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                                    forProtectionSpace:protectionSpace];

    NSURLProtectionSpace *protectionSpace2 = [[NSURLProtectionSpace alloc]
                                         initWithHost:host
                                         port:443
                                         protocol:@"https"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodServerTrust];



[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                                    forProtectionSpace:protectionSpace2];
Run Code Online (Sandbox Code Playgroud)

编辑:根据这个问题:上述方法不适用于证书.

任何提示,为什么它不能在设备上工作,或者另一种解决方案是受欢迎的!

Rah*_*n P 8

从iOS 6开始,AVAssetResourceLoader可用于检索HTTPS安全播放列表或密钥文件.

AVAssetResourceLoader对象使用您提供的委托对象调解来自AVURLAsset对象的资源请求.当请求到达时,资源加载器会询问您的代理是否能够处理请求并将结果报告给资产.

请在下面找到示例代码.

// AVURLAsset + Loader
AVURLAsset      *asset          = [[AVURLAsset alloc] initWithURL:url options:nil];
AVPlayerItem    *playerItem     = [AVPlayerItem playerItemWithAsset:asset];
AVAssetResourceLoader *loader   = asset.resourceLoader;
[loader setDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

// AVPlayer
AVPlayer        *avPlayer       = [AVPlayer playerWithPlayerItem:playerItem];
Run Code Online (Sandbox Code Playgroud)

您将需要处理resourceLoader:shouldWaitForLoadingOfRequestedResource:委托方法,该方法将在需要身份验证时调用,您可以使用NSURLConnection来请求安全资源.

(BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader    shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{

 //Handle NSURLConnection to the SSL secured resource here
  return YES;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

PS:使用CocoaHTTPServer的代理方法运行良好,但使用AVAssetResourceLoader是一种更优雅的解决方案.


Nai*_*ler 6

似乎直到Apple让我们控制AVURlayer使用的NSURLConnections,唯一的答案似乎是实现HTTP环回服务器.

引用回答我们支持问题的苹果代表:

另一个选择是实现环回HTTP服务器并指向客户端对象.客户端可以使用HTTP(因为它们的请求永远不会脱离设备),而环回HTTP服务器本身可以使用HTTPS连接到源服务器.同样,这使您可以访问底层NSURLConnections,从而允许您进行自定义服务器信任评估.

除非您完全控制原始服务器上的内容,否则将此技术与UIWebView一起使用会很棘手.如果源服务器可以返回任意内容,则必须浏览返回的HTTP并重写所有链接,这不是很有趣.类似的限制适用于MPMoviePlayerController/AVPlayer,但在这种情况下,控制所有内容并因此能够避免非相对链接更为常见.

编辑:我设法使用CocoaHTTPServer中的HTTPResponse和HTTPConnection类的自定义实现实现了一个环回服务器

我无法透露消息来源,但我使用NSURLConnection以及AsyncHTTPResponse和DataHTTPResponse演示响应的混合.

编辑:记得设置 myHttpServerObject.interface = @"loopback";

编辑:警告!这种方法似乎不适用于airplay,因为airplay设备会询问127.1.1.1加密密钥.这里似乎定义了正确的方法:https: //developer.apple.com/library/content/documentation/AudioVideo/Conceptual/AirPlayGuide/EncryptionandAuthentication/EncryptionandAuthentication.html#//apple_ref/doc/uid/TP40011045-CH5-SW1

"使用应用程序定义的URL方案指定.m3u8文件中的密钥."

编辑: Apple TV和iOS更新解决了上面编辑中提到的问题!