Android 邊播邊緩存的實現(xiàn)(MP4 未加密m3u8)
實現(xiàn)思路

紅色框的 ProxyServer就是需要實現(xiàn)的一個代理服務器。 當客戶端拿到一個視頻的url(mp4或者m3u8)時,通過proxyServer轉(zhuǎn)化為一個代理的url,然后請求代理服務器;代理服務器接收到客戶端的請求后,先查看本地是否存在緩存,如果不存在則向真實服務器發(fā)送請求,拿到結(jié)果后再存到本地。
實現(xiàn)重點
緩存是一個代理服務器的主要部分,所以這部分是一個重點。本設計的緩存是一個分片的LRU緩存。分片的好處是靈活方便做LRU。當真實服務器返回一個大文件時,我們在進行切割后緩存在本地,并返回給客戶端,不用等待所有數(shù)據(jù)返回后再返回給客戶端。
使用方式
在app初始化的時候 創(chuàng)建代理服務器
public class APP extends Application {
private static VideoCacheServer videoCacheServer;
@Override
public void onCreate() {
super.onCreate();
if (videoCacheServer == null) {
// 緩存路徑
String cachePath = getCacheDir().getAbsolutePath();
// 緩存大小 1024 * 1024 * 500
videoCacheServer = new VideoCacheServer(cachePath, 1024 * 1024 * 500);
}
}
public static VideoCacheServer getVideoProxyServer() {
return videoCacheServer;
}
}
代理服務建立好了 ,使用的時候只需要將真實url轉(zhuǎn)換為代理url就好了
String proxyUrl = APP.getVideoProxyServer().getLocalProxyUrl("https://sina.com-h-sina.com/20181024/21342_8f737b71/1000k/hls/index.m3u8");
videoView.setVideoPath(proxyUrl);
轉(zhuǎn)換的規(guī)則即講https的請求轉(zhuǎn)換為http的請求 ,并且替換域名為代理服務器的地址,將真實服務器的地址作為參數(shù)添加到代理url的后面。
例如 sina.com-h-sina.com/20181024/21… 地址轉(zhuǎn)換后變成了 https://127.0.0.1:3260/20181024/21342_8f737b71/1000k/hls/index.m3u8?RealHostParam=sina.com-h-sina.com 3260是VideoCacheServer監(jiān)聽的端口
實現(xiàn)細節(jié)
代理服務器的建立
public class VideoCacheServer{
private ExecutorService pool = Executors.newFixedThreadPool(20);
public int start() {
if (isRunning) {
return curPort;
}
curPort = new Random().nextInt(65535);
try {
final ServerSocket server = new ServerSocket(curPort);
isRunning = true;
singleService.submit(new Runnable() {
@Override
public void run() {
while (isRunning) {
try {
Socket connection = server.accept();
connection.setKeepAlive(true);
pool.submit(new ProxyHandler(connection));
} catch (IOException ex) {
if (Constant.enableLog) {
logger.log(Level.WARNING, "Exception accepting connection", ex);
}
} catch (Exception ex) {
if (Constant.enableLog) {
logger.log(Level.SEVERE, "Unexpected error", ex);
}
}
}
}
});
return curPort;
} catch (IOException e) {
e.printStackTrace();
return start();
}
}
}
通過socket實現(xiàn)端口的監(jiān)聽,當請求到來時,使用ProxyHandler來處理。
public class ProxyHandler implements Runnable {
private Socket realClientSocket;
ProxyHandler(Socket realClientSocket) {
this.realClientSocket = realClientSocket;
}
@Override
public void run() {
try {
BufferedOutputStream outputStream = new BufferedOutputStream(realClientSocket.getOutputStream());
BufferedInputStream inputStream = new BufferedInputStream(realClientSocket.getInputStream());
HttpRequest realRequest = HttpRequest.parse(inputStream);
HttpResponse response = getResponseWithInterceptorChain(realRequest);
writeResponseAndClose(response, outputStream);
} catch (Exception e) {
if (Constant.enableLog) {
logger.log(Level.SEVERE, "error proxy ", e);
}
} finally {
CloseUtil.close(realClientSocket);
}
}
private HttpResponse getResponseWithInterceptorChain(HttpRequest realRequest) {
List<Interceptor> interceptors = new ArrayList<>();
interceptors.add(new VideoTypeInterceptor());
interceptors.add(new HostFilterInterceptor(curPort));
interceptors.add(new CacheInterceptor(diskCache));
interceptors.add(new ConnectInterceptor());
InterceptorChain interceptorChain = new InterceptorChain(interceptors, realRequest, 0);
return interceptorChain.proceed(realRequest);
}
}
ProxyHandler中使用攔截器的模式,將請求分部處理
VideoTypeInterceptor 將代理的url 還原為真實的url
CacheInterceptor 用于緩存
ConnectInterceptor 建立代理服務器與真實服務器的連接
VideoTypeInterceptor 主要是針對m3u8類型,因為m3u8會先返回一個m3u8的文件,文件里面記錄了每個ts的地址,VideoTypeInterceptor就是將返回的文件中的ts地址轉(zhuǎn)換為代理服務器的地址
項目地址 https://github.com/ZhangHao555/VideoCacheServerDemo
到此這篇關于Android 邊播邊緩存的實現(xiàn)(MP4 未加密m3u8)的文章就介紹到這了,更多相關Android 邊播邊緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
這篇文章主要為大家介紹了Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
Android瀑布流照片墻實現(xiàn) 體驗不規(guī)則排列的美感
這篇文章主要為大家詳細介紹了Android瀑布流照片墻實現(xiàn),體驗不規(guī)則排列的美感,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

