Repository navigation
Expand file tree
/
Copy pathAudioSpectrum.cs
More file actions
523 lines (445 loc) · 17.6 KB
/
Copy pathAudioSpectrum.cs
File metadata and controls
523 lines (445 loc) · 17.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Agora.Rtc;
using io.agora.rtc.demo;
namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.AudioSpectrum
{
public class AudioSpectrum : 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;
public RectTransform spectrums;
public List<float> data = new List<float>();
internal Logger Log;
internal IRtcEngine RtcEngine = null;
internal IMediaPlayer MediaPlayer = null;
private const string MPK_URL =
"https://agoracdn.s3.us-west-1.amazonaws.com/videos/Agora.io-Interactions.mp4";
private Button _button1;
private Button _button2;
private Button _button3;
private Button _button4;
private Button _button5;
private Toggle _urlToggle;
// Use this for initialization
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
SetUpUI();
EnableUI(false);
InitEngine();
InitMediaPlayer();
JoinChannelWithMPK();
}
}
// Update is called once per frame
private void Update()
{
PermissionHelper.RequestMicrophontPermission();
PermissionHelper.RequestCameraPermission();
lock (data)
{
if (data.Count > 0)
{
for (var i = 0; i < this.data.Count; i++)
{
var height = (-data[i] + 1);
if (height <= 1) height = 1;
var child = (RectTransform)this.spectrums.GetChild(i);
child.sizeDelta = new Vector2(15, height);
}
}
data.Clear();
}
}
private void SetUpUI()
{
_button1 = GameObject.Find("Button1").GetComponent<Button>();
_button1.onClick.AddListener(OnPlayButtonPress);
_button2 = GameObject.Find("Button2").GetComponent<Button>();
_button2.onClick.AddListener(OnStopButtonPress);
_button3 = GameObject.Find("Button3").GetComponent<Button>();
_button3.onClick.AddListener(OnPauseButtonPress);
_button4 = GameObject.Find("Button4").GetComponent<Button>();
_button4.onClick.AddListener(OnResumeButtonPress);
_button5 = GameObject.Find("Button5").GetComponent<Button>();
_button5.onClick.AddListener(OnOpenButtonPress);
_urlToggle = GameObject.Find("UrlToggle").GetComponent<Toggle>();
}
public void EnableUI(bool val)
{
var obj = this.transform.Find("Button1").gameObject;
obj.SetActive(val);
obj = this.transform.Find("Button2").gameObject;
obj.SetActive(val);
obj = this.transform.Find("Button3").gameObject;
obj.SetActive(val);
obj = this.transform.Find("Button4").gameObject;
obj.SetActive(val);
}
private bool CheckAppId()
{
Log = new Logger(LogText);
return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
}
//Show data in AgoraBasicProfile
[ContextMenu("ShowAgoraBasicProfileData")]
private void LoadAssetData()
{
if (_appIdInput == null) return;
_appID = _appIdInput.appID;
_token = _appIdInput.token;
_channelName = _appIdInput.channelName;
}
private void InitEngine()
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
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_GAME_STREAMING;
RtcEngine.Initialize(context);
RtcEngine.InitEventHandler(handler);
}
private void InitMediaPlayer()
{
MediaPlayer = RtcEngine.CreateMediaPlayer();
if (MediaPlayer == null)
{
this.Log.UpdateLog("CreateMediaPlayer failed!");
return;
}
MpkEventHandler handler = new MpkEventHandler(this);
MediaPlayer.InitEventHandler(handler);
this.Log.UpdateLog("playerId id: " + MediaPlayer.GetId());
MediaPlayer.RegisterMediaPlayerAudioSpectrumObserver(new UserAudioSpectrumObserver(this), 16);
}
private void JoinChannelWithMPK()
{
RtcEngine.EnableAudio();
RtcEngine.EnableVideo();
RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
ChannelMediaOptions options = new ChannelMediaOptions();
options.autoSubscribeAudio.SetValue(true);
options.autoSubscribeVideo.SetValue(true);
options.publishCustomAudioTrack.SetValue(false);
options.publishCameraTrack.SetValue(false);
options.publishMediaPlayerAudioTrack.SetValue(true);
options.publishMediaPlayerVideoTrack.SetValue(true);
options.publishMediaPlayerId.SetValue(MediaPlayer.GetId());
options.enableAudioRecordingOrPlayout.SetValue(true);
options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
var ret = RtcEngine.JoinChannel(_token, _channelName, 0, options);
this.Log.UpdateLog("RtcEngineController JoinChannel_MPK returns: " + ret);
}
private void OnPlayButtonPress()
{
var ret = MediaPlayer.Play();
this.Log.UpdateLog("Play return" + ret);
this.TestMediaPlayer();
}
private void OnStopButtonPress()
{
var ret = MediaPlayer.Stop();
this.Log.UpdateLog("Stop return" + ret);
}
private void OnPauseButtonPress()
{
var ret = MediaPlayer.Pause();
this.Log.UpdateLog("Pause return" + ret);
}
private void OnResumeButtonPress()
{
var ret = MediaPlayer.Resume();
this.Log.UpdateLog("Resume returns: " + ret);
}
private void OnOpenButtonPress()
{
string path = null;
if (this._urlToggle.isOn)
{
path = MPK_URL;
}
else
{
#if UNITY_ANDROID && !UNITY_EDITOR
// On Android, the StreamingAssetPath is just accessed by /assets instead of Application.streamingAssetPath
path = "/assets/img/MPK.mp4";
#else
path = Path.Combine(Application.streamingAssetsPath, "img/MPK.mp4");
#endif
}
this.Log.UpdateLog("Is opening : " + path);
var ret = MediaPlayer.Open(path, 0);
this.Log.UpdateLog("Open returns: " + ret);
}
private void TestMediaPlayer()
{
long duration = 0;
var ret = MediaPlayer.GetDuration(ref duration);
Debug.Log("_mediaPlayer.GetDuration returns: " + ret + "duration: " + duration);
long pos = 0;
ret = MediaPlayer.GetPlayPosition(ref pos);
Debug.Log("_mediaPlayer.GetPlayPosition returns: " + ret + "position: " + pos);
Debug.Log("_mediaPlayer.GetState:" + MediaPlayer.GetState());
bool mute = true;
ret = MediaPlayer.GetMute(ref mute);
Debug.Log("_mediaPlayer.GetMute returns: " + ret + "mute: " + mute);
int volume = 0;
ret = MediaPlayer.GetPlayoutVolume(ref volume);
Debug.Log("_mediaPlayer.GetPlayoutVolume returns: " + ret + "volume: " + volume);
Debug.Log("SDK Version:" + MediaPlayer.GetPlayerSdkVersion());
Debug.Log("GetPlaySrc:" + MediaPlayer.GetPlaySrc());
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
if (RtcEngine == null) return;
if (MediaPlayer != null)
RtcEngine.DestroyMediaPlayer(MediaPlayer);
RtcEngine.InitEventHandler(null);
RtcEngine.LeaveChannel();
RtcEngine.Dispose();
RtcEngine = null;
}
internal string GetChannelName()
{
return _channelName;
}
#region -- Video Render UI Logic ---
internal static void MakeVideoView(uint uid, string channelId = "", VIDEO_SOURCE_TYPE videoSourceType = VIDEO_SOURCE_TYPE.VIDEO_SOURCE_CAMERA)
{
var go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
return; // reuse
}
// create a GameObject and assign to this new user
var videoSurface = MakeImageSurface(uid.ToString());
if (ReferenceEquals(videoSurface, null)) return;
// configure videoSurface
videoSurface.SetForUser(uid, channelId, videoSourceType);
videoSurface.SetEnable(true);
videoSurface.OnTextureSizeModify += (int width, int height) =>
{
var transform = videoSurface.GetComponent<RectTransform>();
if (transform)
{
//If render in RawImage. just set rawImage size.
transform.sizeDelta = new Vector2(width / 2, height / 2);
transform.localScale = Vector3.one;
}
else
{
//If render in MeshRenderer, just set localSize with MeshRenderer
float scale = (float)height / (float)width;
videoSurface.transform.localScale = new Vector3(-1, 1, scale);
}
Debug.Log("OnTextureSizeModify: " + width + " " + height);
};
}
// VIDEO TYPE 1: 3D Object
private VideoSurface MakePlaneSurface(string goName)
{
var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
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"));
}
// set up transform
go.transform.Rotate(-90.0f, 0.0f, 0.0f);
go.transform.position = Vector3.zero;
go.transform.localScale = new Vector3(1.0f, 1.333f, 0.5f);
// configure videoSurface
var videoSurface = go.AddComponent<VideoSurfaceYUV>();
return videoSurface;
}
// Video TYPE 2: RawImage
private static 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>();
var canvas = GameObject.Find("VideoCanvas");
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(4.5f, 3f, 1f);
// configure videoSurface
var videoSurface = go.AddComponent<VideoSurfaceYUV>();
return videoSurface;
}
internal static void DestroyVideoView(uint uid)
{
var go = GameObject.Find(uid.ToString());
if (!ReferenceEquals(go, null))
{
Destroy(go);
}
}
#endregion
}
#region -- Agora Event ---
internal class MpkEventHandler : IMediaPlayerSourceObserver
{
private readonly AudioSpectrum _sample;
internal MpkEventHandler(AudioSpectrum sample)
{
_sample = sample;
}
public override void OnPlayerSourceStateChanged(MEDIA_PLAYER_STATE state, MEDIA_PLAYER_REASON reason)
{
_sample.Log.UpdateLog(string.Format(
"OnPlayerSourceStateChanged state: {0}, ec: {1}, playId: {2}", state, reason, _sample.MediaPlayer.GetId()));
Debug.Log("OnPlayerSourceStateChanged");
if (state == MEDIA_PLAYER_STATE.PLAYER_STATE_OPEN_COMPLETED)
{
AudioSpectrum.MakeVideoView((uint)_sample.MediaPlayer.GetId(), "", VIDEO_SOURCE_TYPE.VIDEO_SOURCE_MEDIA_PLAYER);
_sample.EnableUI(true);
_sample.Log.UpdateLog("Open Complete. Click start to play media");
}
else if (state == MEDIA_PLAYER_STATE.PLAYER_STATE_STOPPED)
{
AudioSpectrum.DestroyVideoView((uint)_sample.MediaPlayer.GetId());
_sample.EnableUI(false);
}
}
public override void OnPlayerEvent(MEDIA_PLAYER_EVENT @event, Int64 elapsedTime, string message)
{
_sample.Log.UpdateLog(string.Format("OnPlayerEvent state: {0}", @event));
}
}
internal class UserEventHandler : IRtcEngineEventHandler
{
private readonly AudioSpectrum _sample;
internal UserEventHandler(AudioSpectrum 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");
AudioSpectrum.DestroyVideoView(0);
}
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));
}
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));
AudioSpectrum.DestroyVideoView(uid);
}
}
internal class UserPlayerCustomDataProvider : IMediaPlayerCustomDataProvider
{
internal UserPlayerCustomDataProvider()
{
}
public override Int64 OnSeek(Int64 offset, int whence)
{
Debug.Log("UserPlayerCustomDataProvider OnSeek");
return 0;
}
public override int OnReadData(IntPtr bufferPtr, int bufferSize)
{
Debug.Log("UserPlayerCustomDataProvider OnReadData");
return 0;
}
}
internal class UserAudioSpectrumObserver : IAudioSpectrumObserver
{
AudioSpectrum _sample;
internal UserAudioSpectrumObserver(AudioSpectrum sample)
{
this._sample = sample;
}
public override bool OnLocalAudioSpectrum(AudioSpectrumData data)
{
if (data.dataLength > 0)
{
lock (this._sample.data)
{
this._sample.data.Clear();
var interval = (int)(data.dataLength / 15);
for (var i = 0; i < 15; i++)
{
this._sample.data.Add(data.audioSpectrumData[i * interval]);
}
}
}
return true;
}
public override bool OnRemoteAudioSpectrum(UserAudioSpectrumInfo[] spectrums, uint spectrumNumber)
{
Debug.Log("OnRemoteAudioSpectrum");
return true;
}
}
#endregion
}