1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <cstdio>
16 #include <iostream>
17
18 #include "audio_capturer.h"
19 #include "audio_errors.h"
20 #include "audio_info.h"
21 #include "audio_service_log.h"
22 #include "audio_renderer.h"
23 #include "audio_system_manager.h"
24 #include "audio_utils.h"
25 #include "parameter.h"
26 #include "pcm2wav.h"
27
28 using namespace std;
29 namespace OHOS {
30 namespace AudioStandard {
31 enum AudioOptCode : int32_t {
32 INVALID_OPERATION = -1,
33 INIT_LOCAL_SPK = 0,
34 INIT_REMOTE_SPK = 1,
35 START_SPK = 2,
36 STOP_SPK = 3,
37 SWITCH_SPK = 4,
38 RELEASE_SPK = 5,
39 INIT_LOCAL_MIC = 6,
40 INIT_REMOTE_MIC = 7,
41 START_MIC = 8,
42 STOP_MIC = 9,
43 SWITCH_MIC = 10,
44 RELEASE_MIC = 11,
45 EXIT_DEMO = 12,
46 };
47
48 std::map<int32_t, std::string> g_OptStrMap = {
49 {INIT_LOCAL_SPK, "call local spk init process"},
50 {INIT_REMOTE_SPK, "call remote spk init process"},
51 {START_SPK, "call start spk process"},
52 {STOP_SPK, "call stop spk process"},
53 {SWITCH_SPK, "call switch spk device process"},
54 {RELEASE_SPK, "release spk process"},
55
56 {INIT_LOCAL_MIC, "call local mic init process"},
57 {INIT_REMOTE_MIC, "call remote mic init process"},
58 {START_MIC, "call start mic process"},
59 {STOP_MIC, "call stop mic process"},
60 {SWITCH_MIC, "call switch mic device process"},
61 {RELEASE_MIC, "release mic process"},
62
63 {EXIT_DEMO, "exit interactive run test"},
64 };
65
66 class PlaybackTest : public AudioRendererWriteCallback,
67 public AudioCapturerReadCallback,
68 public std::enable_shared_from_this<PlaybackTest> {
69 public:
70 int32_t InitRenderer(bool isFast);
71 int32_t StartPlay();
72 int32_t StopPlay();
73 int32_t ReleaseRenderer();
74 int32_t InitCapturer(bool isFast);
75 int32_t StartCapture();
76 int32_t StopCapture();
77 int32_t ReleaseCapture();
78 void OnWriteData(size_t length) override;
79 void OnReadData(size_t length) override;
80 bool OpenSpkFile(const std::string &spkFilePath);
81 bool OpenMicFile(const std::string &micFilePath);
82 void CloseSpkFile();
83 void CloseMicFile();
84 int32_t SwitchDevice(DeviceRole deviceRole);
85 void SetSpkRemote(bool isRemote);
86 bool GetSpkRemote();
87 void SetMicRemote(bool isRemote);
88 bool GetMicRemote();
89
90 private:
91 int32_t SwitchOutputDevice();
92 int32_t SwitchInputDevice();
93
94 private:
95 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr;
96 std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
97 static constexpr long WAV_HEADER_SIZE = 44;
98 bool needSkipWavHeader_ = true;
99 FILE *spkWavFile_ = nullptr;
100 FILE *micWavFile_ = nullptr;
101 bool isSpkFast_ = false;
102 bool isMicFast_ = false;
103 bool isSpkRemote_ = false;
104 bool isMicRemote_ = false;
105 };
106
OnWriteData(size_t length)107 void PlaybackTest::OnWriteData(size_t length)
108 {
109 BufferDesc bufDesc;
110 if (audioRenderer_ == nullptr) {
111 AUDIO_ERR_LOG("audioRenderer is nullptr.");
112 return;
113 }
114 audioRenderer_->GetBufferDesc(bufDesc);
115
116 if (spkWavFile_ == nullptr) {
117 AUDIO_ERR_LOG("spkWavFile_ is nullptr.");
118 return;
119 }
120 if (needSkipWavHeader_) {
121 fseek(spkWavFile_, WAV_HEADER_SIZE, SEEK_SET);
122 needSkipWavHeader_ = false;
123 }
124 if (feof(spkWavFile_)) {
125 fseek(spkWavFile_, WAV_HEADER_SIZE, SEEK_SET); // infinite loop
126 }
127 fread(bufDesc.buffer, 1, bufDesc.bufLength, spkWavFile_);
128 AUDIO_INFO_LOG("%{public}s OnWriteData data length: %{public}zu.", __func__, bufDesc.bufLength);
129 audioRenderer_->Enqueue(bufDesc);
130 }
131
OpenSpkFile(const std::string & spkFilePath)132 bool PlaybackTest::OpenSpkFile(const std::string &spkFilePath)
133 {
134 if (spkWavFile_ != nullptr) {
135 AUDIO_ERR_LOG("Spk file has been opened, spkFilePath %{public}s", spkFilePath.c_str());
136 return true;
137 }
138
139 char path[PATH_MAX] = { 0x00 };
140 if ((strlen(spkFilePath.c_str()) > PATH_MAX) || (realpath(spkFilePath.c_str(), path) == nullptr)) {
141 return false;
142 }
143 AUDIO_INFO_LOG("spk path = %{public}s", path);
144 spkWavFile_ = fopen(path, "rb");
145 if (spkWavFile_ == nullptr) {
146 AUDIO_ERR_LOG("Unable to open wave file");
147 return false;
148 }
149 return true;
150 }
151
CloseSpkFile()152 void PlaybackTest::CloseSpkFile()
153 {
154 if (spkWavFile_ != nullptr) {
155 fclose(spkWavFile_);
156 spkWavFile_ = nullptr;
157 }
158 }
159
SetSpkRemote(bool isRemote)160 void PlaybackTest::SetSpkRemote(bool isRemote)
161 {
162 isSpkRemote_ = isRemote;
163 }
164
GetSpkRemote()165 bool PlaybackTest::GetSpkRemote()
166 {
167 return isSpkRemote_;
168 }
169
SetMicRemote(bool isRemote)170 void PlaybackTest::SetMicRemote(bool isRemote)
171 {
172 isMicRemote_ = isRemote;
173 }
174
GetMicRemote()175 bool PlaybackTest::GetMicRemote()
176 {
177 return isMicRemote_;
178 }
179
InitRenderer(bool isFast)180 int32_t PlaybackTest::InitRenderer(bool isFast)
181 {
182 AudioStandard::AudioRendererOptions rendererOptions = {
183 {
184 AudioStandard::AudioSamplingRate::SAMPLE_RATE_48000,
185 AudioStandard::AudioEncodingType::ENCODING_PCM,
186 AudioStandard::AudioSampleFormat::SAMPLE_S16LE,
187 AudioStandard::AudioChannel::STEREO,
188 },
189 {
190 AudioStandard::ContentType::CONTENT_TYPE_UNKNOWN,
191 AudioStandard::StreamUsage::STREAM_USAGE_GAME,
192 isFast ? AudioStandard::STREAM_FLAG_FAST : AudioStandard::STREAM_FLAG_NORMAL, // fast audio stream
193 }
194 };
195 audioRenderer_ = AudioStandard::AudioRenderer::Create(rendererOptions);
196 if (audioRenderer_ == nullptr) {
197 AUDIO_ERR_LOG("Audio renderer create failed.");
198 return -1;
199 }
200 std::string path = "/data/test.wav";
201 OpenSpkFile(path);
202 int32_t ret = audioRenderer_->SetRenderMode(RENDER_MODE_CALLBACK);
203 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Set render mode callback fail, ret %{public}d.", ret);
204 ret = audioRenderer_->SetRendererWriteCallback(shared_from_this());
205 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client test save data callback fail, ret %{public}d.", ret);
206 AUDIO_INFO_LOG("Audio renderer create success.");
207 isSpkFast_ = isFast;
208 return 0;
209 }
210
SwitchDevice(DeviceRole deviceRole)211 int32_t PlaybackTest::SwitchDevice(DeviceRole deviceRole)
212 {
213 if (deviceRole == OUTPUT_DEVICE) {
214 return SwitchOutputDevice();
215 } else {
216 return SwitchInputDevice();
217 }
218 }
219
SwitchOutputDevice()220 int32_t PlaybackTest::SwitchOutputDevice()
221 {
222 cout << "Select output device. current play device: " << isSpkRemote_ << "(0 : local, 1 : remote)" << endl;
223 AudioSystemManager *manager = AudioSystemManager::GetInstance();
224 if (manager == nullptr) {
225 cout << "Get AudioSystemManager failed" << std::endl;
226 return ERR_INVALID_OPERATION;
227 }
228
229 vector<sptr<AudioDeviceDescriptor>> devices;
230 if (isSpkRemote_) {
231 devices = manager->GetDevices(OUTPUT_DEVICES_FLAG);
232 vector<sptr<AudioDeviceDescriptor>>::iterator it;
233 for (it = devices.begin(); it != devices.end();) {
234 if ((*it)->deviceType_ != DEVICE_TYPE_SPEAKER) {
235 it = devices.erase(it);
236 } else {
237 it++;
238 }
239 }
240 } else {
241 devices = manager->GetDevices(DISTRIBUTED_OUTPUT_DEVICES_FLAG);
242 }
243 if (devices.size() != 1) {
244 cout << "GetDevices failed, find no device, unsupported size:" << devices.size() << endl;
245 return ERR_INVALID_OPERATION;
246 }
247
248 cout << "using device:" << devices[0]->networkId_ << endl;
249
250 int32_t ret = -1;
251
252 sptr<AudioRendererFilter> filter = new AudioRendererFilter();
253 filter->uid = getuid();
254 if (isSpkFast_) {
255 filter->rendererInfo.rendererFlags = STREAM_FLAG_FAST;
256 }
257 ret = manager->SelectOutputDevice(filter, devices);
258 if (ret == SUCCESS) {
259 isSpkRemote_ = !isSpkRemote_;
260 cout << "Select output device success. current play device:" <<
261 isSpkRemote_ << "(0 : local, 1 : remote)" << endl;
262 } else {
263 cout << "SelectOutputDevice failed, ret:" << ret << endl;
264 }
265 return ret;
266 }
267
SwitchInputDevice()268 int32_t PlaybackTest::SwitchInputDevice()
269 {
270 AudioSystemManager *manager = AudioSystemManager::GetInstance();
271 if (manager == nullptr) {
272 cout << "Get AudioSystemManager failed" << std::endl;
273 return ERR_INVALID_OPERATION;
274 }
275
276 vector<sptr<AudioDeviceDescriptor>> devices;
277 if (isMicRemote_) {
278 devices = manager->GetDevices(INPUT_DEVICES_FLAG);
279 } else {
280 devices = manager->GetDevices(DISTRIBUTED_INPUT_DEVICES_FLAG);
281 }
282 if (devices.size() != 1) {
283 cout << "GetDevices failed, find no device, unsupported size:" << devices.size() << endl;
284 return ERR_INVALID_OPERATION;
285 }
286
287 cout << "using device:" << devices[0]->networkId_ << endl;
288
289 int32_t ret = -1;
290
291 sptr<AudioCapturerFilter> filter = new AudioCapturerFilter();
292 filter->uid = getuid();
293 if (isMicFast_) {
294 filter->capturerInfo.sourceType = SOURCE_TYPE_MIC;
295 filter->capturerInfo.capturerFlags = STREAM_FLAG_FAST;
296 }
297 ret = manager->SelectInputDevice(filter, devices);
298 if (ret == SUCCESS) {
299 isMicRemote_ = !isMicRemote_;
300 cout << "SelectInputDevice success" << endl;
301 } else {
302 cout << "SelectInputDevice failed, ret:" << ret << endl;
303 }
304 return ret;
305 }
306
StartPlay()307 int32_t PlaybackTest::StartPlay()
308 {
309 if (audioRenderer_ == nullptr) {
310 AUDIO_ERR_LOG("Audiorenderer init failed.");
311 return -1;
312 }
313 if (!audioRenderer_->Start()) {
314 AUDIO_ERR_LOG("Audio renderer start failed.");
315 return -1;
316 }
317 cout << "Start play. current play device: " << isSpkRemote_ << "(0 : local, 1 : remote)" << endl;
318 return 0;
319 }
320
StopPlay()321 int32_t PlaybackTest::StopPlay()
322 {
323 if (audioRenderer_ == nullptr) {
324 AUDIO_ERR_LOG("Audiorenderer init failed.");
325 return -1;
326 }
327 if (!audioRenderer_->Stop()) {
328 AUDIO_ERR_LOG("Audio renderer stop failed.");
329 return -1;
330 }
331 return 0;
332 }
333
ReleaseRenderer()334 int32_t PlaybackTest::ReleaseRenderer()
335 {
336 StopPlay();
337 isSpkFast_ = false;
338 if (audioRenderer_ != nullptr && !audioRenderer_->Release()) {
339 AUDIO_ERR_LOG("Audio renderer release failed.");
340 cout << "Audio render release failed" << endl;
341 }
342 audioRenderer_ = nullptr;
343 CloseSpkFile();
344 AUDIO_INFO_LOG("Audio renderer release success.");
345 return 0;
346 }
347
OnReadData(size_t length)348 void PlaybackTest::OnReadData(size_t length)
349 {
350 BufferDesc bufDesc;
351 if (audioCapturer_ == nullptr) {
352 AUDIO_ERR_LOG("audioCapturer is nullptr.");
353 return;
354 }
355 int32_t ret = audioCapturer_->GetBufferDesc(bufDesc);
356 if (ret != 0 || bufDesc.buffer == nullptr || bufDesc.bufLength == 0) {
357 AUDIO_ERR_LOG("Get buffer desc failed. On read data.");
358 return;
359 }
360 if (micWavFile_ == nullptr) {
361 AUDIO_ERR_LOG("micWavFile_ is nullptr.");
362 return;
363 }
364 size_t cnt = fwrite(bufDesc.buffer, 1, bufDesc.bufLength, micWavFile_);
365 if (cnt != bufDesc.bufLength) {
366 AUDIO_ERR_LOG("fwrite fail, cnt %{public}zu, bufLength %{public}zu.", cnt, bufDesc.bufLength);
367 return;
368 }
369 audioCapturer_->Enqueue(bufDesc);
370 }
371
OpenMicFile(const std::string & micFilePath)372 bool PlaybackTest::OpenMicFile(const std::string &micFilePath)
373 {
374 if (micWavFile_ != nullptr) {
375 AUDIO_ERR_LOG("Mic file has been opened, micFilePath %{public}s.", micFilePath.c_str());
376 return true;
377 }
378
379 char path[PATH_MAX] = { 0x00 };
380 if ((strlen(micFilePath.c_str()) > PATH_MAX) || (realpath(micFilePath.c_str(), path) == nullptr)) {
381 AUDIO_ERR_LOG("micFilePath is not valid.");
382 return false;
383 }
384 AUDIO_INFO_LOG("mic path = %{public}s.", path);
385 micWavFile_ = fopen(path, "ab+");
386 if (micWavFile_ == nullptr) {
387 AUDIO_ERR_LOG("Unable to open wave file");
388 return false;
389 }
390 return true;
391 }
392
CloseMicFile()393 void PlaybackTest::CloseMicFile()
394 {
395 if (micWavFile_ != nullptr) {
396 fclose(micWavFile_);
397 micWavFile_ = nullptr;
398 }
399 }
400
InitCapturer(bool isFast)401 int32_t PlaybackTest::InitCapturer(bool isFast)
402 {
403 AudioStandard::AudioCapturerOptions capturerOptions = {
404 {
405 AudioStandard::AudioSamplingRate::SAMPLE_RATE_48000,
406 AudioStandard::AudioEncodingType::ENCODING_PCM,
407 AudioStandard::AudioSampleFormat::SAMPLE_S16LE,
408 AudioStandard::AudioChannel::STEREO,
409 },
410 {
411 AudioStandard::SourceType::SOURCE_TYPE_MIC,
412 isFast ? AudioStandard::STREAM_FLAG_FAST : AudioStandard::STREAM_FLAG_NORMAL,
413 }
414 };
415 audioCapturer_ = AudioStandard::AudioCapturer::Create(capturerOptions);
416 if (audioCapturer_ == nullptr) {
417 AUDIO_ERR_LOG("Audio capturer create failed.");
418 return -1;
419 }
420 std::string path = "/data/mic.pcm";
421 OpenMicFile(path);
422 int32_t ret = audioCapturer_->SetCaptureMode(CAPTURE_MODE_CALLBACK);
423 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Set capture mode callback fail, ret %{public}d.", ret);
424 ret = audioCapturer_->SetCapturerReadCallback(shared_from_this());
425 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client test save data callback fail, ret %{public}d.", ret);
426 AUDIO_INFO_LOG("Audio capturer create success.");
427 isMicFast_ = isFast;
428 return 0;
429 }
430
StartCapture()431 int32_t PlaybackTest::StartCapture()
432 {
433 if (audioCapturer_ == nullptr) {
434 AUDIO_ERR_LOG("audioCapturer init failed.");
435 return -1;
436 }
437 if (!audioCapturer_->Start()) {
438 AUDIO_ERR_LOG("Audio capture start failed.");
439 return -1;
440 }
441 return 0;
442 }
443
StopCapture()444 int32_t PlaybackTest::StopCapture()
445 {
446 if (audioCapturer_ == nullptr) {
447 AUDIO_ERR_LOG("audioCapturer init failed.");
448 return -1;
449 }
450 if (!audioCapturer_->Stop()) {
451 AUDIO_ERR_LOG("Audio capture stop failed.");
452 return -1;
453 }
454 return 0;
455 }
456
ReleaseCapture()457 int32_t PlaybackTest::ReleaseCapture()
458 {
459 isMicFast_ = false;
460 if (audioCapturer_ != nullptr && !audioCapturer_->Release()) {
461 AUDIO_ERR_LOG("Audio capturer release failed.");
462 cout << "Audio capturer release failed" << endl;
463 }
464 audioCapturer_ = nullptr;
465 CloseMicFile();
466 AUDIO_INFO_LOG("Audio capturer release success.");
467 return 0;
468 }
469 using CallTestOptCodeFunc = int32_t (*)(std::shared_ptr<PlaybackTest> playTest);
470
SetSysPara(const std::string & key,int32_t & value)471 bool SetSysPara(const std::string &key, int32_t &value)
472 {
473 auto res = SetParameter(key.c_str(), std::to_string(value).c_str());
474 if (res < 0) {
475 AUDIO_WARNING_LOG("SetSysPara fail, key:%{public}s res:%{public}d", key.c_str(), res);
476 return false;
477 }
478 AUDIO_INFO_LOG("SetSysPara success.");
479 return true;
480 }
481
GetUserInput()482 int32_t GetUserInput()
483 {
484 int32_t res = -1; // result
485 size_t count = 3; // try three time
486 cout << ">>";
487 cin >> res;
488 while (cin.fail() && count-- > 0) {
489 cin.clear();
490 cin.ignore();
491 cout << "invalid input, not a number! Please retry with a number." << endl;
492 cout << ">>";
493 cin >> res;
494 }
495 return res;
496 }
497
PrintUsage()498 void PrintUsage()
499 {
500 cout << "Supported Functionalities:" << endl;
501 cout << "================================Usage=======================================" << endl << endl;
502 cout << " 0: Init local spk." << endl;
503 cout << " 1: Init remote spk." << endl;
504 cout << " 2: Start play." << endl;
505 cout << " 3: Stop play." << endl;
506 cout << " 4: Switch play device between local and remote." << endl;
507 cout << " 5: Release spk." << endl;
508 cout << " 6: Init local mic." << endl;
509 cout << " 7: Init remote mic." << endl;
510 cout << " 8: Start record." << endl;
511 cout << " 9: Stop record." << endl;
512 cout << " 10: Switch record device between local and remote." << endl;
513 cout << " 11: Release mic." << endl;
514 cout << " 12: exit demo." << endl;
515 cout << " Please input your choice: " << endl;
516 }
517
InitPlayback(std::shared_ptr<PlaybackTest> playTest,bool isRemote,bool isFast)518 int32_t InitPlayback(std::shared_ptr<PlaybackTest> playTest, bool isRemote, bool isFast)
519 {
520 cout << "Init renderer." << endl;
521 cout << "--isRemote: " << isRemote << "-- --isSpkFast: " << isFast << " --" <<endl;
522 if (playTest == nullptr) {
523 cout << "Play test is nullptr, init spk error." << endl;
524 return -1;
525 }
526 if (isRemote) {
527 cout << "Use remote device, select remote spk." << endl;
528 int32_t ret = playTest->SwitchDevice(OUTPUT_DEVICE);
529 if (ret != 0) {
530 cout << "find no remote device." << endl;
531 return -1;
532 }
533 }
534 int32_t ret = playTest->InitRenderer(isFast);
535 if (ret != 0) {
536 cout << "Init renderer error!" << endl;
537 return -1;
538 }
539 if (isRemote) {
540 playTest->SetSpkRemote(true);
541 }
542 cout << "Init renderer success." << endl << endl;
543 return 0;
544 }
545
ReleasePlayback(std::shared_ptr<PlaybackTest> playTest)546 int32_t ReleasePlayback(std::shared_ptr<PlaybackTest> playTest)
547 {
548 if (playTest == nullptr) {
549 cout << "Play test is nullptr, release spk error." << endl;
550 return -1;
551 }
552 int32_t ret = playTest->ReleaseRenderer();
553 if (ret != 0) {
554 cout << "Release renderer error!" << endl;
555 return -1;
556 }
557 playTest->SetSpkRemote(false);
558 cout << "Release renderer success." << endl << endl;
559 return 0;
560 }
561
StartPlay(std::shared_ptr<PlaybackTest> playTest)562 int32_t StartPlay(std::shared_ptr<PlaybackTest> playTest)
563 {
564 if (playTest == nullptr) {
565 cout << "Play test is nullptr, start play error." << endl;
566 return -1;
567 }
568 int32_t ret = playTest->StartPlay();
569 if (ret != 0) {
570 cout << "Start play error!" << endl;
571 return -1;
572 }
573 cout << "Start play success." << endl << endl;
574 return 0;
575 }
576
StopPlay(std::shared_ptr<PlaybackTest> playTest)577 int32_t StopPlay(std::shared_ptr<PlaybackTest> playTest)
578 {
579 if (playTest == nullptr) {
580 cout << "Play test is nullptr, stop play error." << endl;
581 return -1;
582 }
583 int32_t ret = playTest->StopPlay();
584 if (ret != 0) {
585 cout << "Stop play error!" << endl;
586 return -1;
587 }
588 cout << "Stop play success." << endl << endl;
589 return 0;
590 }
591
SwitchPlayDevice(std::shared_ptr<PlaybackTest> playTest)592 int32_t SwitchPlayDevice(std::shared_ptr<PlaybackTest> playTest)
593 {
594 if (playTest == nullptr) {
595 cout << "Play test is nullptr, switch play device error." << endl;
596 return -1;
597 }
598 int32_t ret = playTest->SwitchDevice(OUTPUT_DEVICE);
599 if (ret != 0) {
600 cout << "Switch play device error!" << endl;
601 return -1;
602 }
603 cout << "Switch play device success." << endl << endl;
604 return 0;
605 }
606
InitMic(std::shared_ptr<PlaybackTest> playTest,bool isRemote,bool isFast)607 int32_t InitMic(std::shared_ptr<PlaybackTest> playTest, bool isRemote, bool isFast)
608 {
609 if (playTest == nullptr) {
610 cout << "Play test is nullptr, init mic error." << endl;
611 return -1;
612 }
613 if (isRemote) {
614 cout << "Use remote device, select remote mic." << endl;
615 int32_t ret = playTest->SwitchDevice(INPUT_DEVICE);
616 if (ret != 0) {
617 cout << "find no remote device." << endl;
618 return -1;
619 }
620 }
621 int32_t ret = playTest->InitCapturer(isFast);
622 if (ret != 0) {
623 cout << "Init capturer error!" << endl;
624 return -1;
625 }
626 if (isRemote) {
627 playTest->SetMicRemote(true);
628 }
629 cout << "Init capturer success." << endl << endl;
630 return 0;
631 }
632
ReleaseMic(std::shared_ptr<PlaybackTest> playTest)633 int32_t ReleaseMic(std::shared_ptr<PlaybackTest> playTest)
634 {
635 if (playTest == nullptr) {
636 cout << "Play test is nullptr, release capturer error." << endl;
637 return -1;
638 }
639 int32_t ret = playTest->ReleaseCapture();
640 if (ret != 0) {
641 cout << "Release capturer error!" << endl;
642 return -1;
643 }
644 playTest->SetMicRemote(false);
645 cout << "Release capturer success." << endl << endl;
646 return 0;
647 }
648
StartCapture(std::shared_ptr<PlaybackTest> playTest)649 int32_t StartCapture(std::shared_ptr<PlaybackTest> playTest)
650 {
651 if (playTest == nullptr) {
652 cout << "Play test is nullptr, start capturer error." << endl;
653 return -1;
654 }
655 int32_t ret = playTest->StartCapture();
656 if (ret != 0) {
657 cout << "Start capturer error!" << endl;
658 return -1;
659 }
660 cout << "Start capturer success." << endl << endl;
661 return 0;
662 }
663
StopCapture(std::shared_ptr<PlaybackTest> playTest)664 int32_t StopCapture(std::shared_ptr<PlaybackTest> playTest)
665 {
666 if (playTest == nullptr) {
667 cout << "Play test is nullptr, stop capturer error." << endl;
668 return -1;
669 }
670 int32_t ret = playTest->StopCapture();
671 if (ret != 0) {
672 cout << "Stop capturer error!" << endl;
673 return -1;
674 }
675 cout << "Stop capturer success." << endl << endl;
676 return 0;
677 }
678
SwitchCaptureDevice(std::shared_ptr<PlaybackTest> playTest)679 int32_t SwitchCaptureDevice(std::shared_ptr<PlaybackTest> playTest)
680 {
681 if (playTest == nullptr) {
682 cout << "Play test is nullptr, switch capture device error." << endl;
683 return -1;
684 }
685 int32_t ret = playTest->SwitchDevice(INPUT_DEVICE);
686 if (ret != 0) {
687 cout << "Switch capture device error!" << endl;
688 return -1;
689 }
690 cout << "Switch capture device success." << endl << endl;
691 return 0;
692 }
693
694 std::map<int32_t, CallTestOptCodeFunc> g_optFuncMap = {
695 {START_SPK, StartPlay},
696 {STOP_SPK, StopPlay},
697 {SWITCH_SPK, SwitchPlayDevice},
698 {RELEASE_SPK, ReleasePlayback},
699 {START_MIC, StartCapture},
700 {STOP_MIC, StopCapture},
701 {SWITCH_MIC, SwitchCaptureDevice},
702 {RELEASE_MIC, ReleaseMic},
703 };
704
Loop(std::shared_ptr<PlaybackTest> playTest)705 void Loop(std::shared_ptr<PlaybackTest> playTest)
706 {
707 bool isProcTestRun = true;
708 while (isProcTestRun) {
709 PrintUsage();
710 AudioOptCode optCode = INVALID_OPERATION;
711 int32_t res = GetUserInput();
712 if (g_OptStrMap.count(res)) {
713 optCode = static_cast<AudioOptCode>(res);
714 }
715 switch (optCode) {
716 case INIT_LOCAL_SPK:
717 InitPlayback(playTest, false, false);
718 break;
719 case INIT_REMOTE_SPK:
720 InitPlayback(playTest, true, false);
721 break;
722 case INIT_LOCAL_MIC:
723 InitMic(playTest, false, false);
724 break;
725 case INIT_REMOTE_MIC:
726 InitMic(playTest, true, false);
727 break;
728 case EXIT_DEMO:
729 ReleasePlayback(playTest);
730 ReleaseMic(playTest);
731 isProcTestRun = false;
732 cout << "exit demo..." << endl;
733 break;
734 default:
735 auto it = g_optFuncMap.find(optCode);
736 if (it != g_optFuncMap.end() && it->second != nullptr) {
737 CallTestOptCodeFunc &func = it->second;
738 cout << (*func)(playTest) << endl;
739 break;
740 }
741 cout << "Invalid input: " << optCode << endl;
742 break;
743 }
744 }
745 }
746 }
747 }
748
749 using namespace OHOS::AudioStandard;
750 using namespace std;
main(int argc,char * argv[])751 int main(int argc, char* argv[])
752 {
753 cout << "[Fast Audio Stream Test App]" << endl << endl;
754 AUDIO_INFO_LOG("oh fast audio stream test.");
755 std::shared_ptr<PlaybackTest> playTest = std::make_shared<PlaybackTest>();
756 int32_t val = 1;
757 std::string key = "persist.multimedia.audio.mmap.enable";
758 SetSysPara(key, val);
759 Loop(playTest);
760 return 0;
761 }