Android 邊播邊緩存的實現(MP4 未加密m3u8)
實現思路
紅色框的 ProxyServer就是需要實現的一個代理服務器。 當客戶端拿到一個視頻的url(mp4或者m3u8)時,通過proxyServer轉化為一個代理的url,然后請求代理服務器;代理服務器接收到客戶端的請求后,先查看本地是否存在緩存,如果不存在則向真實服務器發送請求,拿到結果后再存到本地。
實現重點
緩存是一個代理服務器的主要部分,所以這部分是一個重點。本設計的緩存是一個分片的LRU緩存。分片的好處是靈活方便做LRU。當真實服務器返回一個大文件時,我們在進行切割后緩存在本地,并返回給客戶端,不用等待所有數據返回后再返回給客戶端。
使用方式
在app初始化的時候 創建代理服務器
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轉換為代理url就好了
String proxyUrl = APP.getVideoProxyServer().getLocalProxyUrl('https://sina.com-h-sina.com/20181024/21342_8f737b71/1000k/hls/index.m3u8'); videoView.setVideoPath(proxyUrl);
轉換的規則即講https的請求轉換為http的請求 ,并且替換域名為代理服務器的地址,將真實服務器的地址作為參數添加到代理url的后面。例如 sina.com-h-sina.com/20181024/21… 地址轉換后變成了 https://127.0.0.1:3260/20181024/21342_8f737b71/1000k/hls/index.m3u8?RealHostParam=sina.com-h-sina.com 3260是VideoCacheServer監聽的端口
實現細節
代理服務器的建立
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實現端口的監聽,當請求到來時,使用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 還原為真實的urlCacheInterceptor 用于緩存ConnectInterceptor 建立代理服務器與真實服務器的連接VideoTypeInterceptor 主要是針對m3u8類型,因為m3u8會先返回一個m3u8的文件,文件里面記錄了每個ts的地址,VideoTypeInterceptor就是將返回的文件中的ts地址轉換為代理服務器的地址
項目地址 https://github.com/ZhangHao555/VideoCacheServerDemo
到此這篇關于Android 邊播邊緩存的實現(MP4 未加密m3u8)的文章就介紹到這了,更多相關Android 邊播邊緩存內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: