|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.UI; |
| 5 | +using agora_gaming_rtc; |
| 6 | +using System.Linq; |
| 7 | +using agora_utilities; |
| 8 | + |
| 9 | +public class AgoraDeviceManager : MonoBehaviour |
| 10 | +{ |
| 11 | + [SerializeField] |
| 12 | + private string APP_ID = "YOUR_APPID"; |
| 13 | + [SerializeField] |
| 14 | + private string TOKEN = ""; |
| 15 | + [SerializeField] |
| 16 | + private string CHANNEL_NAME = "YOUR_CHANNEL_NAME"; |
| 17 | + |
| 18 | + public Text LogText, videoDeviceText; |
| 19 | + private Logger _logger; |
| 20 | + private IRtcEngine _rtcEngine = null; |
| 21 | + private AudioRecordingDeviceManager _audioRecordingDeviceManager = null; |
| 22 | + private AudioPlaybackDeviceManager _audioPlaybackDeviceManager = null; |
| 23 | + private VideoDeviceManager _videoDeviceManager = null; |
| 24 | + private Dictionary<int, string> _audioRecordingDeviceDic = new Dictionary<int, string>(); |
| 25 | + private Dictionary<int, string> _audioPlaybackDeviceDic = new Dictionary<int, string>(); |
| 26 | + private Dictionary<int, string> _videoDeviceManagerDic = new Dictionary<int, string>(); |
| 27 | + private Dictionary<int, string> _audioRecordingDeviceNamesDic = new Dictionary<int, string>(); |
| 28 | + private Dictionary<int, string> _audioPlaybackDeviceNamesDic = new Dictionary<int, string>(); |
| 29 | + private Dictionary<int, string> _videoDeviceManagerNamesDic = new Dictionary<int, string>(); |
| 30 | + public Dropdown videoDropdown, recordingDropdown, playbackDropdown; |
| 31 | + [SerializeField] |
| 32 | + private int _recordingDeviceIndex = 0, |
| 33 | + _playbackDeviceIndex = 0, |
| 34 | + _videoDeviceIndex = 0; |
| 35 | + private List<uint> remoteClientIDs = new List<uint>(); |
| 36 | + private const float Offset = 100; |
| 37 | + GameObject LastRemote = null; |
| 38 | + public Slider recordingVolume, playbackVolume; |
| 39 | + public bool joinedChannel; |
| 40 | + public Button videoDeviceButton, joinChannelButton, leaveChannelButton; |
| 41 | + // Start is called before the first frame update |
| 42 | + void Start() |
| 43 | + { |
| 44 | + if (CheckAppId()) |
| 45 | + { |
| 46 | + InitRtcEngine(); |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + void Update() |
| 52 | + { |
| 53 | + PermissionHelper.RequestMicrophontPermission(); |
| 54 | + PermissionHelper.RequestCameraPermission(); |
| 55 | + |
| 56 | + |
| 57 | + //videoDeviceButton.interactable = !joinedChannel; |
| 58 | + //videoDropdown.interactable = !joinedChannel; |
| 59 | + videoDeviceText.gameObject.SetActive(!joinedChannel); |
| 60 | + joinChannelButton.interactable = !joinedChannel; |
| 61 | + leaveChannelButton.interactable = joinedChannel; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + bool CheckAppId() |
| 66 | + { |
| 67 | + _logger = new Logger(LogText); |
| 68 | + return _logger.DebugAssert(APP_ID.Length > 10, "Please fill in your appId in Canvas!!!!!"); |
| 69 | + } |
| 70 | + |
| 71 | + public void playbackUpdate(){ |
| 72 | + _playbackDeviceIndex = playbackDropdown.value; |
| 73 | + } |
| 74 | + |
| 75 | + public void recordingUpdate(){ |
| 76 | + _recordingDeviceIndex = recordingDropdown.value; |
| 77 | + } |
| 78 | + |
| 79 | + public void videoUpdate(){ |
| 80 | + _videoDeviceIndex = videoDropdown.value; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + public void volumeUpdate(){ |
| 86 | + SetCurrentDeviceVolume(); |
| 87 | + } |
| 88 | + |
| 89 | + void InitRtcEngine() |
| 90 | + { |
| 91 | + _rtcEngine = IRtcEngine.GetEngine(APP_ID); |
| 92 | + _rtcEngine.SetLogFile("log.txt"); |
| 93 | + _rtcEngine.EnableAudio(); |
| 94 | + _rtcEngine.EnableVideo(); |
| 95 | + _rtcEngine.EnableVideoObserver(); |
| 96 | + _rtcEngine.SetChannelProfile(CHANNEL_PROFILE.CHANNEL_PROFILE_LIVE_BROADCASTING); |
| 97 | + _rtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); |
| 98 | + _rtcEngine.OnJoinChannelSuccess += OnJoinChannelSuccessHandler; |
| 99 | + _rtcEngine.OnUserJoined += EngineOnUserJoinedHandler; |
| 100 | + _rtcEngine.OnUserOffline += OnUserLeaveChannelHandler; |
| 101 | + _rtcEngine.OnLeaveChannel += OnLeaveChannelHandler; |
| 102 | + _rtcEngine.OnWarning += OnSDKWarningHandler; |
| 103 | + _rtcEngine.OnError += OnSDKErrorHandler; |
| 104 | + _rtcEngine.OnConnectionLost += OnConnectionLostHandler; |
| 105 | + _rtcEngine.OnCameraChanged += OnCameraChangedHandler; |
| 106 | + _rtcEngine.OnMicrophoneChanged += OnMicrophoneChangedHandler; |
| 107 | + _rtcEngine.OnPlaybackChanged += OnPlaybackChangedHandler; |
| 108 | + } |
| 109 | + |
| 110 | + void GetAudioRecordingDevice() |
| 111 | + { |
| 112 | + string audioRecordingDeviceName = ""; |
| 113 | + string audioRecordingDeviceId = ""; |
| 114 | + _audioRecordingDeviceManager = (AudioRecordingDeviceManager)_rtcEngine.GetAudioRecordingDeviceManager(); |
| 115 | + _audioRecordingDeviceManager.CreateAAudioRecordingDeviceManager(); |
| 116 | + int count = _audioRecordingDeviceManager.GetAudioRecordingDeviceCount(); |
| 117 | + _logger.UpdateLog(string.Format("AudioRecordingDevice count: {0}", count)); |
| 118 | + recordingDropdown.ClearOptions(); |
| 119 | + _audioRecordingDeviceDic.Clear(); |
| 120 | + _audioRecordingDeviceNamesDic.Clear(); |
| 121 | + for (int i = 0; i < count ; i ++) { |
| 122 | + _audioRecordingDeviceManager.GetAudioRecordingDevice(i, ref audioRecordingDeviceName, ref audioRecordingDeviceId); |
| 123 | + if (!_audioRecordingDeviceDic.ContainsKey(i)) { |
| 124 | + _audioRecordingDeviceDic.Add(i, audioRecordingDeviceId); |
| 125 | + _audioRecordingDeviceNamesDic.Add(i, audioRecordingDeviceName); |
| 126 | + } |
| 127 | + _logger.UpdateLog(string.Format("AudioRecordingDevice device index: {0}, name: {1}, id: {2}", i, audioRecordingDeviceName, audioRecordingDeviceId)); |
| 128 | + } |
| 129 | + |
| 130 | + recordingDropdown.AddOptions(_audioRecordingDeviceNamesDic.Values.ToList()); |
| 131 | + recordingDropdown.value = _audioRecordingDeviceNamesDic.Count - 1; |
| 132 | + } |
| 133 | + |
| 134 | + void GetAudioPlaybackDevice() |
| 135 | + { |
| 136 | + string audioPlaybackDeviceName = ""; |
| 137 | + string audioPlaybackDeviceId = ""; |
| 138 | + _audioPlaybackDeviceManager = (AudioPlaybackDeviceManager)_rtcEngine.GetAudioPlaybackDeviceManager(); |
| 139 | + _audioPlaybackDeviceManager.CreateAAudioPlaybackDeviceManager(); |
| 140 | + int count = _audioPlaybackDeviceManager.GetAudioPlaybackDeviceCount(); |
| 141 | + _logger.UpdateLog(string.Format("AudioPlaybackDeviceManager count: {0}", count)); |
| 142 | + playbackDropdown.ClearOptions(); |
| 143 | + _audioPlaybackDeviceDic.Clear(); |
| 144 | + _audioPlaybackDeviceNamesDic.Clear(); |
| 145 | + for (int i = 0; i < count ; i ++) { |
| 146 | + _audioPlaybackDeviceManager.GetAudioPlaybackDevice(i, ref audioPlaybackDeviceName, ref audioPlaybackDeviceId); |
| 147 | + if (!_audioPlaybackDeviceDic.ContainsKey(i)) |
| 148 | + { |
| 149 | + _audioPlaybackDeviceDic.Add(i, audioPlaybackDeviceId); |
| 150 | + _audioPlaybackDeviceNamesDic.Add(i, audioPlaybackDeviceName); |
| 151 | + } |
| 152 | + _logger.UpdateLog(string.Format("AudioPlaybackDevice device index: {0}, name: {1}, id: {2}", i, audioPlaybackDeviceName, audioPlaybackDeviceId)); |
| 153 | + } |
| 154 | + |
| 155 | + playbackDropdown.AddOptions(_audioPlaybackDeviceNamesDic.Values.ToList()); |
| 156 | + playbackDropdown.value = _audioPlaybackDeviceNamesDic.Count - 1; |
| 157 | + } |
| 158 | + |
| 159 | + void GetVideoDeviceManager() |
| 160 | + { |
| 161 | + string videoDeviceName = ""; |
| 162 | + string videoDeviceId = ""; |
| 163 | + /// If you want to getVideoDeviceManager, you need to call startPreview() first; |
| 164 | + _rtcEngine.StartPreview(); |
| 165 | + _videoDeviceManager = (VideoDeviceManager)_rtcEngine.GetVideoDeviceManager(); |
| 166 | + _videoDeviceManager.CreateAVideoDeviceManager(); |
| 167 | + int count = _videoDeviceManager.GetVideoDeviceCount(); |
| 168 | + _logger.UpdateLog(string.Format("VideoDevice count: {0}", count)); |
| 169 | + videoDropdown.ClearOptions(); |
| 170 | + _videoDeviceManagerDic.Clear(); |
| 171 | + _videoDeviceManagerNamesDic.Clear(); |
| 172 | + for (int i = 0; i < count; i++) |
| 173 | + { |
| 174 | + _videoDeviceManager.GetVideoDevice(i, ref videoDeviceName, ref videoDeviceId); |
| 175 | + if (!_videoDeviceManagerDic.ContainsKey(i)) |
| 176 | + { |
| 177 | + _videoDeviceManagerDic.Add(i, videoDeviceId); |
| 178 | + _videoDeviceManagerNamesDic.Add(i, videoDeviceName); |
| 179 | + } |
| 180 | + |
| 181 | + _logger.UpdateLog(string.Format("VideoDeviceManager device index: {0}, name: {1}, id: {2}", i, videoDeviceName, videoDeviceId)); |
| 182 | + } |
| 183 | + |
| 184 | + videoDropdown.AddOptions(_videoDeviceManagerNamesDic.Values.ToList()); |
| 185 | + videoDropdown.value = _videoDeviceManagerNamesDic.Count - 1; |
| 186 | + } |
| 187 | + |
| 188 | + void SetCurrentDevice() |
| 189 | + { |
| 190 | + _audioRecordingDeviceManager.SetAudioRecordingDevice(_audioRecordingDeviceDic[_recordingDeviceIndex]); |
| 191 | + _audioPlaybackDeviceManager.SetAudioPlaybackDevice(_audioPlaybackDeviceDic[_playbackDeviceIndex]); |
| 192 | + _videoDeviceManager.SetVideoDevice(_videoDeviceManagerDic[_videoDeviceIndex]); |
| 193 | + } |
| 194 | + |
| 195 | + public void SetCurrentDeviceVolume() |
| 196 | + { |
| 197 | + _audioRecordingDeviceManager.SetAudioRecordingDeviceVolume((int)recordingVolume.value); |
| 198 | + _audioPlaybackDeviceManager.SetAudioPlaybackDeviceVolume((int)playbackVolume.value); |
| 199 | + } |
| 200 | + |
| 201 | + void ReleaseDeviceManager() |
| 202 | + { |
| 203 | + _audioPlaybackDeviceManager.ReleaseAAudioPlaybackDeviceManager(); |
| 204 | + _audioRecordingDeviceManager.ReleaseAAudioRecordingDeviceManager(); |
| 205 | + _videoDeviceManager.ReleaseAVideoDeviceManager(); |
| 206 | + } |
| 207 | + |
| 208 | + public void SetAndReleaseRecordingDevice(){ |
| 209 | + _audioRecordingDeviceManager.SetAudioRecordingDevice(_audioRecordingDeviceDic[_recordingDeviceIndex]); |
| 210 | + _audioRecordingDeviceManager.ReleaseAAudioRecordingDeviceManager(); |
| 211 | + } |
| 212 | + |
| 213 | + public void SetAndReleasePlaybackDevice(){ |
| 214 | + _audioPlaybackDeviceManager.SetAudioPlaybackDevice(_audioPlaybackDeviceDic[_playbackDeviceIndex]); |
| 215 | + _audioPlaybackDeviceManager.ReleaseAAudioPlaybackDeviceManager(); |
| 216 | + } |
| 217 | + |
| 218 | + public void SetAndReleaseVideoDevice(){ |
| 219 | + _videoDeviceManager.SetVideoDevice(_videoDeviceManagerDic[_videoDeviceIndex]); |
| 220 | + _videoDeviceManager.ReleaseAVideoDeviceManager(); |
| 221 | + } |
| 222 | + |
| 223 | + public void JoinChannel() |
| 224 | + { |
| 225 | + _rtcEngine.JoinChannelByKey(TOKEN, CHANNEL_NAME, "", 0); |
| 226 | + makeVideoView(CHANNEL_NAME, 0); |
| 227 | + } |
| 228 | + |
| 229 | + public void LeaveChannel() |
| 230 | + { |
| 231 | + _rtcEngine.LeaveChannel(); |
| 232 | + DestroyVideoView(CHANNEL_NAME, 0); |
| 233 | + } |
| 234 | + |
| 235 | + void EngineOnUserJoinedHandler(uint uid, int elapsed) |
| 236 | + { |
| 237 | + _logger.UpdateLog(string.Format("OnUserJoinedHandler channelId: {0} uid: ${1} elapsed: ${2}", CHANNEL_NAME, |
| 238 | + uid, elapsed)); |
| 239 | + makeVideoView(CHANNEL_NAME, uid); |
| 240 | + remoteClientIDs.Add(uid); |
| 241 | + } |
| 242 | + |
| 243 | + void OnCameraChangedHandler(string state, string device) |
| 244 | + { |
| 245 | + _logger.UpdateLog(string.Format("OnCameraChanged state: {0} device: {1}", state, device)); |
| 246 | + GetVideoDeviceManager(); |
| 247 | + } |
| 248 | + |
| 249 | + void OnMicrophoneChangedHandler(string state, string device) |
| 250 | + { |
| 251 | + _logger.UpdateLog(string.Format("OnMicrophoneChanged state: {0} device: {1}", state, device)); |
| 252 | + GetAudioRecordingDevice(); |
| 253 | + } |
| 254 | + |
| 255 | + void OnPlaybackChangedHandler(string state, string device) |
| 256 | + { |
| 257 | + _logger.UpdateLog(string.Format("OnPlaybackChanged state: {0} device: {1}", state, device)); |
| 258 | + GetAudioPlaybackDevice(); |
| 259 | + } |
| 260 | + |
| 261 | + void OnApplicationQuit() |
| 262 | + { |
| 263 | + Debug.Log("OnApplicationQuit"); |
| 264 | + if (_rtcEngine != null) |
| 265 | + { |
| 266 | + IRtcEngine.Destroy(); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + void OnJoinChannelSuccessHandler(string channelName, uint uid, int elapsed) |
| 271 | + { |
| 272 | + _logger.UpdateLog(string.Format("sdk version: {0}", IRtcEngine.GetSdkVersion())); |
| 273 | + _logger.UpdateLog(string.Format("onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", channelName, uid, elapsed)); |
| 274 | + joinedChannel = true; |
| 275 | + GetAudioPlaybackDevice(); |
| 276 | + GetVideoDeviceManager(); |
| 277 | + GetAudioRecordingDevice(); |
| 278 | + //SetCurrentDevice(); |
| 279 | + //SetCurrentDeviceVolume(); |
| 280 | + //ReleaseDeviceManager(); |
| 281 | + } |
| 282 | + |
| 283 | + void OnLeaveChannelHandler(RtcStats stats) |
| 284 | + { |
| 285 | + _logger.UpdateLog("OnLeaveChannelSuccess"); |
| 286 | + joinedChannel = false; |
| 287 | + } |
| 288 | + |
| 289 | + void OnUserLeaveChannelHandler(uint uid, USER_OFFLINE_REASON reason) |
| 290 | + { |
| 291 | + DestroyVideoView(CHANNEL_NAME, uid); |
| 292 | + } |
| 293 | + |
| 294 | + void OnSDKWarningHandler(int warn, string msg) |
| 295 | + { |
| 296 | + _logger.UpdateLog(string.Format("OnSDKWarning warn: {0}, msg: {1}", warn, msg)); |
| 297 | + } |
| 298 | + |
| 299 | + void OnSDKErrorHandler(int error, string msg) |
| 300 | + { |
| 301 | + _logger.UpdateLog(string.Format("OnSDKError error: {0}, msg: {1}", error, msg)); |
| 302 | + } |
| 303 | + |
| 304 | + void OnConnectionLostHandler() |
| 305 | + { |
| 306 | + _logger.UpdateLog(string.Format("OnConnectionLost ")); |
| 307 | + } |
| 308 | + |
| 309 | + private void makeVideoView(string channelId, uint uid) |
| 310 | + { |
| 311 | + string objName = channelId + "_" + uid.ToString(); |
| 312 | + GameObject go = GameObject.Find(objName); |
| 313 | + if (!ReferenceEquals(go, null)) |
| 314 | + { |
| 315 | + return; // reuse |
| 316 | + } |
| 317 | + |
| 318 | + |
| 319 | + // create a GameObject and assign to this new user |
| 320 | + VideoSurface videoSurface = makeImageSurface(objName); |
| 321 | + if (!ReferenceEquals(videoSurface, null)) |
| 322 | + { |
| 323 | + // configure videoSurface |
| 324 | + videoSurface.SetForUser(uid); |
| 325 | + videoSurface.SetEnable(true); |
| 326 | + videoSurface.SetVideoSurfaceType(AgoraVideoSurfaceType.RawImage); |
| 327 | + // make the object draggable |
| 328 | + //videoSurface.gameObject.AddComponent<UIElementDragger>(); |
| 329 | + |
| 330 | + if (uid != 0) |
| 331 | + { |
| 332 | + LastRemote = videoSurface.gameObject; |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + // Video TYPE 2: RawImage |
| 338 | + public VideoSurface makeImageSurface(string goName) |
| 339 | + { |
| 340 | + GameObject go = new GameObject(); |
| 341 | + |
| 342 | + if (go == null) |
| 343 | + { |
| 344 | + return null; |
| 345 | + } |
| 346 | + |
| 347 | + go.name = goName; |
| 348 | + // make the object draggable |
| 349 | + go.AddComponent<UIElementDrag>(); |
| 350 | + // to be renderered onto |
| 351 | + go.AddComponent<RawImage>(); |
| 352 | + |
| 353 | + GameObject canvas = GameObject.Find("VideoPanel"); |
| 354 | + if (canvas != null) |
| 355 | + { |
| 356 | + go.transform.SetParent(canvas.transform); |
| 357 | + Debug.Log("add video view"); |
| 358 | + } |
| 359 | + else |
| 360 | + { |
| 361 | + Debug.Log("Canvas is null video view"); |
| 362 | + } |
| 363 | + |
| 364 | + // set up transform |
| 365 | + go.transform.Rotate(0f, 0.0f, 180.0f); |
| 366 | + float xPos = Random.Range(Offset - Screen.width / 2f, Screen.width / 2f - Offset); |
| 367 | + float yPos = Random.Range(Offset, Screen.height / 2f - Offset); |
| 368 | + Debug.Log("position x " + xPos + " y: " + yPos); |
| 369 | + go.transform.localPosition = new Vector3(xPos, yPos, 0f); |
| 370 | + go.transform.localScale = new Vector3(1.5f, 1f, 1f); |
| 371 | + |
| 372 | + // configure videoSurface |
| 373 | + VideoSurface videoSurface = go.AddComponent<VideoSurface>(); |
| 374 | + return videoSurface; |
| 375 | + } |
| 376 | + |
| 377 | + private void DestroyVideoView(string channelId, uint uid) |
| 378 | + { |
| 379 | + string objName = channelId + "_" + uid.ToString(); |
| 380 | + GameObject go = GameObject.Find(objName); |
| 381 | + if (!ReferenceEquals(go, null)) |
| 382 | + { |
| 383 | + Object.Destroy(go); |
| 384 | + } |
| 385 | + } |
| 386 | +} |
| 387 | + |
0 commit comments