WebRTC——VideoRenderer解析

VideoRenderer的目的是讓鏈接定義他們自己的渲染行為侨歉,這個是通過回調(diào)產(chǎn)生的,這個方法同樣體統(tǒng)了一個創(chuàng)建GUI的方法,用來創(chuàng)建GUI渲染器在各種各樣的平臺上面。
需要注意的是痒筒,frame只能通過native層進行構(gòu)建。

  //這是I420的一個對象的類茬贵,I420是視頻編碼的一種方式
  public static class I420Frame {
    public final int width;
    public final int height;
    public int[] yuvStrides;      //信號的頻幅
    public ByteBuffer[] yuvPlanes;    //平面的色差信號
    public final boolean yuvFrame;    //是否有幀的色差信號
    // Matrix that transforms standard coordinates to their proper sampling locations in
    // the texture. This transform compensates for any properties of the video source that
    // cause it to appear different from a normalized texture. This matrix does not take
    // |rotationDegree| into account.
    //抽樣矩陣
    //將標準坐標轉(zhuǎn)換為紋理中適當采樣位置的矩陣簿透。該轉(zhuǎn)換補償視頻源的任何屬性,使其與標準化紋理不同解藻。這個矩陣不采取rotationdegree考慮
    public final float[] samplingMatrix;
    //結(jié)構(gòu)Id
    public int textureId;
    // Frame pointer in C++.指針
    private long nativeFramePointer;

    // rotationDegree is the degree that the frame must be rotated clockwisely
    // to be rendered correctly.
    //旋轉(zhuǎn)的角度應(yīng)該是以順時針的角度為標準
    public int rotationDegree;

    /**
     * Construct a frame of the given dimensions with the specified planar data.
     */
    //構(gòu)造方法老充,并且旋轉(zhuǎn)的角度必須是90的整數(shù)倍
    I420Frame(int width, int height, int rotationDegree, int[] yuvStrides, ByteBuffer[] yuvPlanes,
        long nativeFramePointer) {
      this.width = width;
      this.height = height;
      this.yuvStrides = yuvStrides;
      this.yuvPlanes = yuvPlanes;
      this.yuvFrame = true;
      this.rotationDegree = rotationDegree;
      this.nativeFramePointer = nativeFramePointer;
      if (rotationDegree % 90 != 0) {
        throw new IllegalArgumentException("Rotation degree not multiple of 90: " + rotationDegree);
      }
      // The convention in WebRTC is that the first element in a ByteBuffer corresponds to the
      // top-left corner of the image, but in glTexImage2D() the first element corresponds to the
      // bottom-left corner. This discrepancy is corrected by setting a vertical flip as sampling
      // matrix.
      // clang-format off
      samplingMatrix = new float[] {
          1,  0, 0, 0,
          0, -1, 0, 0,
          0,  0, 1, 0,
          0,  1, 0, 1};
      // clang-format on
    }

    /**
     * Construct a texture frame of the given dimensions with data in SurfaceTexture
     */
    //另一個構(gòu)造方法,只是需要手動傳入一個矩陣
    I420Frame(int width, int height, int rotationDegree, int textureId, float[] samplingMatrix,
        long nativeFramePointer) {
      this.width = width;
      this.height = height;
      this.yuvStrides = null;
      this.yuvPlanes = null;
      this.samplingMatrix = samplingMatrix;
      this.textureId = textureId;
      this.yuvFrame = false;
      this.rotationDegree = rotationDegree;
      this.nativeFramePointer = nativeFramePointer;
      if (rotationDegree % 90 != 0) {
        throw new IllegalArgumentException("Rotation degree not multiple of 90: " + rotationDegree);
      }
    }

    //獲取寬和高
    public int rotatedWidth() {
      return (rotationDegree % 180 == 0) ? width : height;
    }

    public int rotatedHeight() {
      return (rotationDegree % 180 == 0) ? height : width;
    }

    @Override
    public String toString() {
      return width + "x" + height + ":" + yuvStrides[0] + ":" + yuvStrides[1] + ":" + yuvStrides[2];
    }
  }
  //釋放掉所有frame的做法
  public static void renderFrameDone(I420Frame frame) {
    frame.yuvPlanes = null;
    frame.textureId = 0;
    if (frame.nativeFramePointer != 0) {
      releaseNativeFrame(frame.nativeFramePointer);
      frame.nativeFramePointer = 0;
    }
  }
long nativeVideoRenderer;

//構(gòu)造方法螟左,需要傳進來一個callbacks
public VideoRenderer(Callbacks callbacks) {
    nativeVideoRenderer = nativeWrapVideoRenderer(callbacks);
  }
  //銷毀掉所有的方法
  public void dispose() {
    if (nativeVideoRenderer == 0) {
      // Already disposed.
      return;
    }

    freeWrappedVideoRenderer(nativeVideoRenderer);
    nativeVideoRenderer = 0;
  }
//native層的初始化的方法
private static native long nativeWrapVideoRenderer(Callbacks callbacks);
//銷毀
private static native void freeWrappedVideoRenderer(long nativeVideoRenderer);
//釋放
private static native void releaseNativeFrame(long nativeFramePointer);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末啡浊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子路狮,更是在濱河造成了極大的恐慌虫啥,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奄妨,死亡現(xiàn)場離奇詭異涂籽,居然都是意外死亡,警方通過查閱死者的電腦和手機砸抛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門评雌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人直焙,你說我怎么就攤上這事景东。” “怎么了奔誓?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵斤吐,是天一觀的道長。 經(jīng)常有香客問我厨喂,道長和措,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任蜕煌,我火速辦了婚禮派阱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘斜纪。我一直安慰自己贫母,他們只是感情好文兑,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著腺劣,像睡著了一般绿贞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上誓酒,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天樟蠕,我揣著相機與錄音,去河邊找鬼靠柑。 笑死寨辩,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的歼冰。 我是一名探鬼主播靡狞,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼隔嫡!你這毒婦竟也來了甸怕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤腮恩,失蹤者是張志新(化名)和其女友劉穎梢杭,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秸滴,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡武契,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荡含。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咒唆。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖释液,靈堂內(nèi)的尸體忽然破棺而出全释,到底是詐尸還是另有隱情,我是刑警寧澤误债,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布浸船,位于F島的核電站,受9級特大地震影響寝蹈,放射性物質(zhì)發(fā)生泄漏糟袁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一躺盛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧形帮,春花似錦槽惫、人聲如沸周叮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仿耽。三九已至,卻和暖如春各薇,著一層夾襖步出監(jiān)牢的瞬間项贺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工峭判, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留开缎,地道東北人。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓林螃,卻偏偏與公主長得像奕删,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子疗认,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355

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