2 * Copyright (C) 2013 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/socket.h>
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
30 #include <audio_utils/resampler.h>
32 #include "hal-utils.h"
34 #include "ipc-common.h"
38 #define AUDIO_STREAM_DEFAULT_RATE 44100
39 #define AUDIO_STREAM_SCO_RATE 8000
40 #define AUDIO_STREAM_DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT
42 #define OUT_BUFFER_SIZE 2560
43 #define OUT_STREAM_FRAMES 2560
44 #define IN_STREAM_FRAMES 5292
46 #define SOCKET_POLL_TIMEOUT_MS 500
48 static int listen_sk = -1;
49 static int ipc_sk = -1;
51 static int sco_fd = -1;
52 static uint16_t sco_mtu = 0;
53 static pthread_mutex_t sco_mutex = PTHREAD_MUTEX_INITIALIZER;
55 static pthread_t ipc_th = 0;
56 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
58 static struct sco_stream_in *sco_stream_in = NULL;
59 static struct sco_stream_out *sco_stream_out = NULL;
61 struct sco_audio_config {
65 audio_format_t format;
68 struct sco_stream_out {
69 struct audio_stream_out stream;
71 struct sco_audio_config cfg;
78 struct timespec start;
80 struct resampler_itfe *resampler;
81 int16_t *resample_buf;
82 uint32_t resample_frame_num;
87 static void sco_close_socket(void)
89 DBG("sco fd %d", sco_fd);
94 shutdown(sco_fd, SHUT_RDWR);
99 struct sco_stream_in {
100 struct audio_stream_in stream;
102 struct sco_audio_config cfg;
104 struct resampler_itfe *resampler;
105 int16_t *resample_buf;
106 uint32_t resample_frame_num;
112 struct audio_hw_device dev;
113 struct sco_stream_out *out;
114 struct sco_stream_in *in;
118 * return the minimum frame numbers from resampling between BT stack's rate
119 * and audio flinger's. For output stream, 'output' shall be true, otherwise
120 * false for input streams at audio flinger side.
122 static size_t get_resample_frame_num(uint32_t sco_rate, uint32_t rate,
123 size_t frame_num, bool output)
125 size_t resample_frames_num = frame_num * sco_rate / rate + output;
127 DBG("resampler: sco_rate %d frame_num %zd rate %d resample frames %zd",
128 sco_rate, frame_num, rate, resample_frames_num);
130 return resample_frames_num;
133 /* SCO IPC functions */
135 static int sco_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
136 void *param, size_t *rsp_len, void *rsp, int *fd)
142 char cmsgbuf[CMSG_SPACE(sizeof(int))];
144 size_t s_len = sizeof(s);
146 pthread_mutex_lock(&sk_mutex);
149 error("sco: Invalid cmd socket passed to sco_ipc_cmd");
153 if (!rsp || !rsp_len) {
154 memset(&s, 0, s_len);
159 memset(&msg, 0, sizeof(msg));
160 memset(&cmd, 0, sizeof(cmd));
162 cmd.service_id = service_id;
166 iv[0].iov_base = &cmd;
167 iv[0].iov_len = sizeof(cmd);
169 iv[1].iov_base = param;
175 ret = sendmsg(ipc_sk, &msg, 0);
177 error("sco: Sending command failed:%s", strerror(errno));
181 /* socket was shutdown */
183 error("sco: Command socket closed");
187 memset(&msg, 0, sizeof(msg));
188 memset(&cmd, 0, sizeof(cmd));
190 iv[0].iov_base = &cmd;
191 iv[0].iov_len = sizeof(cmd);
193 iv[1].iov_base = rsp;
194 iv[1].iov_len = *rsp_len;
200 memset(cmsgbuf, 0, sizeof(cmsgbuf));
201 msg.msg_control = cmsgbuf;
202 msg.msg_controllen = sizeof(cmsgbuf);
205 ret = recvmsg(ipc_sk, &msg, 0);
207 error("sco: Receiving command response failed:%s",
212 if (ret < (ssize_t) sizeof(cmd)) {
213 error("sco: Too small response received(%zd bytes)", ret);
217 if (cmd.service_id != service_id) {
218 error("sco: Invalid service id (%u vs %u)", cmd.service_id,
223 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
224 error("sco: Malformed response received(%zd bytes)", ret);
228 if (cmd.opcode != opcode && cmd.opcode != SCO_OP_STATUS) {
229 error("sco: Invalid opcode received (%u vs %u)",
234 if (cmd.opcode == SCO_OP_STATUS) {
235 struct ipc_status *s = rsp;
237 if (sizeof(*s) != cmd.len) {
238 error("sco: Invalid status length");
242 if (s->code == SCO_STATUS_SUCCESS) {
243 error("sco: Invalid success status response");
247 pthread_mutex_unlock(&sk_mutex);
252 pthread_mutex_unlock(&sk_mutex);
254 /* Receive auxiliary data in msg */
256 struct cmsghdr *cmsg;
260 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
261 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
262 if (cmsg->cmsg_level == SOL_SOCKET
263 && cmsg->cmsg_type == SCM_RIGHTS) {
264 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
275 return SCO_STATUS_SUCCESS;
278 /* Some serious issue happen on IPC - recover */
279 shutdown(ipc_sk, SHUT_RDWR);
280 pthread_mutex_unlock(&sk_mutex);
282 return SCO_STATUS_FAILED;
285 static int ipc_get_sco_fd(bt_bdaddr_t *bd_addr)
287 int ret = SCO_STATUS_SUCCESS;
289 pthread_mutex_lock(&sco_mutex);
292 struct sco_cmd_get_fd cmd;
293 struct sco_rsp_get_fd rsp;
294 size_t rsp_len = sizeof(rsp);
296 DBG("Getting SCO fd");
298 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
300 ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_GET_FD, sizeof(cmd),
301 &cmd, &rsp_len, &rsp, &sco_fd);
303 /* Sometimes mtu returned is wrong */
304 sco_mtu = /* rsp.mtu */ 48;
307 pthread_mutex_unlock(&sco_mutex);
312 /* Audio stream functions */
314 static void downmix_to_mono(struct sco_stream_out *out, const uint8_t *buffer,
317 const int16_t *input = (const void *) buffer;
318 int16_t *output = (void *) out->downmix_buf;
321 for (i = 0; i < frame_num; i++) {
322 int16_t l = get_le16(&input[i * 2]);
323 int16_t r = get_le16(&input[i * 2 + 1]);
325 put_le16((l + r) / 2, &output[i]);
329 static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
333 res.tv_sec = a->tv_sec - b->tv_sec;
334 res.tv_nsec = a->tv_nsec - b->tv_nsec;
336 if (res.tv_nsec < 0) {
338 res.tv_nsec += 1000000000ll; /* 1sec */
341 return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
344 static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
348 size_t len, written = 0;
351 uint64_t audio_sent_us, audio_passed_us;
354 pfd.events = POLLOUT | POLLHUP | POLLNVAL;
356 while (bytes > written) {
359 /* poll for sending */
360 if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
361 DBG("timeout fd %d", sco_fd);
365 if (pfd.revents & (POLLHUP | POLLNVAL)) {
366 error("error fd %d, events 0x%x", sco_fd, pfd.revents);
370 len = bytes - written > sco_mtu ? sco_mtu : bytes - written;
372 clock_gettime(CLOCK_REALTIME, &now);
373 /* Mark start of the stream */
375 memcpy(&out->start, &now, sizeof(out->start));
377 audio_sent_us = out->samples * 1000000ll / AUDIO_STREAM_SCO_RATE;
378 audio_passed_us = timespec_diff_us(&now, &out->start);
379 if ((int) (audio_sent_us - audio_passed_us) > 1500) {
380 struct timespec timeout = {0,
382 audio_passed_us) * 1000};
383 DBG("Sleeping for %d ms",
384 (int) (audio_sent_us - audio_passed_us));
385 nanosleep(&timeout, NULL);
386 } else if ((int)(audio_passed_us - audio_sent_us) > 50000) {
387 DBG("\n\nResync\n\n");
389 memcpy(&out->start, &now, sizeof(out->start));
392 if (out->cache_len) {
393 DBG("First packet cache_len %zd", out->cache_len);
394 memcpy(out->cache + out->cache_len, buffer,
395 sco_mtu - out->cache_len);
398 if (bytes - written >= sco_mtu)
399 p = (void *) buffer + written;
401 memcpy(out->cache, buffer + written,
403 out->cache_len = bytes - written;
404 DBG("Last packet, cache %zd bytes",
406 written += bytes - written;
411 ret = write(sco_fd, p, len);
413 if (out->cache_len) {
414 written = sco_mtu - out->cache_len;
419 out->samples += ret / 2;
421 DBG("written %d samples %zd total %zd bytes",
422 ret, out->samples, written);
426 if (errno == EAGAIN) {
428 warn("write failed (%d)", ret);
432 if (errno != EINTR) {
434 error("write failed (%d) fd %d bytes %zd", ret, sco_fd,
440 DBG("written %zd bytes", bytes);
445 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
448 struct sco_stream_out *out = (struct sco_stream_out *) stream;
449 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
450 size_t frame_num = bytes / audio_stream_out_frame_size(stream);
452 size_t frame_num = bytes / audio_stream_frame_size(&out->stream.common);
454 size_t output_frame_num = frame_num;
455 void *send_buf = out->downmix_buf;
458 DBG("write to fd %d bytes %zu", sco_fd, bytes);
460 if (ipc_get_sco_fd(&out->bd_addr) != SCO_STATUS_SUCCESS)
463 if (!out->downmix_buf) {
464 error("sco: downmix buffer not initialized");
468 downmix_to_mono(out, buffer, frame_num);
470 if (out->resampler) {
473 /* limit resampler's output within what resample buf can hold */
474 output_frame_num = out->resample_frame_num;
476 ret = out->resampler->resample_from_input(out->resampler,
482 error("Failed to resample frames: %zd input %zd (%s)",
483 frame_num, output_frame_num, strerror(ret));
487 send_buf = out->resample_buf;
489 DBG("Resampled: frame_num %zd, output_frame_num %zd",
490 frame_num, output_frame_num);
493 total = output_frame_num * sizeof(int16_t) * 1;
495 DBG("total %zd", total);
497 if (!write_data(out, send_buf, total))
503 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
505 struct sco_stream_out *out = (struct sco_stream_out *) stream;
507 DBG("rate %u", out->cfg.rate);
509 return out->cfg.rate;
512 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
514 DBG("rate %u", rate);
519 static size_t out_get_buffer_size(const struct audio_stream *stream)
521 struct sco_stream_out *out = (struct sco_stream_out *) stream;
522 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
523 size_t size = audio_stream_out_frame_size(&out->stream) *
526 size_t size = audio_stream_frame_size(&out->stream.common) *
530 /* buffer size without resampling */
531 if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
534 DBG("buf size %zd", size);
539 static uint32_t out_get_channels(const struct audio_stream *stream)
541 struct sco_stream_out *out = (struct sco_stream_out *) stream;
543 DBG("channels num: %u", popcount(out->cfg.channels));
545 return out->cfg.channels;
548 static audio_format_t out_get_format(const struct audio_stream *stream)
550 struct sco_stream_out *out = (struct sco_stream_out *) stream;
552 DBG("format: %u", out->cfg.format);
554 return out->cfg.format;
557 static int out_set_format(struct audio_stream *stream, audio_format_t format)
564 static int out_standby(struct audio_stream *stream)
571 static int out_dump(const struct audio_stream *stream, int fd)
578 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
585 static char *out_get_parameters(const struct audio_stream *stream,
593 static uint32_t out_get_latency(const struct audio_stream_out *stream)
600 static int out_set_volume(struct audio_stream_out *stream, float left,
608 static int out_get_render_position(const struct audio_stream_out *stream,
609 uint32_t *dsp_frames)
616 static int out_add_audio_effect(const struct audio_stream *stream,
617 effect_handle_t effect)
624 static int out_remove_audio_effect(const struct audio_stream *stream,
625 effect_handle_t effect)
632 static int sco_open_output_stream_real(struct audio_hw_device *dev,
633 audio_io_handle_t handle,
634 audio_devices_t devices,
635 audio_output_flags_t flags,
636 struct audio_config *config,
637 struct audio_stream_out **stream_out,
640 struct sco_dev *adev = (struct sco_dev *) dev;
641 struct sco_stream_out *out;
643 size_t resample_size;
645 DBG("config %p device flags 0x%02x", config, devices);
647 if (sco_stream_out) {
648 DBG("stream_out already open");
652 out = calloc(1, sizeof(struct sco_stream_out));
656 DBG("stream %p sco fd %d mtu %u", out, sco_fd, sco_mtu);
658 out->stream.common.get_sample_rate = out_get_sample_rate;
659 out->stream.common.set_sample_rate = out_set_sample_rate;
660 out->stream.common.get_buffer_size = out_get_buffer_size;
661 out->stream.common.get_channels = out_get_channels;
662 out->stream.common.get_format = out_get_format;
663 out->stream.common.set_format = out_set_format;
664 out->stream.common.standby = out_standby;
665 out->stream.common.dump = out_dump;
666 out->stream.common.set_parameters = out_set_parameters;
667 out->stream.common.get_parameters = out_get_parameters;
668 out->stream.common.add_audio_effect = out_add_audio_effect;
669 out->stream.common.remove_audio_effect = out_remove_audio_effect;
670 out->stream.get_latency = out_get_latency;
671 out->stream.set_volume = out_set_volume;
672 out->stream.write = out_write;
673 out->stream.get_render_position = out_get_render_position;
675 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
677 DBG("address %s", address);
679 str2bt_bdaddr_t(address, &out->bd_addr);
683 if (ipc_get_sco_fd(&out->bd_addr) != SCO_STATUS_SUCCESS)
684 DBG("SCO is not connected yet; get fd on write()");
687 DBG("config: rate %u chan mask %x format %d offload %p",
688 config->sample_rate, config->channel_mask,
689 config->format, &config->offload_info);
691 out->cfg.format = config->format;
692 out->cfg.channels = config->channel_mask;
693 out->cfg.rate = config->sample_rate;
695 out->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
696 out->cfg.channels = AUDIO_CHANNEL_OUT_STEREO;
697 out->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
700 out->cfg.frame_num = OUT_STREAM_FRAMES;
702 out->downmix_buf = malloc(out_get_buffer_size(&out->stream.common));
703 if (!out->downmix_buf) {
708 out->cache = malloc(sco_mtu);
710 free(out->downmix_buf);
715 if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
718 /* Channel numbers for resampler */
721 ret = create_resampler(out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
722 RESAMPLER_QUALITY_DEFAULT, NULL,
725 error("Failed to create resampler (%s)", strerror(-ret));
729 out->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
731 out->cfg.frame_num, 1);
733 if (!out->resample_frame_num) {
734 error("frame num is too small to resample, discard it");
738 resample_size = sizeof(int16_t) * chan_num * out->resample_frame_num;
740 out->resample_buf = malloc(resample_size);
741 if (!out->resample_buf) {
742 error("failed to allocate resample buffer for %u frames",
743 out->resample_frame_num);
747 DBG("Resampler: input %d output %d chan %d frames %u size %zd",
748 out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
749 out->resample_frame_num, resample_size);
751 *stream_out = &out->stream;
753 sco_stream_out = out;
758 release_resampler(out->resampler);
761 free(out->downmix_buf);
765 sco_stream_out = NULL;
770 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
771 static int sco_open_output_stream(struct audio_hw_device *dev,
772 audio_io_handle_t handle,
773 audio_devices_t devices,
774 audio_output_flags_t flags,
775 struct audio_config *config,
776 struct audio_stream_out **stream_out,
779 return sco_open_output_stream_real(dev, handle, devices, flags,
780 config, stream_out, address);
783 static int sco_open_output_stream(struct audio_hw_device *dev,
784 audio_io_handle_t handle,
785 audio_devices_t devices,
786 audio_output_flags_t flags,
787 struct audio_config *config,
788 struct audio_stream_out **stream_out)
790 return sco_open_output_stream_real(dev, handle, devices, flags,
791 config, stream_out, NULL);
795 static void sco_close_output_stream(struct audio_hw_device *dev,
796 struct audio_stream_out *stream_out)
798 struct sco_dev *sco_dev = (struct sco_dev *) dev;
799 struct sco_stream_out *out = (struct sco_stream_out *) stream_out;
801 DBG("dev %p stream %p fd %d", dev, out, sco_fd);
803 if (out->resampler) {
804 release_resampler(out->resampler);
805 free(out->resample_buf);
809 free(out->downmix_buf);
813 pthread_mutex_lock(&sco_mutex);
815 sco_stream_out = NULL;
820 pthread_mutex_unlock(&sco_mutex);
823 static int sco_set_parameters(struct audio_hw_device *dev,
831 static char *sco_get_parameters(const struct audio_hw_device *dev,
839 static int sco_init_check(const struct audio_hw_device *dev)
846 static int sco_set_voice_volume(struct audio_hw_device *dev, float volume)
853 static int sco_set_master_volume(struct audio_hw_device *dev, float volume)
860 static int sco_set_mode(struct audio_hw_device *dev, int mode)
867 static int sco_set_mic_mute(struct audio_hw_device *dev, bool state)
874 static int sco_get_mic_mute(const struct audio_hw_device *dev, bool *state)
881 static size_t sco_get_input_buffer_size(const struct audio_hw_device *dev,
882 const struct audio_config *config)
889 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
891 struct sco_stream_in *in = (struct sco_stream_in *) stream;
893 DBG("rate %u", in->cfg.rate);
898 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
900 DBG("rate %u", rate);
905 static size_t in_get_buffer_size(const struct audio_stream *stream)
907 struct sco_stream_in *in = (struct sco_stream_in *) stream;
908 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
909 size_t size = audio_stream_in_frame_size(&in->stream) *
912 size_t size = audio_stream_frame_size(&in->stream.common) *
916 /* buffer size without resampling */
917 if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
920 DBG("buf size %zd", size);
925 static uint32_t in_get_channels(const struct audio_stream *stream)
927 struct sco_stream_in *in = (struct sco_stream_in *) stream;
929 DBG("channels num: %u", popcount(in->cfg.channels));
931 return in->cfg.channels;
934 static audio_format_t in_get_format(const struct audio_stream *stream)
936 struct sco_stream_in *in = (struct sco_stream_in *) stream;
938 DBG("format: %u", in->cfg.format);
940 return in->cfg.format;
943 static int in_set_format(struct audio_stream *stream, audio_format_t format)
950 static int in_standby(struct audio_stream *stream)
957 static int in_dump(const struct audio_stream *stream, int fd)
964 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
971 static char *in_get_parameters(const struct audio_stream *stream,
979 static int in_add_audio_effect(const struct audio_stream *stream,
980 effect_handle_t effect)
987 static int in_remove_audio_effect(const struct audio_stream *stream,
988 effect_handle_t effect)
995 static int in_set_gain(struct audio_stream_in *stream, float gain)
1002 static bool read_data(struct sco_stream_in *in, char *buffer, size_t bytes)
1005 size_t len, read_bytes = 0;
1008 pfd.events = POLLIN | POLLHUP | POLLNVAL;
1010 while (bytes > read_bytes) {
1013 /* poll for reading */
1014 if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
1015 DBG("timeout fd %d", sco_fd);
1019 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1020 error("error fd %d, events 0x%x", sco_fd, pfd.revents);
1024 len = bytes - read_bytes > sco_mtu ? sco_mtu :
1027 ret = read(sco_fd, buffer + read_bytes, len);
1030 DBG("read %d total %zd", ret, read_bytes);
1034 if (errno == EAGAIN) {
1036 warn("read failed (%d)", ret);
1040 if (errno != EINTR) {
1042 error("read failed (%d) fd %d bytes %zd", ret, sco_fd,
1048 DBG("read %zd bytes", read_bytes);
1053 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1056 struct sco_stream_in *in = (struct sco_stream_in *) stream;
1057 size_t frame_size, frame_num, input_frame_num;
1058 void *read_buf = buffer;
1059 size_t total = bytes;
1062 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1063 frame_size = audio_stream_in_frame_size(&in->stream);
1065 frame_size = audio_stream_frame_size(&stream->common);
1071 frame_num = bytes / frame_size;
1072 input_frame_num = frame_num;
1074 DBG("Read from fd %d bytes %zu", sco_fd, bytes);
1076 if (ipc_get_sco_fd(&in->bd_addr) != SCO_STATUS_SUCCESS)
1079 if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
1080 error("Cannot find resampler");
1084 if (in->resampler) {
1085 input_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
1088 if (input_frame_num > in->resample_frame_num) {
1089 DBG("resize input frames from %zd to %d",
1090 input_frame_num, in->resample_frame_num);
1091 input_frame_num = in->resample_frame_num;
1094 read_buf = in->resample_buf;
1096 total = input_frame_num * sizeof(int16_t) * 1;
1099 if(!read_data(in, read_buf, total))
1102 if (in->resampler) {
1103 ret = in->resampler->resample_from_input(in->resampler,
1109 error("Failed to resample frames: %zd input %zd (%s)",
1110 frame_num, input_frame_num,
1115 DBG("resampler: remain %zd output %zd frames", input_frame_num,
1122 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1129 static int sco_open_input_stream_real(struct audio_hw_device *dev,
1130 audio_io_handle_t handle,
1131 audio_devices_t devices,
1132 struct audio_config *config,
1133 struct audio_stream_in **stream_in,
1134 audio_input_flags_t flags,
1135 const char *address,
1136 audio_source_t source)
1138 struct sco_dev *sco_dev = (struct sco_dev *) dev;
1139 struct sco_stream_in *in;
1141 size_t resample_size;
1143 DBG("config %p device flags 0x%02x", config, devices);
1145 if (sco_stream_in) {
1146 DBG("stream_in already open");
1151 in = calloc(1, sizeof(struct sco_stream_in));
1155 DBG("stream %p sco fd %d mtu %u", in, sco_fd, sco_mtu);
1157 in->stream.common.get_sample_rate = in_get_sample_rate;
1158 in->stream.common.set_sample_rate = in_set_sample_rate;
1159 in->stream.common.get_buffer_size = in_get_buffer_size;
1160 in->stream.common.get_channels = in_get_channels;
1161 in->stream.common.get_format = in_get_format;
1162 in->stream.common.set_format = in_set_format;
1163 in->stream.common.standby = in_standby;
1164 in->stream.common.dump = in_dump;
1165 in->stream.common.set_parameters = in_set_parameters;
1166 in->stream.common.get_parameters = in_get_parameters;
1167 in->stream.common.add_audio_effect = in_add_audio_effect;
1168 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1169 in->stream.set_gain = in_set_gain;
1170 in->stream.read = in_read;
1171 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1173 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1175 DBG("address %s", address);
1177 str2bt_bdaddr_t(address, &in->bd_addr);
1182 DBG("config: rate %u chan mask %x format %d offload %p",
1183 config->sample_rate, config->channel_mask,
1184 config->format, &config->offload_info);
1186 in->cfg.format = config->format;
1187 in->cfg.channels = config->channel_mask;
1188 in->cfg.rate = config->sample_rate;
1190 in->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
1191 in->cfg.channels = AUDIO_CHANNEL_OUT_MONO;
1192 in->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
1195 in->cfg.frame_num = IN_STREAM_FRAMES;
1197 if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
1198 goto skip_resampler;
1200 /* Channel numbers for resampler */
1203 ret = create_resampler(AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
1204 RESAMPLER_QUALITY_DEFAULT, NULL,
1207 error("Failed to create resampler (%s)", strerror(-ret));
1211 in->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
1213 in->cfg.frame_num, 0);
1215 resample_size = sizeof(int16_t) * chan_num * in->resample_frame_num;
1217 in->resample_buf = malloc(resample_size);
1218 if (!in->resample_buf) {
1219 error("failed to allocate resample buffer for %d frames",
1220 in->resample_frame_num);
1224 DBG("Resampler: input %d output %d chan %d frames %u size %zd",
1225 AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
1226 in->resample_frame_num, resample_size);
1228 *stream_in = &in->stream;
1235 release_resampler(in->resampler);
1240 sco_stream_in = NULL;
1245 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1246 static int sco_open_input_stream(struct audio_hw_device *dev,
1247 audio_io_handle_t handle,
1248 audio_devices_t devices,
1249 struct audio_config *config,
1250 struct audio_stream_in **stream_in,
1251 audio_input_flags_t flags,
1252 const char *address,
1253 audio_source_t source)
1255 return sco_open_input_stream_real(dev, handle, devices, config,
1256 stream_in, flags, address,
1260 static int sco_open_input_stream(struct audio_hw_device *dev,
1261 audio_io_handle_t handle,
1262 audio_devices_t devices,
1263 struct audio_config *config,
1264 struct audio_stream_in **stream_in)
1266 return sco_open_input_stream_real(dev, handle, devices, config,
1267 stream_in, 0, NULL, 0);
1271 static void sco_close_input_stream(struct audio_hw_device *dev,
1272 struct audio_stream_in *stream_in)
1274 struct sco_dev *sco_dev = (struct sco_dev *) dev;
1275 struct sco_stream_in *in = (struct sco_stream_in *) stream_in;
1277 DBG("dev %p stream %p fd %d", dev, in, sco_fd);
1279 if (in->resampler) {
1280 release_resampler(in->resampler);
1281 free(in->resample_buf);
1287 pthread_mutex_lock(&sco_mutex);
1289 sco_stream_in = NULL;
1291 if (!sco_stream_out)
1294 pthread_mutex_unlock(&sco_mutex);
1297 static int sco_dump(const audio_hw_device_t *device, int fd)
1304 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1305 static int set_master_mute(struct audio_hw_device *dev, bool mute)
1311 static int get_master_mute(struct audio_hw_device *dev, bool *mute)
1317 static int create_audio_patch(struct audio_hw_device *dev,
1318 unsigned int num_sources,
1319 const struct audio_port_config *sources,
1320 unsigned int num_sinks,
1321 const struct audio_port_config *sinks,
1322 audio_patch_handle_t *handle)
1328 static int release_audio_patch(struct audio_hw_device *dev,
1329 audio_patch_handle_t handle)
1335 static int get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1341 static int set_audio_port_config(struct audio_hw_device *dev,
1342 const struct audio_port_config *config)
1349 static int sco_close(hw_device_t *device)
1358 static void *ipc_handler(void *data)
1367 DBG("Waiting for connection ...");
1369 sk = accept(listen_sk, NULL, NULL);
1376 if (err != ECONNABORTED && err != EINVAL)
1377 error("sco: Failed to accept socket: %d (%s)",
1378 err, strerror(err));
1383 pthread_mutex_lock(&sk_mutex);
1385 pthread_mutex_unlock(&sk_mutex);
1387 DBG("SCO IPC: Connected");
1389 memset(&pfd, 0, sizeof(pfd));
1391 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1393 /* Check if socket is still alive. Empty while loop.*/
1394 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1396 info("SCO HAL: Socket closed");
1398 pthread_mutex_lock(&sk_mutex);
1401 pthread_mutex_unlock(&sk_mutex);
1404 info("Closing SCO IPC thread");
1408 static int sco_ipc_init(void)
1410 struct sockaddr_un addr;
1416 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1419 error("sco: Failed to create socket: %d (%s)", -err,
1424 memset(&addr, 0, sizeof(addr));
1425 addr.sun_family = AF_UNIX;
1427 memcpy(addr.sun_path, BLUEZ_SCO_SK_PATH, sizeof(BLUEZ_SCO_SK_PATH));
1429 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1431 error("sco: Failed to bind socket: %d (%s)", -err,
1436 if (listen(sk, 1) < 0) {
1438 error("sco: Failed to listen on the socket: %d (%s)", -err,
1445 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1449 error("sco: Failed to start IPC thread: %d (%s)",
1450 -err, strerror(-err));
1461 static int sco_open(const hw_module_t *module, const char *name,
1462 hw_device_t **device)
1464 struct sco_dev *dev;
1469 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1470 error("SCO: interface %s not matching [%s]", name,
1471 AUDIO_HARDWARE_INTERFACE);
1475 err = sco_ipc_init();
1479 dev = calloc(1, sizeof(struct sco_dev));
1483 dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1484 dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1485 dev->dev.common.module = (struct hw_module_t *) module;
1486 dev->dev.common.close = sco_close;
1488 dev->dev.init_check = sco_init_check;
1489 dev->dev.set_voice_volume = sco_set_voice_volume;
1490 dev->dev.set_master_volume = sco_set_master_volume;
1491 dev->dev.set_mode = sco_set_mode;
1492 dev->dev.set_mic_mute = sco_set_mic_mute;
1493 dev->dev.get_mic_mute = sco_get_mic_mute;
1494 dev->dev.set_parameters = sco_set_parameters;
1495 dev->dev.get_parameters = sco_get_parameters;
1496 dev->dev.get_input_buffer_size = sco_get_input_buffer_size;
1497 dev->dev.open_output_stream = sco_open_output_stream;
1498 dev->dev.close_output_stream = sco_close_output_stream;
1499 dev->dev.open_input_stream = sco_open_input_stream;
1500 dev->dev.close_input_stream = sco_close_input_stream;
1501 dev->dev.dump = sco_dump;
1502 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1503 dev->dev.set_master_mute = set_master_mute;
1504 dev->dev.get_master_mute = get_master_mute;
1505 dev->dev.create_audio_patch = create_audio_patch;
1506 dev->dev.release_audio_patch = release_audio_patch;
1507 dev->dev.get_audio_port = get_audio_port;
1508 dev->dev.set_audio_port_config = set_audio_port_config;
1511 *device = &dev->dev.common;
1516 static struct hw_module_methods_t hal_module_methods = {
1520 struct audio_module HAL_MODULE_INFO_SYM = {
1522 .tag = HARDWARE_MODULE_TAG,
1525 .id = AUDIO_HARDWARE_MODULE_ID,
1526 .name = "SCO Audio HW HAL",
1527 .author = "Intel Corporation",
1528 .methods = &hal_module_methods,