webrtc編譯

最近研究 libmediasoupclient,而 libmediasoupclient 又依賴 libwebrtc,所以首先就想著先在windows上編譯一個webrtc的庫,先是在網上找了一大堆,發現都不全面,導致各種問題,這裡做個歸納總結。

由於 webrtc 是google的產物,而且很多東西都是自成一家,所以前提是需要FQ的。準備一個本地的代理,例如127.0.0.1:10080

windows

掛代理

代理需要掛在git和cmd下,後面通過git拉取depot_tools和webrtc都要藉助git的代理,而掛在cmd下是為了在depot_tools下載好之後安裝一系列工具鏈時下載用。

git

通過git拉取程式碼有http、https、ssh三種方式,對於這三種方式要分兩種方式掛代理。詳細可參考Git設定代理。本文後續使用的是https方式,所以只需要配置http和https即可。

  • http和https

    • git命令方式

      這裡只針對後續可能使用的url地址使用代理,如果不帶特定的url的話,會作用於全域性。
      1. git config --global http.https://*.googlesource.com.proxy http://127.0.0.1:10080
      2. git config --global https.https://*.googlesource.com.proxy http://127.0.0.1:10080
      3. git config --global http.https://*.appspot.com.proxy http://127.0.0.1:10080
      4. git config --global https.https://*.appspot.com.proxy http://127.0.0.1:10080
    • 修改git配置檔案

      修改git的配置檔案 .gitconfig
      1. [http "https://*.googlesource.com"]
      2. proxy = http://127.0.0.1:10080
      3. [https "https://*.googlesource.com"]
      4. proxy = http://127.0.0.1:10080
      5. [http "https://*.appspot.com"]
      6. proxy = http://127.0.0.1:10080
      7. [https "https://*.appspot.com"]
      8. proxy = http://127.0.0.1:10080
  • ssh

    修改 .ssh 資料夾下的 config 檔案
    1. Host googlesource.com
    2. User git
    3. ProxyCommand connect -H 127.0.0.1:10080 %h %p

    如果是socks5代理的話,最後的選項 -H 改成 -S

cmd

在cmd中直接設定環境變數,這兩條命令只在當前視窗有效!

  1. set http_proxy=127.0.0.1:10080
  2. set https_proxy=127.0.0.1:10080

安裝depot_tools

按照webrtc原始碼編譯要求的,編譯webrtc之前的準備工作需要安裝google自家的depot_tools工具鏈。這個工具鏈的安裝部署直接關係到後面是否能順利下載webrtc的原始碼。

按照depot_tools安裝描述的過程,大致分為以下幾步:

  1. 獲取 depot_tools

    • 使用 git 工具直接從google原始碼庫中拉取

      1. git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    • 直接下載depot_tools包並解壓(解壓時要以防某些解壓工具不將壓縮包中的隱藏檔案一併解壓!) https://storage.googleapis.com/chrome-infra/depot_tools.zip

  2. 設定環境變數

    將depot_tools資料夾所在路徑設定在系統環境變數 PATH 的最前面!

  3. 執行 gclient

    通過cmd執行gclient,注意在此之前設定了cmd的代理!這條命令執行完成後,depot_tools目錄下會多出很多東西,像python、git等工具都會被下載並安裝在depot_tools的子目錄下,被用於後續的過程,所以即使本地沒有安裝這些工具也不要緊。

    這條指令執行完後,大致會下載700-800左右的內容,最後會提示錯誤,因為後續只需要用來同步webrtc原始碼以及構建編譯指令碼等,所以忽略這個報錯即可。

    WARNING: Your metrics.cfg file was invalid or nonexistent. A new one will be created.

    Error: client not configured; see 'gclient config'

  4. 建立chrome/4147d的分支

    1. git checkout -b m84 origin/chrome/4147

    並再次執行 gclient sync

編譯webrtc

下載原始碼

按照以下命令一步一步執行,第三步的fetch最花費時間,大概需要下載10G的內容,所以留意你代理的流量套餐!

  1. mkdir webrtc
  2. cd webrtc
  3. fetch --nohooks webrtc
  4. gclient sync

編譯

