欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年11月09日 10:24:28   作者:ahaoIsMe  
這篇文章主要介紹了Android 邊播邊緩存的實(shí)現(xiàn)(MP4 未加密m3u8),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)現(xiàn)思路

紅色框的 ProxyServer就是需要實(shí)現(xiàn)的一個代理服務(wù)器。 當(dāng)客戶端拿到一個視頻的url(mp4或者m3u8)時(shí),通過proxyServer轉(zhuǎn)化為一個代理的url,然后請求代理服務(wù)器;代理服務(wù)器接收到客戶端的請求后,先查看本地是否存在緩存,如果不存在則向真實(shí)服務(wù)器發(fā)送請求,拿到結(jié)果后再存到本地。

實(shí)現(xiàn)重點(diǎn)

緩存是一個代理服務(wù)器的主要部分,所以這部分是一個重點(diǎn)。本設(shè)計(jì)的緩存是一個分片的LRU緩存。分片的好處是靈活方便做LRU。當(dāng)真實(shí)服務(wù)器返回一個大文件時(shí),我們在進(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的請求轉(zhuǎn)換為http的請求 ,并且替換域名為代理服務(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() {
        @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實(shí)現(xiàn)端口的監(jiān)聽,當(dā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中使用攔截器的模式,將請求分部處理

VideoTypeInterceptor 將代理的url 還原為真實(shí)的url
CacheInterceptor 用于緩存
ConnectInterceptor 建立代理服務(wù)器與真實(shí)服務(wù)器的連接
VideoTypeInterceptor 主要是針對m3u8類型,因?yàn)閙3u8會先返回一個m3u8的文件,文件里面記錄了每個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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論