George Zhou | 2770c3d | 2018-03-07 09:58:54 -0800 | [diff] [blame] | 1 | Instruction of running webrtc_unity_plugin on Android Unity |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 2 | |
| 3 | 1. On Linux machine, compile target webrtc_unity_plugin. |
| 4 | Checkout WebRTC codebase: fetch --no-hooks webrtc_android |
| 5 | If you already have a checkout for linux, add target_os=”android” into .gclient file. |
| 6 | Run gclient sync |
| 7 | Run gn args out/Android, and again set target_os=”android” in the args.gn |
gyzhou | 3abb738 | 2017-08-10 16:31:22 -0700 | [diff] [blame] | 8 | Run ninja -C out/Android webrtc_unity_plugin |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 9 | |
George Zhou | 2770c3d | 2018-03-07 09:58:54 -0800 | [diff] [blame] | 10 | 2. On Linux machine, build target libwebrtc_unity under webrtc checkout. This is the java code for webrtc to work on Android. |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 11 | |
| 12 | 3. Copy libwebrtc_unity.jar and libwebrtc_unity_plugin.so into Unity project folder, under Assets/Plugins/Android folder. |
| 13 | |
George Zhou | 2770c3d | 2018-03-07 09:58:54 -0800 | [diff] [blame] | 14 | 4. Rename libwebrtc_unity_plugin.so to libjingle_peerconnection_so.so. This is hacky, and the purpose is to let the java code in libwebrtc_unity.jar to find their JNI implementations. Simultaneously, in your C# wrapper script for the native plugin libjingle_peerconnection_so.so, the dll_path should be set to “jingle_peerconnection_so”. |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 15 | |
| 16 | 5. In the Unity Main Scene’s Start method, write the following code to initialize the Java environment for webrtc (otherwise, webrtc will not be able to access audio device or camera from C++ code): |
| 17 | |
| 18 | #if UNITY_ANDROID |
George Zhou | 2770c3d | 2018-03-07 09:58:54 -0800 | [diff] [blame] | 19 | AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); |
| 20 | AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); |
| 21 | AndroidJavaClass utilityClass = new AndroidJavaClass("org.webrtc.UnityUtility"); |
| 22 | utilityClass.CallStatic("InitializePeerConncectionFactory", new object[1] { activity }); |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
gyzhou | 3abb738 | 2017-08-10 16:31:22 -0700 | [diff] [blame] | 25 | 6. Compile the unity project into an APK, and decompile the apk using apktool that you can download from https://ibotpeaches.github.io/Apktool/ |
| 26 | Run apktool d apkname.apk. |
| 27 | Then copy the AndroidManifest.xml in the decompiled folder to the Assets/Plugins/Android folder, and add two lines: |
| 28 | <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
| 29 | <uses-permission android:name="android.permission.CAMERA" /> |
qiangchen | 42f96d5 | 2017-08-08 17:08:03 -0700 | [diff] [blame] | 30 | |
George Zhou | 2770c3d | 2018-03-07 09:58:54 -0800 | [diff] [blame] | 31 | The purpose of using apktool is to get a well-written android manifest xml file. If you know how to write manifest file from scratch, you can skip using apktool. |
gyzhou | 3abb738 | 2017-08-10 16:31:22 -0700 | [diff] [blame] | 32 | |
| 33 | 7. Compile the unity project into an APK again and deploy it to an android device. |