• 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 "audio_capturer_source.h"
17 #include "audio_errors.h"
18 #include "audio_renderer_sink.h"
19 #include "iservice_registry.h"
20 #include "media_log.h"
21 #include "system_ability_definition.h"
22 #include "audio_server.h"
23 
24 #include <fstream>
25 #include <sstream>
26 
27 #define PA
28 #ifdef PA
29 extern "C" {
30     extern int ohos_pa_main(int argc, char *argv[]);
31 }
32 #endif
33 
34 using namespace std;
35 
36 namespace OHOS {
37 namespace AudioStandard {
38 std::map<std::string, std::string> AudioServer::audioParameters;
39 const string DEFAULT_COOKIE_PATH = "/data/data/.pulse_dir/state/cookie";
40 
41 REGISTER_SYSTEM_ABILITY_BY_ID(AudioServer, AUDIO_DISTRIBUTED_SERVICE_ID, true)
42 
43 #ifdef PA
44 constexpr int PA_ARG_COUNT = 1;
45 
paDaemonThread(void * arg)46 void *AudioServer::paDaemonThread(void *arg)
47 {
48     /* Load the mandatory pulseaudio modules at start */
49     char *argv[] = {
50         (char*)"pulseaudio",
51     };
52 
53     MEDIA_INFO_LOG("Calling ohos_pa_main\n");
54     ohos_pa_main(PA_ARG_COUNT, argv);
55     MEDIA_INFO_LOG("Exiting ohos_pa_main\n");
56     exit(-1);
57 }
58 #endif
59 
AudioServer(int32_t systemAbilityId,bool runOnCreate)60 AudioServer::AudioServer(int32_t systemAbilityId, bool runOnCreate)
61     : SystemAbility(systemAbilityId, runOnCreate)
62 {}
63 
OnDump()64 void AudioServer::OnDump()
65 {}
66 
OnStart()67 void AudioServer::OnStart()
68 {
69     MEDIA_DEBUG_LOG("AudioService OnStart");
70     bool res = Publish(this);
71     if (res) {
72         MEDIA_DEBUG_LOG("AudioService OnStart res=%{public}d", res);
73     }
74 
75 #ifdef PA
76     int32_t ret = pthread_create(&m_paDaemonThread, nullptr, AudioServer::paDaemonThread, nullptr);
77     if (ret != 0) {
78         MEDIA_ERR_LOG("pthread_create failed %d", ret);
79     }
80     MEDIA_INFO_LOG("Created paDaemonThread\n");
81 #endif
82 }
83 
OnStop()84 void AudioServer::OnStop()
85 {
86     MEDIA_DEBUG_LOG("AudioService OnStop");
87 }
88 
SetAudioParameter(const std::string & key,const std::string & value)89 void AudioServer::SetAudioParameter(const std::string &key, const std::string &value)
90 {
91     MEDIA_DEBUG_LOG("server: set audio parameter");
92     AudioServer::audioParameters[key] = value;
93 }
94 
GetAudioParameter(const std::string & key)95 const std::string AudioServer::GetAudioParameter(const std::string &key)
96 {
97     MEDIA_DEBUG_LOG("server: get audio parameter");
98 
99     if (AudioServer::audioParameters.count(key)) {
100         return AudioServer::audioParameters[key];
101     } else {
102         return "";
103     }
104 }
105 
RetrieveCookie(int32_t & size)106 const char *AudioServer::RetrieveCookie(int32_t &size)
107 {
108     char *cookieInfo = nullptr;
109     size = 0;
110     std::ifstream cookieFile(DEFAULT_COOKIE_PATH, std::ifstream::binary);
111     if (!cookieFile) {
112         return cookieInfo;
113     }
114 
115     cookieFile.seekg (0, cookieFile.end);
116     size = cookieFile.tellg();
117     cookieFile.seekg (0, cookieFile.beg);
118 
119     if ((size > 0) && (size < PATH_MAX)) {
120         cookieInfo = (char *)malloc(size * sizeof(char));
121         if (cookieInfo == nullptr) {
122             MEDIA_ERR_LOG("AudioServer::RetrieveCookie: No memory");
123             cookieFile.close();
124             return cookieInfo;
125         }
126         MEDIA_DEBUG_LOG("Reading: %{public}d characters...", size);
127         cookieFile.read(cookieInfo, size);
128     }
129     cookieFile.close();
130     return cookieInfo;
131 }
132 
GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType)133 int32_t AudioServer::GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType)
134 {
135     MEDIA_DEBUG_LOG("GetMaxVolume server");
136     return MAX_VOLUME;
137 }
138 
GetMinVolume(AudioSystemManager::AudioVolumeType volumeType)139 int32_t AudioServer::GetMinVolume(AudioSystemManager::AudioVolumeType volumeType)
140 {
141     MEDIA_DEBUG_LOG("GetMinVolume server");
142     return MIN_VOLUME;
143 }
144 
SetMicrophoneMute(bool isMute)145 int32_t AudioServer::SetMicrophoneMute(bool isMute)
146 {
147     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance();
148 
149     if (!audioCapturerSourceInstance->capturerInited_) {
150             MEDIA_INFO_LOG("Capturer is not initialized. Set the flag mute state flag");
151             AudioCapturerSource::micMuteState_ = isMute;
152             return 0;
153     }
154 
155     return audioCapturerSourceInstance->SetMute(isMute);
156 }
157 
IsMicrophoneMute()158 bool AudioServer::IsMicrophoneMute()
159 {
160     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance();
161     bool isMute = false;
162 
163     if (!audioCapturerSourceInstance->capturerInited_) {
164         MEDIA_INFO_LOG("Capturer is not initialized. Get the mic mute state flag value!");
165         return AudioCapturerSource::micMuteState_;
166     }
167 
168     if (audioCapturerSourceInstance->GetMute(isMute)) {
169         MEDIA_ERR_LOG("GetMute status in capturer returned Error !");
170     }
171 
172     return isMute;
173 }
174 
SetAudioScene(AudioScene audioScene)175 int32_t AudioServer::SetAudioScene(AudioScene audioScene)
176 {
177     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance();
178     AudioRendererSink *audioRendererSinkInstance = AudioRendererSink::GetInstance();
179 
180     if (!audioCapturerSourceInstance->capturerInited_) {
181         MEDIA_WARNING_LOG("Capturer is not initialized.");
182     } else {
183         audioCapturerSourceInstance->SetAudioScene(audioScene);
184     }
185 
186     if (!audioRendererSinkInstance->rendererInited_) {
187         MEDIA_WARNING_LOG("Renderer is not initialized.");
188     } else {
189         audioRendererSinkInstance->SetAudioScene(audioScene);
190     }
191 
192     return SUCCESS;
193 }
194 
UpdateActiveDeviceRoute(DeviceType type,DeviceFlag flag)195 int32_t AudioServer::UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag)
196 {
197     MEDIA_INFO_LOG("[%{public}s]", __func__);
198     AudioCapturerSource *audioCapturerSourceInstance = AudioCapturerSource::GetInstance();
199     AudioRendererSink *audioRendererSinkInstance = AudioRendererSink::GetInstance();
200 
201     switch (flag) {
202         case DeviceFlag::INPUT_DEVICES_FLAG: {
203             audioCapturerSourceInstance->OpenInput(type);
204             break;
205         }
206         case DeviceFlag::OUTPUT_DEVICES_FLAG: {
207             audioRendererSinkInstance->OpenOutput(type);
208             break;
209         }
210         case DeviceFlag::ALL_DEVICES_FLAG: {
211             audioCapturerSourceInstance->OpenInput(type);
212             audioRendererSinkInstance->OpenOutput(type);
213             break;
214         }
215         default:
216             break;
217     }
218 
219     return SUCCESS;
220 }
221 
GetDevices(DeviceFlag deviceFlag)222 std::vector<sptr<AudioDeviceDescriptor>> AudioServer::GetDevices(DeviceFlag deviceFlag)
223 {
224     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptor = {};
225     return audioDeviceDescriptor;
226 }
227 } // namespace AudioStandard
228 } // namespace OHOS
229