我這裡按照mediasoap要求的m84版本的webrtc編譯為例

  1. 先建立m84版本(branch-heads/4147)的分支m84,然後執行 gclient sync

    1. git checkout -b m84 refs/remotes/branch-heads/4147
  2. 在環境變數中先設定 DEPOT_TOOLS_WIN_TOOLCHAIN 為0,否則在生成ninja工程時會報錯

    1. set DEPOT_TOOLS_WIN_TOOLCHAIN=0
  3. 生成ninja工程

    1. gn gen out/m84 --args='is_debug=false is_component_build=false is_clang=false rtc_include_tests=false rtc_use_h264=true use_rtti=true use_custom_libcxx=false treat_warnings_as_errors=false'
  4. 編譯

    1. ninja -C out/m84 >> compile.log
  5. 編譯過程中出現錯誤終端,開啟compile.log檔案檢視失敗的地方:

    1. [1466/3605] CC obj/third_party/ffmpeg/ffmpeg_internal/pcm.obj
    2. FAILED: obj/third_party/ffmpeg/ffmpeg_internal/pcm.obj
    3. ...
    4. ...
    5. ...
    6. ../../third_party/ffmpeg/libavcodec/pcm.c(623): error C2059: 語法錯誤:“字串”

    這裡我將原始檔中對應報錯的行註釋掉。

  6. 繼續執行第4步,然後又出現下列錯誤

    1. [1834/2133] CXX obj/modules/video_coding/webrtc_h264/h264.obj
    2. FAILED: obj/modules/video_coding/webrtc_h264/h264.obj
    3. ...
    4. ...
    5. ...
    6. E:\google\webrtc\src\modules/video_coding/codecs/h264/h264_decoder_impl.h(21): fatal error C1189: #error: "See: bugs.webrtc.org/9213#c13."

    仍然註釋掉報錯行之後繼續第4步,發現後續還會出現類似的報錯,所以這裡一次性註釋掉另外三個檔案中的行:See: bugs.webrtc.org/9213#c13.。三個檔案分別是:

    • webrtc\src\modules/video_coding/codecs/h264/h264_decoder_impl.h(21)
    • webrtc\src\modules/video_coding/codecs/h264/h264_encoder_impl.h(21)
    • webrtc\src\modules/video_coding/codecs/h264/h264_color_space.h(20)
  7. 最終生成結果顯示一大堆連結錯誤

    1. [286/289] LINK peerconnection_client.exe peerconnection_client.exe.pdb
    2. FAILED: peerconnection_client.exe peerconnection_client.exe.pdb
    3. ...
    4. error LNK2001: 無法解析的外部符號 avpriv_emms_asm
    5. ...
    6. ./peerconnection_client.exe : fatal error LNK1120: 1 個無法解析的外部命令
    7. [287/289] LINK(DLL) webrtc_unity_plugin.dll webrtc_unity_plugin.dll.lib webrtc_unity_plugin.dll.pdb
    8. FAILED: webrtc_unity_plugin.dll webrtc_unity_plugin.dll.lib webrtc_unity_plugin.dll.pdb
    9. ...
    10. error LNK2001: 無法解析的外部符號 avpriv_emms_as
    11. ...
    12. ./webrtc_unity_plugin.dll : fatal error LNK1120: 1 個無法解析的外部命令

    忽略,反正在out/m84/obj目錄下已經生成我要的webrtc.lib檔案了。

Linux

安裝depot_tools

  1. 拉取depot_tools

    先設定git的代理,然後拉取depot_tools的程式碼,之後設定depot_tools的路徑到 PATH 環變數。

  2. gclient sync

    在depot_tools目錄下執行 gclient sync 之前,先做兩件事:

    • 修改 update_depot_tools 檔案

      將depot_tools目錄下的 update_depot_tools 檔案中關於使用root使用者時就退出的地方去掉,否則執行這條指令的時候會提示Running depot tools as root is sad.,然後就沒了反應。
    • 設定 curl 或者整個linux環境代理

      這裡直接設定整個linux環境的代理,否則執行這條指令之後會提示curl https://chrome-infra-packages.appspot.com/ 失敗之類的提示。

      可以在 ~/.bashrc 檔案中最後加入下列兩條,之後每次進入終端後,輸入命令setproxy就可以了。
      1. alias setproxy="export http_proxy=http://10.18.0.60:10080;export https_proxy=http://10.18.0.60:10080;echo 'HTTP Proxy on';"
      2. alias unsetproxy="unset http_proxy; unset https_proxy; echo 'HTTP Proxy off';"

編譯webrtc

參考windows上編譯webrtc的步驟。

ubuntu上gcc版本最好在6.3之上,並且pkg-config和python已經安裝,若pkg-config未安裝,則會在生成ninja工程時報錯,若gcc版本過低,則在編譯時會報錯!

參考

WebRTC development

WebRTC Development

depot_tools_tutorial

Git設定代理

curl 設定代理

Windows配置代理下載depot_tools

在win10上編譯webRTC(問題篇)

Windows上WebRTC的原始碼下載和編譯

Windows下webrtc.lib 的編譯及vs工程中使用