Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8)
實(shí)現(xiàn)思路
紅色框的 ProxyServer就是需要實(shí)現(xiàn)的一個(gè)代理服務(wù)器。 當(dāng)客戶端拿到一個(gè)視頻的url(mp4或者m3u8)時(shí),通過proxyServer轉(zhuǎn)化為一個(gè)代理的url,然后請(qǐng)求代理服務(wù)器;代理服務(wù)器接收到客戶端的請(qǐng)求后,先查看本地是否存在緩存,如果不存在則向真實(shí)服務(wù)器發(fā)送請(qǐng)求,拿到結(jié)果后再存到本地。
實(shí)現(xiàn)重點(diǎn)
緩存是一個(gè)代理服務(wù)器的主要部分,所以這部分是一個(gè)重點(diǎn)。本設(shè)計(jì)的緩存是一個(gè)分片的LRU緩存。分片的好處是靈活方便做LRU。當(dāng)真實(shí)服務(wù)器返回一個(gè)大文件時(shí),我們?cè)谶M(jìn)行切割后緩存在本地,并返回給客戶端,不用等待所有數(shù)據(jù)返回后再返回給客戶端。
使用方式
在app初始化的時(shí)候 創(chuàng)建代理服務(wù)器
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; }}
代理服務(wù)建立好了 ,使用的時(shí)候只需要將真實(shí)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的請(qǐng)求轉(zhuǎn)換為http的請(qǐng)求 ,并且替換域名為代理服務(wù)器的地址,將真實(shí)服務(wù)器的地址作為參數(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)聽的端口
實(shí)現(xiàn)細(xì)節(jié)
代理服務(wù)器的建立
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() {@Overridepublic 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實(shí)現(xiàn)端口的監(jiān)聽,當(dāng)請(qǐng)求到來時(shí),使用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中使用攔截器的模式,將請(qǐng)求分部處理
VideoTypeInterceptor 將代理的url 還原為真實(shí)的urlCacheInterceptor 用于緩存ConnectInterceptor 建立代理服務(wù)器與真實(shí)服務(wù)器的連接VideoTypeInterceptor 主要是針對(duì)m3u8類型,因?yàn)閙3u8會(huì)先返回一個(gè)m3u8的文件,文件里面記錄了每個(gè)ts的地址,VideoTypeInterceptor就是將返回的文件中的ts地址轉(zhuǎn)換為代理服務(wù)器的地址
項(xiàng)目地址 https://github.com/ZhangHao555/VideoCacheServerDemo
到此這篇關(guān)于Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8)的文章就介紹到這了,更多相關(guān)Android 邊播邊緩存內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)6. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法7. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息8. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. CSS hack用法案例詳解
