-->

分類

2018年9月13日 星期四

Windows build WebRTC for unity plugin

just notes about building webrtc unity plugin

ref: https://webrtc.org/native-code/development/



System requirements

  • A 64-bit Intel machine with at least 8GB of RAM. More than 16GB is highly recommended.
  • At least 100GB of free disk space on an NTFS-formatted hard drive. FAT32 will not work, as some of the Git packfiles are larger than 4GB.
  • An appropriate version of Visual Studio, as described below.
  • Windows 7 or newer.

Setting up Windows

Visual Studio

As of September, 2017 (R503915) Chromium requires Visual Studio 2017 (15.7.2) to build. The clang-cl compiler is used but Visual Studio's header files, libraries, and some tools are required. Visual Studio Community Edition should work if its license is appropriate for you. You must install the “Desktop development with C++” component and the “MFC and ATL support” sub-component. This can be done from the command line by passing these arguments to the Visual Studio installer that you download:
--add Microsoft.VisualStudio.Workload.NativeDesktop
    --add Microsoft.VisualStudio.Component.VC.ATLMFC --includeRecommended
You must have the version 10.0.17134 Windows 10 SDK installed. This can be installed separately or by checking the appropriate box in the Visual Studio Installer.
The SDK Debugging Tools must also be installed. If the Windows 10 SDK was installed via the Visual Studio installer, then they can be installed by going to: Control Panel → Programs → Programs and Features → Select the “Windows Software Development Kit” → Change → Change → Check “Debugging Tools For Windows” → Change. Or, you can download the standalone SDK installer and use it to install the Debugging Tools.

Install depot_tools

Download the depot_tools bundle and extract it somewhere.
Warning: DO NOT use drag-n-drop or copy-n-paste extract from Explorer, this will not extract the hidden “.git” folder which is necessary for depot_tools to autoupdate itself. You can use “Extract all…” from the context menu though.
Add depot_tools to the start of your PATH (must be ahead of any installs of Python). Assuming you unzipped the bundle to C:\src\depot_tools, open:
Control Panel → System and Security → System → Advanced system settings
If you have Administrator access, Modify the PATH system variable and put C:\src\depot_tools at the front (or at least in front of any directory that might already have a copy of Python or Git).
If you don't have Administrator access, you can add a user-level PATH environment variable and put C:\src\depot_tools at the front, but if your system PATH has a Python in it, you will be out of luck.
Also, add a DEPOT_TOOLS_WIN_TOOLCHAIN system variable in the same way, and set it to 0. This tells depot_tools to use your locally installed version of Visual Studio (by default, depot_tools will try to use a google-internal version).
From a cmd.exe shell, run the command gclient (without arguments). On first run, gclient will install all the Windows-specific bits needed to work with the code, including msysgit and python.
  • If you run gclient from a non-cmd shell (e.g., cygwin, PowerShell), it may appear to run properly, but msysgit, python, and other tools may not get installed correctly.
  • If you see strange errors with the file system on the first run of gclient, you may want to disable Windows Indexing.
After running gclient open a command prompt and type where python and confirm that the depot_tools python.bat comes ahead of any copies of python.exe. Failing to ensure this can lead to overbuilding when using gn - see crbug.com/611087.

11/29/2018 update
Install Windows sdk then
1. add a LIB system variable then set its values as Windows SDK lib folders (e.g.  C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64,  C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt_enclave\x64, C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64, ...)

2. add a INCLUDE system variable then set its values as Windows SDK include folders (e.g. C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt, C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared, C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um, ...)

Get the code

First, configure Git:
$ git config --global user.name "My Name"
$ git config --global user.email "my-name@chromium.org"
$ git config --global core.autocrlf false
$ git config --global core.filemode false
$ git config --global branch.autosetuprebase always


Add System Variable or User Variable of Environment Variables: 
name: GYP_MSVS_OVERRIDE_PATH
value: path to MSVS 2017 (e.g. D:\VisualStudioInstall\2017\Community)

restart cmd

