-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathCustomRenderAudio.cs
More file actions
331 lines (273 loc) · 10.8 KB
/
CustomRenderAudio.cs
File metadata and controls
331 lines (273 loc) · 10.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
using System;
using System.Threading;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Agora.Rtc;
using RingBuffer;
using io.agora.rtc.demo;
namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.CustomRenderAudio
{
public class CustomRenderAudio : 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 IRtcEngine RtcEngine;
private const int CHANNEL = 2;
private const int SAMPLE_RATE = 48000;
private const int PULL_FREQ_PER_SEC = 100;
private RingBuffer<float> _audioBuffer;
private AudioSource _audioSource;
private AudioClip _audioClip;
Pcm16AudioWriter _audioWriter;
private Thread _pullAudioFrameThread;
private System.Object _rtcLock = new System.Object();
private int _writeCount;
private int _readCount;
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
_audioWriter = new Pcm16AudioWriter("CustomRenderAudio_" + DateTime.Now.ToString("yyyyMMddHHmmss"), SAMPLE_RATE, CHANNEL);
InitRtcEngine();
JoinChannel();
_audioSource = InitAudioSource();
StartPullAudioFrame(_audioSource, "externalClip");
}
}
private void Update()
{
PermissionHelper.RequestMicrophontPermission();
}
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 InitRtcEngine()
{
lock (_rtcLock)
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
UserEventHandler handler = new UserEventHandler(this);
//be care, enableAudioDevice need be false
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.SetParameters("{\"che.audio.use.call.mode\":false}");
RtcEngine.SetParameters("{\"che.audio.keep.audiosession\":true}");
}
}
private void JoinChannel()
{
lock (_rtcLock)
{
RtcEngine.EnableAudio();
RtcEngine.EnableVideo();
RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
//no enableAudioDevice to set false? how this methond work?
var nRet = RtcEngine.SetExternalAudioSink(true, SAMPLE_RATE, CHANNEL);
this.Log.UpdateLog("SetExternalAudioSink ret:" + nRet);
RtcEngine.JoinChannel(_token, _channelName,"",0);
}
}
private AudioSource InitAudioSource()
{
var aud = GetComponent<AudioSource>();
if (aud == null)
{
aud = gameObject.AddComponent<AudioSource>();
}
return aud;
}
private void StartPullAudioFrame(AudioSource aud, string clipName)
{
// 1-sec-length buffer
var bufferLength = SAMPLE_RATE * CHANNEL * 2;
_audioBuffer = new RingBuffer<float>(bufferLength, true);
_pullAudioFrameThread = new Thread(PullAudioFrameThread);
_pullAudioFrameThread.Start();
_audioClip = AudioClip.Create(clipName,
SAMPLE_RATE / PULL_FREQ_PER_SEC * CHANNEL,
CHANNEL, SAMPLE_RATE, true,
OnAudioRead);
aud.clip = _audioClip;
aud.loop = true;
aud.Play();
this.Log.UpdateLog("Because the api of rtcEngine is called in different threads, it is necessary to use locks to ensure that different threads do not call the api of rtcEngine at the same time");
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
lock (_rtcLock)
{
if (RtcEngine == null) return;
RtcEngine.InitEventHandler(null);
RtcEngine.LeaveChannel();
RtcEngine.Dispose();
RtcEngine = null;
_audioWriter.Flush();
}
//need wait pullAudioFrameThread stop
_pullAudioFrameThread.Join();
if (_audioSource != null)
{
_audioSource.Stop();
}
if (_audioClip != null)
{
Destroy(_audioClip);
}
}
private void PullAudioFrameThread()
{
var avsync_type = 0;
var bytesPerSample = 2;
var type = AUDIO_FRAME_TYPE.FRAME_TYPE_PCM16;
var channels = CHANNEL;
var samplesPerChannel = SAMPLE_RATE / PULL_FREQ_PER_SEC;
var samplesPerSec = SAMPLE_RATE;
var byteBuffer = new byte[samplesPerChannel * bytesPerSample * channels];
var freq = 1000 / PULL_FREQ_PER_SEC;
AudioFrame audioFrame = new AudioFrame
{
type = type,
samplesPerChannel = samplesPerChannel,
bytesPerSample = BYTES_PER_SAMPLE.TWO_BYTES_PER_SAMPLE,
channels = channels,
samplesPerSec = samplesPerSec,
avsync_type = avsync_type
};
audioFrame.buffer = Marshal.AllocHGlobal(samplesPerChannel * bytesPerSample * channels);
double startMillisecond = GetTimestamp();
long tick = 0;
while (true)
{
int nRet;
lock (_rtcLock)
{
if (RtcEngine == null)
{
break;
}
nRet = -1;
nRet = RtcEngine.PullAudioFrame(audioFrame);
// Debug.Log("PullAudioFrame returns: " + nRet);
if (nRet == 0)
{
Marshal.Copy((IntPtr)audioFrame.buffer, byteBuffer, 0, byteBuffer.Length);
var floatArray = ConvertByteToFloat16(byteBuffer);
_audioWriter.PutData(byteBuffer);
lock (_audioBuffer)
{
_audioBuffer.Put(floatArray);
}
_writeCount += floatArray.Length;
}
}
if (nRet == 0)
{
tick++;
double nextMillisecond = startMillisecond + tick * freq;
double curMillisecond = GetTimestamp();
int sleepMillisecond = (int)Math.Ceiling(nextMillisecond - curMillisecond);
//Debug.Log("sleepMillisecond : " + sleepMillisecond);
if (sleepMillisecond > 0)
{
Thread.Sleep(sleepMillisecond);
}
}
}
Marshal.FreeHGlobal(audioFrame.buffer);
}
private static float[] ConvertByteToFloat16(byte[] byteArray)
{
var floatArray = new float[byteArray.Length / 2];
for (var i = 0; i < floatArray.Length; i++)
{
floatArray[i] = BitConverter.ToInt16(byteArray, i * 2) / 32768f; // -Int16.MinValue
}
return floatArray;
}
private void OnAudioRead(float[] data)
{
//if (!_startSignal) return;
lock (_audioBuffer)
{
if(_audioBuffer.Count >= data.Length)
{
_audioBuffer.MoveTo(data);
}
else
{
Array.Clear(data,0, data.Length);
}
//readCount += 1;
}
//Debug.LogFormat("buffer length remains: {0}", _writeCount - _readCount);
}
//get timestamp millisecond
private double GetTimestamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return ts.TotalMilliseconds;
}
}
#region -- Agora Event ---
internal class UserEventHandler : IRtcEngineEventHandler
{
private readonly CustomRenderAudio _customAudioSinkSample;
internal UserEventHandler(CustomRenderAudio customAudioSinkSample)
{
_customAudioSinkSample = customAudioSinkSample;
}
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
int build = 0;
_customAudioSinkSample.Log.UpdateLog(string.Format("sdk version: {0}", _customAudioSinkSample.RtcEngine.GetVersion(ref build)));
_customAudioSinkSample.Log.UpdateLog(string.Format("onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", connection.channelId,
connection.localUid, elapsed));
}
public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
{
_customAudioSinkSample.Log.UpdateLog("OnLeaveChannelSuccess");
}
public override void OnError(int error, string msg)
{
_customAudioSinkSample.Log.UpdateLog(string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
}
public override void OnConnectionLost(RtcConnection connection)
{
_customAudioSinkSample.Log.UpdateLog(string.Format("OnConnectionLost "));
}
}
#endregion
}