• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "AudioPolicyIntefaceImpl"
18 //#define LOG_NDEBUG 0
19 
20 #include "AudioPolicyService.h"
21 #include "TypeConverter.h"
22 #include <media/MediaAnalyticsItem.h>
23 #include <media/AudioPolicy.h>
24 #include <utils/Log.h>
25 
26 namespace android {
27 
28 
29 // ----------------------------------------------------------------------------
30 
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name,audio_format_t encodedFormat)31 status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
32                                                   audio_policy_dev_state_t state,
33                                                   const char *device_address,
34                                                   const char *device_name,
35                                                   audio_format_t encodedFormat)
36 {
37     if (mAudioPolicyManager == NULL) {
38         return NO_INIT;
39     }
40     if (!settingsAllowed()) {
41         return PERMISSION_DENIED;
42     }
43     if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
44             state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
45         return BAD_VALUE;
46     }
47 
48     ALOGV("setDeviceConnectionState()");
49     Mutex::Autolock _l(mLock);
50     AutoCallerClear acc;
51     return mAudioPolicyManager->setDeviceConnectionState(device, state,
52                                                          device_address, device_name, encodedFormat);
53 }
54 
getDeviceConnectionState(audio_devices_t device,const char * device_address)55 audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
56                                                               audio_devices_t device,
57                                                               const char *device_address)
58 {
59     if (mAudioPolicyManager == NULL) {
60         return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
61     }
62     AutoCallerClear acc;
63     return mAudioPolicyManager->getDeviceConnectionState(device,
64                                                       device_address);
65 }
66 
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name,audio_format_t encodedFormat)67 status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device,
68                                                   const char *device_address,
69                                                   const char *device_name,
70                                                   audio_format_t encodedFormat)
71 {
72     if (mAudioPolicyManager == NULL) {
73         return NO_INIT;
74     }
75     if (!settingsAllowed()) {
76         return PERMISSION_DENIED;
77     }
78 
79     ALOGV("handleDeviceConfigChange()");
80     Mutex::Autolock _l(mLock);
81     AutoCallerClear acc;
82     return mAudioPolicyManager->handleDeviceConfigChange(device, device_address,
83                                                          device_name, encodedFormat);
84 }
85 
setPhoneState(audio_mode_t state)86 status_t AudioPolicyService::setPhoneState(audio_mode_t state)
87 {
88     if (mAudioPolicyManager == NULL) {
89         return NO_INIT;
90     }
91     if (!settingsAllowed()) {
92         return PERMISSION_DENIED;
93     }
94     if (uint32_t(state) >= AUDIO_MODE_CNT) {
95         return BAD_VALUE;
96     }
97 
98     ALOGV("setPhoneState()");
99 
100     // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic
101     // operation from policy manager standpoint (no other operation (e.g track start or stop)
102     // can be interleaved).
103     Mutex::Autolock _l(mLock);
104     // TODO: check if it is more appropriate to do it in platform specific policy manager
105     AudioSystem::setMode(state);
106 
107     AutoCallerClear acc;
108     mAudioPolicyManager->setPhoneState(state);
109     mPhoneState = state;
110     return NO_ERROR;
111 }
112 
getPhoneState()113 audio_mode_t AudioPolicyService::getPhoneState()
114 {
115     Mutex::Autolock _l(mLock);
116     return mPhoneState;
117 }
118 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)119 status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
120                                          audio_policy_forced_cfg_t config)
121 {
122     if (mAudioPolicyManager == NULL) {
123         return NO_INIT;
124     }
125 
126     if (!modifyAudioRoutingAllowed()) {
127         return PERMISSION_DENIED;
128     }
129 
130     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
131         return BAD_VALUE;
132     }
133     if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
134         return BAD_VALUE;
135     }
136     ALOGV("setForceUse()");
137     Mutex::Autolock _l(mLock);
138     AutoCallerClear acc;
139     mAudioPolicyManager->setForceUse(usage, config);
140     return NO_ERROR;
141 }
142 
getForceUse(audio_policy_force_use_t usage)143 audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
144 {
145     if (mAudioPolicyManager == NULL) {
146         return AUDIO_POLICY_FORCE_NONE;
147     }
148     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
149         return AUDIO_POLICY_FORCE_NONE;
150     }
151     AutoCallerClear acc;
152     return mAudioPolicyManager->getForceUse(usage);
153 }
154 
getOutput(audio_stream_type_t stream)155 audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream)
156 {
157     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
158         return AUDIO_IO_HANDLE_NONE;
159     }
160     if (mAudioPolicyManager == NULL) {
161         return AUDIO_IO_HANDLE_NONE;
162     }
163     ALOGV("getOutput()");
164     Mutex::Autolock _l(mLock);
165     AutoCallerClear acc;
166     return mAudioPolicyManager->getOutput(stream);
167 }
168 
getOutputForAttr(audio_attributes_t * attr,audio_io_handle_t * output,audio_session_t session,audio_stream_type_t * stream,pid_t pid,uid_t uid,const audio_config_t * config,audio_output_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId,std::vector<audio_io_handle_t> * secondaryOutputs)169 status_t AudioPolicyService::getOutputForAttr(audio_attributes_t *attr,
170                                               audio_io_handle_t *output,
171                                               audio_session_t session,
172                                               audio_stream_type_t *stream,
173                                               pid_t pid,
174                                               uid_t uid,
175                                               const audio_config_t *config,
176                                               audio_output_flags_t flags,
177                                               audio_port_handle_t *selectedDeviceId,
178                                               audio_port_handle_t *portId,
179                                               std::vector<audio_io_handle_t> *secondaryOutputs)
180 {
181     if (mAudioPolicyManager == NULL) {
182         return NO_INIT;
183     }
184     ALOGV("getOutputForAttr()");
185     Mutex::Autolock _l(mLock);
186 
187     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
188     if (!isAudioServerOrMediaServerUid(callingUid) || uid == (uid_t)-1) {
189         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
190                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
191         uid = callingUid;
192     }
193     if (!mPackageManager.allowPlaybackCapture(uid)) {
194         attr->flags |= AUDIO_FLAG_NO_MEDIA_PROJECTION;
195     }
196     if (((attr->flags & (AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY|AUDIO_FLAG_BYPASS_MUTE)) != 0)
197             && !bypassInterruptionPolicyAllowed(pid, uid)) {
198         attr->flags &= ~(AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY|AUDIO_FLAG_BYPASS_MUTE);
199     }
200     audio_output_flags_t originalFlags = flags;
201     AutoCallerClear acc;
202     status_t result = mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
203                                                  config,
204                                                  &flags, selectedDeviceId, portId,
205                                                  secondaryOutputs);
206 
207     // FIXME: Introduce a way to check for the the telephony device before opening the output
208     if ((result == NO_ERROR) &&
209         (flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) &&
210         !modifyPhoneStateAllowed(pid, uid)) {
211         // If the app tries to play music through the telephony device and doesn't have permission
212         // the fallback to the default output device.
213         mAudioPolicyManager->releaseOutput(*portId);
214         flags = originalFlags;
215         *selectedDeviceId = AUDIO_PORT_HANDLE_NONE;
216         *portId = AUDIO_PORT_HANDLE_NONE;
217         secondaryOutputs->clear();
218         result = mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, config,
219                                                        &flags, selectedDeviceId, portId,
220                                                        secondaryOutputs);
221     }
222 
223     if (result == NO_ERROR) {
224         sp <AudioPlaybackClient> client =
225             new AudioPlaybackClient(*attr, *output, uid, pid, session, *selectedDeviceId, *stream);
226         mAudioPlaybackClients.add(*portId, client);
227     }
228     return result;
229 }
230 
getPlaybackClientAndEffects(audio_port_handle_t portId,sp<AudioPlaybackClient> & client,sp<AudioPolicyEffects> & effects,const char * context)231 void AudioPolicyService::getPlaybackClientAndEffects(audio_port_handle_t portId,
232                                                      sp<AudioPlaybackClient>& client,
233                                                      sp<AudioPolicyEffects>& effects,
234                                                      const char *context)
235 {
236     Mutex::Autolock _l(mLock);
237     const ssize_t index = mAudioPlaybackClients.indexOfKey(portId);
238     if (index < 0) {
239         ALOGE("%s AudioTrack client not found for portId %d", context, portId);
240         return;
241     }
242     client = mAudioPlaybackClients.valueAt(index);
243     effects = mAudioPolicyEffects;
244 }
245 
startOutput(audio_port_handle_t portId)246 status_t AudioPolicyService::startOutput(audio_port_handle_t portId)
247 {
248     if (mAudioPolicyManager == NULL) {
249         return NO_INIT;
250     }
251     ALOGV("startOutput()");
252     sp<AudioPlaybackClient> client;
253     sp<AudioPolicyEffects>audioPolicyEffects;
254 
255     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__);
256 
257     if (audioPolicyEffects != 0) {
258         // create audio processors according to stream
259         status_t status = audioPolicyEffects->addOutputSessionEffects(
260             client->io, client->stream, client->session);
261         if (status != NO_ERROR && status != ALREADY_EXISTS) {
262             ALOGW("Failed to add effects on session %d", client->session);
263         }
264     }
265     Mutex::Autolock _l(mLock);
266     AutoCallerClear acc;
267     status_t status = mAudioPolicyManager->startOutput(portId);
268     if (status == NO_ERROR) {
269         client->active = true;
270     }
271     return status;
272 }
273 
stopOutput(audio_port_handle_t portId)274 status_t AudioPolicyService::stopOutput(audio_port_handle_t portId)
275 {
276     if (mAudioPolicyManager == NULL) {
277         return NO_INIT;
278     }
279     ALOGV("stopOutput()");
280     mOutputCommandThread->stopOutputCommand(portId);
281     return NO_ERROR;
282 }
283 
doStopOutput(audio_port_handle_t portId)284 status_t  AudioPolicyService::doStopOutput(audio_port_handle_t portId)
285 {
286     ALOGV("doStopOutput");
287     sp<AudioPlaybackClient> client;
288     sp<AudioPolicyEffects>audioPolicyEffects;
289 
290     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__);
291 
292     if (audioPolicyEffects != 0) {
293         // release audio processors from the stream
294         status_t status = audioPolicyEffects->releaseOutputSessionEffects(
295             client->io, client->stream, client->session);
296         if (status != NO_ERROR && status != ALREADY_EXISTS) {
297             ALOGW("Failed to release effects on session %d", client->session);
298         }
299     }
300     Mutex::Autolock _l(mLock);
301     AutoCallerClear acc;
302     status_t status = mAudioPolicyManager->stopOutput(portId);
303     if (status == NO_ERROR) {
304         client->active = false;
305     }
306     return status;
307 }
308 
releaseOutput(audio_port_handle_t portId)309 void AudioPolicyService::releaseOutput(audio_port_handle_t portId)
310 {
311     if (mAudioPolicyManager == NULL) {
312         return;
313     }
314     ALOGV("releaseOutput()");
315     mOutputCommandThread->releaseOutputCommand(portId);
316 }
317 
doReleaseOutput(audio_port_handle_t portId)318 void AudioPolicyService::doReleaseOutput(audio_port_handle_t portId)
319 {
320     ALOGV("doReleaseOutput from tid %d", gettid());
321     sp<AudioPlaybackClient> client;
322     sp<AudioPolicyEffects> audioPolicyEffects;
323 
324     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__);
325 
326     if (audioPolicyEffects != 0 && client->active) {
327         // clean up effects if output was not stopped before being released
328         audioPolicyEffects->releaseOutputSessionEffects(
329             client->io, client->stream, client->session);
330     }
331     Mutex::Autolock _l(mLock);
332     mAudioPlaybackClients.removeItem(portId);
333 
334     // called from internal thread: no need to clear caller identity
335     mAudioPolicyManager->releaseOutput(portId);
336 }
337 
getInputForAttr(const audio_attributes_t * attr,audio_io_handle_t * input,audio_unique_id_t riid,audio_session_t session,pid_t pid,uid_t uid,const String16 & opPackageName,const audio_config_base_t * config,audio_input_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId)338 status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
339                                              audio_io_handle_t *input,
340                                              audio_unique_id_t riid,
341                                              audio_session_t session,
342                                              pid_t pid,
343                                              uid_t uid,
344                                              const String16& opPackageName,
345                                              const audio_config_base_t *config,
346                                              audio_input_flags_t flags,
347                                              audio_port_handle_t *selectedDeviceId,
348                                              audio_port_handle_t *portId)
349 {
350     if (mAudioPolicyManager == NULL) {
351         return NO_INIT;
352     }
353 
354     // already checked by client, but double-check in case the client wrapper is bypassed
355     if ((attr->source < AUDIO_SOURCE_DEFAULT)
356             || (attr->source >= AUDIO_SOURCE_CNT
357                 && attr->source != AUDIO_SOURCE_HOTWORD
358                 && attr->source != AUDIO_SOURCE_FM_TUNER
359                 && attr->source != AUDIO_SOURCE_ECHO_REFERENCE)) {
360         return BAD_VALUE;
361     }
362 
363     bool updatePid = (pid == -1);
364     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
365     if (!isAudioServerOrMediaServerUid(callingUid)) {
366         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
367                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
368         uid = callingUid;
369         updatePid = true;
370     }
371 
372     if (updatePid) {
373         const pid_t callingPid = IPCThreadState::self()->getCallingPid();
374         ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
375                  "%s uid %d pid %d tried to pass itself off as pid %d",
376                  __func__, callingUid, callingPid, pid);
377         pid = callingPid;
378     }
379 
380     // check calling permissions
381     if (!recordingAllowed(opPackageName, pid, uid)) {
382         ALOGE("%s permission denied: recording not allowed for uid %d pid %d",
383                 __func__, uid, pid);
384         return PERMISSION_DENIED;
385     }
386 
387     bool canCaptureOutput = captureAudioOutputAllowed(pid, uid);
388     if ((attr->source == AUDIO_SOURCE_VOICE_UPLINK ||
389         attr->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
390         attr->source == AUDIO_SOURCE_VOICE_CALL ||
391         attr->source == AUDIO_SOURCE_ECHO_REFERENCE) &&
392         !canCaptureOutput) {
393         return PERMISSION_DENIED;
394     }
395 
396     bool canCaptureHotword = captureHotwordAllowed(opPackageName, pid, uid);
397     if ((attr->source == AUDIO_SOURCE_HOTWORD) && !canCaptureHotword) {
398         return BAD_VALUE;
399     }
400 
401     sp<AudioPolicyEffects>audioPolicyEffects;
402     {
403         status_t status;
404         AudioPolicyInterface::input_type_t inputType;
405 
406         Mutex::Autolock _l(mLock);
407         {
408             AutoCallerClear acc;
409             // the audio_in_acoustics_t parameter is ignored by get_input()
410             status = mAudioPolicyManager->getInputForAttr(attr, input, riid, session, uid,
411                                                          config,
412                                                          flags, selectedDeviceId,
413                                                          &inputType, portId);
414         }
415         audioPolicyEffects = mAudioPolicyEffects;
416 
417         if (status == NO_ERROR) {
418             // enforce permission (if any) required for each type of input
419             switch (inputType) {
420             case AudioPolicyInterface::API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK:
421                 // this use case has been validated in audio service with a MediaProjection token,
422                 // and doesn't rely on regular permissions
423             case AudioPolicyInterface::API_INPUT_LEGACY:
424                 break;
425             case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
426                 // FIXME: use the same permission as for remote submix for now.
427             case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
428                 if (!canCaptureOutput) {
429                     ALOGE("getInputForAttr() permission denied: capture not allowed");
430                     status = PERMISSION_DENIED;
431                 }
432                 break;
433             case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
434                 if (!modifyAudioRoutingAllowed()) {
435                     ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
436                     status = PERMISSION_DENIED;
437                 }
438                 break;
439             case AudioPolicyInterface::API_INPUT_INVALID:
440             default:
441                 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
442                         (int)inputType);
443             }
444         }
445 
446         if (status != NO_ERROR) {
447             if (status == PERMISSION_DENIED) {
448                 AutoCallerClear acc;
449                 mAudioPolicyManager->releaseInput(*portId);
450             }
451             return status;
452         }
453 
454         sp<AudioRecordClient> client = new AudioRecordClient(*attr, *input, uid, pid, session,
455                                                              *selectedDeviceId, opPackageName,
456                                                              canCaptureOutput, canCaptureHotword);
457         mAudioRecordClients.add(*portId, client);
458     }
459 
460     if (audioPolicyEffects != 0) {
461         // create audio pre processors according to input source
462         status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
463         if (status != NO_ERROR && status != ALREADY_EXISTS) {
464             ALOGW("Failed to add effects on input %d", *input);
465         }
466     }
467     return NO_ERROR;
468 }
469 
getDeviceTypeStrForPortId(audio_port_handle_t portId)470 std::string AudioPolicyService::getDeviceTypeStrForPortId(audio_port_handle_t portId) {
471     struct audio_port port = {};
472     port.id = portId;
473     status_t status = mAudioPolicyManager->getAudioPort(&port);
474     if (status == NO_ERROR && port.type == AUDIO_PORT_TYPE_DEVICE) {
475         return toString(port.ext.device.type);
476     }
477     return {};
478 }
479 
startInput(audio_port_handle_t portId)480 status_t AudioPolicyService::startInput(audio_port_handle_t portId)
481 {
482     if (mAudioPolicyManager == NULL) {
483         return NO_INIT;
484     }
485     sp<AudioRecordClient> client;
486     {
487         Mutex::Autolock _l(mLock);
488 
489         ssize_t index = mAudioRecordClients.indexOfKey(portId);
490         if (index < 0) {
491             return INVALID_OPERATION;
492         }
493         client = mAudioRecordClients.valueAt(index);
494     }
495 
496     // check calling permissions
497     if (!startRecording(client->opPackageName, client->pid, client->uid)) {
498         ALOGE("%s permission denied: recording not allowed for uid %d pid %d",
499                 __func__, client->uid, client->pid);
500         return PERMISSION_DENIED;
501     }
502 
503     Mutex::Autolock _l(mLock);
504 
505     client->active = true;
506     client->startTimeNs = systemTime();
507     updateUidStates_l();
508 
509     status_t status;
510     {
511         AutoCallerClear acc;
512         status = mAudioPolicyManager->startInput(portId);
513 
514     }
515 
516     // including successes gets very verbose
517     // but once we cut over to westworld, log them all.
518     if (status != NO_ERROR) {
519 
520         static constexpr char kAudioPolicy[] = "audiopolicy";
521 
522         static constexpr char kAudioPolicyStatus[] = "android.media.audiopolicy.status";
523         static constexpr char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src";
524         static constexpr char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg";
525         static constexpr char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session";
526         static constexpr char kAudioPolicyRqstDevice[] =
527                 "android.media.audiopolicy.rqst.device";
528         static constexpr char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src";
529         static constexpr char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg";
530         static constexpr char kAudioPolicyActiveSession[] =
531                 "android.media.audiopolicy.active.session";
532         static constexpr char kAudioPolicyActiveDevice[] =
533                 "android.media.audiopolicy.active.device";
534 
535         MediaAnalyticsItem *item = MediaAnalyticsItem::create(kAudioPolicy);
536         if (item != NULL) {
537 
538             item->setInt32(kAudioPolicyStatus, status);
539 
540             item->setCString(kAudioPolicyRqstSrc,
541                              toString(client->attributes.source).c_str());
542             item->setInt32(kAudioPolicyRqstSession, client->session);
543             if (client->opPackageName.size() != 0) {
544                 item->setCString(kAudioPolicyRqstPkg,
545                                  std::string(String8(client->opPackageName).string()).c_str());
546             } else {
547                 item->setCString(kAudioPolicyRqstPkg, std::to_string(client->uid).c_str());
548             }
549             item->setCString(
550                     kAudioPolicyRqstDevice, getDeviceTypeStrForPortId(client->deviceId).c_str());
551 
552             int count = mAudioRecordClients.size();
553             for (int i = 0; i < count ; i++) {
554                 if (portId == mAudioRecordClients.keyAt(i)) {
555                     continue;
556                 }
557                 sp<AudioRecordClient> other = mAudioRecordClients.valueAt(i);
558                 if (other->active) {
559                     // keeps the last of the clients marked active
560                     item->setCString(kAudioPolicyActiveSrc,
561                                      toString(other->attributes.source).c_str());
562                     item->setInt32(kAudioPolicyActiveSession, other->session);
563                     if (other->opPackageName.size() != 0) {
564                         item->setCString(kAudioPolicyActivePkg,
565                              std::string(String8(other->opPackageName).string()).c_str());
566                     } else {
567                         item->setCString(kAudioPolicyRqstPkg,
568                                          std::to_string(other->uid).c_str());
569                     }
570                     item->setCString(kAudioPolicyActiveDevice,
571                                      getDeviceTypeStrForPortId(other->deviceId).c_str());
572                 }
573             }
574             item->selfrecord();
575             delete item;
576             item = NULL;
577         }
578     }
579 
580     if (status != NO_ERROR) {
581         client->active = false;
582         client->startTimeNs = 0;
583         updateUidStates_l();
584         finishRecording(client->opPackageName, client->uid);
585     }
586 
587     return status;
588 }
589 
stopInput(audio_port_handle_t portId)590 status_t AudioPolicyService::stopInput(audio_port_handle_t portId)
591 {
592     if (mAudioPolicyManager == NULL) {
593         return NO_INIT;
594     }
595 
596     Mutex::Autolock _l(mLock);
597 
598     ssize_t index = mAudioRecordClients.indexOfKey(portId);
599     if (index < 0) {
600         return INVALID_OPERATION;
601     }
602     sp<AudioRecordClient> client = mAudioRecordClients.valueAt(index);
603 
604     client->active = false;
605     client->startTimeNs = 0;
606 
607     updateUidStates_l();
608 
609     // finish the recording app op
610     finishRecording(client->opPackageName, client->uid);
611     AutoCallerClear acc;
612     return mAudioPolicyManager->stopInput(portId);
613 }
614 
releaseInput(audio_port_handle_t portId)615 void AudioPolicyService::releaseInput(audio_port_handle_t portId)
616 {
617     if (mAudioPolicyManager == NULL) {
618         return;
619     }
620     sp<AudioPolicyEffects>audioPolicyEffects;
621     sp<AudioRecordClient> client;
622     {
623         Mutex::Autolock _l(mLock);
624         audioPolicyEffects = mAudioPolicyEffects;
625         ssize_t index = mAudioRecordClients.indexOfKey(portId);
626         if (index < 0) {
627             return;
628         }
629         client = mAudioRecordClients.valueAt(index);
630 
631         if (client->active) {
632             ALOGW("%s releasing active client portId %d", __FUNCTION__, portId);
633             client->active = false;
634             client->startTimeNs = 0;
635             updateUidStates_l();
636         }
637 
638         mAudioRecordClients.removeItem(portId);
639     }
640     if (client == 0) {
641         return;
642     }
643     if (audioPolicyEffects != 0) {
644         // release audio processors from the input
645         status_t status = audioPolicyEffects->releaseInputEffects(client->io, client->session);
646         if(status != NO_ERROR) {
647             ALOGW("Failed to release effects on input %d", client->io);
648         }
649     }
650     {
651         Mutex::Autolock _l(mLock);
652         AutoCallerClear acc;
653         mAudioPolicyManager->releaseInput(portId);
654     }
655 }
656 
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)657 status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
658                                             int indexMin,
659                                             int indexMax)
660 {
661     if (mAudioPolicyManager == NULL) {
662         return NO_INIT;
663     }
664     if (!settingsAllowed()) {
665         return PERMISSION_DENIED;
666     }
667     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
668         return BAD_VALUE;
669     }
670     Mutex::Autolock _l(mLock);
671     AutoCallerClear acc;
672     mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
673     return NO_ERROR;
674 }
675 
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)676 status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
677                                                   int index,
678                                                   audio_devices_t device)
679 {
680     if (mAudioPolicyManager == NULL) {
681         return NO_INIT;
682     }
683     if (!settingsAllowed()) {
684         return PERMISSION_DENIED;
685     }
686     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
687         return BAD_VALUE;
688     }
689     Mutex::Autolock _l(mLock);
690     AutoCallerClear acc;
691     return mAudioPolicyManager->setStreamVolumeIndex(stream,
692                                                     index,
693                                                     device);
694 }
695 
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)696 status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
697                                                   int *index,
698                                                   audio_devices_t device)
699 {
700     if (mAudioPolicyManager == NULL) {
701         return NO_INIT;
702     }
703     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
704         return BAD_VALUE;
705     }
706     Mutex::Autolock _l(mLock);
707     AutoCallerClear acc;
708     return mAudioPolicyManager->getStreamVolumeIndex(stream,
709                                                     index,
710                                                     device);
711 }
712 
setVolumeIndexForAttributes(const audio_attributes_t & attributes,int index,audio_devices_t device)713 status_t AudioPolicyService::setVolumeIndexForAttributes(const audio_attributes_t &attributes,
714                                                          int index, audio_devices_t device)
715 {
716     if (mAudioPolicyManager == NULL) {
717         return NO_INIT;
718     }
719     if (!settingsAllowed()) {
720         return PERMISSION_DENIED;
721     }
722     Mutex::Autolock _l(mLock);
723     AutoCallerClear acc;
724     return mAudioPolicyManager->setVolumeIndexForAttributes(attributes, index, device);
725 }
726 
getVolumeIndexForAttributes(const audio_attributes_t & attributes,int & index,audio_devices_t device)727 status_t AudioPolicyService::getVolumeIndexForAttributes(const audio_attributes_t &attributes,
728                                                          int &index, audio_devices_t device)
729 {
730     if (mAudioPolicyManager == NULL) {
731         return NO_INIT;
732     }
733     Mutex::Autolock _l(mLock);
734     AutoCallerClear acc;
735     return mAudioPolicyManager->getVolumeIndexForAttributes(attributes, index, device);
736 }
737 
getMinVolumeIndexForAttributes(const audio_attributes_t & attributes,int & index)738 status_t AudioPolicyService::getMinVolumeIndexForAttributes(const audio_attributes_t &attributes,
739                                                             int &index)
740 {
741     if (mAudioPolicyManager == NULL) {
742         return NO_INIT;
743     }
744     Mutex::Autolock _l(mLock);
745     AutoCallerClear acc;
746     return mAudioPolicyManager->getMinVolumeIndexForAttributes(attributes, index);
747 }
748 
getMaxVolumeIndexForAttributes(const audio_attributes_t & attributes,int & index)749 status_t AudioPolicyService::getMaxVolumeIndexForAttributes(const audio_attributes_t &attributes,
750                                                             int &index)
751 {
752     if (mAudioPolicyManager == NULL) {
753         return NO_INIT;
754     }
755     Mutex::Autolock _l(mLock);
756     AutoCallerClear acc;
757     return mAudioPolicyManager->getMaxVolumeIndexForAttributes(attributes, index);
758 }
759 
getStrategyForStream(audio_stream_type_t stream)760 uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
761 {
762     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
763         return PRODUCT_STRATEGY_NONE;
764     }
765     if (mAudioPolicyManager == NULL) {
766         return PRODUCT_STRATEGY_NONE;
767     }
768     // DO NOT LOCK, may be called from AudioFlinger with lock held, reaching deadlock
769     AutoCallerClear acc;
770     return mAudioPolicyManager->getStrategyForStream(stream);
771 }
772 
773 //audio policy: use audio_device_t appropriately
774 
getDevicesForStream(audio_stream_type_t stream)775 audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
776 {
777     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
778         return AUDIO_DEVICE_NONE;
779     }
780     if (mAudioPolicyManager == NULL) {
781         return AUDIO_DEVICE_NONE;
782     }
783     Mutex::Autolock _l(mLock);
784     AutoCallerClear acc;
785     return mAudioPolicyManager->getDevicesForStream(stream);
786 }
787 
getOutputForEffect(const effect_descriptor_t * desc)788 audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
789 {
790     // FIXME change return type to status_t, and return NO_INIT here
791     if (mAudioPolicyManager == NULL) {
792         return 0;
793     }
794     Mutex::Autolock _l(mLock);
795     AutoCallerClear acc;
796     return mAudioPolicyManager->getOutputForEffect(desc);
797 }
798 
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)799 status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
800                                 audio_io_handle_t io,
801                                 uint32_t strategy,
802                                 audio_session_t session,
803                                 int id)
804 {
805     if (mAudioPolicyManager == NULL) {
806         return NO_INIT;
807     }
808     Mutex::Autolock _l(mLock);
809     AutoCallerClear acc;
810     return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
811 }
812 
unregisterEffect(int id)813 status_t AudioPolicyService::unregisterEffect(int id)
814 {
815     if (mAudioPolicyManager == NULL) {
816         return NO_INIT;
817     }
818     Mutex::Autolock _l(mLock);
819     AutoCallerClear acc;
820     return mAudioPolicyManager->unregisterEffect(id);
821 }
822 
setEffectEnabled(int id,bool enabled)823 status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
824 {
825     if (mAudioPolicyManager == NULL) {
826         return NO_INIT;
827     }
828     Mutex::Autolock _l(mLock);
829     AutoCallerClear acc;
830     return mAudioPolicyManager->setEffectEnabled(id, enabled);
831 }
832 
moveEffectsToIo(const std::vector<int> & ids,audio_io_handle_t io)833 status_t AudioPolicyService::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
834 {
835     if (mAudioPolicyManager == NULL) {
836         return NO_INIT;
837     }
838     Mutex::Autolock _l(mLock);
839     AutoCallerClear acc;
840     return mAudioPolicyManager->moveEffectsToIo(ids, io);
841 }
842 
isStreamActive(audio_stream_type_t stream,uint32_t inPastMs) const843 bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
844 {
845     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
846         return false;
847     }
848     if (mAudioPolicyManager == NULL) {
849         return false;
850     }
851     Mutex::Autolock _l(mLock);
852     AutoCallerClear acc;
853     return mAudioPolicyManager->isStreamActive(stream, inPastMs);
854 }
855 
isStreamActiveRemotely(audio_stream_type_t stream,uint32_t inPastMs) const856 bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
857 {
858     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
859         return false;
860     }
861     if (mAudioPolicyManager == NULL) {
862         return false;
863     }
864     Mutex::Autolock _l(mLock);
865     AutoCallerClear acc;
866     return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
867 }
868 
isSourceActive(audio_source_t source) const869 bool AudioPolicyService::isSourceActive(audio_source_t source) const
870 {
871     if (mAudioPolicyManager == NULL) {
872         return false;
873     }
874     Mutex::Autolock _l(mLock);
875     AutoCallerClear acc;
876     return mAudioPolicyManager->isSourceActive(source);
877 }
878 
getAudioPolicyEffects(sp<AudioPolicyEffects> & audioPolicyEffects)879 status_t AudioPolicyService::getAudioPolicyEffects(sp<AudioPolicyEffects>& audioPolicyEffects)
880 {
881     if (mAudioPolicyManager == NULL) {
882         return NO_INIT;
883     }
884     {
885         Mutex::Autolock _l(mLock);
886         audioPolicyEffects = mAudioPolicyEffects;
887     }
888     if (audioPolicyEffects == 0) {
889         return NO_INIT;
890     }
891 
892     return OK;
893 }
894 
queryDefaultPreProcessing(audio_session_t audioSession,effect_descriptor_t * descriptors,uint32_t * count)895 status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
896                                                        effect_descriptor_t *descriptors,
897                                                        uint32_t *count)
898 {
899     sp<AudioPolicyEffects>audioPolicyEffects;
900     status_t status = getAudioPolicyEffects(audioPolicyEffects);
901     if (status != OK) {
902         *count = 0;
903         return status;
904     }
905     return audioPolicyEffects->queryDefaultInputEffects(
906             (audio_session_t)audioSession, descriptors, count);
907 }
908 
addSourceDefaultEffect(const effect_uuid_t * type,const String16 & opPackageName,const effect_uuid_t * uuid,int32_t priority,audio_source_t source,audio_unique_id_t * id)909 status_t AudioPolicyService::addSourceDefaultEffect(const effect_uuid_t *type,
910                                                     const String16& opPackageName,
911                                                     const effect_uuid_t *uuid,
912                                                     int32_t priority,
913                                                     audio_source_t source,
914                                                     audio_unique_id_t* id)
915 {
916     sp<AudioPolicyEffects>audioPolicyEffects;
917     status_t status = getAudioPolicyEffects(audioPolicyEffects);
918     if (status != OK) {
919         return status;
920     }
921     if (!modifyDefaultAudioEffectsAllowed()) {
922         return PERMISSION_DENIED;
923     }
924     return audioPolicyEffects->addSourceDefaultEffect(
925             type, opPackageName, uuid, priority, source, id);
926 }
927 
addStreamDefaultEffect(const effect_uuid_t * type,const String16 & opPackageName,const effect_uuid_t * uuid,int32_t priority,audio_usage_t usage,audio_unique_id_t * id)928 status_t AudioPolicyService::addStreamDefaultEffect(const effect_uuid_t *type,
929                                                     const String16& opPackageName,
930                                                     const effect_uuid_t *uuid,
931                                                     int32_t priority,
932                                                     audio_usage_t usage,
933                                                     audio_unique_id_t* id)
934 {
935     sp<AudioPolicyEffects>audioPolicyEffects;
936     status_t status = getAudioPolicyEffects(audioPolicyEffects);
937     if (status != OK) {
938         return status;
939     }
940     if (!modifyDefaultAudioEffectsAllowed()) {
941         return PERMISSION_DENIED;
942     }
943     return audioPolicyEffects->addStreamDefaultEffect(
944             type, opPackageName, uuid, priority, usage, id);
945 }
946 
removeSourceDefaultEffect(audio_unique_id_t id)947 status_t AudioPolicyService::removeSourceDefaultEffect(audio_unique_id_t id)
948 {
949     sp<AudioPolicyEffects>audioPolicyEffects;
950     status_t status = getAudioPolicyEffects(audioPolicyEffects);
951     if (status != OK) {
952         return status;
953     }
954     if (!modifyDefaultAudioEffectsAllowed()) {
955         return PERMISSION_DENIED;
956     }
957     return audioPolicyEffects->removeSourceDefaultEffect(id);
958 }
959 
removeStreamDefaultEffect(audio_unique_id_t id)960 status_t AudioPolicyService::removeStreamDefaultEffect(audio_unique_id_t id)
961 {
962     sp<AudioPolicyEffects>audioPolicyEffects;
963     status_t status = getAudioPolicyEffects(audioPolicyEffects);
964     if (status != OK) {
965         return status;
966     }
967     if (!modifyDefaultAudioEffectsAllowed()) {
968         return PERMISSION_DENIED;
969     }
970     return audioPolicyEffects->removeStreamDefaultEffect(id);
971 }
972 
setAllowedCapturePolicy(uid_t uid,audio_flags_mask_t capturePolicy)973 status_t AudioPolicyService::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) {
974     Mutex::Autolock _l(mLock);
975     if (mAudioPolicyManager == NULL) {
976         ALOGV("%s() mAudioPolicyManager == NULL", __func__);
977         return NO_INIT;
978     }
979     uint_t callingUid = IPCThreadState::self()->getCallingUid();
980     if (uid != callingUid) {
981         ALOGD("%s() uid invalid %d != %d", __func__, uid, callingUid);
982         return PERMISSION_DENIED;
983     }
984     return mAudioPolicyManager->setAllowedCapturePolicy(uid, capturePolicy);
985 }
986 
isOffloadSupported(const audio_offload_info_t & info)987 bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
988 {
989     if (mAudioPolicyManager == NULL) {
990         ALOGV("mAudioPolicyManager == NULL");
991         return false;
992     }
993     Mutex::Autolock _l(mLock);
994     AutoCallerClear acc;
995     return mAudioPolicyManager->isOffloadSupported(info);
996 }
997 
isDirectOutputSupported(const audio_config_base_t & config,const audio_attributes_t & attributes)998 bool AudioPolicyService::isDirectOutputSupported(const audio_config_base_t& config,
999                                                  const audio_attributes_t& attributes) {
1000     if (mAudioPolicyManager == NULL) {
1001         ALOGV("mAudioPolicyManager == NULL");
1002         return false;
1003     }
1004     Mutex::Autolock _l(mLock);
1005     return mAudioPolicyManager->isDirectOutputSupported(config, attributes);
1006 }
1007 
1008 
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)1009 status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
1010                                             audio_port_type_t type,
1011                                             unsigned int *num_ports,
1012                                             struct audio_port *ports,
1013                                             unsigned int *generation)
1014 {
1015     Mutex::Autolock _l(mLock);
1016     if (mAudioPolicyManager == NULL) {
1017         return NO_INIT;
1018     }
1019     AutoCallerClear acc;
1020     return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
1021 }
1022 
getAudioPort(struct audio_port * port)1023 status_t AudioPolicyService::getAudioPort(struct audio_port *port)
1024 {
1025     Mutex::Autolock _l(mLock);
1026     if (mAudioPolicyManager == NULL) {
1027         return NO_INIT;
1028     }
1029     AutoCallerClear acc;
1030     return mAudioPolicyManager->getAudioPort(port);
1031 }
1032 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)1033 status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
1034         audio_patch_handle_t *handle)
1035 {
1036     Mutex::Autolock _l(mLock);
1037     if(!modifyAudioRoutingAllowed()) {
1038         return PERMISSION_DENIED;
1039     }
1040     if (mAudioPolicyManager == NULL) {
1041         return NO_INIT;
1042     }
1043     AutoCallerClear acc;
1044     return mAudioPolicyManager->createAudioPatch(patch, handle,
1045                                                   IPCThreadState::self()->getCallingUid());
1046 }
1047 
releaseAudioPatch(audio_patch_handle_t handle)1048 status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
1049 {
1050     Mutex::Autolock _l(mLock);
1051     if(!modifyAudioRoutingAllowed()) {
1052         return PERMISSION_DENIED;
1053     }
1054     if (mAudioPolicyManager == NULL) {
1055         return NO_INIT;
1056     }
1057     AutoCallerClear acc;
1058     return mAudioPolicyManager->releaseAudioPatch(handle,
1059                                                      IPCThreadState::self()->getCallingUid());
1060 }
1061 
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)1062 status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
1063         struct audio_patch *patches,
1064         unsigned int *generation)
1065 {
1066     Mutex::Autolock _l(mLock);
1067     if (mAudioPolicyManager == NULL) {
1068         return NO_INIT;
1069     }
1070     AutoCallerClear acc;
1071     return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
1072 }
1073 
setAudioPortConfig(const struct audio_port_config * config)1074 status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
1075 {
1076     Mutex::Autolock _l(mLock);
1077     if(!modifyAudioRoutingAllowed()) {
1078         return PERMISSION_DENIED;
1079     }
1080     if (mAudioPolicyManager == NULL) {
1081         return NO_INIT;
1082     }
1083     AutoCallerClear acc;
1084     return mAudioPolicyManager->setAudioPortConfig(config);
1085 }
1086 
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)1087 status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
1088                                        audio_io_handle_t *ioHandle,
1089                                        audio_devices_t *device)
1090 {
1091     Mutex::Autolock _l(mLock);
1092     if (mAudioPolicyManager == NULL) {
1093         return NO_INIT;
1094     }
1095     AutoCallerClear acc;
1096     return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
1097 }
1098 
releaseSoundTriggerSession(audio_session_t session)1099 status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
1100 {
1101     Mutex::Autolock _l(mLock);
1102     if (mAudioPolicyManager == NULL) {
1103         return NO_INIT;
1104     }
1105     AutoCallerClear acc;
1106     return mAudioPolicyManager->releaseSoundTriggerSession(session);
1107 }
1108 
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)1109 status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
1110 {
1111     Mutex::Autolock _l(mLock);
1112 
1113     // loopback|render only need a MediaProjection (checked in caller AudioService.java)
1114     bool needModifyAudioRouting = std::any_of(mixes.begin(), mixes.end(), [](auto& mix) {
1115             return !is_mix_loopback_render(mix.mRouteFlags); });
1116     if (needModifyAudioRouting && !modifyAudioRoutingAllowed()) {
1117         return PERMISSION_DENIED;
1118     }
1119 
1120     bool needCaptureMediaOutput = std::any_of(mixes.begin(), mixes.end(), [](auto& mix) {
1121             return mix.mAllowPrivilegedPlaybackCapture; });
1122     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1123     const pid_t callingPid = IPCThreadState::self()->getCallingPid();
1124     if (needCaptureMediaOutput && !captureMediaOutputAllowed(callingPid, callingUid)) {
1125         return PERMISSION_DENIED;
1126     }
1127 
1128     if (mAudioPolicyManager == NULL) {
1129         return NO_INIT;
1130     }
1131     AutoCallerClear acc;
1132     if (registration) {
1133         return mAudioPolicyManager->registerPolicyMixes(mixes);
1134     } else {
1135         return mAudioPolicyManager->unregisterPolicyMixes(mixes);
1136     }
1137 }
1138 
setUidDeviceAffinities(uid_t uid,const Vector<AudioDeviceTypeAddr> & devices)1139 status_t AudioPolicyService::setUidDeviceAffinities(uid_t uid,
1140         const Vector<AudioDeviceTypeAddr>& devices) {
1141     Mutex::Autolock _l(mLock);
1142     if(!modifyAudioRoutingAllowed()) {
1143         return PERMISSION_DENIED;
1144     }
1145     if (mAudioPolicyManager == NULL) {
1146         return NO_INIT;
1147     }
1148     AutoCallerClear acc;
1149     return mAudioPolicyManager->setUidDeviceAffinities(uid, devices);
1150 }
1151 
removeUidDeviceAffinities(uid_t uid)1152 status_t AudioPolicyService::removeUidDeviceAffinities(uid_t uid) {
1153     Mutex::Autolock _l(mLock);
1154     if(!modifyAudioRoutingAllowed()) {
1155         return PERMISSION_DENIED;
1156     }
1157     if (mAudioPolicyManager == NULL) {
1158         return NO_INIT;
1159     }
1160     AutoCallerClear acc;
1161     return mAudioPolicyManager->removeUidDeviceAffinities(uid);
1162 }
1163 
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_port_handle_t * portId)1164 status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
1165                                               const audio_attributes_t *attributes,
1166                                               audio_port_handle_t *portId)
1167 {
1168     Mutex::Autolock _l(mLock);
1169     if (mAudioPolicyManager == NULL) {
1170         return NO_INIT;
1171     }
1172     // startAudioSource should be created as the calling uid
1173     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1174     AutoCallerClear acc;
1175     return mAudioPolicyManager->startAudioSource(source, attributes, portId, callingUid);
1176 }
1177 
stopAudioSource(audio_port_handle_t portId)1178 status_t AudioPolicyService::stopAudioSource(audio_port_handle_t portId)
1179 {
1180     Mutex::Autolock _l(mLock);
1181     if (mAudioPolicyManager == NULL) {
1182         return NO_INIT;
1183     }
1184     AutoCallerClear acc;
1185     return mAudioPolicyManager->stopAudioSource(portId);
1186 }
1187 
setMasterMono(bool mono)1188 status_t AudioPolicyService::setMasterMono(bool mono)
1189 {
1190     if (mAudioPolicyManager == NULL) {
1191         return NO_INIT;
1192     }
1193     if (!settingsAllowed()) {
1194         return PERMISSION_DENIED;
1195     }
1196     Mutex::Autolock _l(mLock);
1197     AutoCallerClear acc;
1198     return mAudioPolicyManager->setMasterMono(mono);
1199 }
1200 
getMasterMono(bool * mono)1201 status_t AudioPolicyService::getMasterMono(bool *mono)
1202 {
1203     if (mAudioPolicyManager == NULL) {
1204         return NO_INIT;
1205     }
1206     Mutex::Autolock _l(mLock);
1207     AutoCallerClear acc;
1208     return mAudioPolicyManager->getMasterMono(mono);
1209 }
1210 
1211 
getStreamVolumeDB(audio_stream_type_t stream,int index,audio_devices_t device)1212 float AudioPolicyService::getStreamVolumeDB(
1213             audio_stream_type_t stream, int index, audio_devices_t device)
1214 {
1215     if (mAudioPolicyManager == NULL) {
1216         return NAN;
1217     }
1218     Mutex::Autolock _l(mLock);
1219     AutoCallerClear acc;
1220     return mAudioPolicyManager->getStreamVolumeDB(stream, index, device);
1221 }
1222 
getSurroundFormats(unsigned int * numSurroundFormats,audio_format_t * surroundFormats,bool * surroundFormatsEnabled,bool reported)1223 status_t AudioPolicyService::getSurroundFormats(unsigned int *numSurroundFormats,
1224                                                 audio_format_t *surroundFormats,
1225                                                 bool *surroundFormatsEnabled,
1226                                                 bool reported)
1227 {
1228     if (mAudioPolicyManager == NULL) {
1229         return NO_INIT;
1230     }
1231     Mutex::Autolock _l(mLock);
1232     AutoCallerClear acc;
1233     return mAudioPolicyManager->getSurroundFormats(numSurroundFormats, surroundFormats,
1234                                                    surroundFormatsEnabled, reported);
1235 }
1236 
getHwOffloadEncodingFormatsSupportedForA2DP(std::vector<audio_format_t> * formats)1237 status_t AudioPolicyService::getHwOffloadEncodingFormatsSupportedForA2DP(
1238                                         std::vector<audio_format_t> *formats)
1239 {
1240     if (mAudioPolicyManager == NULL) {
1241         return NO_INIT;
1242     }
1243     Mutex::Autolock _l(mLock);
1244     AutoCallerClear acc;
1245     return mAudioPolicyManager->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1246 }
1247 
setSurroundFormatEnabled(audio_format_t audioFormat,bool enabled)1248 status_t AudioPolicyService::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1249 {
1250     if (mAudioPolicyManager == NULL) {
1251         return NO_INIT;
1252     }
1253     Mutex::Autolock _l(mLock);
1254     AutoCallerClear acc;
1255     return mAudioPolicyManager->setSurroundFormatEnabled(audioFormat, enabled);
1256 }
1257 
setAssistantUid(uid_t uid)1258 status_t AudioPolicyService::setAssistantUid(uid_t uid)
1259 {
1260     Mutex::Autolock _l(mLock);
1261     mUidPolicy->setAssistantUid(uid);
1262     return NO_ERROR;
1263 }
1264 
setA11yServicesUids(const std::vector<uid_t> & uids)1265 status_t AudioPolicyService::setA11yServicesUids(const std::vector<uid_t>& uids)
1266 {
1267     Mutex::Autolock _l(mLock);
1268     mUidPolicy->setA11yUids(uids);
1269     return NO_ERROR;
1270 }
1271 
isHapticPlaybackSupported()1272 bool AudioPolicyService::isHapticPlaybackSupported()
1273 {
1274     if (mAudioPolicyManager == NULL) {
1275         ALOGW("%s, mAudioPolicyManager == NULL", __func__);
1276         return false;
1277     }
1278     Mutex::Autolock _l(mLock);
1279     AutoCallerClear acc;
1280     return mAudioPolicyManager->isHapticPlaybackSupported();
1281 }
1282 
listAudioProductStrategies(AudioProductStrategyVector & strategies)1283 status_t AudioPolicyService::listAudioProductStrategies(AudioProductStrategyVector &strategies)
1284 {
1285     if (mAudioPolicyManager == NULL) {
1286         return NO_INIT;
1287     }
1288     Mutex::Autolock _l(mLock);
1289     return mAudioPolicyManager->listAudioProductStrategies(strategies);
1290 }
1291 
getProductStrategyFromAudioAttributes(const AudioAttributes & aa,product_strategy_t & productStrategy)1292 status_t AudioPolicyService::getProductStrategyFromAudioAttributes(
1293         const AudioAttributes &aa, product_strategy_t &productStrategy)
1294 {
1295     if (mAudioPolicyManager == NULL) {
1296         return NO_INIT;
1297     }
1298     Mutex::Autolock _l(mLock);
1299     return mAudioPolicyManager->getProductStrategyFromAudioAttributes(aa, productStrategy);
1300 }
1301 
listAudioVolumeGroups(AudioVolumeGroupVector & groups)1302 status_t AudioPolicyService::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1303 {
1304     if (mAudioPolicyManager == NULL) {
1305         return NO_INIT;
1306     }
1307     Mutex::Autolock _l(mLock);
1308     return mAudioPolicyManager->listAudioVolumeGroups(groups);
1309 }
1310 
getVolumeGroupFromAudioAttributes(const AudioAttributes & aa,volume_group_t & volumeGroup)1311 status_t AudioPolicyService::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1312                                                                volume_group_t &volumeGroup)
1313 {
1314     if (mAudioPolicyManager == NULL) {
1315         return NO_INIT;
1316     }
1317     Mutex::Autolock _l(mLock);
1318     return mAudioPolicyManager->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
1319 }
1320 
setRttEnabled(bool enabled)1321 status_t AudioPolicyService::setRttEnabled(bool enabled)
1322 {
1323     Mutex::Autolock _l(mLock);
1324     mUidPolicy->setRttEnabled(enabled);
1325     return NO_ERROR;
1326 }
1327 
1328 } // namespace android
1329