$mkdir webrtc-checkout
$cd webrtc-checkout
$webrtc-checkout>fetch --nohooks webrtc
$webrtc-checkout>gclient sync
$webrtc-checkout>git config branch.autosetupmerge always
$webrtc-checkout>git config branch.autosetuprebase always
$webrtc-checkout>cd src
$webrtc-checkout\src>git checkout master
$webrtc-checkout\src>git new-branch your-branch-name   
$webrtc-checkout\src>ninja -C out/Default webrtc_unity_plugin
$webrtc-checkout\src>gn gen --ide=vs out/VS //this will generate a file called "all.sln" in webrtc-checkout\src\out\VS, you can open it by MSVS

gn set args(optional):
$gn gen out/Default --args="target_os=\"win\" target_cpu=\"x64\""

then modify files in webrtc-checkout\src\examples\unityplugin

if new files are added, webrtc-checkout\src\examples\BUILD.gn needs to be modified. Suppose you add new files in webrtc-checkout\src\examples\unityplugin, then find the following part and add your new files:


if (is_win || is_android) {

  rtc_shared_library("webrtc_unity_plugin") {

    testonly = true

    sources = [

      "unityplugin/simple_peer_connection.cc",
      "unityplugin/simple_peer_connection.h",
      "unityplugin/unity_plugin_apis.cc",
      "unityplugin/unity_plugin_apis.h",
      "unityplugin/video_observer.cc",
      "unityplugin/video_observer.h",
      "unityplugin/your_new_file.cc",
      "unityplugin/your_new_file.h",
    ]

Updating the Code
Update your current branch with:

$webrtc-checkout\src>git checkout master
$webrtc-checkout\src>git pull origin master
$webrtc-checkout\src>gclient sync
$webrtc-checkout\src>git checkout my-branch
$webrtc-checkout\src>git merge master

To clean all build artifacts in a directory but leave the current GN configuration untouched (stored in the args.gn file), do:

$webrtc-checkout\src>gn clean out/Default

if you keep getting compile error, try
1. restart cmd
2.
$webrtc-checkout\src>gn clean out/Default
$webrtc-checkout\src>gn gen --ide=vs out/VS 
$webrtc-checkout\src>ninja -C out/Default webrtc_unity_plugin

Unity plugin trouble shooting:

Create data channel failed:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/discuss-webrtc/XRkuUS30ahc/aZ8yCuEFBgAJ

stun servers:
https://stackoverflow.com/questions/20068944/webrtc-stun-stun-l-google-com19302/20134888#20134888


some implementation details for webrtc Unity plugin:

Basically I just modify the src/examples/unityplugin

I created a customized VideoCapturer  (say MyVideoCapturer) and make SimplePeerConnection::OpenVideoCaptureDevice return MyVideoCapturer

(I need to create fake device and fake device module for MyVideoCapturer, see FakeWebRtcDeviceInfo class)

MyVideoCapturer takes unity texture's rgb data then convert it to I420 and call OnFrame to send the VideoFrame out

(Sorry, lazy to format the code) code snippet (data is rgba8888 byte array from Unity):
///////////////////////////////////////////////////
 //https://www.jianshu.com/p/050234c5fff2    this really helps!
 int yuvBufSize = width * height * 3 / 2;
  uint8_t* yuvBuf = new uint8_t[yuvBufSize];

  // source-stride
  int Dst_Stride_Y = width;
  const int uv_stride = (width + 1) / 2;

  // source-length
  const int y_length = width * height;
  int uv_length = uv_stride * ((height + 1) / 2);

  // source-data
  unsigned char* Y_data_Dst = yuvBuf;
  unsigned char* U_data_Dst = yuvBuf + y_length;
  unsigned char* V_data_Dst = U_data_Dst + uv_length;

  libyuv::ABGRToI420(data, width * 4, Y_data_Dst, Dst_Stride_Y,
                     U_data_Dst, uv_stride, V_data_Dst, uv_stride, width,
                     height);

  rtc::scoped_refptr<webrtc::I420Buffer> buffer =
      webrtc::I420Buffer::Copy(width, height, Y_data_Dst, Dst_Stride_Y, U_data_Dst, uv_stride, V_data_Dst, uv_stride);

  webrtc::VideoFrame frame = webrtc::VideoFrame::Builder().set_video_frame_buffer(buffer).build();

//video capturer OnFrame(frame)
////////////////////////////////////////////////////////////
kind of working...