|
@@ -1,15 +1,140 @@
|
1
|
1
|
package com.syan.agora;
|
2
|
2
|
|
|
3
|
+import android.content.Context;
|
|
4
|
+import android.os.Handler;
|
3
|
5
|
import android.util.Log;
|
|
6
|
+import android.view.SurfaceView;
|
|
7
|
+import android.view.View;
|
4
|
8
|
|
5
|
9
|
import com.facebook.react.bridge.ReactApplicationContext;
|
6
|
10
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
7
|
11
|
import com.facebook.react.bridge.ReactMethod;
|
8
|
12
|
import com.facebook.react.bridge.ReadableMap;
|
|
13
|
+import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
14
|
+import com.facebook.react.uimanager.NativeViewHierarchyManager;
|
|
15
|
+import com.facebook.react.uimanager.UIBlock;
|
|
16
|
+import com.facebook.react.uimanager.UIManagerModule;
|
|
17
|
+import com.facebook.react.views.view.ReactViewGroup;
|
|
18
|
+
|
|
19
|
+import io.agora.rtc.Constants;
|
|
20
|
+import io.agora.rtc.IRtcEngineEventHandler;
|
|
21
|
+import io.agora.rtc.RtcEngine;
|
|
22
|
+import io.agora.rtc.video.VideoCanvas;
|
9
|
23
|
|
10
|
24
|
public class AgoraModule extends ReactContextBaseJavaModule {
|
11
|
25
|
|
|
26
|
+ private RtcEngine mRtcEngine;
|
|
27
|
+
|
|
28
|
+ private Context mContext;
|
|
29
|
+
|
|
30
|
+ private static ReactApplicationContext mRAC;
|
|
31
|
+
|
|
32
|
+ private IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
|
|
33
|
+
|
|
34
|
+ /**
|
|
35
|
+ * 当获取用户uid的远程视频的回调
|
|
36
|
+ */
|
|
37
|
+ @Override
|
|
38
|
+ public void onFirstRemoteVideoDecoded(final int uid, int width, int height, int elapsed) {
|
|
39
|
+// runOnUiThread(new Runnable() {
|
|
40
|
+// @Override
|
|
41
|
+// public void run() {
|
|
42
|
+// mRAC
|
|
43
|
+// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
44
|
+// .emit("onFirstRemoteVideoDecoded", uid);
|
|
45
|
+// }
|
|
46
|
+// });
|
|
47
|
+
|
|
48
|
+ new Handler().post(new Runnable() {
|
|
49
|
+ @Override
|
|
50
|
+ public void run() {
|
|
51
|
+ mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
52
|
+ .emit("onFirstRemoteVideoDecoded", uid);
|
|
53
|
+ }
|
|
54
|
+ });
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /**
|
|
61
|
+ * 加入频道成功的回调
|
|
62
|
+ */
|
|
63
|
+ @Override
|
|
64
|
+ public void onJoinChannelSuccess(String channel,final int uid, int elapsed) {
|
|
65
|
+
|
|
66
|
+ Log.i("Agora", "加入房间成功---");
|
|
67
|
+ mRAC
|
|
68
|
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
69
|
+ .emit("onJoinChannelSuccess",uid);
|
|
70
|
+
|
|
71
|
+// new Handler().post(new Runnable() {
|
|
72
|
+// @Override
|
|
73
|
+// public void run() {
|
|
74
|
+// mRAC
|
|
75
|
+// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
76
|
+// .emit("onJoinChannelSuccess",uid);
|
|
77
|
+// }
|
|
78
|
+// });
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ /**
|
|
84
|
+ * 其他用户加入当前频道
|
|
85
|
+ */
|
|
86
|
+ @Override
|
|
87
|
+ public void onUserJoined(final int uid, int elapsed) {
|
|
88
|
+
|
|
89
|
+ Log.i("Agora", "有人来了----");
|
|
90
|
+
|
|
91
|
+ new Handler().post(new Runnable() {
|
|
92
|
+ @Override
|
|
93
|
+ public void run() {
|
|
94
|
+ mRAC
|
|
95
|
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
96
|
+ .emit("onUserJoined", uid);
|
|
97
|
+ }
|
|
98
|
+ });
|
|
99
|
+
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ /**
|
|
103
|
+ * 错误信息
|
|
104
|
+ */
|
|
105
|
+ @Override
|
|
106
|
+ public void onError(int err) {
|
|
107
|
+ Log.i("Agora", err + "错误---");
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ /**
|
|
111
|
+ * 警告
|
|
112
|
+ */
|
|
113
|
+ @Override
|
|
114
|
+ public void onWarning(int warn) {
|
|
115
|
+ Log.i("Agora", warn + "警告---");
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ /**
|
|
119
|
+ * 退出频道
|
|
120
|
+ */
|
|
121
|
+ @Override
|
|
122
|
+ public void onLeaveChannel(RtcStats stats) {
|
|
123
|
+
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * 用户uid离线时的回调
|
|
128
|
+ */
|
|
129
|
+ @Override
|
|
130
|
+ public void onUserOffline(int uid, int reason) {
|
|
131
|
+
|
|
132
|
+ }
|
|
133
|
+ };
|
|
134
|
+
|
|
135
|
+
|
12
|
136
|
public AgoraModule(ReactApplicationContext context) {
|
|
137
|
+
|
13
|
138
|
super(context);
|
14
|
139
|
}
|
15
|
140
|
|
|
@@ -18,9 +143,111 @@ public class AgoraModule extends ReactContextBaseJavaModule {
|
18
|
143
|
return "RCTAgora";
|
19
|
144
|
}
|
20
|
145
|
|
|
146
|
+
|
21
|
147
|
@ReactMethod
|
22
|
148
|
public void loadAgoraKit(ReadableMap options) {
|
23
|
149
|
Log.i("Agora", options.toString());
|
|
150
|
+
|
|
151
|
+ mContext = getCurrentActivity();
|
|
152
|
+ mRAC = getReactApplicationContext();
|
|
153
|
+
|
|
154
|
+ mRtcEngine = RtcEngine.create(mContext, options.getString("appid"), mRtcEventHandler);
|
|
155
|
+ mRtcEngine.enableVideo();
|
|
156
|
+// mRtcEngine.setChannelProfile(options.getInt("channelProfile"));
|
|
157
|
+// mRtcEngine.setVideoProfile(options.getInt("videoProfile"), true);
|
|
158
|
+
|
|
159
|
+ mRtcEngine.enableWebSdkInteroperability(true);
|
|
160
|
+ mRtcEngine.setVideoProfile(Constants.VIDEO_PROFILE_360P, false);
|
|
161
|
+ mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
|
|
162
|
+
|
|
163
|
+ mRtcEngine.joinChannel(null, options.getString("channelName"), options.getString("info"), 0);
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ @ReactMethod
|
|
170
|
+ public void setupLocalVideo(final int uid, final int tag) {
|
|
171
|
+
|
|
172
|
+ final UIManagerModule uiManager = getReactApplicationContext().getNativeModule
|
|
173
|
+ (UIManagerModule.class);
|
|
174
|
+ uiManager.addUIBlock(new UIBlock() {
|
|
175
|
+ @Override
|
|
176
|
+ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
|
|
177
|
+
|
|
178
|
+ ReactViewGroup dView = (ReactViewGroup)nativeViewHierarchyManager.resolveView(tag);
|
|
179
|
+
|
|
180
|
+ Log.i("Agora", dView.getWidth() + "dView");
|
|
181
|
+ Log.i("Agora", dView.getHeight() + "dView");
|
|
182
|
+
|
|
183
|
+ SurfaceView surfaceView = RtcEngine.CreateRendererView(getReactApplicationContext());
|
|
184
|
+
|
|
185
|
+ Log.i("Agora", surfaceView.getWidth() + "surfaceView");
|
|
186
|
+ Log.i("Agora", getReactApplicationContext().toString());
|
|
187
|
+ Log.i("Agora", surfaceView.toString());
|
|
188
|
+
|
|
189
|
+ dView.addView(surfaceView);
|
|
190
|
+
|
|
191
|
+ mRtcEngine.setupLocalVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_HIDDEN, uid));
|
|
192
|
+ mRtcEngine.startPreview();
|
|
193
|
+
|
|
194
|
+ }
|
|
195
|
+ });
|
24
|
196
|
}
|
25
|
197
|
|
|
198
|
+
|
|
199
|
+ @ReactMethod
|
|
200
|
+ public void setupRemoteVideo(final int uid, final int tag) {
|
|
201
|
+
|
|
202
|
+ final UIManagerModule uiManager = getReactApplicationContext().getNativeModule
|
|
203
|
+ (UIManagerModule.class);
|
|
204
|
+ uiManager.addUIBlock(new UIBlock() {
|
|
205
|
+ @Override
|
|
206
|
+ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
|
|
207
|
+
|
|
208
|
+ View dView = nativeViewHierarchyManager.resolveView(tag);
|
|
209
|
+
|
|
210
|
+ SurfaceView surfaceView = RtcEngine.CreateRendererView(dView.getContext());
|
|
211
|
+
|
|
212
|
+// ViewGroup gView = (ViewGroup) dView;
|
|
213
|
+// gView.addView(surfaceView);
|
|
214
|
+
|
|
215
|
+// Log.i("Agora", surfaceView.getWidth() + "");
|
|
216
|
+// Log.i("Agora", uid+"---"+tag);
|
|
217
|
+//
|
|
218
|
+ mRtcEngine.setupRemoteVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_HIDDEN, uid));
|
|
219
|
+
|
|
220
|
+ }
|
|
221
|
+ });
|
|
222
|
+ }
|
26
|
223
|
}
|
|
224
|
+
|
|
225
|
+// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
|
226
|
+// surfaceView.setZOrderOnTop(false);
|
|
227
|
+// surfaceView.setZOrderMediaOverlay(false);
|
|
228
|
+
|
|
229
|
+// int w = dView.getWidth();
|
|
230
|
+// int h = dView.getHeight();
|
|
231
|
+// surfaceView.measure(w, h);
|
|
232
|
+// int height =surfaceView.getMeasuredHeight();
|
|
233
|
+
|
|
234
|
+// ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) surfaceView.getLayoutParams();
|
|
235
|
+// params.width = 120;
|
|
236
|
+// params.height = 120;
|
|
237
|
+// surfaceView.setLayoutParams(params);
|
|
238
|
+
|
|
239
|
+// Log.i("Agora", surfaceView.getWidth() + "surfaceView");
|
|
240
|
+// Log.i("Agora", uid+"---"+tag);
|
|
241
|
+
|
|
242
|
+// ViewGroup gView = (ViewGroup) dView;
|
|
243
|
+// Log.i("Agora", gView.getWidth() + "gView");
|
|
244
|
+// Log.i("Agora", gView.getHeight() + "gView");
|
|
245
|
+//
|
|
246
|
+// gView.addView(surfaceView);
|
|
247
|
+//Button button = new Button(dView.getContext());
|
|
248
|
+//button.setText("123");
|
|
249
|
+//
|
|
250
|
+// dView.addView(button);
|
|
251
|
+// ArrayList<View> list = new ArrayList<>();
|
|
252
|
+// list.add(surfaceView);
|
|
253
|
+// dView.addChildrenForAccessibility(list);
|