• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 "r_submix"
18 //#define LOG_NDEBUG 0
19 
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <sys/time.h>
24 #include <stdlib.h>
25 
26 #include <cutils/log.h>
27 #include <cutils/str_parms.h>
28 #include <cutils/properties.h>
29 
30 #include <hardware/hardware.h>
31 #include <system/audio.h>
32 #include <hardware/audio.h>
33 
34 #include <media/nbaio/MonoPipe.h>
35 #include <media/nbaio/MonoPipeReader.h>
36 #include <media/AudioBufferProvider.h>
37 
38 #include <utils/String8.h>
39 #include <media/AudioParameter.h>
40 
41 extern "C" {
42 
43 namespace android {
44 
45 #define MAX_PIPE_DEPTH_IN_FRAMES     (1024*8)
46 // The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
47 //   the duration of a record buffer at the current record sample rate (of the device, not of
48 //   the recording itself). Here we have:
49 //      3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
50 #define MAX_READ_ATTEMPTS            3
51 #define READ_ATTEMPT_SLEEP_MS        5 // 5ms between two read attempts when pipe is empty
52 #define DEFAULT_RATE_HZ              48000 // default sample rate
53 
54 struct submix_config {
55     audio_format_t format;
56     audio_channel_mask_t channel_mask;
57     unsigned int rate; // sample rate for the device
58     unsigned int period_size; // size of the audio pipe is period_size * period_count in frames
59     unsigned int period_count;
60 };
61 
62 struct submix_audio_device {
63     struct audio_hw_device device;
64     bool output_standby;
65     bool input_standby;
66     submix_config config;
67     // Pipe variables: they handle the ring buffer that "pipes" audio:
68     //  - from the submix virtual audio output == what needs to be played
69     //    remotely, seen as an output for AudioFlinger
70     //  - to the virtual audio source == what is captured by the component
71     //    which "records" the submix / virtual audio source, and handles it as needed.
72     // A usecase example is one where the component capturing the audio is then sending it over
73     // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
74     // TV with Wifi Display capabilities), or to a wireless audio player.
75     sp<MonoPipe>       rsxSink;
76     sp<MonoPipeReader> rsxSource;
77 
78     // device lock, also used to protect access to the audio pipe
79     pthread_mutex_t lock;
80 };
81 
82 struct submix_stream_out {
83     struct audio_stream_out stream;
84     struct submix_audio_device *dev;
85 };
86 
87 struct submix_stream_in {
88     struct audio_stream_in stream;
89     struct submix_audio_device *dev;
90     bool output_standby; // output standby state as seen from record thread
91 
92     // wall clock when recording starts
93     struct timespec record_start_time;
94     // how many frames have been requested to be read
95     int64_t read_counter_frames;
96 };
97 
98 
99 /* audio HAL functions */
100 
out_get_sample_rate(const struct audio_stream * stream)101 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
102 {
103     const struct submix_stream_out *out =
104             reinterpret_cast<const struct submix_stream_out *>(stream);
105     uint32_t out_rate = out->dev->config.rate;
106     //ALOGV("out_get_sample_rate() returns %u", out_rate);
107     return out_rate;
108 }
109 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)110 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
111 {
112     if ((rate != 44100) && (rate != 48000)) {
113         ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
114         return -ENOSYS;
115     }
116     struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
117     //ALOGV("out_set_sample_rate(rate=%u)", rate);
118     out->dev->config.rate = rate;
119     return 0;
120 }
121 
out_get_buffer_size(const struct audio_stream * stream)122 static size_t out_get_buffer_size(const struct audio_stream *stream)
123 {
124     const struct submix_stream_out *out =
125             reinterpret_cast<const struct submix_stream_out *>(stream);
126     const struct submix_config& config_out = out->dev->config;
127     size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask)
128                             * sizeof(int16_t); // only PCM 16bit
129     //ALOGV("out_get_buffer_size() returns %u, period size=%u",
130     //        buffer_size, config_out.period_size);
131     return buffer_size;
132 }
133 
out_get_channels(const struct audio_stream * stream)134 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
135 {
136     const struct submix_stream_out *out =
137             reinterpret_cast<const struct submix_stream_out *>(stream);
138     uint32_t channels = out->dev->config.channel_mask;
139     //ALOGV("out_get_channels() returns %08x", channels);
140     return channels;
141 }
142 
out_get_format(const struct audio_stream * stream)143 static audio_format_t out_get_format(const struct audio_stream *stream)
144 {
145     return AUDIO_FORMAT_PCM_16_BIT;
146 }
147 
out_set_format(struct audio_stream * stream,audio_format_t format)148 static int out_set_format(struct audio_stream *stream, audio_format_t format)
149 {
150     if (format != AUDIO_FORMAT_PCM_16_BIT) {
151         return -ENOSYS;
152     } else {
153         return 0;
154     }
155 }
156 
out_standby(struct audio_stream * stream)157 static int out_standby(struct audio_stream *stream)
158 {
159     ALOGI("out_standby()");
160 
161     const struct submix_stream_out *out = reinterpret_cast<const struct submix_stream_out *>(stream);
162 
163     pthread_mutex_lock(&out->dev->lock);
164 
165     out->dev->output_standby = true;
166 
167     pthread_mutex_unlock(&out->dev->lock);
168 
169     return 0;
170 }
171 
out_dump(const struct audio_stream * stream,int fd)172 static int out_dump(const struct audio_stream *stream, int fd)
173 {
174     return 0;
175 }
176 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)177 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
178 {
179     int exiting = -1;
180     AudioParameter parms = AudioParameter(String8(kvpairs));
181     // FIXME this is using hard-coded strings but in the future, this functionality will be
182     //       converted to use audio HAL extensions required to support tunneling
183     if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
184         const struct submix_stream_out *out =
185                 reinterpret_cast<const struct submix_stream_out *>(stream);
186 
187         pthread_mutex_lock(&out->dev->lock);
188 
189         MonoPipe* sink = out->dev->rsxSink.get();
190         if (sink != NULL) {
191             sink->incStrong(out);
192         } else {
193             pthread_mutex_unlock(&out->dev->lock);
194             return 0;
195         }
196 
197         ALOGI("shutdown");
198         sink->shutdown(true);
199 
200         sink->decStrong(out);
201 
202         pthread_mutex_unlock(&out->dev->lock);
203     }
204 
205     return 0;
206 }
207 
out_get_parameters(const struct audio_stream * stream,const char * keys)208 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
209 {
210     return strdup("");
211 }
212 
out_get_latency(const struct audio_stream_out * stream)213 static uint32_t out_get_latency(const struct audio_stream_out *stream)
214 {
215     const struct submix_stream_out *out =
216             reinterpret_cast<const struct submix_stream_out *>(stream);
217     const struct submix_config * config_out = &(out->dev->config);
218     uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate;
219     ALOGV("out_get_latency() returns %u", latency);
220     return latency;
221 }
222 
out_set_volume(struct audio_stream_out * stream,float left,float right)223 static int out_set_volume(struct audio_stream_out *stream, float left,
224                           float right)
225 {
226     return -ENOSYS;
227 }
228 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)229 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
230                          size_t bytes)
231 {
232     //ALOGV("out_write(bytes=%d)", bytes);
233     ssize_t written_frames = 0;
234     struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
235 
236     const size_t frame_size = audio_stream_frame_size(&stream->common);
237     const size_t frames = bytes / frame_size;
238 
239     pthread_mutex_lock(&out->dev->lock);
240 
241     out->dev->output_standby = false;
242 
243     MonoPipe* sink = out->dev->rsxSink.get();
244     if (sink != NULL) {
245         if (sink->isShutdown()) {
246             pthread_mutex_unlock(&out->dev->lock);
247             // the pipe has already been shutdown, this buffer will be lost but we must
248             //   simulate timing so we don't drain the output faster than realtime
249             usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
250             return bytes;
251         }
252         sink->incStrong(buffer);
253     } else {
254         pthread_mutex_unlock(&out->dev->lock);
255         ALOGE("out_write without a pipe!");
256         ALOG_ASSERT("out_write without a pipe!");
257         return 0;
258     }
259 
260     pthread_mutex_unlock(&out->dev->lock);
261 
262     written_frames = sink->write(buffer, frames);
263     if (written_frames < 0) {
264         if (written_frames == (ssize_t)NEGOTIATE) {
265             ALOGE("out_write() write to pipe returned NEGOTIATE");
266 
267             pthread_mutex_lock(&out->dev->lock);
268             sink->decStrong(buffer);
269             pthread_mutex_unlock(&out->dev->lock);
270 
271             written_frames = 0;
272             return 0;
273         } else {
274             // write() returned UNDERRUN or WOULD_BLOCK, retry
275             ALOGE("out_write() write to pipe returned unexpected %16lx", written_frames);
276             written_frames = sink->write(buffer, frames);
277         }
278     }
279 
280     pthread_mutex_lock(&out->dev->lock);
281 
282     sink->decStrong(buffer);
283 
284     pthread_mutex_unlock(&out->dev->lock);
285 
286     if (written_frames < 0) {
287         ALOGE("out_write() failed writing to pipe with %16lx", written_frames);
288         return 0;
289     } else {
290         ALOGV("out_write() wrote %lu bytes)", written_frames * frame_size);
291         return written_frames * frame_size;
292     }
293 }
294 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)295 static int out_get_render_position(const struct audio_stream_out *stream,
296                                    uint32_t *dsp_frames)
297 {
298     return -EINVAL;
299 }
300 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)301 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
302 {
303     return 0;
304 }
305 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)306 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
307 {
308     return 0;
309 }
310 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)311 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
312                                         int64_t *timestamp)
313 {
314     return -EINVAL;
315 }
316 
317 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)318 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
319 {
320     const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
321     //ALOGV("in_get_sample_rate() returns %u", in->dev->config.rate);
322     return in->dev->config.rate;
323 }
324 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)325 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
326 {
327     return -ENOSYS;
328 }
329 
in_get_buffer_size(const struct audio_stream * stream)330 static size_t in_get_buffer_size(const struct audio_stream *stream)
331 {
332     const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
333     ALOGV("in_get_buffer_size() returns %u",
334             in->dev->config.period_size * audio_stream_frame_size(stream));
335     return in->dev->config.period_size * audio_stream_frame_size(stream);
336 }
337 
in_get_channels(const struct audio_stream * stream)338 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
339 {
340     return AUDIO_CHANNEL_IN_STEREO;
341 }
342 
in_get_format(const struct audio_stream * stream)343 static audio_format_t in_get_format(const struct audio_stream *stream)
344 {
345     return AUDIO_FORMAT_PCM_16_BIT;
346 }
347 
in_set_format(struct audio_stream * stream,audio_format_t format)348 static int in_set_format(struct audio_stream *stream, audio_format_t format)
349 {
350     if (format != AUDIO_FORMAT_PCM_16_BIT) {
351         return -ENOSYS;
352     } else {
353         return 0;
354     }
355 }
356 
in_standby(struct audio_stream * stream)357 static int in_standby(struct audio_stream *stream)
358 {
359     ALOGI("in_standby()");
360     const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
361 
362     pthread_mutex_lock(&in->dev->lock);
363 
364     in->dev->input_standby = true;
365 
366     pthread_mutex_unlock(&in->dev->lock);
367 
368     return 0;
369 }
370 
in_dump(const struct audio_stream * stream,int fd)371 static int in_dump(const struct audio_stream *stream, int fd)
372 {
373     return 0;
374 }
375 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)376 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
377 {
378     return 0;
379 }
380 
in_get_parameters(const struct audio_stream * stream,const char * keys)381 static char * in_get_parameters(const struct audio_stream *stream,
382                                 const char *keys)
383 {
384     return strdup("");
385 }
386 
in_set_gain(struct audio_stream_in * stream,float gain)387 static int in_set_gain(struct audio_stream_in *stream, float gain)
388 {
389     return 0;
390 }
391 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)392 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
393                        size_t bytes)
394 {
395     //ALOGV("in_read bytes=%u", bytes);
396     ssize_t frames_read = -1977;
397     struct submix_stream_in *in = reinterpret_cast<struct submix_stream_in *>(stream);
398     const size_t frame_size = audio_stream_frame_size(&stream->common);
399     const size_t frames_to_read = bytes / frame_size;
400 
401     pthread_mutex_lock(&in->dev->lock);
402 
403     const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
404     in->output_standby = in->dev->output_standby;
405 
406     if (in->dev->input_standby || output_standby_transition) {
407         in->dev->input_standby = false;
408         // keep track of when we exit input standby (== first read == start "real recording")
409         // or when we start recording silence, and reset projected time
410         int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
411         if (rc == 0) {
412             in->read_counter_frames = 0;
413         }
414     }
415 
416     in->read_counter_frames += frames_to_read;
417 
418     MonoPipeReader* source = in->dev->rsxSource.get();
419     if (source != NULL) {
420         source->incStrong(buffer);
421     } else {
422         ALOGE("no audio pipe yet we're trying to read!");
423         pthread_mutex_unlock(&in->dev->lock);
424         usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
425         memset(buffer, 0, bytes);
426         return bytes;
427     }
428 
429     pthread_mutex_unlock(&in->dev->lock);
430 
431     // read the data from the pipe (it's non blocking)
432     size_t remaining_frames = frames_to_read;
433     int attempts = 0;
434     char* buff = (char*)buffer;
435     while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
436         attempts++;
437         frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
438         if (frames_read > 0) {
439             remaining_frames -= frames_read;
440             buff += frames_read * frame_size;
441             //ALOGV("  in_read (att=%d) got %ld frames, remaining=%u",
442             //      attempts, frames_read, remaining_frames);
443         } else {
444             //ALOGE("  in_read read returned %ld", frames_read);
445             usleep(READ_ATTEMPT_SLEEP_MS * 1000);
446         }
447     }
448 
449     // done using the source
450     pthread_mutex_lock(&in->dev->lock);
451 
452     source->decStrong(buffer);
453 
454     pthread_mutex_unlock(&in->dev->lock);
455 
456     if (remaining_frames > 0) {
457         ALOGV("  remaining_frames = %d", remaining_frames);
458         memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,
459                 remaining_frames * frame_size);
460     }
461 
462     // compute how much we need to sleep after reading the data by comparing the wall clock with
463     //   the projected time at which we should return.
464     struct timespec time_after_read;// wall clock after reading from the pipe
465     struct timespec record_duration;// observed record duration
466     int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
467     const uint32_t sample_rate = in_get_sample_rate(&stream->common);
468     if (rc == 0) {
469         // for how long have we been recording?
470         record_duration.tv_sec  = time_after_read.tv_sec - in->record_start_time.tv_sec;
471         record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
472         if (record_duration.tv_nsec < 0) {
473             record_duration.tv_sec--;
474             record_duration.tv_nsec += 1000000000;
475         }
476 
477         // read_counter_frames contains the number of frames that have been read since the beginning
478         // of recording (including this call): it's converted to usec and compared to how long we've
479         // been recording for, which gives us how long we must wait to sync the projected recording
480         // time, and the observed recording time
481         long projected_vs_observed_offset_us =
482                 ((int64_t)(in->read_counter_frames
483                             - (record_duration.tv_sec*sample_rate)))
484                         * 1000000 / sample_rate
485                 - (record_duration.tv_nsec / 1000);
486 
487         ALOGV("  record duration %5lds %3ldms, will wait: %7ldus",
488                 record_duration.tv_sec, record_duration.tv_nsec/1000000,
489                 projected_vs_observed_offset_us);
490         if (projected_vs_observed_offset_us > 0) {
491             usleep(projected_vs_observed_offset_us);
492         }
493     }
494 
495 
496     ALOGV("in_read returns %d", bytes);
497     return bytes;
498 
499 }
500 
in_get_input_frames_lost(struct audio_stream_in * stream)501 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
502 {
503     return 0;
504 }
505 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)506 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
507 {
508     return 0;
509 }
510 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)511 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
512 {
513     return 0;
514 }
515 
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out)516 static int adev_open_output_stream(struct audio_hw_device *dev,
517                                    audio_io_handle_t handle,
518                                    audio_devices_t devices,
519                                    audio_output_flags_t flags,
520                                    struct audio_config *config,
521                                    struct audio_stream_out **stream_out)
522 {
523     ALOGV("adev_open_output_stream()");
524     struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
525     struct submix_stream_out *out;
526     int ret;
527 
528     out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
529     if (!out) {
530         ret = -ENOMEM;
531         goto err_open;
532     }
533 
534     pthread_mutex_lock(&rsxadev->lock);
535 
536     out->stream.common.get_sample_rate = out_get_sample_rate;
537     out->stream.common.set_sample_rate = out_set_sample_rate;
538     out->stream.common.get_buffer_size = out_get_buffer_size;
539     out->stream.common.get_channels = out_get_channels;
540     out->stream.common.get_format = out_get_format;
541     out->stream.common.set_format = out_set_format;
542     out->stream.common.standby = out_standby;
543     out->stream.common.dump = out_dump;
544     out->stream.common.set_parameters = out_set_parameters;
545     out->stream.common.get_parameters = out_get_parameters;
546     out->stream.common.add_audio_effect = out_add_audio_effect;
547     out->stream.common.remove_audio_effect = out_remove_audio_effect;
548     out->stream.get_latency = out_get_latency;
549     out->stream.set_volume = out_set_volume;
550     out->stream.write = out_write;
551     out->stream.get_render_position = out_get_render_position;
552     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
553 
554     config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
555     rsxadev->config.channel_mask = config->channel_mask;
556 
557     if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) {
558         config->sample_rate = DEFAULT_RATE_HZ;
559     }
560     rsxadev->config.rate = config->sample_rate;
561 
562     config->format = AUDIO_FORMAT_PCM_16_BIT;
563     rsxadev->config.format = config->format;
564 
565     rsxadev->config.period_size = 1024;
566     rsxadev->config.period_count = 4;
567     out->dev = rsxadev;
568 
569     *stream_out = &out->stream;
570 
571     // initialize pipe
572     {
573         ALOGV("  initializing pipe");
574         const NBAIO_Format format =
575                 config->sample_rate == 48000 ? Format_SR48_C2_I16 : Format_SR44_1_C2_I16;
576         const NBAIO_Format offers[1] = {format};
577         size_t numCounterOffers = 0;
578         // creating a MonoPipe with optional blocking set to true.
579         MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/);
580         ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
581         ALOG_ASSERT(index == 0);
582         MonoPipeReader* source = new MonoPipeReader(sink);
583         numCounterOffers = 0;
584         index = source->negotiate(offers, 1, NULL, numCounterOffers);
585         ALOG_ASSERT(index == 0);
586         rsxadev->rsxSink = sink;
587         rsxadev->rsxSource = source;
588     }
589 
590     pthread_mutex_unlock(&rsxadev->lock);
591 
592     return 0;
593 
594 err_open:
595     *stream_out = NULL;
596     return ret;
597 }
598 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)599 static void adev_close_output_stream(struct audio_hw_device *dev,
600                                      struct audio_stream_out *stream)
601 {
602     ALOGV("adev_close_output_stream()");
603     struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
604 
605     pthread_mutex_lock(&rsxadev->lock);
606 
607     rsxadev->rsxSink.clear();
608     rsxadev->rsxSource.clear();
609     free(stream);
610 
611     pthread_mutex_unlock(&rsxadev->lock);
612 }
613 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)614 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
615 {
616     return -ENOSYS;
617 }
618 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)619 static char * adev_get_parameters(const struct audio_hw_device *dev,
620                                   const char *keys)
621 {
622     return strdup("");;
623 }
624 
adev_init_check(const struct audio_hw_device * dev)625 static int adev_init_check(const struct audio_hw_device *dev)
626 {
627     ALOGI("adev_init_check()");
628     return 0;
629 }
630 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)631 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
632 {
633     return -ENOSYS;
634 }
635 
adev_set_master_volume(struct audio_hw_device * dev,float volume)636 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
637 {
638     return -ENOSYS;
639 }
640 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)641 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
642 {
643     return -ENOSYS;
644 }
645 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)646 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
647 {
648     return -ENOSYS;
649 }
650 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)651 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
652 {
653     return -ENOSYS;
654 }
655 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)656 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
657 {
658     return 0;
659 }
660 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)661 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
662 {
663     return -ENOSYS;
664 }
665 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)666 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
667 {
668     return -ENOSYS;
669 }
670 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)671 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
672                                          const struct audio_config *config)
673 {
674     //### TODO correlate this with pipe parameters
675     return 4096;
676 }
677 
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in)678 static int adev_open_input_stream(struct audio_hw_device *dev,
679                                   audio_io_handle_t handle,
680                                   audio_devices_t devices,
681                                   struct audio_config *config,
682                                   struct audio_stream_in **stream_in)
683 {
684     ALOGI("adev_open_input_stream()");
685 
686     struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
687     struct submix_stream_in *in;
688     int ret;
689 
690     in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
691     if (!in) {
692         ret = -ENOMEM;
693         goto err_open;
694     }
695 
696     pthread_mutex_lock(&rsxadev->lock);
697 
698     in->stream.common.get_sample_rate = in_get_sample_rate;
699     in->stream.common.set_sample_rate = in_set_sample_rate;
700     in->stream.common.get_buffer_size = in_get_buffer_size;
701     in->stream.common.get_channels = in_get_channels;
702     in->stream.common.get_format = in_get_format;
703     in->stream.common.set_format = in_set_format;
704     in->stream.common.standby = in_standby;
705     in->stream.common.dump = in_dump;
706     in->stream.common.set_parameters = in_set_parameters;
707     in->stream.common.get_parameters = in_get_parameters;
708     in->stream.common.add_audio_effect = in_add_audio_effect;
709     in->stream.common.remove_audio_effect = in_remove_audio_effect;
710     in->stream.set_gain = in_set_gain;
711     in->stream.read = in_read;
712     in->stream.get_input_frames_lost = in_get_input_frames_lost;
713 
714     config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
715     rsxadev->config.channel_mask = config->channel_mask;
716 
717     if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) {
718         config->sample_rate = DEFAULT_RATE_HZ;
719     }
720     rsxadev->config.rate = config->sample_rate;
721 
722     config->format = AUDIO_FORMAT_PCM_16_BIT;
723     rsxadev->config.format = config->format;
724 
725     rsxadev->config.period_size = 1024;
726     rsxadev->config.period_count = 4;
727 
728     *stream_in = &in->stream;
729 
730     in->dev = rsxadev;
731 
732     in->read_counter_frames = 0;
733     in->output_standby = rsxadev->output_standby;
734 
735     pthread_mutex_unlock(&rsxadev->lock);
736 
737     return 0;
738 
739 err_open:
740     *stream_in = NULL;
741     return ret;
742 }
743 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)744 static void adev_close_input_stream(struct audio_hw_device *dev,
745                                    struct audio_stream_in *stream)
746 {
747     ALOGV("adev_close_input_stream()");
748     struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
749 
750     pthread_mutex_lock(&rsxadev->lock);
751 
752     MonoPipe* sink = rsxadev->rsxSink.get();
753     if (sink != NULL) {
754         ALOGI("shutdown");
755         sink->shutdown(true);
756     }
757 
758     free(stream);
759 
760     pthread_mutex_unlock(&rsxadev->lock);
761 }
762 
adev_dump(const audio_hw_device_t * device,int fd)763 static int adev_dump(const audio_hw_device_t *device, int fd)
764 {
765     return 0;
766 }
767 
adev_close(hw_device_t * device)768 static int adev_close(hw_device_t *device)
769 {
770     ALOGI("adev_close()");
771     free(device);
772     return 0;
773 }
774 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)775 static int adev_open(const hw_module_t* module, const char* name,
776                      hw_device_t** device)
777 {
778     ALOGI("adev_open(name=%s)", name);
779     struct submix_audio_device *rsxadev;
780 
781     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
782         return -EINVAL;
783 
784     rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
785     if (!rsxadev)
786         return -ENOMEM;
787 
788     rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
789     rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
790     rsxadev->device.common.module = (struct hw_module_t *) module;
791     rsxadev->device.common.close = adev_close;
792 
793     rsxadev->device.init_check = adev_init_check;
794     rsxadev->device.set_voice_volume = adev_set_voice_volume;
795     rsxadev->device.set_master_volume = adev_set_master_volume;
796     rsxadev->device.get_master_volume = adev_get_master_volume;
797     rsxadev->device.set_master_mute = adev_set_master_mute;
798     rsxadev->device.get_master_mute = adev_get_master_mute;
799     rsxadev->device.set_mode = adev_set_mode;
800     rsxadev->device.set_mic_mute = adev_set_mic_mute;
801     rsxadev->device.get_mic_mute = adev_get_mic_mute;
802     rsxadev->device.set_parameters = adev_set_parameters;
803     rsxadev->device.get_parameters = adev_get_parameters;
804     rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
805     rsxadev->device.open_output_stream = adev_open_output_stream;
806     rsxadev->device.close_output_stream = adev_close_output_stream;
807     rsxadev->device.open_input_stream = adev_open_input_stream;
808     rsxadev->device.close_input_stream = adev_close_input_stream;
809     rsxadev->device.dump = adev_dump;
810 
811     rsxadev->input_standby = true;
812     rsxadev->output_standby = true;
813 
814     *device = &rsxadev->device.common;
815 
816     return 0;
817 }
818 
819 static struct hw_module_methods_t hal_module_methods = {
820     /* open */ adev_open,
821 };
822 
823 struct audio_module HAL_MODULE_INFO_SYM = {
824     /* common */ {
825         /* tag */                HARDWARE_MODULE_TAG,
826         /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
827         /* hal_api_version */    HARDWARE_HAL_API_VERSION,
828         /* id */                 AUDIO_HARDWARE_MODULE_ID,
829         /* name */               "Wifi Display audio HAL",
830         /* author */             "The Android Open Source Project",
831         /* methods */            &hal_module_methods,
832         /* dso */                NULL,
833         /* reserved */           { 0 },
834     },
835 };
836 
837 } //namespace android
838 
839 } //extern "C"
840