1. 實驗環(huán)境
- 服務(wù)器環(huán)境:Mac + Php + Nginx (以我的環(huán)境)
- 客戶端:AVPlayer + AVAssetResourceLoader
2. 服務(wù)端實現(xiàn)
// private_key.php
<?php
$token = $_SERVER["HTTP_TOKEN"];
// 檢查是否授權(quán)等
if ( $token == "test" ) {
$file = "/Users/Shared/nginx/private/keys/test.key"; // key所在的目錄或從數(shù)據(jù)庫獲取
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
// header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
http_response_code(401);
header('Content-Type: application/json');
echo json_encode([
"code" => 99,
"result" => NULL,
"message" => "invalidate username and password"
]);
}
?>
# 在nginx的配置文件nginx.conf內(nèi)的server塊中添加以下配置
rewrite ^/private/keys/(.+?).key$ /private_key.php last;
location / {
root /Users/Shared/nginx/www;
index index.html index.htm index.php;
}
location ^~ /files {
root /Users/Shared/nginx/;
}
location ~ \.php$ {
root /Users/Shared/nginx/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Users/Shared/nginx/www/$fastcgi_script_name;
include fastcgi_params;
}
客戶端實現(xiàn)
// ViewController.swift
import UIKit
import AVKit
class ViewController: UIViewController {
let dispatchQueue = DispatchQueue(label: "cn.wessonwu.demos.AVPlayDemo.resourceLoader")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func onPlayPressed(_ sender: Any) {
let vc = AVPlayerViewController()
let urlAsset = AVURLAsset(url: URL(string: "http://localhost:8080/files/test.m3u8")!)
urlAsset.resourceLoader.setDelegate(self, queue: dispatchQueue)
let playerItem = AVPlayerItem(asset: urlAsset)
vc.player = AVPlayer(playerItem: playerItem)
self.present(vc, animated: true, completion: nil)
}
func shouldLoadOrRenewRequestedResource(_ loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
guard let url = loadingRequest.request.url,
url.pathExtension == "key" else {
return false
}
let keyServerURL = url
var request = URLRequest(url: keyServerURL)
request.addValue("test", forHTTPHeaderField: "TOKEN") // 添加驗證(該例子只用了簡單的驗證)
URLSession.shared.dataTask(with: request) { (data, resp, error) in
self.dispatchQueue.async {
if let ckcData = data {
loadingRequest.dataRequest?.respond(with: ckcData)
loadingRequest.contentInformationRequest?.contentType = AVStreamingKeyDeliveryContentKeyType
loadingRequest.finishLoading()
} else {
loadingRequest.finishLoading(with: error)
}
}
}
.resume()
return true
}
}
extension ViewController: AVAssetResourceLoaderDelegate {
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
return shouldLoadOrRenewRequestedResource(loadingRequest)
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForRenewalOfRequestedResource renewalRequest: AVAssetResourceRenewalRequest) -> Bool {
return shouldLoadOrRenewRequestedResource(renewalRequest)
}
}
Enjoy it!!!