2 * Copyright (C) 2014 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.
23 #include "../hal-utils.h"
25 audio_hw_device_t *if_audio_sco = NULL;
26 static struct audio_stream_out *stream_out = NULL;
27 static struct audio_stream_in *stream_in = NULL;
29 static size_t buffer_size = 0;
30 static size_t buffer_size_in = 0;
31 static pthread_t play_thread = 0;
32 static pthread_mutex_t outstream_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER;
43 SINTMAP(audio_channel_mask_t, -1, "(AUDIO_CHANNEL_INVALID)")
44 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
45 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_RIGHT),
46 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_CENTER),
47 DELEMENT(AUDIO_CHANNEL_OUT_LOW_FREQUENCY),
48 DELEMENT(AUDIO_CHANNEL_OUT_BACK_LEFT),
49 DELEMENT(AUDIO_CHANNEL_OUT_BACK_RIGHT),
50 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER),
51 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER),
52 DELEMENT(AUDIO_CHANNEL_OUT_BACK_CENTER),
53 DELEMENT(AUDIO_CHANNEL_OUT_SIDE_LEFT),
54 DELEMENT(AUDIO_CHANNEL_OUT_SIDE_RIGHT),
55 DELEMENT(AUDIO_CHANNEL_OUT_TOP_CENTER),
56 DELEMENT(AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT),
57 DELEMENT(AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER),
58 DELEMENT(AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT),
59 DELEMENT(AUDIO_CHANNEL_OUT_TOP_BACK_LEFT),
60 DELEMENT(AUDIO_CHANNEL_OUT_TOP_BACK_CENTER),
61 DELEMENT(AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT),
62 DELEMENT(AUDIO_CHANNEL_OUT_MONO),
63 DELEMENT(AUDIO_CHANNEL_OUT_STEREO),
64 DELEMENT(AUDIO_CHANNEL_OUT_QUAD),
65 #if ANDROID_VERSION < PLATFORM_VER(5, 0, 0)
66 DELEMENT(AUDIO_CHANNEL_OUT_SURROUND),
68 DELEMENT(AUDIO_CHANNEL_OUT_QUAD_BACK),
69 DELEMENT(AUDIO_CHANNEL_OUT_QUAD_SIDE),
70 DELEMENT(AUDIO_CHANNEL_OUT_5POINT1_BACK),
71 DELEMENT(AUDIO_CHANNEL_OUT_5POINT1_SIDE),
73 DELEMENT(AUDIO_CHANNEL_OUT_5POINT1),
74 DELEMENT(AUDIO_CHANNEL_OUT_7POINT1),
75 DELEMENT(AUDIO_CHANNEL_OUT_ALL),
76 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
77 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
78 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
79 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
80 DELEMENT(AUDIO_CHANNEL_OUT_FRONT_LEFT),
83 SINTMAP(audio_format_t, -1, "(AUDIO_FORMAT_INVALID)")
84 DELEMENT(AUDIO_FORMAT_DEFAULT),
85 DELEMENT(AUDIO_FORMAT_PCM),
86 DELEMENT(AUDIO_FORMAT_MP3),
87 DELEMENT(AUDIO_FORMAT_AMR_NB),
88 DELEMENT(AUDIO_FORMAT_AMR_WB),
89 DELEMENT(AUDIO_FORMAT_AAC),
90 DELEMENT(AUDIO_FORMAT_HE_AAC_V1),
91 DELEMENT(AUDIO_FORMAT_HE_AAC_V2),
92 DELEMENT(AUDIO_FORMAT_VORBIS),
93 DELEMENT(AUDIO_FORMAT_MAIN_MASK),
94 DELEMENT(AUDIO_FORMAT_SUB_MASK),
95 DELEMENT(AUDIO_FORMAT_PCM_16_BIT),
96 DELEMENT(AUDIO_FORMAT_PCM_8_BIT),
97 DELEMENT(AUDIO_FORMAT_PCM_32_BIT),
98 DELEMENT(AUDIO_FORMAT_PCM_8_24_BIT),
101 static int current_state = STATE_STOPPED;
103 #define SAMPLERATE 44100
104 static short sample[SAMPLERATE];
105 static uint16_t sample_pos;
107 static void init_p(int argc, const char **argv)
110 const hw_module_t *module;
111 audio_hw_device_t *device;
113 err = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, "sco", &module);
115 haltest_error("hw_get_module_by_class returned %d\n", err);
119 err = audio_hw_device_open(module, &device);
121 haltest_error("audio_hw_device_open returned %d\n", err);
125 if_audio_sco = device;
128 static int feed_from_file(short *buffer, void *data)
131 return fread(buffer, buffer_size, 1, in);
134 static int feed_from_generator(short *buffer, void *data)
144 /* buffer_size is in bytes but we are using buffer of shorts (2 bytes)*/
145 for (i = 0; i < buffer_size / sizeof(*buffer) - 1;) {
146 if (sample_pos >= SAMPLERATE)
147 sample_pos = sample_pos % SAMPLERATE;
149 /* Use the same sample for both channels */
150 buffer[i++] = sample[sample_pos] * volume;
151 buffer[i++] = sample[sample_pos] * volume;
159 static int feed_from_in(short *buffer, void *data)
161 return stream_in->read(stream_in, buffer, buffer_size_in);
164 static void prepare_sample(void)
169 haltest_info("Preparing audio sample...\n");
171 for (x = 0; x < SAMPLERATE; x++) {
172 /* prepare sinusoidal 1Hz sample */
173 s = (2.0 * 3.14159) * ((double)x / SAMPLERATE);
176 /* remap <-1, 1> to signed 16bit PCM range */
177 sample[x] = s * 32767;
183 static void mono_to_stereo_pcm16(const int16_t *in, int16_t *out, size_t samples)
187 for (i = 0; i < samples; i++) {
189 out[2 * i + 1] = in[i];
193 static void *playback_thread(void *data)
195 int (*filbuff_cb) (short*, void*);
196 short buffer[buffer_size / sizeof(short)];
197 short buffer_in[buffer_size_in / sizeof(short)];
201 void *cb_data = NULL;
204 /* Use file or fall back to generator */
206 if (data == stream_in)
207 filbuff_cb = feed_from_in;
209 filbuff_cb = feed_from_file;
214 filbuff_cb = feed_from_generator;
218 pthread_mutex_lock(&state_mutex);
219 current_state = STATE_PLAYING;
220 pthread_mutex_unlock(&state_mutex);
223 pthread_mutex_lock(&state_mutex);
225 if (current_state == STATE_STOPPING) {
226 haltest_info("Detected stopping\n");
227 pthread_mutex_unlock(&state_mutex);
229 } else if (current_state == STATE_SUSPENDED) {
230 pthread_mutex_unlock(&state_mutex);
235 pthread_mutex_unlock(&state_mutex);
237 if (data && data == stream_in) {
238 int chan_in = popcount(stream_in->common.get_channels(&stream_in->common));
239 int chan_out = popcount(stream_out->common.get_channels(&stream_out->common));
241 len = filbuff_cb(buffer_in, cb_data);
243 if (chan_in == 1 && chan_out == 2) {
244 mono_to_stereo_pcm16(buffer_in,
249 len = filbuff_cb(buffer, cb_data);
251 pthread_mutex_lock(&outstream_mutex);
253 pthread_mutex_unlock(&outstream_mutex);
257 w_len = stream_out->write(stream_out, buffer, buffer_size);
258 pthread_mutex_unlock(&outstream_mutex);
259 } while (len && w_len > 0);
261 if (in && data != stream_in)
264 pthread_mutex_lock(&state_mutex);
265 current_state = STATE_STOPPED;
266 pthread_mutex_unlock(&state_mutex);
268 haltest_info("Done playing.\n");
273 static void write_stereo_pcm16(const short *input, size_t len, FILE *out)
278 for (i = 0; i < len / 2; i++) {
279 sample[0] = input[i];
280 sample[1] = input[i];
282 fwrite(sample, sizeof(sample), 1, out);
286 static void *read_thread(void *data)
288 int (*filbuff_cb) (short*, void*) = feed_from_in;
289 short buffer[buffer_size_in / sizeof(short)];
291 void *cb_data = NULL;
294 pthread_mutex_lock(&state_mutex);
295 current_state = STATE_PLAYING;
296 pthread_mutex_unlock(&state_mutex);
299 pthread_mutex_lock(&state_mutex);
301 if (current_state == STATE_STOPPING) {
302 haltest_info("Detected stopping\n");
303 pthread_mutex_unlock(&state_mutex);
305 } else if (current_state == STATE_SUSPENDED) {
306 pthread_mutex_unlock(&state_mutex);
311 pthread_mutex_unlock(&state_mutex);
313 len = filbuff_cb(buffer, cb_data);
315 haltest_error("Error receiving SCO data");
319 haltest_info("Read %zd bytes\n", len);
322 write_stereo_pcm16(buffer, len, out);
323 haltest_info("Written %zd bytes\n", len * 2);
330 pthread_mutex_lock(&state_mutex);
331 current_state = STATE_STOPPED;
332 pthread_mutex_unlock(&state_mutex);
334 haltest_info("Done reading.\n");
339 static void play_p(int argc, const char **argv)
341 const char *fname = NULL;
344 RETURN_IF_NULL(if_audio_sco);
345 RETURN_IF_NULL(stream_out);
348 haltest_error("Invalid audio file path.\n");
349 haltest_info("Using sound generator.\n");
352 in = fopen(fname, "r");
355 haltest_error("Cannot open file: %s\n", fname);
358 haltest_info("Playing file: %s\n", fname);
361 if (buffer_size == 0) {
362 haltest_error("Invalid buffer size. Was stream_out opened?\n");
366 pthread_mutex_lock(&state_mutex);
367 if (current_state != STATE_STOPPED) {
368 haltest_error("Already playing or stream suspended!\n");
369 pthread_mutex_unlock(&state_mutex);
372 pthread_mutex_unlock(&state_mutex);
374 if (pthread_create(&play_thread, NULL, playback_thread, in) != 0) {
375 haltest_error("Cannot create playback thread!\n");
385 static void loop_p(int argc, const char **argv)
387 int chan_out, chan_in;
389 RETURN_IF_NULL(if_audio_sco);
390 RETURN_IF_NULL(stream_out);
391 RETURN_IF_NULL(stream_in);
393 chan_out = popcount(stream_out->common.get_channels(&stream_out->common));
394 chan_in = popcount(stream_in->common.get_channels(&stream_in->common));
396 if (!buffer_size || !buffer_size_in) {
397 haltest_error("Invalid buffer sizes. Streams opened\n");
401 if (buffer_size / chan_out != buffer_size_in / chan_in) {
402 haltest_error("read/write buffers differ, not supported\n");
406 pthread_mutex_lock(&state_mutex);
407 if (current_state != STATE_STOPPED) {
408 haltest_error("Already playing or stream suspended!\n");
409 pthread_mutex_unlock(&state_mutex);
412 pthread_mutex_unlock(&state_mutex);
414 if (pthread_create(&play_thread, NULL, playback_thread,
416 haltest_error("Cannot create playback thread!\n");
419 static void read_p(int argc, const char **argv)
421 const char *fname = NULL;
424 RETURN_IF_NULL(if_audio_sco);
425 RETURN_IF_NULL(stream_in);
427 pthread_mutex_lock(&state_mutex);
428 if (current_state != STATE_STOPPED) {
429 haltest_error("Already playing or stream suspended!\n");
430 pthread_mutex_unlock(&state_mutex);
433 pthread_mutex_unlock(&state_mutex);
436 haltest_error("Invalid audio file path.\n");
437 haltest_info("Using read and through away\n");
440 out = fopen(fname, "w");
442 haltest_error("Cannot open file: %s\n", fname);
446 haltest_info("Reading to file: %s\n", fname);
449 if (!buffer_size_in) {
450 haltest_error("Invalid buffer size.\n");
454 if (pthread_create(&play_thread, NULL, read_thread, out) != 0) {
455 haltest_error("Cannot create playback thread!\n");
465 static void stop_p(int argc, const char **argv)
467 RETURN_IF_NULL(if_audio_sco);
468 RETURN_IF_NULL(play_thread);
470 pthread_mutex_lock(&state_mutex);
471 if (current_state == STATE_STOPPED || current_state == STATE_STOPPING) {
472 pthread_mutex_unlock(&state_mutex);
477 pthread_mutex_lock(&outstream_mutex);
478 stream_out->common.standby(&stream_out->common);
479 pthread_mutex_unlock(&outstream_mutex);
482 current_state = STATE_STOPPING;
483 pthread_mutex_unlock(&state_mutex);
485 pthread_join(play_thread, NULL);
488 haltest_info("Ended %s\n", __func__);
491 static void open_output_stream_p(int argc, const char **argv)
493 struct audio_config *config;
496 RETURN_IF_NULL(if_audio_sco);
498 pthread_mutex_lock(&state_mutex);
499 if (current_state == STATE_PLAYING) {
500 haltest_error("Already playing!\n");
501 pthread_mutex_unlock(&state_mutex);
504 pthread_mutex_unlock(&state_mutex);
507 haltest_info("No sampling rate specified. Use default conf\n");
510 config = calloc(1, sizeof(struct audio_config));
514 config->sample_rate = atoi(argv[2]);
515 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
516 config->format = AUDIO_FORMAT_PCM_16_BIT;
519 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
520 err = if_audio_sco->open_output_stream(if_audio_sco,
522 AUDIO_DEVICE_OUT_ALL_SCO,
523 AUDIO_OUTPUT_FLAG_NONE,
527 err = if_audio_sco->open_output_stream(if_audio_sco,
529 AUDIO_DEVICE_OUT_ALL_SCO,
530 AUDIO_OUTPUT_FLAG_NONE,
535 haltest_error("open output stream returned %d\n", err);
539 buffer_size = stream_out->common.get_buffer_size(&stream_out->common);
540 if (buffer_size == 0)
541 haltest_error("Invalid buffer size received!\n");
543 haltest_info("Using buffer size: %zu\n", buffer_size);
549 static void close_output_stream_p(int argc, const char **argv)
551 RETURN_IF_NULL(if_audio_sco);
552 RETURN_IF_NULL(stream_out);
557 if_audio_sco->close_output_stream(if_audio_sco, stream_out);
563 static void open_input_stream_p(int argc, const char **argv)
565 struct audio_config *config;
568 RETURN_IF_NULL(if_audio_sco);
570 pthread_mutex_lock(&state_mutex);
571 if (current_state == STATE_PLAYING) {
572 haltest_error("Already playing!\n");
573 pthread_mutex_unlock(&state_mutex);
576 pthread_mutex_unlock(&state_mutex);
579 haltest_info("No sampling rate specified. Use default conf\n");
582 config = calloc(1, sizeof(struct audio_config));
586 config->sample_rate = atoi(argv[2]);
587 config->channel_mask = AUDIO_CHANNEL_OUT_MONO;
588 config->format = AUDIO_FORMAT_PCM_16_BIT;
591 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
592 err = if_audio_sco->open_input_stream(if_audio_sco,
594 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
596 &stream_in, 0, NULL, 0);
598 err = if_audio_sco->open_input_stream(if_audio_sco,
600 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
605 haltest_error("open output stream returned %d\n", err);
609 buffer_size_in = stream_in->common.get_buffer_size(&stream_in->common);
610 if (buffer_size_in == 0)
611 haltest_error("Invalid buffer size received!\n");
613 haltest_info("Using buffer size: %zu\n", buffer_size_in);
619 static void close_input_stream_p(int argc, const char **argv)
621 RETURN_IF_NULL(if_audio_sco);
622 RETURN_IF_NULL(stream_in);
627 if_audio_sco->close_input_stream(if_audio_sco, stream_in);
633 static void cleanup_p(int argc, const char **argv)
637 RETURN_IF_NULL(if_audio_sco);
639 pthread_mutex_lock(&state_mutex);
640 if (current_state != STATE_STOPPED) {
641 pthread_mutex_unlock(&state_mutex);
642 close_output_stream_p(0, NULL);
644 pthread_mutex_unlock(&state_mutex);
647 err = audio_hw_device_close(if_audio_sco);
649 haltest_error("audio_hw_device_close returned %d\n", err);
656 static void suspend_p(int argc, const char **argv)
658 RETURN_IF_NULL(if_audio_sco);
659 RETURN_IF_NULL(stream_out);
661 pthread_mutex_lock(&state_mutex);
662 if (current_state != STATE_PLAYING) {
663 pthread_mutex_unlock(&state_mutex);
666 current_state = STATE_SUSPENDED;
667 pthread_mutex_unlock(&state_mutex);
669 pthread_mutex_lock(&outstream_mutex);
670 stream_out->common.standby(&stream_out->common);
671 pthread_mutex_unlock(&outstream_mutex);
674 static void resume_p(int argc, const char **argv)
676 RETURN_IF_NULL(if_audio_sco);
677 RETURN_IF_NULL(stream_out);
679 pthread_mutex_lock(&state_mutex);
680 if (current_state == STATE_SUSPENDED)
681 current_state = STATE_PLAYING;
682 pthread_mutex_unlock(&state_mutex);
685 static void get_latency_p(int argc, const char **argv)
687 RETURN_IF_NULL(if_audio_sco);
688 RETURN_IF_NULL(stream_out);
690 haltest_info("Output audio stream latency: %d\n",
691 stream_out->get_latency(stream_out));
694 static void get_buffer_size_p(int argc, const char **argv)
696 RETURN_IF_NULL(if_audio_sco);
697 RETURN_IF_NULL(stream_out);
699 haltest_info("Current output buffer size: %zu\n",
700 stream_out->common.get_buffer_size(&stream_out->common));
703 static void get_channels_p(int argc, const char **argv)
705 audio_channel_mask_t channels;
707 RETURN_IF_NULL(if_audio_sco);
708 RETURN_IF_NULL(stream_out);
710 channels = stream_out->common.get_channels(&stream_out->common);
712 haltest_info("Channels: %s\n", audio_channel_mask_t2str(channels));
715 static void get_format_p(int argc, const char **argv)
717 audio_format_t format;
719 RETURN_IF_NULL(if_audio_sco);
720 RETURN_IF_NULL(stream_out);
722 format = stream_out->common.get_format(&stream_out->common);
724 haltest_info("Format: %s\n", audio_format_t2str(format));
727 static void get_sample_rate_p(int argc, const char **argv)
729 RETURN_IF_NULL(if_audio_sco);
730 RETURN_IF_NULL(stream_out);
732 haltest_info("Current sample rate: %d\n",
733 stream_out->common.get_sample_rate(&stream_out->common));
736 static void get_parameters_p(int argc, const char **argv)
740 RETURN_IF_NULL(if_audio_sco);
741 RETURN_IF_NULL(stream_out);
744 haltest_info("No keys given.\n");
750 haltest_info("Current parameters: %s\n",
751 stream_out->common.get_parameters(&stream_out->common,
755 static void set_parameters_p(int argc, const char **argv)
757 RETURN_IF_NULL(if_audio_sco);
758 RETURN_IF_NULL(stream_out);
761 haltest_error("No key=value; pairs given.\n");
765 stream_out->common.set_parameters(&stream_out->common, argv[2]);
768 static void set_sample_rate_p(int argc, const char **argv)
770 RETURN_IF_NULL(if_audio_sco);
771 RETURN_IF_NULL(stream_out);
776 stream_out->common.set_sample_rate(&stream_out->common, atoi(argv[2]));
779 static void init_check_p(int argc, const char **argv)
781 RETURN_IF_NULL(if_audio_sco);
783 haltest_info("Init check result: %d\n",
784 if_audio_sco->init_check(if_audio_sco));
787 static struct method methods[] = {
790 STD_METHODH(open_output_stream, "sample_rate"),
791 STD_METHOD(close_output_stream),
792 STD_METHODH(open_input_stream, "sampling rate"),
793 STD_METHOD(close_input_stream),
794 STD_METHODH(play, "<path to pcm file>"),
800 STD_METHOD(get_latency),
801 STD_METHOD(get_buffer_size),
802 STD_METHOD(get_channels),
803 STD_METHOD(get_format),
804 STD_METHOD(get_sample_rate),
805 STD_METHODH(get_parameters, "<closing>"),
806 STD_METHODH(set_parameters, "<closing=value>"),
807 STD_METHODH(set_sample_rate, "<sample rate>"),
808 STD_METHOD(init_check),
812 const struct interface sco_if = {