WebRTC——VideoFileRenderer解析

VideoFileRenderer實現(xiàn)了VideoRenderer的Callbacks曹铃,可以回調(diào)到產(chǎn)生的Frame盒延。
它的主要功能是將產(chǎn)生的視頻流以文件的形式存儲在本地嘴拢。

  //定義一個新的工作的線程
  private final HandlerThread renderThread;

  //定義一個線程鎖
  private final Object handlerLock = new Object();

  //定義一個Handler
  private final Handler renderThreadHandler;

  //文件輸出的流
  private final FileOutputStream videoOutFile;

  //流的屬性
  private final int outputFileWidth;
  private final int outputFileHeight;

  //幀的數(shù)量
  private final int outputFrameSize;

  //一個輸出的Buffer
  private final ByteBuffer outputFrameBuffer;

  //全局的上下文
  private EglBase eglBase;

  //信號的轉(zhuǎn)化
  private YuvConverter yuvConverter;
//構(gòu)造方法,四個參數(shù)分別是目標文件眯勾,文件的寬,文件的高度婆誓,上下文
public VideoFileRenderer(String outputFile, int outputFileWidth, int outputFileHeight,
      final EglBase.Context sharedContext) throws IOException {
    //向外寫文件寬高都應(yīng)該是偶數(shù)
    if ((outputFileWidth % 2) == 1 || (outputFileHeight % 2) == 1) {
      throw new IllegalArgumentException("Does not support uneven width or height");
    }

    this.outputFileWidth = outputFileWidth;
    this.outputFileHeight = outputFileHeight;
    
    //計算frameSize
    outputFrameSize = outputFileWidth * outputFileHeight * 3 / 2;
    outputFrameBuffer = ByteBuffer.allocateDirect(outputFrameSize);

    //寫文件
    videoOutFile = new FileOutputStream(outputFile);
    videoOutFile.write(
        ("YUV4MPEG2 C420 W" + outputFileWidth + " H" + outputFileHeight + " Ip F30:1 A1:1\n")
            .getBytes());

    //開啟一個新線程
    renderThread = new HandlerThread(TAG);
    renderThread.start();
    renderThreadHandler = new Handler(renderThread.getLooper());

    ThreadUtils.invokeAtFrontUninterruptibly(renderThreadHandler, new Runnable() {
      @Override
      public void run() {
        //在新的線程中初始化全局吃环,并且進行編碼轉(zhuǎn)化
        eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_BUFFER);
        eglBase.createDummyPbufferSurface();
        eglBase.makeCurrent();
        yuvConverter = new YuvConverter();
      }
    });
  }

@Override
  public void renderFrame(final VideoRenderer.I420Frame frame) {
    renderThreadHandler.post(new Runnable() {
      @Override
      public void run() {
        //當有流產(chǎn)生的時候要進行回調(diào)4
        renderFrameOnRenderThread(frame);
      }
    });
  }

  private void renderFrameOnRenderThread(VideoRenderer.I420Frame frame) {
    //呈現(xiàn)畫面在新的線程中

    //畫面呈現(xiàn)的比例
    final float frameAspectRatio = (float) frame.rotatedWidth() / (float) frame.rotatedHeight();

    //旋轉(zhuǎn)抽樣矩陣
    final float[] rotatedSamplingMatrix =
        RendererCommon.rotateTextureMatrix(frame.samplingMatrix, frame.rotationDegree);


    final float[] layoutMatrix = RendererCommon.getLayoutMatrix(
        false, frameAspectRatio, (float) outputFileWidth / outputFileHeight);
    final float[] texMatrix = RendererCommon.multiplyMatrices(rotatedSamplingMatrix, layoutMatrix);

    //==========以下是圖像處理的代碼,真的看不太懂洋幻,日后鉆研明白再來解析====================
    try {
      videoOutFile.write("FRAME\n".getBytes());
      if (!frame.yuvFrame) {
        yuvConverter.convert(outputFrameBuffer, outputFileWidth, outputFileHeight, outputFileWidth,
            frame.textureId, texMatrix);

        int stride = outputFileWidth;
        byte[] data = outputFrameBuffer.array();
        int offset = outputFrameBuffer.arrayOffset();

        // Write Y
        videoOutFile.write(data, offset, outputFileWidth * outputFileHeight);

        // Write U
        for (int r = outputFileHeight; r < outputFileHeight * 3 / 2; ++r) {
          videoOutFile.write(data, offset + r * stride, stride / 2);
        }

        // Write V
        for (int r = outputFileHeight; r < outputFileHeight * 3 / 2; ++r) {
          videoOutFile.write(data, offset + r * stride + stride / 2, stride / 2);
        }
      } else {
        nativeI420Scale(frame.yuvPlanes[0], frame.yuvStrides[0], frame.yuvPlanes[1],
            frame.yuvStrides[1], frame.yuvPlanes[2], frame.yuvStrides[2], frame.width, frame.height,
            outputFrameBuffer, outputFileWidth, outputFileHeight);
        videoOutFile.write(
            outputFrameBuffer.array(), outputFrameBuffer.arrayOffset(), outputFrameSize);
      }
    } catch (IOException e) {
      Logging.e(TAG, "Failed to write to file for video out");
      throw new RuntimeException(e);
    } finally {
      VideoRenderer.renderFrameDone(frame);
    }
  }

//釋放資源郁轻,關(guān)閉文件等處理
public void release() {
    final CountDownLatch cleanupBarrier = new CountDownLatch(1);
    renderThreadHandler.post(new Runnable() {
      @Override
      public void run() {
        try {
          videoOutFile.close();
        } catch (IOException e) {
          Logging.d(TAG, "Error closing output video file");
        }
        yuvConverter.release();
        eglBase.release();
        renderThread.quit();
        cleanupBarrier.countDown();
      }
    });
    ThreadUtils.awaitUninterruptibly(cleanupBarrier);
  }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市文留,隨后出現(xiàn)的幾起案子好唯,更是在濱河造成了極大的恐慌,老刑警劉巖厂庇,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渠啊,死亡現(xiàn)場離奇詭異输吏,居然都是意外死亡权旷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門贯溅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拄氯,“玉大人,你說我怎么就攤上這事它浅∫氚兀” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵姐霍,是天一觀的道長鄙麦。 經(jīng)常有香客問我典唇,道長,這世上最難降的妖魔是什么胯府? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任介衔,我火速辦了婚禮,結(jié)果婚禮上骂因,老公的妹妹穿的比我還像新娘炎咖。我一直安慰自己,他們只是感情好寒波,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布乘盼。 她就那樣靜靜地躺著,像睡著了一般俄烁。 火紅的嫁衣襯著肌膚如雪绸栅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天页屠,我揣著相機與錄音阴幌,去河邊找鬼。 笑死卷中,一個胖子當著我的面吹牛矛双,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蟆豫,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼议忽,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了十减?” 一聲冷哼從身側(cè)響起栈幸,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎帮辟,沒想到半個月后速址,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡由驹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年芍锚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蔓榄。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡并炮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出甥郑,到底是詐尸還是另有隱情逃魄,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布澜搅,位于F島的核電站伍俘,受9級特大地震影響邪锌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜癌瘾,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一秃流、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧柳弄,春花似錦舶胀、人聲如沸碧注。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽基茵。三九已至拱层,卻和暖如春宴咧,著一層夾襖步出監(jiān)牢的瞬間掺栅,已是汗流浹背氧卧。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工沙绝, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人脚祟。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓为黎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子炕檩,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容