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