-
Notifications
You must be signed in to change notification settings - Fork 93
/
HelloVideoAgora.cs
215 lines (184 loc) · 6.2 KB
/
HelloVideoAgora.cs
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
using UnityEngine;
using UnityEngine.UI;
using agora_gaming_rtc;
using agora_utilities;
public class HelloVideoAgora : MonoBehaviour
{
[SerializeField]
private string APP_ID = "";
[SerializeField]
private string TOKEN = "";
[SerializeField]
private string CHANNEL_NAME = "YOUR_CHANNEL_NAME";
public Text LogText;
public bool UseCustomSink = false;
private Logger _logger;
private IRtcEngine _rtcEngine = null;
private const float _offset = 100;
// Use this for initialization
void Start()
{
if (CheckAppId())
{
InitEngine();
JoinChannel();
}
}
// Update is called once per frame
void Update()
{
PermissionHelper.RequestMicrophontPermission();
PermissionHelper.RequestCameraPermission();
}
bool CheckAppId()
{
_logger = new Logger(LogText);
return _logger.DebugAssert(APP_ID.Length > 10, "Please fill in your appId in VideoCanvas!!!!!");
}
void InitEngine()
{
_rtcEngine = IRtcEngine.GetEngine(APP_ID);
_rtcEngine.SetLogFile("log.txt");
_rtcEngine.SetChannelProfile(CHANNEL_PROFILE.CHANNEL_PROFILE_LIVE_BROADCASTING);
_rtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
if (UseCustomSink)
{
_rtcEngine.SetExternalAudioSink(true, 44100, 1);
}
_rtcEngine.OnJoinChannelSuccess += OnJoinChannelSuccessHandler;
_rtcEngine.OnLeaveChannel += OnLeaveChannelHandler;
_rtcEngine.OnWarning += OnSDKWarningHandler;
_rtcEngine.OnError += OnSDKErrorHandler;
_rtcEngine.OnConnectionLost += OnConnectionLostHandler;
_rtcEngine.OnUserJoined += OnUserJoinedHandler;
_rtcEngine.OnUserOffline += OnUserOfflineHandler;
}
void JoinChannel()
{
_rtcEngine.EnableAudio();
_rtcEngine.EnableVideo();
_rtcEngine.EnableVideoObserver();
_rtcEngine.JoinChannelByKey(TOKEN, CHANNEL_NAME, "", 0);
}
void OnJoinChannelSuccessHandler(string channelName, uint uid, int elapsed)
{
_logger.UpdateLog(string.Format("sdk version: ${0}", IRtcEngine.GetSdkVersion()));
_logger.UpdateLog(string.Format("onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", channelName, uid, elapsed));
makeVideoView(0);
}
void OnLeaveChannelHandler(RtcStats stats)
{
_logger.UpdateLog("OnLeaveChannelSuccess");
DestroyVideoView(0);
}
void OnUserJoinedHandler(uint uid, int elapsed)
{
_logger.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
makeVideoView(uid);
}
void OnUserOfflineHandler(uint uid, USER_OFFLINE_REASON reason)
{
_logger.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid, (int)reason));
DestroyVideoView(uid);
}
void OnSDKWarningHandler(int warn, string msg)
{
_logger.UpdateLog(string.Format("OnSDKWarning warn: {0}, msg: {1}", warn, msg));
}
void OnSDKErrorHandler(int error, string msg)
{
_logger.UpdateLog(string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
}
void OnConnectionLostHandler()
{
_logger.UpdateLog(string.Format("OnConnectionLost "));
}
void OnApplicationQuit()
{
Debug.Log("OnApplicationQuit");
if (_rtcEngine != null)
{
_rtcEngine.LeaveChannel();
_rtcEngine.DisableVideoObserver();
IRtcEngine.Destroy();
}
}
private void DestroyVideoView(uint uid)
{
GameObject go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
Object.Destroy(go);
}
}
private void makeVideoView(uint uid)
{
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
videoSurface.SetForUser(uid);
videoSurface.SetEnable(true);
videoSurface.SetVideoSurfaceType(AgoraVideoSurfaceType.RawImage);
}
}
// VIDEO TYPE 1: 3D Object
public VideoSurface makePlaneSurface(string goName)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
if (go == null)
{
return null;
}
go.name = goName;
// set up transform
go.transform.Rotate(-90.0f, 0.0f, 0.0f);
float yPos = Random.Range(3.0f, 5.0f);
float xPos = Random.Range(-2.0f, 2.0f);
go.transform.position = new Vector3(xPos, yPos, 0f);
go.transform.localScale = new Vector3(0.25f, 0.5f, .5f);
// configure videoSurface
VideoSurface videoSurface = go.AddComponent<VideoSurface>();
return videoSurface;
}
// Video TYPE 2: RawImage
public VideoSurface makeImageSurface(string goName)
{
GameObject go = new GameObject();
if (go == null)
{
return null;
}
go.name = goName;
// to be renderered onto
go.AddComponent<RawImage>();
// make the object draggable
go.AddComponent<UIElementDrag>();
GameObject canvas = GameObject.Find("VideoCanvas");
if (canvas != null)
{
go.transform.SetParent(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);
float xPos = Random.Range(_offset - Screen.width / 2f, Screen.width / 2f - _offset);
float yPos = Random.Range(_offset, Screen.height / 2f - _offset);
Debug.Log("position x " + xPos + " y: " + yPos);
go.transform.localPosition = new Vector3(xPos, yPos, 0f);
go.transform.localScale = new Vector3(3f, 4f, 1f);
// configure videoSurface
VideoSurface videoSurface = go.AddComponent<VideoSurface>();
return videoSurface;
}
}