-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamingmgr.cpp
More file actions
436 lines (348 loc) · 11.5 KB
/
streamingmgr.cpp
File metadata and controls
436 lines (348 loc) · 11.5 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
#include "streamingmgr.h"
/*
* XAudio2を使ったオーディオストリーミングマネージャー
*
*/
class engcallback : public IXAudio2EngineCallback
{
public:
// EngineCallback の I/F の実装
void STDMETHODCALLTYPE OnProcessingPassEnd() override;
void STDMETHODCALLTYPE OnProcessingPassStart() override;
void STDMETHODCALLTYPE OnCriticalError(HRESULT Error) override;
engcallback();
};
class StreamManager;
class voicecallback : public IXAudio2VoiceCallback
{
private:
StreamManager* sm;
public:
void STDMETHODCALLTYPE OnBufferEnd(void* pBufferContext) override;
void STDMETHODCALLTYPE OnBufferStart(void* pBufferContext) override;
void STDMETHODCALLTYPE OnLoopEnd(void* pBufferContext) override;
void STDMETHODCALLTYPE OnStreamEnd() override;
void STDMETHODCALLTYPE OnVoiceError(void* pBufferContext, HRESULT Error) override;
void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() override;
void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 BytesRequired) override;
voicecallback(StreamManager* _s);
};
// 内部で使用する管理用ハンドル
class StreamManager {
public:
// XAudio2のI/F
IXAudio2* xaudio;
// XAudio2MasteringVoiceのI/F
IXAudio2MasteringVoice* mvoice;
IXAudio2SourceVoice* sourceV;
int prepare_index;
int submit_index;
std::function<void(void*, int)> wavecallback; // 波形要求コールバック関数
int callbacksamples;
int bufsize;
XAUDIO2_BUFFER xbuffer[2];
int next_buffer;
std::shared_ptr<BYTE> bufbody[2];
std::atomic<bool> stopflag;
engcallback* eng;
voicecallback* vbk;
StreamManager() : xaudio(nullptr), mvoice(nullptr), sourceV(nullptr), stopflag(false), eng(new engcallback()), bufsize(0), callbacksamples(0), next_buffer(0), prepare_index(0)
{
this->vbk = new voicecallback(this);
this->submit_index = 0;
this->xbuffer[0] = {};
this->xbuffer[1] = {};
}
void PrepareBuffer(UINT32 samples)
{
LOG_INFO(logger, "PrepareBuffer {0:d}-{1:d}bytes.", this->prepare_index, samples);
XAUDIO2_BUFFER* _b = &this->xbuffer[this->prepare_index];
// コールバック呼び出し
if (_b->pAudioData != NULL) {
delete[] _b->pAudioData;
}
_b->pAudioData = new BYTE[samples];
this->wavecallback((void*)_b->pAudioData, samples / this->bufsize);
_b->AudioBytes = samples;
this->prepare_index = 1 - this->prepare_index;
}
void SubmitBuffer()
{
if (this->stopflag) return;
//LOG_INFO(logger, "SubmitBuffer {0:d}", this->submit_index);
XAUDIO2_BUFFER* _b = &this->xbuffer[this->submit_index];
// バッファをsubmitする
HRESULT hr = this->sourceV->SubmitSourceBuffer(_b, nullptr);
if (FAILED(hr)) {
//LOG_INFO(logger, "[ERROR] Failed submit buffer: hr={0:x}", hr);
}
this->submit_index = 1 - this->submit_index;
}
// オーディオストリーム初期化処理
bool Init()
{
// まずは COMの初期化
HRESULT hr = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr)) {
// COM初期化に失敗
//LOG_INFO(logger, "[ERROR] AudioStreamThread can't initialized : COM初期化に失敗 hr={0:08X}", hr);
return false;
}
// XAudio2初期化
hr = ::XAudio2Create(&(this->xaudio), 0, XAUDIO2_DEFAULT_PROCESSOR);
if (FAILED(hr)) {
// XAudio2の初期化に失敗
//LOG_INFO(logger, "[ERROR] AudioStreamThread can't initialized : XAudio2初期化に失敗 hr={0:08X}", hr);
return false;
}
// EngineCallbackの設定
hr = this->xaudio->RegisterForCallbacks(this->eng);
if (FAILED(hr)) {
// XAudio2のCallback設定に失敗
//LOG_INFO(logger, "[ERROR] AudioStreamThread can't initialized : XAudio2初期化に失敗 hr={0:08X}", hr);
return false;
}
// XAudio2MasteringVoice初期化
hr = this->xaudio->CreateMasteringVoice(&(this->mvoice), 2, XAUDIO2_DEFAULT_SAMPLERATE, 0, NULL, NULL, AUDIO_STREAM_CATEGORY::AudioCategory_Other);
if (FAILED(hr)) {
// XAudio2MasteringVoiceの初期化に失敗
//LOG_INFO(logger, "[ERROR] AudioStreamThread can't initialized : XAudio2MasteringVoice初期化に失敗 hr={0:08X}", hr);
return false;
}
return true;
}
// チャンネル確保処理
bool MakeChannel(const WAVEFORMATEX& format, int bufsamples, std::function<void(void*, int)> callback)
{
HRESULT hr;
// mgr_handle->voices.push_back(VoiceManager(callback, bufsamples, format.nBlockAlign));
hr = this->xaudio->CreateSourceVoice(&(this->sourceV), &format, 0, 2.0F, this->vbk, NULL, NULL);
if (FAILED(hr)) {
// SourceVoice作成に失敗
//LOG_INFO(logger, "[ERROR] sourcevoice can't created : SourceVoice作成に失敗 hr={0:08X}", hr);
return false;
}
// バッファの準備
this->callbacksamples = bufsamples;
this->wavecallback = callback;
this->bufsize = format.nBlockAlign;
int bsize = this->callbacksamples * this->bufsize;
//LOG_INFO(logger, "Makebuffer {0:d}bytes.", bsize);
//this->bufbody[0] = std::make_shared<BYTE>(0x4000);
//LOG_INFO(logger, "Makebuffer {0:d}bytes.", bsize);
//this->bufbody[1] = std::make_shared<BYTE>(0x4000);
for (int _i = 0; _i < 2; _i++) {
xbuffer[_i].Flags = 0;
xbuffer[_i].AudioBytes = bsize;
xbuffer[_i].pAudioData = NULL;
xbuffer[_i].PlayBegin = 0;
xbuffer[_i].PlayLength = 0;
xbuffer[_i].LoopBegin = 0;
xbuffer[_i].LoopLength = 0;
xbuffer[_i].LoopCount = 0;
xbuffer[_i].pContext = (void*)_i;
}
this->next_buffer = 0;
this->prepare_index = 0;
this->submit_index = 0;
return true;
}
// チャンネル再生処理
bool PlayChannel()
{
HRESULT hr;
this->stopflag = false;
// 再生開始前に、まずバッファ一つ目の準備
this->PrepareBuffer(this->callbacksamples * this->bufsize);
// 作ったバッファをキューへ登録
this->SubmitBuffer();
// 2つめの準備をする
// this->PrepareBuffer();
// 再生開始
//LOG_INFO(logger, "play start.");
hr = this->sourceV->Start(0, 0);
if (FAILED(hr)) {
// 再生開始に失敗
//LOG_INFO(logger, "[ERROR] voice can't play : hr={0:08X}", hr);
return false;
}
return true;
}
// チャンネル停止処理
bool StopChannel()
{
HRESULT hr;
this->stopflag = true;
hr = this->sourceV->Stop(0, 0);
if (FAILED(hr)) {
// 停止に失敗
//LOG_INFO(logger, "[ERROR] sourcevoice can't stopped : Stop()に失敗 hr={0:08X}", hr);
return false;
}
// バッファをリフレッシュする
hr = this->sourceV->FlushSourceBuffers();
if (FAILED(hr)) {
// バッファのフラッシュに失敗
//LOG_INFO(logger, "[ERROR] sourcevoice can't flushed : FlushSourceBuffers()に失敗 hr={0:08X}", hr);
return false;
}
return true;
}
// オーディオストリーム解放処理
bool Release()
{
// XAudio2を解放(すると関連してすべてのオブジェクトも解放される模様)
if (this->xaudio != nullptr) {
// this->StopChannel();
this->sourceV->DestroyVoice();
this->mvoice->DestroyVoice();
//this->xaudio->Release();
this->xaudio = nullptr;
this->mvoice = nullptr;
}
//this->bufbody[0].reset();
//this->bufbody[1].reset();
// COMの解放
::CoUninitialize();
delete this->eng;
delete this->vbk;
return true;
}
};
// EngineCallback の I/F の実装
void STDMETHODCALLTYPE engcallback::OnProcessingPassEnd()
{
// オーディオ処理パスが終了した直後に呼び出される
}
void STDMETHODCALLTYPE engcallback::OnProcessingPassStart()
{
// オーディオ処理パスが開始される直前に呼び出される
}
void STDMETHODCALLTYPE engcallback::OnCriticalError(HRESULT Error)
{
// XAudio2を閉じて再起動する必要がある時に呼び出される
// 抜粋:https://learn.microsoft.com/ja-jp/windows/win32/api/xaudio2/nf-xaudio2-ixaudio2enginecallback-oncriticalerror
// szDeviceId パラメーターで特定のデバイスの ID を IXAudio2::CreateMasteringVoice に指定するか、XAUDIO2_NO_VIRTUAL_AUDIO_CLIENT フラグを使用すると、
// 重大なエラーが発生し、基になる WASAPI レンダリング デバイスが使用できなくなった場合は OnCriticalError が発生します。
// これは、ヘッドセットやスピーカーが取り外された場合や、USB オーディオ デバイスが取り外された場合などに発生する可能性があります。
}
engcallback::engcallback()
{
}
void STDMETHODCALLTYPE voicecallback::OnBufferEnd(void* pBufferContext)
{
}
void STDMETHODCALLTYPE voicecallback::OnBufferStart(void* pBufferContext)
{
// 準備済みを登録
// this->sm->SubmitBuffer();
// 次のバッファの準備を通知する
//this->logfunc("SetEvent");
//::SetEvent(this->notifyPrepared);
//this->sm->PrepareBuffer();
}
void STDMETHODCALLTYPE voicecallback::OnLoopEnd(void* pBufferContext)
{
}
void STDMETHODCALLTYPE voicecallback::OnStreamEnd()
{
}
void STDMETHODCALLTYPE voicecallback::OnVoiceError(void* pBufferContext, HRESULT Error)
{
}
void STDMETHODCALLTYPE voicecallback::OnVoiceProcessingPassEnd()
{
}
void STDMETHODCALLTYPE voicecallback::OnVoiceProcessingPassStart(UINT32 BytesRequired)
{
if (BytesRequired > 0)
{
this->sm->PrepareBuffer(BytesRequired);
this->sm->SubmitBuffer();
//LOG_INFO(logger, "OnVoiceProcessingPassStart {0:d}", BytesRequired);
}
}
voicecallback::voicecallback(StreamManager* _s) : sm(_s) {}
std::unique_ptr<StreamManager> mgr_handle = nullptr;
// 初期化
// 引数:
// logfunc : ログ出力のための関数を指定する
bool Initialize_STMGR()
{
if (mgr_handle != nullptr) {
//LOG_INFO(logger, "[ERROR] already initialized.");
return false;
}
mgr_handle = std::make_unique<StreamManager>();
// 初期化コマンド発行
if (mgr_handle->Init() == false) {
// エラー発生
//LOG_INFO(logger, "[ERROR] failed initialized.");
return false;
}
return true;
}
// 終了処理
// 引数:なし
void Release_STMGR()
{
// 初期化していないなのでnullを返す
if (mgr_handle == nullptr)
{
//LOG_INFO(logger, "[ERROR] not initialized AudioStream.");
return;
}
// 終了コマンド発行
mgr_handle->Release();
mgr_handle.release();
}
// デバイスリスト取得
// デバイス選択
// 能力取得?
// 能力のセット
// チャンネル作成
// 引数:format : 出力したい波形情報
// buffersamples : 1回のリクエストに必要なバッファサイズ(サンプル数)
// 戻値 : int <0 でエラー、>=0で作成されたチャンネルのindex
int MakeChannel_STMGR(WAVEFORMATEX format, int buffersamples, std::function<void(void*, int)> callbackf)
{
// 初期化していないなのでnullを返す
if (mgr_handle == nullptr )
{
//LOG_INFO(logger, "[ERROR] not initialized AudioStream.");
return -1;
}
// チャンネル作成コマンド発行
mgr_handle->MakeChannel(format, buffersamples, callbackf);
// 確保出来たハンドルindexを返す
return 0;
}
// チャンネル破棄
// 再生開始
// 引数: ch : チャンネルのindex
// 戻値: true で再生処理OK
// 備考:再生を始める前に、渡されたコールバック関数が2回呼び出されて、バッファ2つ分の波形データの作成が必要となります。
bool PlayChannel_STMGR(int ch)
{
// 初期化していないなのでnullを返す
if (mgr_handle == nullptr)
{
//LOG_INFO(logger, "[ERROR] not initialized AudioStream.");
return false;
}
// チャンネル作成コマンド発行
mgr_handle->PlayChannel();
return true;
}
// 停止
void StopChannel_STMGR(int ch)
{
// 初期化していないなのでnullを返す
if (mgr_handle == nullptr )
{
//LOG_INFO(logger, "[ERROR] not initialized AudioStream.");
return;
}
// チャンネル作成コマンド発行
mgr_handle->StopChannel();
}