2 This file is part of PulseAudio.
4 Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/rtclock.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
36 #include <pulsecore/i18n.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/thread.h>
44 #include <pulsecore/thread-mq.h>
45 #include <pulsecore/rtpoll.h>
46 #include <pulsecore/poll.h>
48 #include "hal-interface.h"
51 #include "aec-manager.h"
54 PA_MODULE_AUTHOR("Tizen");
55 PA_MODULE_DESCRIPTION("Tizen Audio Sink");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(false);
59 "sink_name=<name of sink> "
60 "sink_properties=<properties for the sink> "
61 "device=<device to use, card comma device (e.g. 0,0)> "
62 "format=<sample format> "
64 "channels=<number of channels> "
65 "channel_map=<channel map>"
66 "fragments=<number of fragments> "
67 "fragment_size=<fragment size> "
68 "block_msec=<sink keep> "
69 "max_request_msec=<maximum buffer request of device> ");
71 #define DEFAULT_SINK_NAME "tizenaudio-sink"
73 /* block_usec should be less than amount of fragment_size * fragments */
74 #define DEFAULT_BLOCK_USEC (PA_USEC_PER_SEC * 0.05)
76 /* Sink device consumes that amount of maximum buffer at every request */
77 #define DEFAULT_MAX_REQUEST_USEC (PA_USEC_PER_SEC * 0.032)
79 #define DEVICE_NAME_MAX 30
83 PA_SINK_MESSAGE_SET_AEC_STATE = PA_SINK_MESSAGE_MAX,
84 PA_SINK_MESSAGE_GET_AEC_PARAMS,
94 pa_thread_mq thread_mq;
101 pa_usec_t block_usec;
102 pa_usec_t max_request_usec;
104 pa_usec_t timestamp_written;
110 pa_rtpoll_item *rtpoll_item;
112 uint64_t write_count;
113 pa_hal_interface *hal_interface;
121 static const char* const valid_modargs[] = {
136 static int build_pollfd(struct userdata *u) {
138 struct pollfd *pollfd;
142 pa_assert(u->pcm_handle);
143 pa_assert(u->rtpoll);
146 pa_rtpoll_item_free(u->rtpoll_item);
148 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
149 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
150 ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
151 if (ret < 0 || fd < 0) {
152 pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
156 pollfd->events = /* POLLOUT | */ POLLERR | POLLNVAL;
161 /* Called from IO context */
162 static int suspend(struct userdata *u) {
165 pa_assert(u->pcm_handle);
167 ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
169 pa_log_error("Error closing PCM device %x", ret);
171 u->pcm_handle = NULL;
173 if (u->rtpoll_item) {
174 pa_rtpoll_item_free(u->rtpoll_item);
175 u->rtpoll_item = NULL;
178 pa_log_info("Device suspended...");
183 /* Called from IO context */
184 static int unsuspend(struct userdata *u) {
185 pa_sample_spec sample_spec;
189 char *card = u->card;
190 char *device = u->device;
193 pa_assert(!u->pcm_handle);
195 pa_log_info("Trying resume...");
197 sample_spec = u->sink->sample_spec;
198 frame_size = pa_frame_size(&sample_spec);
199 if (frame_size == 0) {
200 pa_log_error("Unexpected frame size zero!");
211 ret = pa_hal_interface_pcm_open(u->hal_interface,
216 u->frag_size / frame_size,
218 (void **)&u->pcm_handle);
220 pa_log_error("Error opening PCM device %x", ret);
224 if (build_pollfd(u) < 0)
230 pa_log_info("Resumed successfully...device(%s:%s)", card, device);
236 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
237 u->pcm_handle = NULL;
242 /* Called from the IO thread. */
243 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
248 pa_assert_se(u = s->userdata);
250 /* It may be that only the suspend cause is changing, in which case there's
252 if (new_state == s->thread_info.state)
256 case PA_SINK_SUSPENDED: {
257 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
258 if ((r = suspend(u)) < 0)
264 case PA_SINK_RUNNING: {
265 if (s->thread_info.state == PA_SINK_INIT) {
266 if (build_pollfd(u) < 0)
270 if (s->thread_info.state == PA_SINK_SUSPENDED) {
271 if ((r = unsuspend(u)) < 0)
277 case PA_SINK_UNLINKED:
279 case PA_SINK_INVALID_STATE:
286 static int sink_process_msg(
291 pa_memchunk *chunk) {
293 struct userdata *u = PA_SINK(o)->userdata;
296 case PA_SINK_MESSAGE_GET_LATENCY: {
297 pa_usec_t now = pa_rtclock_now();
298 pa_usec_t latency = 0ULL;
299 if (u->timestamp > now) {
300 if ((u->timestamp - now) > (now - u->timestamp_written)) {
301 latency = (u->timestamp - now) + (u->timestamp_written - now);
304 *((pa_usec_t*)data) = latency;
308 case PA_SINK_MESSAGE_SET_AEC_STATE: {
309 pa_sink *s = PA_SINK(o);
310 bool enable = (bool)data;
312 if (u->aec_enable == enable)
315 pa_log_info("AEC enable(%d)", enable);
317 u->aec_enable = enable;
318 if (s->thread_info.state == PA_SINK_RUNNING) {
325 case PA_SINK_MESSAGE_GET_AEC_PARAMS: {
326 aec_params_t *params = (aec_params_t *)data;
327 params->rate = u->ss.rate;
328 params->channels = u->ss.channels;
329 params->format = u->ss.format;
330 params->frag_size = u->frag_size;
331 params->nfrags = u->nfrags;
332 snprintf(params->card, DEVICE_NAME_MAX, "%s", u->card);
333 snprintf(params->device, DEVICE_NAME_MAX, "%s", u->device);
339 return pa_sink_process_msg(o, code, data, offset, chunk);
342 static void sink_update_requested_latency_cb(pa_sink *s) {
345 pa_sink_assert_ref(s);
346 pa_assert_se((u = s->userdata));
348 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
350 if (u->block_usec == (pa_usec_t)-1)
351 u->block_usec = s->thread_info.max_latency;
353 pa_sink_set_max_rewind_within_thread(s, pa_usec_to_bytes(u->block_usec, &s->sample_spec));
354 pa_sink_set_max_request_within_thread(s, pa_usec_to_bytes(u->max_request_usec, &s->sample_spec));
357 static void process_rewind(struct userdata *u, pa_usec_t now) {
359 /* Rewind not supported */
360 pa_sink_process_rewind(u->sink, 0);
362 size_t rewind_nbytes, in_buffer;
367 rewind_nbytes = u->sink->thread_info.rewind_nbytes;
369 if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
372 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
374 if (u->timestamp <= now)
377 delay = u->timestamp - now;
378 in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
383 if (rewind_nbytes > in_buffer)
384 rewind_nbytes = in_buffer;
386 pa_sink_process_rewind(u->sink, rewind_nbytes);
387 u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
389 pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
393 pa_sink_process_rewind(u->sink, 0);
397 static int process_render(struct userdata *u, pa_usec_t now) {
401 size_t frames_to_write, frame_size;
406 /* Fill the buffer up the latency size */
407 while (u->timestamp < now + u->block_usec) {
409 frame_size = pa_frame_size(&u->sink->sample_spec);
410 if (frame_size == 0) {
411 pa_log_error("Unexpected frame size zero!");
415 pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
416 if ((avail == 0) && !(u->first)) {
421 pa_log_debug("Fill initial buffer");
422 frames_to_write = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec) / frame_size;
423 u->timestamp = u->timestamp_written = now;
425 /* Write pcm every 10ms */
426 frames_to_write = pa_usec_to_bytes((10 * PA_USEC_PER_MSEC), &u->sink->sample_spec) / frame_size;
427 if (frames_to_write > avail)
431 pa_sink_render_full(u->sink, frames_to_write * frame_size, &chunk);
432 p = pa_memblock_acquire(chunk.memblock);
434 pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write);
436 pa_memblock_release(chunk.memblock);
437 pa_memblock_unref(chunk.memblock);
438 u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
439 u->timestamp_written = pa_rtclock_now();
444 if (ate >= u->sink->thread_info.max_request) {
452 static void thread_func(void *userdata) {
453 struct userdata *u = userdata;
454 unsigned short revents = 0;
458 pa_log_debug("Thread starting up");
460 if (u->core->realtime_scheduling)
461 pa_thread_make_realtime(u->core->realtime_priority);
463 pa_thread_mq_install(&u->thread_mq);
469 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
470 now = pa_rtclock_now();
472 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
473 process_rewind(u, now);
475 /* Render some data and drop it immediately */
476 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
477 int work_done = process_render(u, now);
482 if (work_done == 0) {
483 pa_rtpoll_set_timer_relative(u->rtpoll, (10 * PA_USEC_PER_MSEC));
486 pa_log_info("Starting playback.");
487 pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
490 pa_rtpoll_set_timer_relative(u->rtpoll, (10 * PA_USEC_PER_MSEC));
493 pa_rtpoll_set_timer_disabled(u->rtpoll);
496 /* Hmm, nothing to do. Let's sleep */
497 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
503 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
504 struct pollfd *pollfd;
505 if (u->rtpoll_item) {
506 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
507 revents = pollfd->revents;
508 if (revents & ~POLLOUT) {
509 pa_log_debug("Poll error 0x%x occured, try recover.", revents);
510 pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
514 //pa_log_debug("Poll wakeup.", revents);
521 /* If this was no regular exit from the loop we have to continue
522 * processing messages until we received PA_MESSAGE_SHUTDOWN */
523 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
524 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
527 pa_log_debug("Thread shutting down");
530 static int parse_to_get_card(const char *modarg_device, char *card) {
534 if (!strchr(modarg_device, ',')) {
535 pa_log_error("Failed to parse device argument : no comma");
539 name_p = modarg_device;
541 while (*name_p != ',')
542 *(card_p++) = *(name_p++);
548 static int parse_to_get_device(const char *modarg_device, char *device) {
552 if (!(comma_p = strchr(modarg_device, ','))) {
553 pa_log_error("Failed to parse device argument : no comma");
559 while (*comma_p != '\0')
560 *(device_p++) = *(comma_p++);
566 int pa__init(pa_module*m) {
567 struct userdata *u = NULL;
570 pa_modargs *ma = NULL;
571 pa_sink_new_data data;
572 uint32_t alternate_sample_rate;
574 uint32_t max_request_msec;
575 const char *modarg_device;
576 char card[DEVICE_NAME_MAX];
577 char device[DEVICE_NAME_MAX];
578 size_t frame_size, buffer_size, period_frames, buffer_frames;
582 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
583 pa_log_error("Failed to parse module arguments.");
587 ss = m->core->default_sample_spec;
588 map = m->core->default_channel_map;
589 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
590 pa_log_error("Invalid sample format specification or channel map");
594 alternate_sample_rate = m->core->alternate_sample_rate;
595 if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
596 pa_log_error("Failed to parse alternate sample rate");
600 m->userdata = u = pa_xnew0(struct userdata, 1);
604 u->hal_interface = pa_hal_interface_get(u->core);
605 u->rtpoll = pa_rtpoll_new();
609 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
611 if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
612 pa_log_error("device is invalid");
616 if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
617 pa_log_error("failed to parse device module argument, %s", modarg_device);
621 u->card = pa_xstrdup(card);
622 u->device = pa_xstrdup(device);
624 u->frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
625 u->nfrags = m->core->default_n_fragments;
626 if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
627 pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
628 pa_log_error("fragment_size or fragments are invalid.");
631 pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
634 block_msec = DEFAULT_BLOCK_USEC / PA_USEC_PER_MSEC;
635 pa_modargs_get_value_u32(ma, "block_msec", &block_msec);
636 u->block_usec = (pa_usec_t) block_msec * PA_USEC_PER_MSEC;
639 max_request_msec = DEFAULT_MAX_REQUEST_USEC / PA_USEC_PER_MSEC;
640 pa_modargs_get_value_u32(ma, "max_request_msec", &max_request_msec);
641 u->max_request_usec = (pa_usec_t) max_request_msec * PA_USEC_PER_MSEC;
644 u->timestamp_written = 0ULL;
646 pa_sink_new_data_init(&data);
647 data.driver = __FILE__;
649 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
650 pa_sink_new_data_set_sample_spec(&data, &ss);
651 pa_sink_new_data_set_channel_map(&data, &map);
652 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio sink"));
653 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
654 pa_proplist_sets(data.proplist, "tizen.card", u->card);
655 pa_proplist_sets(data.proplist, "tizen.device", u->device);
656 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
658 frame_size = pa_frame_size(&ss);
659 buffer_size = u->frag_size * u->nfrags;
660 buffer_frames = buffer_size / frame_size;
661 period_frames = u->frag_size / frame_size;
662 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
663 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
665 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
666 pa_log_error("Invalid properties.");
667 pa_sink_new_data_done(&data);
671 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
672 pa_sink_new_data_done(&data);
675 pa_log_error("Failed to create sink object.");
679 u->sink->parent.process_msg = sink_process_msg;
680 u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
681 u->sink->update_requested_latency = sink_update_requested_latency_cb;
682 u->sink->userdata = u;
684 if (pa_hal_interface_pcm_open(u->hal_interface,
688 &u->sink->sample_spec,
689 u->frag_size / pa_frame_size(&u->sink->sample_spec),
691 (void **)&u->pcm_handle)) {
692 pa_log_error("Error opening PCM device");
696 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
697 pa_sink_set_rtpoll(u->sink, u->rtpoll);
699 pa_sink_set_max_rewind(u->sink, 0);
700 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->max_request_usec, &u->sink->sample_spec));
702 if (!(u->thread = pa_thread_new("tizenaudio-sink", thread_func, u))) {
703 pa_log_error("Failed to create thread.");
706 pa_sink_set_fixed_latency(u->sink, u->block_usec);
707 pa_sink_put(u->sink);
720 int pa__get_n_used(pa_module *m) {
724 pa_assert_se((u = m->userdata));
726 return pa_sink_linked_by(u->sink);
729 void pa__done(pa_module*m) {
734 if (!(u = m->userdata))
738 pa_sink_unlink(u->sink);
741 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
742 pa_thread_free(u->thread);
745 pa_thread_mq_done(&u->thread_mq);
748 pa_sink_unref(u->sink);
754 pa_rtpoll_free(u->rtpoll);
757 pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
758 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);