2 This file is part of PulseAudio.
4 Copyright (c) 2021 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"
50 PA_MODULE_AUTHOR("Tizen");
51 PA_MODULE_DESCRIPTION("Tizen Audio Sink");
52 PA_MODULE_VERSION(PACKAGE_VERSION);
53 PA_MODULE_LOAD_ONCE(false);
55 "sink_name=<name of sink> "
56 "sink_properties=<properties for the sink> "
57 "device=<device to use, card comma device (e.g. 0,0)> "
58 "format=<sample format> "
60 "channels=<number of channels> "
61 "channel_map=<channel map>"
62 "fragments=<number of fragments> "
63 "fragment_size=<fragment size> "
64 "block_msec=<sink keep> "
65 "max_request_msec=<maximum buffer request of device> ");
67 #define DEFAULT_SINK_NAME "tizenaudio-sink"
69 /* block_usec should be less than amount of fragment_size * fragments */
70 #define DEFAULT_BLOCK_USEC (PA_USEC_PER_SEC * 0.05)
72 /* Sink device consumes that amount of maximum buffer at every request */
73 #define DEFAULT_MAX_REQUEST_USEC (PA_USEC_PER_SEC * 0.032)
75 #define DEVICE_NAME_MAX 30
83 pa_thread_mq thread_mq;
91 pa_usec_t max_request_usec;
93 pa_usec_t timestamp_written;
99 pa_rtpoll_item *rtpoll_item;
101 uint64_t write_count;
102 pa_hal_interface *hal_interface;
105 static const char* const valid_modargs[] = {
120 static int build_pollfd(struct userdata *u) {
122 struct pollfd *pollfd;
126 pa_assert(u->pcm_handle);
127 pa_assert(u->rtpoll);
130 pa_rtpoll_item_free(u->rtpoll_item);
132 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
133 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
134 ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
135 if (ret < 0 || fd < 0) {
136 pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
140 pollfd->events = /* POLLOUT | */ POLLERR | POLLNVAL;
145 /* Called from IO context */
146 static int suspend(struct userdata *u) {
149 pa_assert(u->pcm_handle);
151 ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
153 pa_log_error("Error closing PCM device %x", ret);
155 u->pcm_handle = NULL;
157 if (u->rtpoll_item) {
158 pa_rtpoll_item_free(u->rtpoll_item);
159 u->rtpoll_item = NULL;
162 pa_log_info("Device suspended...");
167 /* Called from IO context */
168 static int unsuspend(struct userdata *u) {
169 pa_sample_spec sample_spec;
174 pa_assert(!u->pcm_handle);
176 pa_log_info("Trying resume...");
178 sample_spec = u->sink->sample_spec;
179 frame_size = pa_frame_size(&sample_spec);
180 if (frame_size == 0) {
181 pa_log_error("Unexpected frame size zero!");
185 ret = pa_hal_interface_pcm_open(u->hal_interface,
190 u->frag_size / frame_size,
192 (void **)&u->pcm_handle);
194 pa_log_error("Error opening PCM device %x", ret);
198 if (build_pollfd(u) < 0)
204 pa_log_info("Resumed successfully...");
210 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
211 u->pcm_handle = NULL;
216 /* Called from the IO thread. */
217 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) {
222 pa_assert_se(u = s->userdata);
224 /* It may be that only the suspend cause is changing, in which case there's
226 if (new_state == s->thread_info.state)
230 case PA_SINK_SUSPENDED: {
231 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
232 if ((r = suspend(u)) < 0)
238 case PA_SINK_RUNNING: {
239 if (s->thread_info.state == PA_SINK_INIT) {
240 if (build_pollfd(u) < 0)
244 if (s->thread_info.state == PA_SINK_SUSPENDED) {
245 if ((r = unsuspend(u)) < 0)
251 case PA_SINK_UNLINKED:
253 case PA_SINK_INVALID_STATE:
260 static int sink_process_msg(
265 pa_memchunk *chunk) {
267 struct userdata *u = PA_SINK(o)->userdata;
270 case PA_SINK_MESSAGE_GET_LATENCY: {
271 pa_usec_t now = pa_rtclock_now();
272 pa_usec_t latency = 0ULL;
273 if (u->timestamp > now) {
274 if ((u->timestamp - now) > (now - u->timestamp_written)) {
275 latency = (u->timestamp - now) + (u->timestamp_written - now);
278 *((pa_usec_t*)data) = latency;
283 return pa_sink_process_msg(o, code, data, offset, chunk);
286 static void sink_update_requested_latency_cb(pa_sink *s) {
289 pa_sink_assert_ref(s);
290 pa_assert_se((u = s->userdata));
292 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
294 if (u->block_usec == (pa_usec_t)-1)
295 u->block_usec = s->thread_info.max_latency;
297 pa_sink_set_max_rewind_within_thread(s, pa_usec_to_bytes(u->block_usec, &s->sample_spec));
298 pa_sink_set_max_request_within_thread(s, pa_usec_to_bytes(u->max_request_usec, &s->sample_spec));
301 static void process_rewind(struct userdata *u, pa_usec_t now) {
303 /* Rewind not supported */
304 pa_sink_process_rewind(u->sink, 0);
306 size_t rewind_nbytes, in_buffer;
311 rewind_nbytes = u->sink->thread_info.rewind_nbytes;
313 if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
316 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
318 if (u->timestamp <= now)
321 delay = u->timestamp - now;
322 in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
327 if (rewind_nbytes > in_buffer)
328 rewind_nbytes = in_buffer;
330 pa_sink_process_rewind(u->sink, rewind_nbytes);
331 u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
333 pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
337 pa_sink_process_rewind(u->sink, 0);
341 static int process_render(struct userdata *u, pa_usec_t now) {
345 size_t frames_to_write, frame_size;
350 /* Fill the buffer up the latency size */
351 while (u->timestamp < now + u->block_usec) {
353 frame_size = pa_frame_size(&u->sink->sample_spec);
354 if (frame_size == 0) {
355 pa_log_error("Unexpected frame size zero!");
359 pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
360 if ((avail == 0) && !(u->first)) {
365 pa_log_debug("Fill initial buffer");
367 frames_to_write = u->frag_size / frame_size;
368 u->timestamp = u->timestamp_written = now;
370 frames_to_write = (u->frag_size / u->nfrags);
372 if (frames_to_write > avail)
376 pa_sink_render_full(u->sink, frames_to_write * frame_size, &chunk);
377 p = pa_memblock_acquire(chunk.memblock);
379 pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write);
381 pa_memblock_release(chunk.memblock);
382 pa_memblock_unref(chunk.memblock);
383 u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
384 u->timestamp_written = pa_rtclock_now();
389 if (ate >= u->sink->thread_info.max_request) {
397 static void thread_func(void *userdata) {
398 struct userdata *u = userdata;
399 unsigned short revents = 0;
403 pa_log_debug("Thread starting up");
405 if (u->core->realtime_scheduling)
406 pa_thread_make_realtime(u->core->realtime_priority);
408 pa_thread_mq_install(&u->thread_mq);
414 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
415 now = pa_rtclock_now();
417 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
418 process_rewind(u, now);
420 /* Render some data and drop it immediately */
421 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
422 int work_done = process_render(u, now);
427 if (work_done == 0) {
428 pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
431 pa_log_info("Starting playback.");
432 pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
435 pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
438 pa_rtpoll_set_timer_disabled(u->rtpoll);
441 /* Hmm, nothing to do. Let's sleep */
442 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
448 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
449 struct pollfd *pollfd;
450 if (u->rtpoll_item) {
451 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
452 revents = pollfd->revents;
453 if (revents & ~POLLOUT) {
454 pa_log_debug("Poll error 0x%x occured, try recover.", revents);
455 pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
459 //pa_log_debug("Poll wakeup.", revents);
466 /* If this was no regular exit from the loop we have to continue
467 * processing messages until we received PA_MESSAGE_SHUTDOWN */
468 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
469 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
472 pa_log_debug("Thread shutting down");
475 static int parse_to_get_card(const char *modarg_device, char *card) {
479 if (!strchr(modarg_device, ',')) {
480 pa_log_error("Failed to parse device argument : no comma");
484 name_p = modarg_device;
486 while (*name_p != ',')
487 *(card_p++) = *(name_p++);
493 static int parse_to_get_device(const char *modarg_device, char *device) {
497 if (!(comma_p = strchr(modarg_device, ','))) {
498 pa_log_error("Failed to parse device argument : no comma");
504 while (*comma_p != '\0')
505 *(device_p++) = *(comma_p++);
511 int pa__init(pa_module*m) {
512 struct userdata *u = NULL;
515 pa_modargs *ma = NULL;
516 pa_sink_new_data data;
517 uint32_t alternate_sample_rate;
519 uint32_t max_request_msec;
520 const char *modarg_device;
521 char card[DEVICE_NAME_MAX];
522 char device[DEVICE_NAME_MAX];
523 size_t frame_size, buffer_size, period_frames, buffer_frames;
527 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
528 pa_log_error("Failed to parse module arguments.");
532 ss = m->core->default_sample_spec;
533 map = m->core->default_channel_map;
534 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
535 pa_log_error("Invalid sample format specification or channel map");
539 alternate_sample_rate = m->core->alternate_sample_rate;
540 if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
541 pa_log_error("Failed to parse alternate sample rate");
545 m->userdata = u = pa_xnew0(struct userdata, 1);
549 u->hal_interface = pa_hal_interface_get(u->core);
550 u->rtpoll = pa_rtpoll_new();
551 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
553 if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
554 pa_log_error("device is invalid");
558 if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
559 pa_log_error("failed to parse device module argument, %s", modarg_device);
563 u->card = pa_xstrdup(card);
564 u->device = pa_xstrdup(device);
566 u->frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
567 u->nfrags = m->core->default_n_fragments;
568 if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
569 pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
570 pa_log_error("fragment_size or fragments are invalid.");
573 pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
576 block_msec = DEFAULT_BLOCK_USEC / PA_USEC_PER_MSEC;
577 pa_modargs_get_value_u32(ma, "block_msec", &block_msec);
578 u->block_usec = (pa_usec_t) block_msec * PA_USEC_PER_MSEC;
581 max_request_msec = DEFAULT_MAX_REQUEST_USEC / PA_USEC_PER_MSEC;
582 pa_modargs_get_value_u32(ma, "max_request_msec", &max_request_msec);
583 u->max_request_usec = (pa_usec_t) max_request_msec * PA_USEC_PER_MSEC;
586 u->timestamp_written = 0ULL;
588 pa_sink_new_data_init(&data);
589 data.driver = __FILE__;
591 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
592 pa_sink_new_data_set_sample_spec(&data, &ss);
593 pa_sink_new_data_set_channel_map(&data, &map);
594 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio sink"));
595 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
596 pa_proplist_sets(data.proplist, "tizen.card", u->card);
597 pa_proplist_sets(data.proplist, "tizen.device", u->device);
598 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
600 frame_size = pa_frame_size(&ss);
601 buffer_size = u->frag_size * u->nfrags;
602 buffer_frames = buffer_size / frame_size;
603 period_frames = u->frag_size / frame_size;
604 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
605 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
607 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
608 pa_log_error("Invalid properties.");
609 pa_sink_new_data_done(&data);
613 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
614 pa_sink_new_data_done(&data);
617 pa_log_error("Failed to create sink object.");
621 u->sink->parent.process_msg = sink_process_msg;
622 u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
623 u->sink->update_requested_latency = sink_update_requested_latency_cb;
624 u->sink->userdata = u;
626 if (pa_hal_interface_pcm_open(u->hal_interface,
630 &u->sink->sample_spec,
631 u->frag_size / pa_frame_size(&u->sink->sample_spec),
633 (void **)&u->pcm_handle)) {
634 pa_log_error("Error opening PCM device");
638 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
639 pa_sink_set_rtpoll(u->sink, u->rtpoll);
641 pa_sink_set_max_rewind(u->sink, 0);
642 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->max_request_usec, &u->sink->sample_spec));
644 if (!(u->thread = pa_thread_new("tizenaudio-sink", thread_func, u))) {
645 pa_log_error("Failed to create thread.");
648 pa_sink_set_fixed_latency(u->sink, u->block_usec);
649 pa_sink_put(u->sink);
662 int pa__get_n_used(pa_module *m) {
666 pa_assert_se((u = m->userdata));
668 return pa_sink_linked_by(u->sink);
671 void pa__done(pa_module*m) {
676 if (!(u = m->userdata))
680 pa_sink_unlink(u->sink);
683 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
684 pa_thread_free(u->thread);
687 pa_thread_mq_done(&u->thread_mq);
690 pa_sink_unref(u->sink);
696 pa_rtpoll_free(u->rtpoll);
699 pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
700 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);