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     const audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
130             ~AUDIO_DEVICE_BIT_IN;
131 
132     if (inputSource == AUDIO_SOURCE_VOICE_RECOGNITION ||
133             inputSource == AUDIO_SOURCE_UNPROCESSED) {
134 #ifdef REMOTE_CONTROL_INTERFACE
135       ALOGI("Using REMOTE_CONTROL_INTERFACE.");
136       // Check if remote is actually connected or we should move on
137       sp<IRemoteControlService> service = IRemoteControlService::getInstance();
138       if (service == NULL) {
139           ALOGV("getDeviceForInputSource No RemoteControl service detected, ignoring");
140           usePhysRemote = false;
141       } else if (!service->hasActiveRemote()) {
142           if (mForceSubmixInputSelection == false && service->hasConnectedRemotes()) {
143               ALOGV("getDeviceForInputSource connected remote and submix not forced");
144               usePhysRemote = true;
145           } else {
146               ALOGV("getDeviceForInputSource No active connected device, passing onto submix");
147               usePhysRemote = false;
148           }
149       }
150 #endif
151       ALOGV("getDeviceForInputSource %s %s", usePhysRemote ? "use physical" : "",
152           mForceSubmixInputSelection ? "use virtual" : "");
153       if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET &&
154             usePhysRemote) {
155           // User a wired headset (physical remote) if available, connected and active
156           ALOGV("Wired Headset available");
157           device = AUDIO_DEVICE_IN_WIRED_HEADSET;
158       } else if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX &&
159             mForceSubmixInputSelection) {
160           // REMOTE_SUBMIX should always be avaible, let's make sure it's being forced at the moment
161           ALOGV("Virtual remote available");
162           device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
163       } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
164           ALOGV("Use USB audio input");
165           device = AUDIO_DEVICE_IN_USB_DEVICE;
166       }
167     } else if ((availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) &&
168             (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX)) {
169         device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
170     }
171 
172     ALOGV("getDeviceForInputSource() input source %d, device %08x", inputSource, device);
173     return device;
174 }
175 
176 }  // namespace android
177