1 /*
2  * Copyright (C) 2014 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 "ATVAudioPolicyManager"
18 //#define LOG_NDEBUG 0
19 #include <media/AudioParameter.h>
20 #include <media/mediarecorder.h>
21 #include <utils/Log.h>
22 #include <utils/String16.h>
23 #include <utils/String8.h>
24 #include <utils/StrongPointer.h>
25 
26 #include "AudioHardwareOutput.h"
27 #include "ATVAudioPolicyManager.h"
28 
29 #ifdef REMOTE_CONTROL_INTERFACE
30 #include <IRemoteControlService.h>
31 #endif
32 
33 
34 namespace android {
35 extern AudioHardwareOutput gAudioHardwareOutput;
36 
37 // ----------------------------------------------------------------------------
38 // Common audio policy manager code is implemented in AudioPolicyManager class
39 // ----------------------------------------------------------------------------
40 
41 // ---  class factory
42 
43 
createAudioPolicyManager(AudioPolicyClientInterface * clientInterface)44 extern "C" AudioPolicyInterface* createAudioPolicyManager(
45         AudioPolicyClientInterface *clientInterface)
46 {
47     return new ATVAudioPolicyManager(clientInterface);
48 }
49 
destroyAudioPolicyManager(AudioPolicyInterface * interface)50 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
51 {
52     delete interface;
53 }
54 
ATVAudioPolicyManager(AudioPolicyClientInterface * clientInterface)55 ATVAudioPolicyManager::ATVAudioPolicyManager(
56         AudioPolicyClientInterface *clientInterface)
57     : AudioPolicyManager(clientInterface), mForceSubmixInputSelection(false)
58 {
59 }
60 
computeVolume(audio_stream_type_t stream,int index,audio_devices_t device)61 float ATVAudioPolicyManager::computeVolume(audio_stream_type_t stream,
62                                            int index,
63                                            audio_devices_t device)
64 {
65     // We only use master volume, so all audio flinger streams
66     // should be set to maximum
67     (void)stream;
68     (void)index;
69     (void)device;
70     return 0.0; // no attenuation == 0.0dB
71 }
72 
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name)73 status_t ATVAudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
74                                                          audio_policy_dev_state_t state,
75                                                          const char *device_address,
76                                                          const char *device_name)
77 {
78     audio_devices_t tmp = AUDIO_DEVICE_NONE;;
79     ALOGE("setDeviceConnectionState %08x %x %s", device, state,
80           device_address ? device_address : "(null)");
81 
82     // If the input device is the remote submix and an address starting with "force=" was
83     // specified, enable "force=1" / disable "force=0" the forced selection of the remote submix
84     // input device over hardware input devices (e.g RemoteControl).
85     if (device == AUDIO_DEVICE_IN_REMOTE_SUBMIX && device_address) {
86         AudioParameter parameters = AudioParameter(String8(device_address));
87         int forceValue;
88         if (parameters.getInt(String8("force"), forceValue) == OK) {
89             mForceSubmixInputSelection = forceValue != 0;
90         }
91     }
92 
93     if (audio_is_output_device(device)) {
94       switch (state) {
95           case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
96               tmp = mAvailableOutputDevices.types() | device;
97               break;
98 
99           case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE:
100               tmp = mAvailableOutputDevices.types() & ~device;
101               break;
102           default:
103               ALOGE("setDeviceConnectionState() invalid state: %x", state);
104               return BAD_VALUE;
105       }
106 
107       gAudioHardwareOutput.updateRouting(tmp);
108       tmp = mAvailableOutputDevices.types();
109     }
110 
111     status_t ret = 0;
112     if (device != AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
113       ret = AudioPolicyManager::setDeviceConnectionState(
114                     device, state, device_address, device_name);
115     }
116 
117     if (audio_is_output_device(device)) {
118       if (tmp != mAvailableOutputDevices.types())
119           gAudioHardwareOutput.updateRouting(mAvailableOutputDevices.types());
120     }
121 
122     return ret;
123 }
124 
getDeviceForInputSource(audio_source_t inputSource)125 audio_devices_t ATVAudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
126 {
127     uint32_t device = AUDIO_DEVICE_NONE;
128     bool usePhysRemote = true;
129 
130     if (inputSource == AUDIO_SOURCE_VOICE_RECOGNITION) {
131 #ifdef REMOTE_CONTROL_INTERFACE
132       ALOGI("Using REMOTE_CONTROL_INTERFACE.");
133       // Check if remote is actually connected or we should move on
134       sp<IRemoteControlService> service = IRemoteControlService::getInstance();
135       if (service == NULL) {
136           ALOGV("getDeviceForInputSource No RemoteControl service detected, ignoring");
137           usePhysRemote = false;
138       } else if (!service->hasActiveRemote()) {
139           if (mForceSubmixInputSelection == false && service->hasConnectedRemotes()) {
140               ALOGV("getDeviceForInputSource connected remote and submix not forced");
141               usePhysRemote = true;
142           } else {
143               ALOGV("getDeviceForInputSource No active connected device, passing onto submix");
144               usePhysRemote = false;
145           }
146       }
147 #endif
148       ALOGV("getDeviceForInputSource %s %s", usePhysRemote ? "use physical" : "",
149           mForceSubmixInputSelection ? "use virtual" : "");
150       audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
151                                                 ~AUDIO_DEVICE_BIT_IN;
152       if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET &&
153             usePhysRemote) {
154           // User a wired headset (physical remote) if available, connected and active
155           ALOGV("Wired Headset available");
156           device = AUDIO_DEVICE_IN_WIRED_HEADSET;
157       } else if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX &&
158             mForceSubmixInputSelection) {
159           // REMOTE_SUBMIX should always be avaible, let's make sure it's being forced at the moment
160           ALOGV("Virtual remote available");
161           device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
162       } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
163           ALOGV("Use USB audio input");
164           device = AUDIO_DEVICE_IN_USB_DEVICE;
165       }
166     }
167 
168     ALOGV("getDeviceForInputSource() input source %d, device %08x", inputSource, device);
169     return device;
170 }
171 
172 }  // namespace android
173