• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #pragma once
17 
18 #include <list>
19 #include <map>
20 #include <memory>
21 
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/threads/cuttlefish_thread.h"
24 #include "common/vsoc/lib/audio_data_region_view.h"
25 #include "common/vsoc/lib/vsoc_audio_message.h"
26 #include "guest/hals/audio/audio_hal.h"
27 #include "guest/hals/audio/vsoc_audio_input_stream.h"
28 #include "guest/libs/platform_support/api_level_fixes.h"
29 
30 namespace cvd {
31 
32 class GceAudioInputStream;
33 class GceAudioOutputStream;
34 
35 class GceAudio : public audio_hw_device {
36  public:
37   // This common code manipulates the parameters of input and output streams.
38   static int SetStreamParameters(struct audio_stream *, const char *);
39 
40   ~GceAudio();
41 
42   // Non-HAL methods that are part of the GCE implementation.
43   // Most of these are used by the input and output streams.
44 
45   // Returns true if the microphone is muted. Used by input streams.
46   bool IsMicrophoneMuted() {
47     cvd::LockGuard<cvd::Mutex> guard(lock_);
48     return mic_muted_;
49   }
50 
51   // Send a message to the connected streamer.
52   // Returns:
53   //   0 if there is no streamer.
54   //   >0 if the message was sent.
55   //   -1 if there was an error.
56   ssize_t SendMsg(const msghdr&, int flags);
57 
58   // Sends a stream update to the connected streamer.
59   // Stream updates have no frames. Use SendMsg if the message has frames.
60   //   0 if there is no streamer.
61   //   >0 if the message was sent.
62   //   -1 if there was an error.
63   ssize_t SendStreamUpdate(
64       const gce_audio_message& stream_info, int flags);
65 
66   // Callbacks for the Android audio_module HAL interface.
67   // Most of the comments below are copied from
68   // libhardware/include/hardware/audio.h
69   //
70   // Where the is a conflict the comments there apply.
71   // By default these methods return 0 on success -<errno> for failure.
72 
73   // Opens the device.
74   static int Open(const hw_module_t* module, const char* name,
75                   hw_device_t** device);
76 
77   // Closes the device, closing any open input streams and output streams.
78   int Close();
79 
80   // Closes the input stream, throwing away any data in the buffer.
81   void CloseInputStream(audio_stream_in* stream);
82 
83   // Closes the output stream without waiting for the buffer to clear.
84   void CloseOutputStream(audio_stream_out* stream);
85 
86   // Creates an audio patch between several source and sink ports.
87   // The handle is allocated by the HAL and should be unique for this
88   // audio HAL module.
89   // TODO(ghartman): Implement this as part of HAL 3.0
90   //int CreateAudioPatch(unsigned int num_sources,
91   //                     const struct audio_port_config *sources,
92   //                     unsigned int num_sinks,
93   //                     const struct audio_port_config *sinks,
94   //                     audio_patch_handle_t *handle);
95 
96   // dumps the state of the audio hardware to the given fd.
97   // This information can be retrieved using the dumpsys utility.
98   int Dump(int fd) const;
99 
100   // Fills the list of supported attributes for a given audio port.
101   // As input, "port" contains the information (type, role, address etc...)
102   // needed by the HAL to identify the port.
103   // As output, "port" contains possible attributes (sampling rates, formats,
104   // channel masks, gain controllers...) for this port.
105   // TODO(ghartman): Implement this as part of HAL 3.0
106   // int GetAudioPort(struct audio_port *port);
107 
108   // Sets audio port configuration
109   // TODO(ghartman): Implement this as part of HAL 3.0
110   // int SetAudioPortConfig(const struct audio_port_config *config);
111 
112   size_t GetInputBufferSize(const audio_config*) const;
113 
114   // Gets the current master volume value for the HAL, if the HAL supports
115   // master volume control.  AudioFlinger will query this value from the
116   // primary audio HAL when the service starts and use the value for setting
117   // the initial master volume across all HALs.  HALs which do not support
118   // this method may leave it set to NULL.
119   int GetMasterVolume(float* /*volume*/);
120 
121   // Get the current master mute status for the HAL, if the HAL supports
122   // master mute control.  AudioFlinger will query this value from the primary
123   // audio HAL when the service starts and use the value for setting the
124   // initial master mute across all HALs.  HALs which do not support this
125   // method may leave it set to NULL.
126   int GetMasterMute(bool* muted);
127 
128   // Gets the audio mute status for the microphone.
129   int GetMicMute(bool* state) const;
130 
131   // Retrieves the global audio parameters.
132   // TODO(ghartman): Implement this.
133   char* GetParameters(const char* keys) const;
134 
135   // Enumerates what devices are supported by each audio_hw_device
136   // implementation.
137   // Return value is a bitmask of 1 or more values of audio_devices_t
138   // used by audio flinger.
139   // NOTE: audio HAL implementations starting with
140   // AUDIO_DEVICE_API_VERSION_2_0 do not implement this function.
141   // AUDIO_DEVICE_API_VERSION_2_0 was the current version as of JB-MR1
142   // All supported devices should be listed in audio_policy.conf
143   // file and the audio policy manager must choose the appropriate
144   // audio module based on information in this file.
145   uint32_t GetSupportedDevices() const;
146 
147   // Checks to see if the audio hardware interface has been initialized.
148   // Always returns 0 to indicate success, but -ENODEV is also allowed to
149   // indicate failure.
150   int InitCheck() const;
151 
152   // Creates an additional hardware input stream.
153   // Additional parameters were added in the 3.0 version of the HAL.
154   // These defaults make it easier to implement a cross-branch device.
155   int OpenInputStream(
156       audio_io_handle_t handle,
157       audio_devices_t devices, audio_config *config,
158       audio_stream_in **stream_in,
159       audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
160       const char* address = 0,
161       audio_source_t source = AUDIO_SOURCE_DEFAULT);
162 
163   // Creates an additional output stream.
164   // The "address" parameter qualifies the "devices" audio device type if
165   // needed. On GCE we ignore it for now because we simulate a single SoC
166   // hw devices.
167   //
168   // The format format depends on the device type:
169   //   Bluetooth devices use the MAC address of the device in the form
170   //     "00:11:22:AA:BB:CC"
171   // USB devices use the ALSA card and device numbers in the form
172   //     "card=X;device=Y"
173   // Other devices may use a number or any other string.
174   int OpenOutputStream(
175       audio_io_handle_t handle,
176       audio_devices_t devices, audio_output_flags_t flags,
177       audio_config* config, audio_stream_out** stream_out,
178       const char* address = 0);
179 
180   // Releases an audio patch.
181   // TODO(ghartman): Implement this as part of HAL 3.0
182   //int ReleaseAudioPatch(audio_patch_handle_t handle);
183 
184   // Sets the audio mute status for all audio activities.  If any value other
185   // than 0 is returned, the software mixer will emulate this capability.
186   // The GCE implementation always returns 0.
187   int SetMasterMute(bool muted);
188 
189   // Sets the audio volume for all audio activities other than voice call.
190   // Range between 0.0 and 1.0. If any value other than 0 is returned,
191   // the software mixer will emulate this capability.
192   // The GCE implementation always returns 0.
193   int SetMasterVolume(float volume);
194 
195   // Sets the audio mute status for the microphone.
196   int SetMicMute(bool state);
197 
198   // set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
199   // is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
200   // playing, and AUDIO_MODE_IN_CALL when a call is in progress.
201   int SetMode(audio_mode_t mode);
202 
203   // Sets the global audio parameters.
204   // TODO(ghartman): Create a sensible implementation.
205   int SetParameters(const char* kvpairs);
206 
207   // Sets the audio volume of a voice call. Range is between 0.0 and 1.0
208   int SetVoiceVolume(float volume);
209 
210 
211  private:
212   // HAL 3.0 modifies the signatures of OpenInputStream and OpenOutputStream.
213   // We don't want to fork the implementation, and we don't want #ifdefs all
214   // over the code. The current implementation defines OpenInputStream and
215   // OpenOutputStream with default values for the paramteres that were added,
216   // and then generates a HAL-specific wrapper to be used in the function
217   // table.
218 #if defined(AUDIO_DEVICE_API_VERSION_3_0)
219   typedef int OpenInputStreamHAL_t(
220       audio_io_handle_t, audio_devices_t, audio_config*, audio_stream_in**,
221       audio_input_flags_t, const char*, audio_source_t);
222 
223   int OpenInputStreamCurrentHAL(
224       audio_io_handle_t a, audio_devices_t b, audio_config* c,
225       audio_stream_in** d, audio_input_flags_t e, const char* f,
226       audio_source_t g) {
227     return OpenInputStream(a, b, c, d, e, f, g);
228   }
229 
230   typedef int OpenOutputStreamHAL_t(
231       audio_io_handle_t, audio_devices_t, audio_output_flags_t,
232       audio_config*, audio_stream_out**,
233       const char*);
234 
235   int OpenOutputStreamCurrentHAL(
236       audio_io_handle_t a, audio_devices_t b, audio_output_flags_t c,
237       audio_config* d, audio_stream_out** e,
238       const char* f) {
239     return OpenOutputStream(a, b, c, d, e, f);
240   }
241 #else
242   typedef int OpenInputStreamHAL_t(
243       audio_io_handle_t, audio_devices_t, audio_config*, audio_stream_in**);
244 
245   int OpenInputStreamCurrentHAL(
246       audio_io_handle_t a, audio_devices_t b, audio_config* c,
247       audio_stream_in** d) {
248     return OpenInputStream(a, b, c, d);
249   }
250 
251   typedef int OpenOutputStreamHAL_t(
252       audio_io_handle_t, audio_devices_t, audio_output_flags_t,
253       audio_config*, audio_stream_out**);
254 
255   int OpenOutputStreamCurrentHAL(
256       audio_io_handle_t a, audio_devices_t b, audio_output_flags_t c,
257       audio_config* d, audio_stream_out** e) {
258     return OpenOutputStream(a, b, c, d, e);
259   }
260 #endif
261 
262   //TODO(ghartman): Update this when we support 3.0.
263 #if defined(AUDIO_DEVICE_API_VERSION_2_0)
264   static const unsigned int version_ = AUDIO_DEVICE_API_VERSION_2_0;
265 #else
266   static const unsigned int version_ = AUDIO_DEVICE_API_VERSION_1_0;
267 #endif
268 
269   using AudioDataRegionView = vsoc::audio_data::AudioDataRegionView;
270   AudioDataRegionView* audio_data_rv_{};
271   std::unique_ptr<vsoc::RegionWorker> audio_worker_;
272 
273   // Lock to protect the data below.
274   mutable cvd::Mutex lock_;
275   // State that is managed at the device level.
276   float voice_volume_;
277   float master_volume_;
278   bool master_muted_;
279   bool mic_muted_;
280   audio_mode_t mode_;
281   // There can be multiple input and output streams. This field is used
282   // to assign each one a unique identifier.
283   // TODO(ghartman): This can wrap after 2^32 streams. Ideally we should check
284   // the output_list_ to ensure that the stream number hasn't been assigned.
285   // However, streams don't really appear and disapper that often.
286   // We use the same counter for both input and output streams to make things
287   // a little easier on the client.
288   uint32_t next_stream_number_;
289   // List of the currently active output streams.
290   // Used to clean things up Close()
291   std::list<GceAudioOutputStream *> output_list_;
292   // List of the currently active input streams.
293   // Used to clean things up Close()
294   typedef std::map<uint32_t, GceAudioInputStream *> input_map_t;
295   input_map_t input_map_;
296 
297   GceAudio() :
298       audio_hw_device(),
299       voice_volume_(0.0),
300       master_volume_(0.0),
301       master_muted_(false),
302       mic_muted_(false),
303       mode_(AUDIO_MODE_NORMAL),
304       next_stream_number_(1) { }
305 };
306 
307 }
308