1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/*
* Copyright (C) 2013 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <errno.h>
#include <pthread.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <hardware/audio.h>
#include <hardware/hardware.h>
#include "audio-msg.h"
#include "hal-log.h"
static int listen_sk = -1;
static int audio_sk = -1;
static bool close_thread = false;
static pthread_t ipc_th = 0;
static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
struct a2dp_audio_dev {
struct audio_hw_device dev;
struct audio_stream_out *out;
};
static void audio_ipc_cleanup(void)
{
if (audio_sk >= 0) {
shutdown(audio_sk, SHUT_RDWR);
audio_sk = -1;
}
}
static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
size_t bytes)
{
DBG("");
return -ENOSYS;
}
static uint32_t out_get_sample_rate(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
DBG("");
return -ENOSYS;
}
static size_t out_get_buffer_size(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static uint32_t out_get_channels(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static audio_format_t out_get_format(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int out_set_format(struct audio_stream *stream, audio_format_t format)
{
DBG("");
return -ENOSYS;
}
static int out_standby(struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int out_dump(const struct audio_stream *stream, int fd)
{
DBG("");
return -ENOSYS;
}
static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
DBG("");
return -ENOSYS;
}
static char *out_get_parameters(const struct audio_stream *stream,
const char *keys)
{
DBG("");
return strdup("");
}
static uint32_t out_get_latency(const struct audio_stream_out *stream)
{
DBG("");
return -ENOSYS;
}
static int out_set_volume(struct audio_stream_out *stream, float left,
float right)
{
DBG("");
/* volume controlled in audioflinger mixer (digital) */
return -ENOSYS;
}
static int out_get_render_position(const struct audio_stream_out *stream,
uint32_t *dsp_frames)
{
DBG("");
return -ENOSYS;
}
static int out_add_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
DBG("");
return -ENOSYS;
}
static int out_remove_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
DBG("");
return -ENOSYS;
}
static uint32_t in_get_sample_rate(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
DBG("");
return -ENOSYS;
}
static size_t in_get_buffer_size(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static uint32_t in_get_channels(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static audio_format_t in_get_format(const struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int in_set_format(struct audio_stream *stream, audio_format_t format)
{
DBG("");
return -ENOSYS;
}
static int in_standby(struct audio_stream *stream)
{
DBG("");
return -ENOSYS;
}
static int in_dump(const struct audio_stream *stream, int fd)
{
DBG("");
return -ENOSYS;
}
static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
DBG("");
return -ENOSYS;
}
static char *in_get_parameters(const struct audio_stream *stream,
const char *keys)
{
DBG("");
return strdup("");
}
static int in_set_gain(struct audio_stream_in *stream, float gain)
{
DBG("");
return -ENOSYS;
}
static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
size_t bytes)
{
DBG("");
return -ENOSYS;
}
static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
{
DBG("");
return -ENOSYS;
}
static int in_add_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
DBG("");
return -ENOSYS;
}
static int in_remove_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
DBG("");
return -ENOSYS;
}
static int audio_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)
{
struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
struct audio_stream_out *out;
out = calloc(1, sizeof(struct audio_stream_out));
if (!out)
return -ENOMEM;
DBG("");
out->common.get_sample_rate = out_get_sample_rate;
out->common.set_sample_rate = out_set_sample_rate;
out->common.get_buffer_size = out_get_buffer_size;
out->common.get_channels = out_get_channels;
out->common.get_format = out_get_format;
out->common.set_format = out_set_format;
out->common.standby = out_standby;
out->common.dump = out_dump;
out->common.set_parameters = out_set_parameters;
out->common.get_parameters = out_get_parameters;
out->common.add_audio_effect = out_add_audio_effect;
out->common.remove_audio_effect = out_remove_audio_effect;
out->get_latency = out_get_latency;
out->set_volume = out_set_volume;
out->write = out_write;
out->get_render_position = out_get_render_position;
*stream_out = out;
a2dp_dev->out = out;
return 0;
}
static void audio_close_output_stream(struct audio_hw_device *dev,
struct audio_stream_out *stream)
{
struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
DBG("");
free(stream);
a2dp_dev->out = NULL;
}
static int audio_set_parameters(struct audio_hw_device *dev,
const char *kvpairs)
{
DBG("");
return -ENOSYS;
}
static char *audio_get_parameters(const struct audio_hw_device *dev,
const char *keys)
{
DBG("");
return strdup("");
}
static int audio_init_check(const struct audio_hw_device *dev)
{
DBG("");
return -ENOSYS;
}
static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
{
DBG("");
return -ENOSYS;
}
static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
{
DBG("");
return -ENOSYS;
}
static int audio_set_mode(struct audio_hw_device *dev, int mode)
{
DBG("");
return -ENOSYS;
}
static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
{
DBG("");
return -ENOSYS;
}
static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
{
DBG("");
return -ENOSYS;
}
static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
const struct audio_config *config)
{
DBG("");
return -ENOSYS;
}
static int audio_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)
{
struct audio_stream_in *in;
DBG("");
in = calloc(1, sizeof(struct audio_stream_in));
if (!in)
return -ENOMEM;
in->common.get_sample_rate = in_get_sample_rate;
in->common.set_sample_rate = in_set_sample_rate;
in->common.get_buffer_size = in_get_buffer_size;
in->common.get_channels = in_get_channels;
in->common.get_format = in_get_format;
in->common.set_format = in_set_format;
in->common.standby = in_standby;
in->common.dump = in_dump;
in->common.set_parameters = in_set_parameters;
in->common.get_parameters = in_get_parameters;
in->common.add_audio_effect = in_add_audio_effect;
in->common.remove_audio_effect = in_remove_audio_effect;
in->set_gain = in_set_gain;
in->read = in_read;
in->get_input_frames_lost = in_get_input_frames_lost;
*stream_in = in;
return 0;
}
static void audio_close_input_stream(struct audio_hw_device *dev,
struct audio_stream_in *stream_in)
{
DBG("");
free(stream_in);
}
static int audio_dump(const audio_hw_device_t *device, int fd)
{
DBG("");
return -ENOSYS;
}
static int audio_close(hw_device_t *device)
{
struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
DBG("");
pthread_mutex_lock(&close_mutex);
audio_ipc_cleanup();
close_thread = true;
pthread_mutex_unlock(&close_mutex);
pthread_join(ipc_th, NULL);
close(listen_sk);
listen_sk = -1;
free(a2dp_dev);
return 0;
}
static void *ipc_handler(void *data)
{
bool done = false;
struct pollfd pfd;
DBG("");
while (!done) {
DBG("Waiting for connection ...");
audio_sk = accept(listen_sk, NULL, NULL);
if (audio_sk < 0) {
int err = errno;
error("audio: Failed to accept socket: %d (%s)", err,
strerror(err));
continue;
}
DBG("Audio IPC: Connected");
memset(&pfd, 0, sizeof(pfd));
pfd.fd = audio_sk;
pfd.events = POLLHUP | POLLERR | POLLNVAL;
/* Check if socket is still alive. Empty while loop.*/
while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
info("Audio HAL: Socket closed");
audio_sk = -1;
}
/*Check if audio_dev is closed */
pthread_mutex_lock(&close_mutex);
done = close_thread;
close_thread = false;
pthread_mutex_unlock(&close_mutex);
}
info("Closing Audio IPC thread");
return NULL;
}
static int audio_ipc_init(void)
{
struct sockaddr_un addr;
int err;
int sk;
DBG("");
sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
if (sk < 0) {
err = errno;
error("audio: Failed to create socket: %d (%s)", err,
strerror(err));
return err;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
sizeof(BLUEZ_AUDIO_SK_PATH));
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
err = errno;
error("audio: Failed to bind socket: %d (%s)", err,
strerror(err));
goto failed;
}
if (listen(sk, 1) < 0) {
err = errno;
error("audio: Failed to listen on the socket: %d (%s)", err,
strerror(err));
goto failed;
}
listen_sk = sk;
err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
if (err) {
err = -err;
ipc_th = 0;
error("audio: Failed to start Audio IPC thread: %d (%s)",
err, strerror(err));
goto failed;
}
return 0;
failed:
close(sk);
return err;
}
static int audio_open(const hw_module_t *module, const char *name,
hw_device_t **device)
{
struct a2dp_audio_dev *a2dp_dev;
int err;
DBG("");
if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
error("audio: interface %s not matching [%s]", name,
AUDIO_HARDWARE_INTERFACE);
return -EINVAL;
}
err = audio_ipc_init();
if (err)
return -err;
a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
if (!a2dp_dev)
return -ENOMEM;
a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
a2dp_dev->dev.common.module = (struct hw_module_t *) module;
a2dp_dev->dev.common.close = audio_close;
a2dp_dev->dev.init_check = audio_init_check;
a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
a2dp_dev->dev.set_master_volume = audio_set_master_volume;
a2dp_dev->dev.set_mode = audio_set_mode;
a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
a2dp_dev->dev.set_parameters = audio_set_parameters;
a2dp_dev->dev.get_parameters = audio_get_parameters;
a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
a2dp_dev->dev.open_output_stream = audio_open_output_stream;
a2dp_dev->dev.close_output_stream = audio_close_output_stream;
a2dp_dev->dev.open_input_stream = audio_open_input_stream;
a2dp_dev->dev.close_input_stream = audio_close_input_stream;
a2dp_dev->dev.dump = audio_dump;
/* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
* This results from the structure of following structs:a2dp_audio_dev,
* audio_hw_device. We will rely on this later in the code.*/
*device = &a2dp_dev->dev.common;
return 0;
}
static struct hw_module_methods_t hal_module_methods = {
.open = audio_open,
};
struct audio_module HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = AUDIO_HARDWARE_MODULE_ID,
.name = "A2DP Bluez HW HAL",
.author = "Intel Corporation",
.methods = &hal_module_methods,
},
};
|