-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMultChannelPush.cs
More file actions
369 lines (307 loc) · 12.8 KB
/
MultChannelPush.cs
File metadata and controls
369 lines (307 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Agora.Rtc;
using io.agora.rtc.demo;
namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.MultChannelPush
{
public class MultChannelPush : MonoBehaviour
{
[FormerlySerializedAs("appIdInput")]
[SerializeField]
private AppIdInput _appIdInput;
[Header("_____________Basic Configuration_____________")]
[FormerlySerializedAs("APP_ID")]
[SerializeField]
private string _appID = "";
[FormerlySerializedAs("TOKEN")]
[SerializeField]
private string _token = "";
[FormerlySerializedAs("CHANNEL_NAME")]
[SerializeField]
private string _channelName = "";
public Text LogText;
internal Logger Log;
internal IRtcEngineEx RtcEngine = null;
public Texture2D ShareTexture1;
public Texture2D ShareTexture2;
private byte[] _shareData1 = null;
private byte[] _shareData2 = null;
private Rect _rect1;
private Rect _rect2;
private uint _videoTrack1 = 0;
private uint _videoTrack2 = 0;
private uint _uid1 = 123;
private uint _uid2 = 456;
// Use this for initialization
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
InitEngine();
SetExternalVideoSource();
InitTexture();
JoinChannel1();
JoinChannel2();
}
}
private void Update()
{
PermissionHelper.RequestMicrophontPermission();
StartCoroutine(ShareScreen());
}
//Show data in AgoraBasicProfile
[ContextMenu("ShowAgoraBasicProfileData")]
private void LoadAssetData()
{
if (_appIdInput == null) return;
_appID = _appIdInput.appID;
_token = _appIdInput.token;
_channelName = _appIdInput.channelName;
}
private IEnumerator ShareScreen()
{
yield return new WaitForEndOfFrame();
IRtcEngine rtc = Agora.Rtc.RtcEngine.Instance;
if (rtc != null)
{
if (_videoTrack1 != 0)
{
ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
externalVideoFrame.buffer = _shareData1;
externalVideoFrame.stride = (int)_rect1.width;
externalVideoFrame.height = (int)_rect1.height;
externalVideoFrame.rotation = 180;
externalVideoFrame.timestamp = 0;
var ret = rtc.PushVideoFrame(externalVideoFrame, _videoTrack1);
this.Log.UpdateLog("PushVideoFrame 1: " + ret);
}
if (_videoTrack2 != 0)
{
ExternalVideoFrame externalVideoFrame = new ExternalVideoFrame();
externalVideoFrame.type = VIDEO_BUFFER_TYPE.VIDEO_BUFFER_RAW_DATA;
externalVideoFrame.format = VIDEO_PIXEL_FORMAT.VIDEO_PIXEL_RGBA;
externalVideoFrame.buffer = _shareData2;
externalVideoFrame.stride = (int)_rect2.width;
externalVideoFrame.height = (int)_rect2.height;
externalVideoFrame.rotation = 180;
externalVideoFrame.timestamp = 0;
var ret = rtc.PushVideoFrame(externalVideoFrame, _videoTrack2);
this.Log.UpdateLog("PushVideoFrame 2: " + ret);
}
}
}
private byte[] convertColor32(Color32[] colors)
{
byte[] ret = new byte[colors.Length * 4];
int i = 0;
foreach (var color in colors)
{
ret[i++] = color.r;
ret[i++] = color.g;
ret[i++] = color.b;
ret[i++] = color.a;
}
return ret;
}
private void InitEngine()
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngineEx();
UserEventHandler handler = new UserEventHandler(this);
RtcEngineContext context = new RtcEngineContext();
context.appId = _appID;
context.channelProfile = CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING;
context.audioScenario = AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT;
context.areaCode = AREA_CODE.AREA_CODE_GLOB;
RtcEngine.Initialize(context);
RtcEngine.InitEventHandler(handler);
RtcEngine.EnableAudio();
RtcEngine.EnableVideo();
}
private void SetExternalVideoSource()
{
var ret = RtcEngine.SetExternalVideoSource(true, false, EXTERNAL_VIDEO_SOURCE_TYPE.VIDEO_FRAME, new SenderOptions());
this.Log.UpdateLog("SetExternalVideoSource returns:" + ret);
}
private void JoinChannel1()
{
_videoTrack1 = RtcEngine.CreateCustomVideoTrack();
ChannelMediaOptions channelMediaOptions = new ChannelMediaOptions();
channelMediaOptions.publishCustomVideoTrack.SetValue(true);
channelMediaOptions.customVideoTrackId.SetValue(_videoTrack1);
channelMediaOptions.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
RtcEngine.JoinChannelEx(_token, new RtcConnection(_channelName, _uid1), channelMediaOptions);
VideoEncoderConfiguration videoEncoderConfiguration = new VideoEncoderConfiguration();
videoEncoderConfiguration.dimensions = new VideoDimensions((int)_rect1.width, (int)_rect1.height);
RtcEngine.SetVideoEncoderConfigurationEx(videoEncoderConfiguration, new RtcConnection(_channelName, _uid1));
}
private void JoinChannel2()
{
_videoTrack2 = RtcEngine.CreateCustomVideoTrack();
ChannelMediaOptions channelMediaOptions = new ChannelMediaOptions();
channelMediaOptions.publishCustomVideoTrack.SetValue(true);
channelMediaOptions.customVideoTrackId.SetValue(_videoTrack2);
channelMediaOptions.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
RtcEngine.JoinChannelEx(_token, new RtcConnection(_channelName, _uid2), channelMediaOptions);
VideoEncoderConfiguration videoEncoderConfiguration = new VideoEncoderConfiguration();
videoEncoderConfiguration.dimensions = new VideoDimensions((int)_rect2.width, (int)_rect2.height);
RtcEngine.SetVideoEncoderConfigurationEx(videoEncoderConfiguration, new RtcConnection(_channelName, _uid2));
}
private bool CheckAppId()
{
Log = new Logger(LogText);
return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in Canvas!!!!");
}
private void InitTexture()
{
Color32[] color32s = ShareTexture1.GetPixels32(0);
_shareData1 = convertColor32(color32s);
_rect1 = new UnityEngine.Rect(0, 0, ShareTexture1.width, ShareTexture1.height);
color32s = ShareTexture2.GetPixels32(0);
_shareData2 = convertColor32(color32s);
_rect2 = new UnityEngine.Rect(0, 0, ShareTexture2.width, ShareTexture2.height);
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
if (RtcEngine != null)
{
RtcEngine.InitEventHandler(null);
RtcEngine.LeaveChannel();
RtcEngine.Dispose();
}
}
internal string GetChannelName()
{
return _channelName;
}
#region -- Video Render UI Logic ---
internal static void MakeVideoView(uint uid, string channelId = "")
{
GameObject go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
return; // reuse
}
// create a GameObject and assign to this new user
VideoSurface videoSurface = makeImageSurface(uid.ToString());
if (!ReferenceEquals(videoSurface, null))
{
// configure videoSurface
if (uid == 0)
{
videoSurface.SetForUser(uid, channelId);
}
else
{
videoSurface.SetForUser(uid, channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
}
videoSurface.OnTextureSizeModify += (int width, int height) =>
{
float scale = (float)height / (float)width;
videoSurface.transform.localScale = new Vector3(-5, 5 * scale, 1);
Debug.Log("OnTextureSizeModify: " + width + " " + height);
};
videoSurface.SetEnable(true);
}
}
// Video TYPE 2: RawImage
private static VideoSurface makeImageSurface(string goName)
{
GameObject go = new GameObject();
if (go == null)
{
return null;
}
go.name = goName;
var mesh = go.GetComponent<MeshRenderer>();
if (mesh != null)
{
Debug.LogWarning("VideoSureface update shader");
mesh.material = new Material(Shader.Find("Unlit/Texture"));
}
// to be renderered onto
go.AddComponent<RawImage>();
// make the object draggable
go.AddComponent<UIElementDrag>();
GameObject canvas = GameObject.Find("Canvas");
if (canvas != null)
{
go.transform.parent = canvas.transform;
Debug.Log("add video view");
}
else
{
Debug.Log("Canvas is null video view");
}
// set up transform
go.transform.Rotate(0f, 0.0f, 180.0f);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = new Vector3(3f, 4f, 1f);
// configure videoSurface
VideoSurface videoSurface = go.AddComponent<VideoSurfaceYUV>();
return videoSurface;
}
internal static void DestroyVideoView(uint uid)
{
GameObject go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
Destroy(go);
}
}
#endregion
}
#region -- Agora Event ---
internal class UserEventHandler : IRtcEngineEventHandler
{
private readonly MultChannelPush _sample;
internal UserEventHandler(MultChannelPush sample)
{
_sample = sample;
}
public override void OnError(int err, string msg)
{
_sample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
}
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
int build = 0;
_sample.Log.UpdateLog(string.Format("sdk version: ${0}",
_sample.RtcEngine.GetVersion(ref build)));
_sample.Log.UpdateLog(
string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
connection.channelId, connection.localUid, elapsed));
}
public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
{
_sample.Log.UpdateLog("OnRejoinChannelSuccess");
}
public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
{
_sample.Log.UpdateLog("OnLeaveChannel");
}
public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole,
CLIENT_ROLE_TYPE newRole, ClientRoleOptions newRoleOptions)
{
_sample.Log.UpdateLog("OnClientRoleChanged");
}
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
_sample.Log.UpdateLog(
string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
MultChannelPush.MakeVideoView(uid, _sample.GetChannelName());
}
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
_sample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
(int)reason));
MultChannelPush.DestroyVideoView(uid);
}
}
#endregion
}