1 /*
2 * Copyright (c) 2021-2022 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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyTest"
17 #endif
18
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22 #include <iostream>
23 #include <securec.h>
24 #include <unistd.h>
25
26 #include "audio_errors.h"
27 #include "audio_session_manager.h"
28 #include "audio_system_manager.h"
29 #include "audio_stream_manager.h"
30 #include "audio_policy_log.h"
31
32 using namespace std;
33 using namespace OHOS;
34 using namespace OHOS::AudioStandard;
35
36 namespace AudioPolicyTest {
37 const int FIRST_ARG = 1;
38 const int SECOND_ARG = 2;
39 const int THIRD_ARG = 3;
40 const int FOURTH_ARG = 4;
41 const int FIFTH_ARG = 5;
42 const int SIXTH_ARG = 6;
43 const int SEVENTH_ARG = 7;
44 const int EIGHTH_ARG = 8;
45 const int NINTH_ARG = 9;
46 const int TENTH_ARG = 10;
47 const int ELEVENTH_ARG = 11;
48 const int TWELFTH_ARG = 12;
49 const int OPT_ARG_BASE = 10;
50 const int OPT_SHORT_LEN = 3;
51 const int OPT_GET_VOL_FACTOR = 1;
52 const int OPT_GET_SS_VOL = 2;
53 }
54
PrintUsage(void)55 static void PrintUsage(void)
56 {
57 cout << "NAME" << endl << endl;
58 cout << "\taudio_policy_test - Audio Policy Test " << endl << endl;
59 cout << "SYNOPSIS" << endl << endl;
60 cout << "\t#include <audio_system_manager.h>" << endl << endl;
61 cout << "\t./audio_policy_test [OPTIONS]..." << endl << endl;
62 cout << "DESCRIPTION" << endl << endl;
63 cout << "\tControls audio volume, audio routing, audio mute" << endl << endl;
64 cout << "-V\n\tSets Volume for streams, -S to setStream" << endl << endl;
65 cout << "-v\n\tGets Volume for streams, -S to setStream" << endl << endl;
66 cout << "-S\n\tSet stream type" << endl << endl;
67 cout << "\tSupported Streams are" << endl << endl;
68 cout << "\t4\tMUSIC" << endl << endl;
69 cout << "\t3\tRING" << endl << endl;
70 cout << "-D\n\tSets Device Active" << endl << endl;
71 cout << "\tSupported Devices are" << endl << endl;
72 cout << "\t2\tSPEAKER" << endl << endl;
73 cout << "\t7\tBLUETOOTH_SCO" << endl << endl;
74 cout << "-d\n\tGets Device Active" << endl << endl;
75 cout << "-M\n\tSets Mute for streams, -S to setStream" << endl << endl;
76 cout << "-m\n\tGets Mute for streams, -S to setStream" << endl << endl;
77 cout << "-U\n\t Mutes the Microphone" << endl << endl;
78 cout << "-u\n\t Checks if the Microphone is muted " << endl << endl;
79 cout << "-R\n\tSets RingerMode" << endl << endl;
80 cout << "-r\n\tGets RingerMode status" << endl << endl;
81 cout << "-C\n\tSets AudioScene" << endl << endl;
82 cout << "-c\n\tGets AudioScene status" << endl << endl;
83 cout << "-N\n\tSet the discount volume factor" << endl << endl;
84 cout << "-n\n\tGet the discount volume factor or Get single stream volume" << endl << endl;
85 cout << "-s\n\tGet Stream Status" << endl << endl;
86 cout << "-B\n\tSet AudioMonoState (using 1 or 0 instead of true of false)" << endl;
87 cout << "\tSet AudioBalanceValue (using [9, 11] instead of [-1, 1])" << endl << endl;
88 cout << "-F\n\tAudioFocusInfoListTest (using 1 or 0 instead of true of false)" << endl;
89 cout << "-A\n\t AudioSession: 1.ActivateAudioSession, 2.DeactivateAudioSession, "
90 "3.IsAudioSessionActivated." << endl;
91 cout << "-Y\n\t AudioRendererChangeInfo: 1.GetCurrentRendererChangeInfos, "
92 "2.RegisterAudioRendererEventListener" << endl;
93 cout << "AUTHOR" << endl << endl;
94 cout << "\tWritten by OpenHarmony AudioFramework Team." << endl << endl;
95 }
96
ShowAudioDeviceDescriptorsVector(std::vector<std::shared_ptr<AudioDeviceDescriptor>> & audioDeviceDescriptorsVector)97 static void ShowAudioDeviceDescriptorsVector(
98 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptorsVector)
99 {
100 int vectorLen = audioDeviceDescriptorsVector.size();
101 for (int i = 0; i < vectorLen; i ++) {
102 cout << "------------show Audio Device Descriptors Vector------------" << endl;
103 cout << "deviceRole: " << audioDeviceDescriptorsVector[i]->deviceRole_ << endl;
104 cout << "deviceType: " << audioDeviceDescriptorsVector[i]->deviceType_ << endl;
105 cout << "interruptGroupId: " << audioDeviceDescriptorsVector[i]->interruptGroupId_ << endl;
106 cout << "volumeGroupId: " << audioDeviceDescriptorsVector[i]->volumeGroupId_ << endl;
107 cout << "networkId: " << audioDeviceDescriptorsVector[i]->networkId_ << endl;
108 }
109 }
110
ShowAudioRendererFilter(sptr<AudioRendererFilter> audioRendererFilter)111 static void ShowAudioRendererFilter(sptr<AudioRendererFilter> audioRendererFilter)
112 {
113 cout << "------------show Audio Renderer Filter------------" << endl;
114 cout << "uid: " << audioRendererFilter->uid << endl;
115 cout << "contentType: " << audioRendererFilter->rendererInfo.contentType << endl;
116 cout << "streamUsage: " << audioRendererFilter->rendererInfo.streamUsage << endl;
117 cout << "rendererFlags: " << audioRendererFilter->rendererInfo.rendererFlags << endl;
118 cout << "streamId: " << audioRendererFilter->streamId << endl;
119 }
120
HandleGetDevices(int argc,char * argv[],char option)121 static void HandleGetDevices(int argc, char *argv[], char option)
122 {
123 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
124 if ((option != 'g' && option != 'G') || argc != AudioPolicyTest::THIRD_ARG) {
125 cout << "GetDevices invalid argv["<< argc <<"] "<< endl;
126 }
127 cout << "GetDevices() flag: " << argv[AudioPolicyTest::SECOND_ARG] << endl;
128 int32_t intValue = atoi(argv[AudioPolicyTest::SECOND_ARG]);
129 DeviceFlag deviceFlag = static_cast<DeviceFlag>(intValue);
130 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptorsVector;
131 audioDeviceDescriptorsVector = audioSystemMgr->GetDevices(deviceFlag);
132 cout << "GetDevices(Output Devices) Result: " << endl;
133 ShowAudioDeviceDescriptorsVector(audioDeviceDescriptorsVector);
134 }
135
CallSelectOutputDevice(char option,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & audioDeviceDescriptorsVector,sptr<AudioRendererFilter> audioRendererFilter)136 static void CallSelectOutputDevice(char option,
137 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptorsVector,
138 sptr<AudioRendererFilter> audioRendererFilter)
139 {
140 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
141 if (option == 'o') {
142 int result = audioSystemMgr->SelectOutputDevice(audioDeviceDescriptorsVector);
143 cout << "SelectOutputDevice Result: " << result << endl;
144 } else {
145 int result = audioSystemMgr->SelectOutputDevice(audioRendererFilter, audioDeviceDescriptorsVector);
146 cout << "SelectOutputDevice by filter Result: " << result << endl;
147 }
148 }
149
CreateAudioDeviceDescriptorVector(char * argv[],std::vector<std::shared_ptr<AudioDeviceDescriptor>> & audioDeviceDescriptorsVector)150 static void CreateAudioDeviceDescriptorVector(char *argv[],
151 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptorsVector)
152 {
153 std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = std::make_shared<AudioDeviceDescriptor>();
154 int32_t intValue = atoi(argv[AudioPolicyTest::SECOND_ARG]);
155 audioDeviceDescriptor->deviceRole_ = static_cast<DeviceRole>(intValue);
156 intValue = atoi(argv[AudioPolicyTest::THIRD_ARG]);
157 audioDeviceDescriptor->deviceType_ = static_cast<DeviceType>(intValue);
158 intValue = atoi(argv[AudioPolicyTest::FOURTH_ARG]);
159 audioDeviceDescriptor->interruptGroupId_ = intValue;
160 intValue = atoi(argv[AudioPolicyTest::FIFTH_ARG]);
161 audioDeviceDescriptor->volumeGroupId_ = intValue;
162 audioDeviceDescriptor->networkId_ = std::string(argv[AudioPolicyTest::SIXTH_ARG]);
163 audioDeviceDescriptorsVector.push_back(audioDeviceDescriptor);
164 ShowAudioDeviceDescriptorsVector(audioDeviceDescriptorsVector);
165 }
166
HandleSelectOutputDevice(int argc,char * argv[],char opt)167 static void HandleSelectOutputDevice(int argc, char* argv[], char opt)
168 {
169 if (argc == AudioPolicyTest::SEVENTH_ARG) {
170 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptorsVector;
171 CreateAudioDeviceDescriptorVector(argv, audioDeviceDescriptorsVector);
172
173 CallSelectOutputDevice(opt, audioDeviceDescriptorsVector, nullptr);
174 } else if (argc == AudioPolicyTest::TWELFTH_ARG) {
175 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptorsVector;
176 CreateAudioDeviceDescriptorVector(argv, audioDeviceDescriptorsVector);
177
178 sptr<AudioRendererFilter> audioRendererFilter = new(std::nothrow) AudioRendererFilter();
179 int32_t intValue = atoi(argv[AudioPolicyTest::SEVENTH_ARG]);
180 audioRendererFilter->uid = static_cast<DeviceRole>(intValue);
181 intValue = atoi(argv[AudioPolicyTest::EIGHTH_ARG]);
182 audioRendererFilter->rendererInfo.contentType = static_cast<ContentType>(intValue);
183 intValue = atoi(argv[AudioPolicyTest::NINTH_ARG]);
184 audioRendererFilter->rendererInfo.streamUsage = static_cast<StreamUsage>(intValue);
185 intValue = atoi(argv[AudioPolicyTest::TENTH_ARG]);
186 audioRendererFilter->rendererInfo.rendererFlags = intValue;
187 intValue = atoi(argv[AudioPolicyTest::ELEVENTH_ARG]);
188 audioRendererFilter->streamId = intValue;
189 ShowAudioRendererFilter(audioRendererFilter);
190 CallSelectOutputDevice(opt, audioDeviceDescriptorsVector, audioRendererFilter);
191 } else {
192 cout << "------------Please input right arg Num------------" << endl;
193 cout << "The arg order: " << endl;
194 cout << "audioDevice(deviceRole, deviceType, networkId, interruptGroupId, volumeGroupId)" << endl;
195 cout << "audioRendererFilter(uid,contentType,streamUsage,rendererFlags,streamId)" << endl;
196 }
197 }
198
CallSelectInputDevice(char option,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & audioDeviceDescriptorsVector,sptr<AudioCapturerFilter> audioCapturerFilter)199 static void CallSelectInputDevice(char option,
200 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptorsVector,
201 sptr<AudioCapturerFilter> audioCapturerFilter)
202 {
203 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
204 if (option == 'i') {
205 int result = audioSystemMgr->SelectInputDevice(audioDeviceDescriptorsVector);
206 cout << "SelectInputDevice Result: " << result << endl;
207 } else {
208 int result = audioSystemMgr->SelectInputDevice(audioCapturerFilter, audioDeviceDescriptorsVector);
209 cout << "SelectInputDevice by filter Result: " << result << endl;
210 }
211 }
212
213
HandleSelectInputDevice(int argc,char * argv[],char opt)214 static void HandleSelectInputDevice(int argc, char* argv[], char opt)
215 {
216 if (argc == AudioPolicyTest::SEVENTH_ARG) {
217 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptorsVector;
218 CreateAudioDeviceDescriptorVector(argv, audioDeviceDescriptorsVector);
219
220 CallSelectInputDevice(opt, audioDeviceDescriptorsVector, nullptr);
221 } else if (argc == AudioPolicyTest::EIGHTH_ARG) {
222 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptorsVector;
223 CreateAudioDeviceDescriptorVector(argv, audioDeviceDescriptorsVector);
224
225 sptr<AudioCapturerFilter> audioCapturerFilter = new(std::nothrow) AudioCapturerFilter();
226 int32_t intValue = atoi(argv[AudioPolicyTest::SEVENTH_ARG]);
227 audioCapturerFilter->uid = intValue;
228 cout << "------------show Audio Capturer Filter------------" << endl;
229 cout << "uid: " << audioCapturerFilter->uid << endl;
230 CallSelectInputDevice(opt, audioDeviceDescriptorsVector, audioCapturerFilter);
231 } else {
232 cout << "------------Please input right arg Num------------" << endl;
233 cout << "The arg order: " << endl;
234 cout << "audioDevice(deviceRole, deviceType, networkId, interruptGroupId, volumeGroupId)" << endl;
235 cout << "audioCapturerFilter(uid)" << endl;
236 }
237 }
238
HandleVolume(int argc,char * argv[],int streamType,char option)239 static void HandleVolume(int argc, char* argv[], int streamType, char option)
240 {
241 AudioSystemManager* audioSystemMgr = AudioSystemManager::GetInstance();
242 std::string networkId = LOCAL_NETWORK_ID;
243
244 if ((option != 'v' && option != 'V') || argc > AudioPolicyTest::FOURTH_ARG) {
245 cout << "HandVolume invalid argv[" << argc << "] " << endl;
246 }
247 if (option == 'v' && argc == AudioPolicyTest::THIRD_ARG) {
248 networkId = argv[AudioPolicyTest::SECOND_ARG];
249 cout << "handle volume networkId: " << networkId << endl;
250 std::vector<sptr<VolumeGroupInfo>> groups;
251 audioSystemMgr->GetVolumeGroups(networkId, groups);
252 if (groups.size() > 0) {
253 int32_t groupId = groups[0]->volumeGroupId_;
254 std::shared_ptr<AudioGroupManager> groupManager = audioSystemMgr->GetGroupManager(groupId);
255 float volume = groupManager->GetVolume(static_cast<AudioVolumeType>(streamType));
256 cout << "Get Volume : " << volume << endl;
257 }
258 } else if (option == 'V' && argc == AudioPolicyTest::FOURTH_ARG) {
259 networkId = argv[AudioPolicyTest::THIRD_ARG];
260 cout << "handle volume networkId: " << networkId << endl;
261 std::vector<sptr<VolumeGroupInfo>> groups;
262 audioSystemMgr->GetVolumeGroups(networkId, groups);
263 if (groups.size() > 0) {
264 int32_t groupId = groups[0]->volumeGroupId_;
265 std::shared_ptr<AudioGroupManager> groupManager = audioSystemMgr->GetGroupManager(groupId);
266
267 float volume = strtof(optarg, nullptr);
268 cout << "Set Volume : " << volume << endl;
269 int32_t result = groupManager->SetVolume(static_cast<AudioVolumeType>(streamType), volume);
270 cout << "Set Volume Result: " << result << endl;
271 }
272 } else {
273 cout << "wrong parms " << endl;
274 }
275 }
276
HandleMute(int streamType,char option)277 static void HandleMute(int streamType, char option)
278 {
279 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
280 if (option == 'm') {
281 bool muteStatus = audioSystemMgr->IsStreamMute(static_cast<AudioVolumeType>(streamType));
282 cout << "Get Mute : " << muteStatus << endl;
283 } else {
284 int mute = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
285 cout << "Set Mute : " << mute << endl;
286 int32_t result = audioSystemMgr->SetMute(static_cast<AudioVolumeType>(streamType),
287 (mute) ? true : false);
288 cout << "Set Mute Result: " << result << endl;
289 }
290 }
291
HandleMicMute(char option)292 static void HandleMicMute(char option)
293 {
294 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
295 if (option == 'u') {
296 bool muteStatus = audioSystemMgr->IsMicrophoneMute();
297 cout << "Is Mic Mute : " << muteStatus << endl;
298 } else {
299 int mute = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
300 cout << "Set Mic Mute : " << mute << endl;
301 int32_t result = audioSystemMgr->SetMicrophoneMute((mute) ? true : false);
302 cout << "Set Mic Mute Result: " << result << endl;
303 }
304 }
305
SetStreamType(int & streamType)306 static void SetStreamType(int &streamType)
307 {
308 streamType = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
309 cout << "Set Stream : " << streamType << endl;
310 }
311
IsStreamActive()312 static void IsStreamActive()
313 {
314 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
315 int streamType = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
316 cout << "Stream Active: " << audioSystemMgr->IsStreamActive(
317 static_cast<AudioVolumeType>(streamType)) << endl;
318 }
319
SetDeviceActive(int argc,char * argv[])320 static void SetDeviceActive(int argc, char *argv[])
321 {
322 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
323 int active = -1;
324 int device = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
325 cout << "Set Device : " << device << endl;
326
327 if (optind < argc && *argv[optind] != '-') {
328 active = strtol(argv[optind], nullptr, AudioPolicyTest::OPT_ARG_BASE);
329 optind++;
330 }
331 cout << "Active : " << active << endl << endl;
332
333 int32_t result = audioSystemMgr->SetDeviceActive(DeviceType(device),
334 (active) ? true : false);
335 cout << "Set DeviceActive Result: " << result << endl;
336 }
337
IsDeviceActive()338 static void IsDeviceActive()
339 {
340 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
341 int device = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
342 bool devActiveStatus = audioSystemMgr->IsDeviceActive(DeviceType(device));
343 cout << "GetDevice Active : " << devActiveStatus << endl;
344 }
345
SetAudioParamter(int argc,char * argv[])346 static void SetAudioParamter(int argc, char* argv[])
347 {
348 std::string key = "";
349 std::string value = "";
350 if (argc == AudioPolicyTest::FOURTH_ARG) {
351 key = argv[AudioPolicyTest::SECOND_ARG];
352 value = argv[AudioPolicyTest::THIRD_ARG];
353 AudioSystemManager* audioSystemMgr = AudioSystemManager::GetInstance();
354 audioSystemMgr->SetAudioParameter(key, value);
355 cout << "SetAudioParameter for key " << key << "; value: " << value << endl;
356 }
357 }
358
GetAudioParamter(int argc,char * argv[])359 static void GetAudioParamter(int argc, char* argv[])
360 {
361 std::string key = "";
362 if (argc == AudioPolicyTest::THIRD_ARG) {
363 key = argv[AudioPolicyTest::SECOND_ARG];
364 AudioSystemManager* audioSystemMgr = AudioSystemManager::GetInstance();
365 std::string value = audioSystemMgr->GetAudioParameter(key);
366 cout << "GetAudioParameter for key " << key << "; result: " << value << endl;
367 }
368 }
369
HandleRingerMode(char option)370 static void HandleRingerMode(char option)
371 {
372 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
373 if (option == 'r') {
374 int ringMode = static_cast<int32_t>(audioSystemMgr->GetRingerMode());
375 cout << "Get Ringer Mode : " << ringMode << endl;
376 } else {
377 int ringMode = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
378 cout << "Set Ringer Mode : " << ringMode << endl;
379 audioSystemMgr->SetRingerMode(static_cast<AudioRingerMode>(ringMode));
380 }
381 }
382
HandleAudioScene(char option)383 static void HandleAudioScene(char option)
384 {
385 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
386 if (option == 'c') {
387 int scene = static_cast<int32_t>(audioSystemMgr->GetAudioScene());
388 cout << "Get Audio Scene : " << scene << endl;
389 } else {
390 int scene = strtol(optarg, nullptr, AudioPolicyTest::OPT_ARG_BASE);
391 cout << "Set Audio Scene : " << scene << endl;
392 audioSystemMgr->SetAudioScene(static_cast<AudioScene>(scene));
393 }
394 }
395
NoValueError()396 static void NoValueError()
397 {
398 char option[AudioPolicyTest::OPT_SHORT_LEN];
399 cout << "option ";
400 int len = snprintf_s(option, sizeof(option), sizeof(option) - 1, "-%c", optopt);
401 if (len <= 0) {
402 cout << "NoValueError: snprintf_s error : buffer allocation fails";
403 return;
404 }
405
406 cout << option << " needs a value" << endl << endl;
407 PrintUsage();
408 }
409
UnknownOptionError()410 static void UnknownOptionError()
411 {
412 char option[AudioPolicyTest::OPT_SHORT_LEN];
413 int len = snprintf_s(option, sizeof(option), sizeof(option) - 1, "-%c", optopt);
414 if (len <= 0) {
415 cout << "unknown option: snprintf_s error : buffer allocation fails";
416 return;
417 }
418 cout << "unknown option: " << option << endl;
419 PrintUsage();
420 }
421
HandleUpdateStreamState(int type,char * seg1)422 static void HandleUpdateStreamState(int type, char *seg1)
423 {
424 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
425 cout << "HandleUpdateStreamState : Runing " << seg1 << endl;
426
427 const int32_t uid = atoi(seg1);
428 cout << "HandleUpdateStreamState : uid : " << uid << endl;
429 if (uid == 0) {
430 return;
431 }
432
433 StreamSetState sate = StreamSetState::STREAM_PAUSE;
434 StreamUsage suage = STREAM_USAGE_MEDIA;
435 int32_t result = 0;
436 if (type == 0) {
437 cout << "type :: Stream_Pause :: " << type << endl;
438 } else {
439 sate = StreamSetState::STREAM_RESUME;
440 cout << "type :: Stream_Resume :: " << type << endl;
441 }
442 result = audioSystemMgr->UpdateStreamState(uid, sate, suage);
443 cout << "result : " << result << endl;
444 }
445
HandleSingleStreamVolumeOption(int argc,char * argv[],char opt)446 static void HandleSingleStreamVolumeOption(int argc, char* argv[], char opt)
447 {
448 if (argc != AudioPolicyTest::FOURTH_ARG) {
449 cout << "Incorrect number of test commands." << endl;
450 return;
451 }
452
453 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
454 int32_t streamId = atoi(argv[AudioPolicyTest::SECOND_ARG]);
455 if (opt == 'N') {
456 float volume = atof(argv[AudioPolicyTest::THIRD_ARG]);
457 if (volume < 0 || volume > 1.0f) {
458 cout << "volume out of range." << endl;
459 return;
460 }
461 audioSystemMgr->SetLowPowerVolume(streamId, volume);
462 cout << "Set low power volume :" << volume << endl;
463 } else {
464 int32_t opt_flag = atoi(argv[AudioPolicyTest::THIRD_ARG]);
465 if (opt_flag == AudioPolicyTest::OPT_GET_VOL_FACTOR) {
466 float volume = audioSystemMgr->GetLowPowerVolume(streamId);
467 cout << "Get discounted volume factor: " << volume << endl;
468 } else if (opt_flag == AudioPolicyTest::OPT_GET_SS_VOL) {
469 float volume = audioSystemMgr->GetSingleStreamVolume(streamId);
470 cout << "Get single stream volume: " << volume << endl;
471 } else {
472 cout << "invalid operation." << endl;
473 }
474 }
475 }
476
HandleGetVolumeGroups(int argc,char * argv[])477 static void HandleGetVolumeGroups(int argc, char* argv[])
478 {
479 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
480 if (argc == AudioPolicyTest::THIRD_ARG) {
481 std::string networkId = argv[AudioPolicyTest::SECOND_ARG];
482 cout << "networkId: "<< networkId << endl;
483 std::vector<sptr<VolumeGroupInfo>> volumeGroups;
484 audioSystemMgr->GetVolumeGroups(networkId, volumeGroups);
485 for (auto iter : volumeGroups) {
486 cout << "===============id:" << iter->volumeGroupId_ << "=================" << endl;
487 cout << "name: " << iter->groupName_ << endl;
488 cout << "networkId: " << iter->networkId_ << endl;
489 cout << "connectType: " << iter->connectType_ << endl;
490 cout << "mappingId: " << iter->mappingId_ << endl;
491 }
492 }
493 }
494
HandleAudioBalanceState(int argc,char * argv[])495 static void HandleAudioBalanceState(int argc, char* argv[])
496 {
497 if (argc != AudioPolicyTest::FOURTH_ARG) {
498 cout << "Incorrect number of test commands." << endl;
499 return;
500 }
501 AudioSystemManager *audioSystemMgr = AudioSystemManager::GetInstance();
502
503 int monoValue = atoi(argv[AudioPolicyTest::SECOND_ARG]);
504 if (monoValue != 1 && monoValue != 0) {
505 cout << "Audio mono state is valid." << endl;
506 return;
507 }
508 bool monoState = (monoValue != 0);
509 audioSystemMgr->SetAudioMonoState(monoState);
510 cout << "Audio mono state: " << (monoState? "true" : "false") << endl;
511
512 float balanceValue = atof(argv[AudioPolicyTest::THIRD_ARG]) - 10.0f;
513 if (balanceValue < -1.0f || balanceValue > 1.0f) {
514 cout << "Audio balance value is valid." << endl;
515 return;
516 }
517 audioSystemMgr->SetAudioBalanceValue(balanceValue);
518 cout << "Audio balance value: " << balanceValue << endl;
519 }
520
HandleAudioSession(int argc,char * argv[])521 static void HandleAudioSession(int argc, char *argv[])
522 {
523 if (argc != AudioPolicyTest::FOURTH_ARG) {
524 cout << "Incorrect number of test commands." << endl;
525 return;
526 }
527 AudioSessionManager *sessionManager = AudioSessionManager::GetInstance();
528
529 int32_t option = atoi(argv[AudioPolicyTest::SECOND_ARG]);
530 AudioConcurrencyMode mode = static_cast<AudioConcurrencyMode>(atoi(argv[AudioPolicyTest::THIRD_ARG]));
531 cout << "AudioSession: 1.ActivateAudioSession, 2.DeactivateAudioSession, 3.IsAudioSessionActivated: Input: "
532 << option << endl;
533 int32_t result = 0;
534 switch (option) {
535 case 1: // 1: ActivateAudioSession
536 result = sessionManager->ActivateAudioSession({mode});
537 cout << "ActivateAudioSession: audioConcurrencyMode: " << static_cast<int32_t>(mode) <<
538 ". Result: " << result << endl;
539 break;
540 case 2: // 2: DeactivateAudioSession
541 result = sessionManager->DeactivateAudioSession();
542 cout << "DeactivateAudioSession: Result: " << result << endl;
543 break;
544 case 3: // 3: IsAudioSessionActivated
545 result = sessionManager->IsAudioSessionActivated();
546 cout << "IsAudioSessionActivated: Result: " << result << endl;
547 break;
548 default:
549 break;
550 }
551 }
552
PrintFocusInfoList(const std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)553 static void PrintFocusInfoList(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
554 {
555 cout << "===============FocusInfoList============== size:"<< focusInfoList.size() << endl;
556 for (auto it = focusInfoList.begin(); it != focusInfoList.end(); ++it) {
557 cout <<"| streamUsage: \t\t\t" << it->first.streamUsage << "\t |" << endl;
558 cout <<"| contentType: \t\t\t" << it->first.contentType << "\t |" << endl;
559 cout <<"| audioFocusType.streamType: \t"<< it->first.audioFocusType.streamType<< "\t |" << endl;
560 cout <<"| audioFocusType.sourceType: \t"<< it->first.audioFocusType.sourceType<< "\t |" << endl;
561 cout <<"| audioFocusType.isPlay: \t" << it->first.audioFocusType.isPlay << "\t |" << endl;
562 cout <<"| streamId: \t\t\t" << it->first.streamId << "\t |" << endl;
563 cout <<"| pauseWhenDucked: \t\t" << it->first.pauseWhenDucked << "\t |" << endl;
564 cout <<"| pid: \t\t\t\t" << it->first.pid << "\t |" << endl;
565 cout <<"| mode: \t\t\t" << it->first.mode << "\t |" << endl;
566
567 cout <<"| AudioFocuState: \t\t" << it->second << "\t |" << endl;
568 cout << "------------------------------------------" << endl;
569 }
570 }
571
PrintAudioRendererChangeInfos(const std::vector<shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)572 static void PrintAudioRendererChangeInfos(
573 const std::vector<shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
574 {
575 cout << "===============AudioRendererChangeInfo============== size:"<< audioRendererChangeInfos.size() << endl;
576 for (const auto& uPtraudioRendererChangeInfo : audioRendererChangeInfos) {
577 cout <<"| createrUID: \t\t\t" << uPtraudioRendererChangeInfo->createrUID << "\t |" << endl;
578 cout <<"| clientUID: \t\t\t" << uPtraudioRendererChangeInfo->clientUID << "\t |" << endl;
579 cout <<"| sessionId: \t"<< uPtraudioRendererChangeInfo->sessionId<< "\t |" << endl;
580 cout <<"| streamUsage: \t"<< uPtraudioRendererChangeInfo->rendererInfo.streamUsage<< "\t |" << endl;
581 cout <<"| contentType \t" << uPtraudioRendererChangeInfo->rendererInfo.contentType << "\t |" << endl;
582 cout <<"| rendererState: \t\t\t" << uPtraudioRendererChangeInfo->rendererState << "\t |" << endl;
583 cout << "------------------------------------------" << endl;
584 }
585 cout << "=========================================================="<< endl;
586 }
587
588 class AudioFocusInfoChangeCallbackTest : public AudioFocusInfoChangeCallback {
589 public:
AudioFocusInfoChangeCallbackTest()590 AudioFocusInfoChangeCallbackTest()
591 {
592 cout <<"AudioFocusInfoChangeCallbackTest cosntruct" << endl;
593 }
~AudioFocusInfoChangeCallbackTest()594 ~AudioFocusInfoChangeCallbackTest()
595 {
596 cout <<"AudioFocusInfoChangeCallbackTest destroy" << endl;
597 }
OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)598 void OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
599 {
600 cout << "OnAudioFocusInfoChange" << endl;
601 PrintFocusInfoList(focusInfoList);
602 };
603 };
604
605 class AudioRendererChangeInfoCallbackTest : public AudioRendererStateChangeCallback {
606 public:
OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)607 void OnRendererStateChange(
608 const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) final
609 {
610 PrintAudioRendererChangeInfos(audioRendererChangeInfos);
611 }
612 };
613
RegisterFocusInfoChangeCallback(AudioSystemManager * audioSystemMgr_,std::shared_ptr<AudioFocusInfoChangeCallback> & callback)614 static void RegisterFocusInfoChangeCallback(AudioSystemManager * audioSystemMgr_,
615 std::shared_ptr<AudioFocusInfoChangeCallback> &callback)
616 {
617 if (callback == nullptr) {
618 cout << "RegisterFocusInfoChangeCallback::Failed to allocate memory for callback";
619 return;
620 }
621 auto ret = audioSystemMgr_->RegisterFocusInfoChangeCallback(callback);
622 cout << (ret == SUCCESS ? "Register callback success" : "Register callback fail") << endl;
623 }
624
UnregisterFocusInfoChangeCallback(AudioSystemManager * audioSystemMgr_,std::shared_ptr<AudioFocusInfoChangeCallback> & callback)625 static void UnregisterFocusInfoChangeCallback(AudioSystemManager * audioSystemMgr_,
626 std::shared_ptr<AudioFocusInfoChangeCallback> &callback)
627 {
628 auto ret = audioSystemMgr_->UnregisterFocusInfoChangeCallback(callback);
629 cout << (ret == SUCCESS ? "Unregister callback success" : "Unregister callback fail") << endl;
630 }
631
GetAudioFocusInfoList(AudioSystemManager * audioSystemMgr_)632 static void GetAudioFocusInfoList(AudioSystemManager * audioSystemMgr_)
633 {
634 std::list<std::pair<AudioInterrupt, AudioFocuState>> focusInfoList;
635 int32_t ret = audioSystemMgr_->GetAudioFocusInfoList(focusInfoList);
636 cout << (ret == SUCCESS ? "GetAudioFocusInfoList success" : "GetAudioFocusInfoList fail") << endl;
637 PrintFocusInfoList(focusInfoList);
638 }
639
HandleAudioFocusInfoTest()640 static void HandleAudioFocusInfoTest()
641 {
642 auto audioSystemMgr_ = AudioSystemManager::GetInstance();
643 std::shared_ptr<AudioFocusInfoChangeCallback> callback1 = std::make_shared<AudioFocusInfoChangeCallbackTest>();
644 std::shared_ptr<AudioFocusInfoChangeCallback> callback2 = std::make_shared<AudioFocusInfoChangeCallbackTest>();
645 std::shared_ptr<AudioFocusInfoChangeCallback> callback3 = nullptr;
646
647 cout << "*******************************************************" << endl;
648 cout << "0 \t exit" << endl;
649 cout << "1 \t GetAudioFocusInfoList" << endl;
650 cout << "2 \t RegisterFocusInfoChange callback1" << endl;
651 cout << "3 \t UnregisterFocusInfoChange callback1" << endl;
652 cout << "4 \t RegisterFocusInfoChange callback2" << endl;
653 cout << "5 \t UnregisterFocusInfoChange callback2" << endl;
654 cout << "6 \t UnregisterFocusInfoChange nullptr" << endl;
655 cout << "*******************************************************" << endl << endl;
656
657 int num;
658 while (true) {
659 cin >> num;
660 switch (num) {
661 case 1:
662 GetAudioFocusInfoList(audioSystemMgr_);
663 break;
664
665 case 2:
666 RegisterFocusInfoChangeCallback(audioSystemMgr_, callback1);
667 break;
668
669 case 3:
670 UnregisterFocusInfoChangeCallback(audioSystemMgr_, callback1);
671 break;
672
673 case 4:
674 RegisterFocusInfoChangeCallback(audioSystemMgr_, callback2);
675 break;
676
677 case 5:
678 UnregisterFocusInfoChangeCallback(audioSystemMgr_, callback2);
679 break;
680
681 case 6:
682 UnregisterFocusInfoChangeCallback(audioSystemMgr_, callback3);
683 break;
684
685 case 0:
686 return;
687 default:
688 cout << "unknow cin: " << endl;
689 break;
690 }
691 }
692
693 return;
694 }
695
HandleAudioRendererChangeInfo()696 static void HandleAudioRendererChangeInfo()
697 {
698 auto audioStreamManager = AudioStreamManager::GetInstance();
699 cout << "*******************************************************" << endl;
700 cout << "0 \t exit" << endl;
701 cout << "1 \t GetCurrentRendererChangeInfos" << endl;
702 cout << "2 \t RegisterAudioRendererEventListener callback" << endl;
703 cout << "*******************************************************" << endl << endl;
704
705 char num;
706 while (true) {
707 cin >> num;
708 switch (num) {
709 case '1': {
710 std::vector<shared_ptr<AudioRendererChangeInfo>> res;
711 audioStreamManager->GetCurrentRendererChangeInfos(res);
712 PrintAudioRendererChangeInfos(res);
713 break;
714 }
715 case '2': {
716 audioStreamManager->RegisterAudioRendererEventListener(
717 make_shared<AudioRendererChangeInfoCallbackTest>());
718 break;
719 }
720 case '0':
721 return;
722 default:
723 cout << "unknow cin: " << endl;
724 break;
725 }
726 }
727
728 return;
729 }
730
main(int argc,char * argv[])731 int main(int argc, char* argv[])
732 {
733 int opt = 0;
734 if (((argc >= AudioPolicyTest::SECOND_ARG) && !strcmp(argv[AudioPolicyTest::FIRST_ARG], "--help")) ||
735 (argc == AudioPolicyTest::FIRST_ARG)) {
736 PrintUsage();
737 return ERR_INVALID_PARAM;
738 }
739
740 int streamType = static_cast<int32_t>(AudioVolumeType::STREAM_MUSIC);
741 while ((opt = getopt(argc, argv, ":V:U:S:D:M:R:C:X:Z:d:s:T:B:F:vmrucOoIiGgNntpAY")) != -1) {
742 switch (opt) {
743 case 'A':
744 HandleAudioSession(argc, argv);
745 break;
746 case 'G':
747 case 'g':
748 HandleGetDevices(argc, argv, opt);
749 break;
750 case 'O':
751 case 'o':
752 HandleSelectOutputDevice(argc, argv, opt);
753 break;
754 case 'I':
755 case 'i':
756 HandleSelectInputDevice(argc, argv, opt);
757 break;
758 case 'V':
759 case 'v':
760 HandleVolume(argc, argv, streamType, opt);
761 break;
762 case 'M':
763 case 'm':
764 HandleMute(streamType, opt);
765 break;
766 case 'U':
767 case 'u':
768 HandleMicMute(opt);
769 break;
770 case 'S':
771 SetStreamType(streamType);
772 break;
773 case 's':
774 IsStreamActive();
775 break;
776 case 'D':
777 SetDeviceActive(argc, argv);
778 break;
779 case 'd':
780 IsDeviceActive();
781 break;
782 case 'R':
783 case 'r':
784 HandleRingerMode(opt);
785 break;
786 case 'C':
787 case 'c':
788 HandleAudioScene(opt);
789 break;
790 case 'X':
791 HandleUpdateStreamState(0, optarg);
792 break;
793 case 'Z':
794 HandleUpdateStreamState(1, optarg);
795 break;
796 case 'N':
797 case 'n':
798 HandleSingleStreamVolumeOption(argc, argv, opt);
799 break;
800 case ':':
801 NoValueError();
802 break;
803 case '?':
804 UnknownOptionError();
805 break;
806 case 'T':
807 SetAudioParamter(argc, argv);
808 break;
809 case 't':
810 GetAudioParamter(argc, argv);
811 break;
812 case 'p':
813 HandleGetVolumeGroups(argc, argv);
814 break;
815 case 'B':
816 HandleAudioBalanceState(argc, argv);
817 break;
818 case 'F':
819 HandleAudioFocusInfoTest();
820 break;
821 case 'Y':
822 HandleAudioRendererChangeInfo();
823 break;
824 default:
825 break;
826 }
827 }
828
829 return 0;
830 }
831