f44e738268b1689bbfbc97ab919e04436352adce
[iec.git] / src / type3_AndroidCloud / anbox-master / android / audio / audio_hw.cpp
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 "audio_hw_generic"
18 #define LOG_NDEBUG 0
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sys/socket.h>
26 #include <sys/time.h>
27 #include <sys/un.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #include <cutils/log.h>
33 #include <cutils/str_parms.h>
34
35 #include <hardware/audio.h>
36 #include <hardware/hardware.h>
37 #include <system/audio.h>
38
39 #include "anbox/audio/client_info.h"
40
41 #define AUDIO_DEVICE_NAME "/dev/anbox_audio"
42 #define OUT_SAMPLING_RATE 44100
43 #define OUT_BUFFER_SIZE 4096
44 #define OUT_LATENCY_MS 20
45 #define IN_SAMPLING_RATE 8000
46 #define IN_BUFFER_SIZE 320
47
48 struct generic_audio_device {
49   struct audio_hw_device device;
50   pthread_mutex_t lock;
51   struct audio_stream_out *output;
52   struct audio_stream_in *input;
53   bool mic_mute;
54 };
55
56 struct generic_stream_out {
57   struct audio_stream_out stream;
58   struct generic_audio_device *dev;
59   audio_devices_t device;
60   int fd;
61 };
62
63 struct generic_stream_in {
64   struct audio_stream_in stream;
65   struct generic_audio_device *dev;
66   audio_devices_t device;
67   int fd;
68 };
69
70 static uint32_t out_get_sample_rate(const struct audio_stream *stream) {
71   return OUT_SAMPLING_RATE;
72 }
73
74 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
75   return -ENOSYS;
76 }
77
78 static size_t out_get_buffer_size(const struct audio_stream *stream) {
79   return OUT_BUFFER_SIZE;
80 }
81
82 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) {
83   return AUDIO_CHANNEL_OUT_STEREO;
84 }
85
86 static audio_format_t out_get_format(const struct audio_stream *stream) {
87   return AUDIO_FORMAT_PCM_16_BIT;
88 }
89
90 static int out_set_format(struct audio_stream *stream, audio_format_t format) {
91   return -ENOSYS;
92 }
93
94 static int out_standby(struct audio_stream *stream) {
95   return 0;
96 }
97
98 static int out_dump(const struct audio_stream *stream, int fd) {
99   struct generic_stream_out *out = (struct generic_stream_out *)stream;
100
101   dprintf(fd,
102           "\tout_dump:\n"
103           "\t\tsample rate: %u\n"
104           "\t\tbuffer size: %u\n"
105           "\t\tchannel mask: %08x\n"
106           "\t\tformat: %d\n"
107           "\t\tdevice: %08x\n"
108           "\t\taudio dev: %p\n\n",
109           out_get_sample_rate(stream),
110           out_get_buffer_size(stream),
111           out_get_channels(stream),
112           out_get_format(stream),
113           out->device,
114           out->dev);
115
116   return 0;
117 }
118
119 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) {
120   struct generic_stream_out *out = (struct generic_stream_out *)stream;
121   struct str_parms *parms;
122   char value[32];
123   int ret;
124   long val;
125   char *end;
126
127   parms = str_parms_create_str(kvpairs);
128
129   ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
130                           value, sizeof(value));
131   if (ret >= 0) {
132     errno = 0;
133     val = strtol(value, &end, 10);
134     if (errno == 0 && (end != NULL) && (*end == '\0') && ((int)val == val)) {
135       out->device = (int)val;
136     } else {
137       ret = -EINVAL;
138     }
139   }
140
141   str_parms_destroy(parms);
142   return ret;
143 }
144
145 static char *out_get_parameters(const struct audio_stream *stream, const char *keys) {
146   struct generic_stream_out *out = (struct generic_stream_out *)stream;
147   struct str_parms *query = str_parms_create_str(keys);
148   char *str;
149   char value[256];
150   struct str_parms *reply = str_parms_create();
151   int ret;
152
153   ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
154   if (ret >= 0) {
155     str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, out->device);
156     str = strdup(str_parms_to_str(reply));
157   } else {
158     str = strdup(keys);
159   }
160
161   str_parms_destroy(query);
162   str_parms_destroy(reply);
163   return str;
164 }
165
166 static uint32_t out_get_latency(const struct audio_stream_out *stream) {
167   return OUT_LATENCY_MS;
168 }
169
170 static int out_set_volume(struct audio_stream_out *stream, float left,
171                           float right) {
172   return -ENOSYS;
173 }
174
175 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
176                          size_t bytes) {
177   struct generic_stream_out *out = (struct generic_stream_out *)stream;
178   struct generic_audio_device *adev = out->dev;
179
180   pthread_mutex_lock(&adev->lock);
181   if (out->fd >= 0)
182     bytes = write(out->fd, buffer, bytes);
183   pthread_mutex_unlock(&adev->lock);
184   return bytes;
185 }
186
187 static int out_get_render_position(const struct audio_stream_out *stream,
188                                    uint32_t *dsp_frames) {
189   return -ENOSYS;
190 }
191
192 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
193   return 0;
194 }
195
196 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
197   return 0;
198 }
199
200 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
201                                         int64_t *timestamp) {
202   return -ENOSYS;
203 }
204
205 static uint32_t in_get_sample_rate(const struct audio_stream *stream) {
206   return IN_SAMPLING_RATE;
207 }
208
209 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
210   return -ENOSYS;
211 }
212
213 static size_t in_get_buffer_size(const struct audio_stream *stream) {
214   return IN_BUFFER_SIZE;
215 }
216
217 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) {
218   return AUDIO_CHANNEL_IN_MONO;
219 }
220
221 static audio_format_t in_get_format(const struct audio_stream *stream) {
222   return AUDIO_FORMAT_PCM_16_BIT;
223 }
224
225 static int in_set_format(struct audio_stream *stream, audio_format_t format) {
226   return -ENOSYS;
227 }
228
229 static int in_standby(struct audio_stream *stream) {
230   return 0;
231 }
232
233 static int in_dump(const struct audio_stream *stream, int fd) {
234   struct generic_stream_in *in = (struct generic_stream_in *)stream;
235
236   dprintf(fd,
237           "\tin_dump:\n"
238           "\t\tsample rate: %u\n"
239           "\t\tbuffer size: %u\n"
240           "\t\tchannel mask: %08x\n"
241           "\t\tformat: %d\n"
242           "\t\tdevice: %08x\n"
243           "\t\taudio dev: %p\n\n",
244           in_get_sample_rate(stream),
245           in_get_buffer_size(stream),
246           in_get_channels(stream),
247           in_get_format(stream),
248           in->device,
249           in->dev);
250
251   return 0;
252 }
253
254 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) {
255   struct generic_stream_in *in = (struct generic_stream_in *)stream;
256   struct str_parms *parms;
257   char value[32];
258   int ret;
259   long val;
260   char *end;
261
262   parms = str_parms_create_str(kvpairs);
263
264   ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
265                           value, sizeof(value));
266   if (ret >= 0) {
267     errno = 0;
268     val = strtol(value, &end, 10);
269     if ((errno == 0) && (end != NULL) && (*end == '\0') && ((int)val == val)) {
270       in->device = (int)val;
271     } else {
272       ret = -EINVAL;
273     }
274   }
275
276   str_parms_destroy(parms);
277   return ret;
278 }
279
280 static char *in_get_parameters(const struct audio_stream *stream,
281                                const char *keys) {
282   struct generic_stream_in *in = (struct generic_stream_in *)stream;
283   struct str_parms *query = str_parms_create_str(keys);
284   char *str;
285   char value[256];
286   struct str_parms *reply = str_parms_create();
287   int ret;
288
289   ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
290   if (ret >= 0) {
291     str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, in->device);
292     str = strdup(str_parms_to_str(reply));
293   } else {
294     str = strdup(keys);
295   }
296
297   str_parms_destroy(query);
298   str_parms_destroy(reply);
299   return str;
300 }
301
302 static int in_set_gain(struct audio_stream_in *stream, float gain) {
303   return 0;
304 }
305
306 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
307                        size_t bytes) {
308   struct generic_stream_in *in = (struct generic_stream_in *)stream;
309   struct generic_audio_device *adev = in->dev;
310
311   pthread_mutex_lock(&adev->lock);
312   if (in->fd >= 0)
313     bytes = read(in->fd, buffer, bytes);
314   if (adev->mic_mute && (bytes > 0)) {
315     memset(buffer, 0, bytes);
316   }
317   pthread_mutex_unlock(&adev->lock);
318
319   return bytes;
320 }
321
322 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) {
323   return 0;
324 }
325
326 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
327   return 0;
328 }
329
330 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
331   return 0;
332 }
333
334 static int connect_audio_server(const anbox::audio::ClientInfo::Type &type) {
335   int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
336   if (fd < 0)
337     return -errno;
338
339   struct sockaddr_un addr;
340   memset(&addr, 0, sizeof(addr));
341   addr.sun_family = AF_UNIX;
342   strncpy(addr.sun_path, AUDIO_DEVICE_NAME, sizeof(addr.sun_path));
343
344   if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
345     close(fd);
346     return -errno;
347   }
348
349   // We will send out client type information to the server and the
350   // server will either deny the request by closing the connection
351   // or by sending us the approved client details back.
352   anbox::audio::ClientInfo client_info{type};
353   if (::write(fd, &client_info, sizeof(client_info)) < 0) {
354     close(fd);
355     return -EIO;
356   }
357
358   auto bytes_read = ::read(fd, &client_info, sizeof(client_info));
359   if (bytes_read < 0) {
360     close(fd);
361     return -EIO;
362   }
363
364   // FIXME once we have real client details we need to check if we
365   // got everything we need or if anything is missing.
366
367   ALOGE("Successfully connected Anbox audio server");
368
369   return fd;
370 }
371
372 static int adev_open_output_stream(struct audio_hw_device *dev,
373                                    audio_io_handle_t handle,
374                                    audio_devices_t devices,
375                                    audio_output_flags_t flags,
376                                    struct audio_config *config,
377                                    struct audio_stream_out **stream_out,
378                                    const char *address __unused) {
379   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
380   struct generic_stream_out *out;
381   int ret = 0, fd = 0;
382
383   pthread_mutex_lock(&adev->lock);
384   if (adev->output != NULL) {
385     ret = -ENOSYS;
386     goto error;
387   }
388
389   fd = connect_audio_server(anbox::audio::ClientInfo::Type::Playback);
390   if (fd < 0) {
391     ret = fd;
392     ALOGE("Failed to connect with Anbox audio servers (err %d)", ret);
393     goto error;
394   }
395
396   if ((config->format != AUDIO_FORMAT_PCM_16_BIT) ||
397       (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) ||
398       (config->sample_rate != OUT_SAMPLING_RATE)) {
399     ALOGE("Error opening output stream format %d, channel_mask %04x, sample_rate %u",
400           config->format, config->channel_mask, config->sample_rate);
401     config->format = AUDIO_FORMAT_PCM_16_BIT;
402     config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
403     config->sample_rate = OUT_SAMPLING_RATE;
404     ret = -EINVAL;
405     goto error;
406   }
407
408   out = (struct generic_stream_out *)calloc(1, sizeof(struct generic_stream_out));
409   out->fd = fd;
410
411   out->stream.common.get_sample_rate = out_get_sample_rate;
412   out->stream.common.set_sample_rate = out_set_sample_rate;
413   out->stream.common.get_buffer_size = out_get_buffer_size;
414   out->stream.common.get_channels = out_get_channels;
415   out->stream.common.get_format = out_get_format;
416   out->stream.common.set_format = out_set_format;
417   out->stream.common.standby = out_standby;
418   out->stream.common.dump = out_dump;
419   out->stream.common.set_parameters = out_set_parameters;
420   out->stream.common.get_parameters = out_get_parameters;
421   out->stream.common.add_audio_effect = out_add_audio_effect;
422   out->stream.common.remove_audio_effect = out_remove_audio_effect;
423   out->stream.get_latency = out_get_latency;
424   out->stream.set_volume = out_set_volume;
425   out->stream.write = out_write;
426   out->stream.get_render_position = out_get_render_position;
427   out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
428
429   out->dev = adev;
430   out->device = devices;
431   adev->output = (struct audio_stream_out *)out;
432   *stream_out = &out->stream;
433
434 error:
435   pthread_mutex_unlock(&adev->lock);
436
437   return ret;
438 }
439
440 static void adev_close_output_stream(struct audio_hw_device *dev,
441                                      struct audio_stream_out *stream) {
442   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
443
444   pthread_mutex_lock(&adev->lock);
445   if (stream == adev->output) {
446     free(stream);
447     adev->output = NULL;
448   }
449   pthread_mutex_unlock(&adev->lock);
450 }
451
452 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) {
453   return 0;
454 }
455
456 static char *adev_get_parameters(const struct audio_hw_device *dev,
457                                  const char *keys) {
458   return strdup("");
459 }
460
461 static int adev_init_check(const struct audio_hw_device *dev) {
462   return 0;
463 }
464
465 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) {
466   return 0;
467 }
468
469 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) {
470   return -ENOSYS;
471 }
472
473 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) {
474   return -ENOSYS;
475 }
476
477 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) {
478   return -ENOSYS;
479 }
480
481 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) {
482   return -ENOSYS;
483 }
484
485 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) {
486   return 0;
487 }
488
489 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) {
490   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
491
492   pthread_mutex_lock(&adev->lock);
493   adev->mic_mute = state;
494   pthread_mutex_unlock(&adev->lock);
495   return 0;
496 }
497
498 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) {
499   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
500
501   pthread_mutex_lock(&adev->lock);
502   *state = adev->mic_mute;
503   pthread_mutex_unlock(&adev->lock);
504
505   return 0;
506 }
507
508 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
509                                          const struct audio_config *config) {
510   return IN_BUFFER_SIZE;
511 }
512
513 static int adev_open_input_stream(struct audio_hw_device *dev,
514                                   audio_io_handle_t handle,
515                                   audio_devices_t devices,
516                                   struct audio_config *config,
517                                   struct audio_stream_in **stream_in,
518                                   audio_input_flags_t flags __unused,
519                                   const char *address __unused,
520                                   audio_source_t source __unused) {
521   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
522   struct generic_stream_in *in;
523   int ret = 0, fd = 0;
524
525   pthread_mutex_lock(&adev->lock);
526   if (adev->input != NULL) {
527     ret = -ENOSYS;
528     goto error;
529   }
530
531   if ((config->format != AUDIO_FORMAT_PCM_16_BIT) ||
532       (config->channel_mask != AUDIO_CHANNEL_IN_MONO) ||
533       (config->sample_rate != IN_SAMPLING_RATE)) {
534     ALOGE("Error opening input stream format %d, channel_mask %04x, sample_rate %u",
535           config->format, config->channel_mask, config->sample_rate);
536     config->format = AUDIO_FORMAT_PCM_16_BIT;
537     config->channel_mask = AUDIO_CHANNEL_IN_MONO;
538     config->sample_rate = IN_SAMPLING_RATE;
539     ret = -EINVAL;
540     goto error;
541   }
542
543   fd = connect_audio_server(anbox::audio::ClientInfo::Type::Recording);
544   if (fd < 0) {
545     ret = fd;
546     ALOGE("Failed to connect with Anbox audio servers (err %d)", ret);
547     goto error;
548   }
549
550   in = (struct generic_stream_in *)calloc(1, sizeof(struct generic_stream_in));
551   in->fd = fd;
552
553   in->stream.common.get_sample_rate = in_get_sample_rate;
554   in->stream.common.set_sample_rate = in_set_sample_rate;
555   in->stream.common.get_buffer_size = in_get_buffer_size;
556   in->stream.common.get_channels = in_get_channels;
557   in->stream.common.get_format = in_get_format;
558   in->stream.common.set_format = in_set_format;
559   in->stream.common.standby = in_standby;
560   in->stream.common.dump = in_dump;
561   in->stream.common.set_parameters = in_set_parameters;
562   in->stream.common.get_parameters = in_get_parameters;
563   in->stream.common.add_audio_effect = in_add_audio_effect;
564   in->stream.common.remove_audio_effect = in_remove_audio_effect;
565   in->stream.set_gain = in_set_gain;
566   in->stream.read = in_read;
567   in->stream.get_input_frames_lost = in_get_input_frames_lost;
568
569   in->dev = adev;
570   in->device = devices;
571   adev->input = (struct audio_stream_in *)in;
572   *stream_in = &in->stream;
573
574 error:
575   pthread_mutex_unlock(&adev->lock);
576
577   return ret;
578 }
579
580 static void adev_close_input_stream(struct audio_hw_device *dev,
581                                     struct audio_stream_in *stream) {
582   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
583
584   pthread_mutex_lock(&adev->lock);
585   if (stream == adev->input) {
586     free(stream);
587     adev->input = NULL;
588   }
589   pthread_mutex_unlock(&adev->lock);
590 }
591
592 static int adev_dump(const audio_hw_device_t *dev, int fd) {
593   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
594
595   const size_t SIZE = 256;
596   char buffer[SIZE];
597
598   dprintf(fd,
599           "\nadev_dump:\n"
600           "\tmic_mute: %s\n"
601           "\toutput: %p\n"
602           "\tinput: %p\n\n",
603           adev->mic_mute ? "true" : "false",
604           adev->output,
605           adev->input);
606
607   if (adev->output != NULL)
608     out_dump((const struct audio_stream *)adev->output, fd);
609   if (adev->input != NULL)
610     in_dump((const struct audio_stream *)adev->input, fd);
611
612   return 0;
613 }
614
615 static int adev_close(hw_device_t *dev) {
616   struct generic_audio_device *adev = (struct generic_audio_device *)dev;
617
618   adev_close_output_stream((struct audio_hw_device *)dev, adev->output);
619   adev_close_input_stream((struct audio_hw_device *)dev, adev->input);
620
621   free(dev);
622   return 0;
623 }
624
625 static int adev_open(const hw_module_t *module, const char *name,
626                      hw_device_t **device) {
627   struct generic_audio_device *adev;
628
629   if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
630     return -EINVAL;
631
632   adev = (struct generic_audio_device*) calloc(1, sizeof(struct generic_audio_device));
633
634   adev->device.common.tag = HARDWARE_DEVICE_TAG;
635   adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
636   adev->device.common.module = (struct hw_module_t *)module;
637   adev->device.common.close = adev_close;
638
639   adev->device.init_check = adev_init_check;
640   adev->device.set_voice_volume = adev_set_voice_volume;
641   adev->device.set_master_volume = adev_set_master_volume;
642   adev->device.get_master_volume = adev_get_master_volume;
643   adev->device.set_master_mute = adev_set_master_mute;
644   adev->device.get_master_mute = adev_get_master_mute;
645   adev->device.set_mode = adev_set_mode;
646   adev->device.set_mic_mute = adev_set_mic_mute;
647   adev->device.get_mic_mute = adev_get_mic_mute;
648   adev->device.set_parameters = adev_set_parameters;
649   adev->device.get_parameters = adev_get_parameters;
650   adev->device.get_input_buffer_size = adev_get_input_buffer_size;
651   adev->device.open_output_stream = adev_open_output_stream;
652   adev->device.close_output_stream = adev_close_output_stream;
653   adev->device.open_input_stream = adev_open_input_stream;
654   adev->device.close_input_stream = adev_close_input_stream;
655   adev->device.dump = adev_dump;
656
657   *device = &adev->device.common;
658
659   return 0;
660 }
661
662 static struct hw_module_methods_t hal_module_methods = {
663     .open = adev_open,
664 };
665
666 struct audio_module HAL_MODULE_INFO_SYM = {
667     .common = {
668         .tag = HARDWARE_MODULE_TAG,
669         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
670         .hal_api_version = HARDWARE_HAL_API_VERSION,
671         .id = AUDIO_HARDWARE_MODULE_ID,
672         .name = "Anbox audio HW HAL",
673         .author = "The Android Open Source Project",
674         .methods = &hal_module_methods,
675     },
676 };