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"
49 #include "echo-cancel/echo-cancel-def.h"
51 PA_MODULE_AUTHOR("Tizen");
52 PA_MODULE_DESCRIPTION("Tizen Audio Sink2");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(false);
56 "sink_name=<name of sink> "
57 "sink_properties=<properties for the sink> "
58 "device=<device to use, card comma device (e.g. 0,0)> "
59 "format=<sample format> "
61 "channels=<number of channels> "
62 "channel_map=<channel map>"
63 "fragments=<number of fragments> "
64 "fragment_size=<fragment size> ");
66 #define DEFAULT_SINK_NAME "tizenaudio-sink2"
68 #define DEVICE_NAME_MAX 30
69 #define DEFAULT_FRAGMENT_MSEC 20
70 #define DEFAULT_FRAGMENTS 4
78 pa_thread_mq thread_mq;
91 pa_rtpoll_item *rtpoll_item;
94 pa_hal_interface *hal_interface;
96 pa_msgobject *ec_object;
97 pa_asyncmsgq *ec_asyncmsgq;
98 pa_rtpoll_item *ec_poll_item;
101 static const char* const valid_modargs[] = {
114 static int build_pollfd(struct userdata *u) {
116 struct pollfd *pollfd;
120 pa_assert(u->pcm_handle);
121 pa_assert(u->rtpoll);
124 pa_rtpoll_item_free(u->rtpoll_item);
126 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
127 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
128 ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
129 if (ret < 0 || fd < 0) {
130 pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
134 pollfd->events = POLLOUT | POLLERR | POLLNVAL;
139 /* Called from IO context */
140 static int suspend(struct userdata *u) {
143 pa_assert(u->pcm_handle);
145 ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
147 pa_log_error("Error closing PCM device %x", ret);
149 u->pcm_handle = NULL;
151 if (u->rtpoll_item) {
152 pa_rtpoll_item_free(u->rtpoll_item);
153 u->rtpoll_item = NULL;
156 pa_log_info("Device suspended...");
161 /* Called from IO context */
162 static int unsuspend(struct userdata *u) {
163 pa_sample_spec sample_spec;
168 pa_assert(!u->pcm_handle);
170 pa_log_info("Trying resume...");
172 sample_spec = u->sink->sample_spec;
173 frame_size = pa_frame_size(&sample_spec);
174 if (frame_size == 0) {
175 pa_log_error("Unexpected frame size zero!");
179 ret = pa_hal_interface_pcm_open(u->hal_interface,
184 u->frag_size / frame_size,
186 (void **)&u->pcm_handle);
188 pa_log_error("Error opening PCM device %x", ret);
192 if (build_pollfd(u) < 0)
197 u->timestamp = pa_rtclock_now();
199 pa_log_info("Resumed successfully...");
205 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
206 u->pcm_handle = NULL;
211 /* Called from the IO thread. */
212 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) {
217 pa_assert_se(u = s->userdata);
219 /* It may be that only the suspend cause is changing, in which case there's
221 if (new_state == s->thread_info.state)
225 case PA_SINK_SUSPENDED: {
226 pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
227 if ((r = suspend(u)) < 0)
233 case PA_SINK_RUNNING: {
234 if (s->thread_info.state == PA_SINK_INIT) {
235 if (build_pollfd(u) < 0)
239 if (s->thread_info.state == PA_SINK_SUSPENDED) {
240 if ((r = unsuspend(u)) < 0)
246 case PA_SINK_UNLINKED:
248 case PA_SINK_INVALID_STATE:
255 static int sink_process_msg(
260 pa_memchunk *chunk) {
262 struct userdata *u = PA_SINK(o)->userdata;
265 case PA_SINK_MESSAGE_SET_AEC_STATE: {
267 pa_log_info("EC state changed (%d)", u->echo_on);
270 case PA_SINK_MESSAGE_GET_LATENCY: {
274 r = u->timestamp + pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - pa_rtclock_now();
276 *((int64_t *) data) = r;
280 case PA_SINK_MESSAGE_REBUILD_RTPOLL: {
286 args = (struct arguments *)data;
289 u->ec_object = args->o;
290 u->ec_asyncmsgq = args->q;
291 u->ec_poll_item = pa_rtpoll_item_new_asyncmsgq_write(u->rtpoll, PA_RTPOLL_EARLY, args->q);
293 pa_rtpoll_item_free(u->ec_poll_item);
294 u->ec_poll_item = NULL;
302 return pa_sink_process_msg(o, code, data, offset, chunk);
305 static void process_rewind(struct userdata *u) {
307 /* Rewind not supported */
308 pa_sink_process_rewind(u->sink, 0);
310 size_t rewind_nbytes, in_buffer;
315 rewind_nbytes = u->sink->thread_info.rewind_nbytes;
317 if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
320 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
322 if (u->timestamp <= now)
325 delay = u->timestamp - now;
326 in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
331 if (rewind_nbytes > in_buffer)
332 rewind_nbytes = in_buffer;
334 pa_sink_process_rewind(u->sink, rewind_nbytes);
335 u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
337 pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
341 pa_sink_process_rewind(u->sink, 0);
345 static int process_render(struct userdata *u) {
347 size_t frame_size = pa_frame_size(&u->sink->sample_spec);
348 size_t frames_to_write = u->frag_size / frame_size;
354 pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
356 if (frames_to_write > avail)
359 pa_sink_render_full(u->sink, u->frag_size, &chunk);
360 p = pa_memblock_acquire(chunk.memblock);
362 if (pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write)) {
363 pa_log_error("failed to write pcm. p(%p), size(%zu)", p, frames_to_write);
367 pa_memblock_release(chunk.memblock);
370 pa_asyncmsgq_post(u->ec_asyncmsgq, u->ec_object,
371 PA_ECHO_CANCEL_MESSAGE_PUSH_ECHO, NULL, 0, &chunk, NULL);
374 pa_memblock_unref(chunk.memblock);
376 u->write_count += chunk.length;
381 static void thread_func(void *userdata) {
382 struct userdata *u = userdata;
383 unsigned short revents = 0;
387 pa_log_debug("Thread starting up");
389 if (u->core->realtime_scheduling)
390 pa_thread_make_realtime(u->core->realtime_priority);
392 pa_thread_mq_install(&u->thread_mq);
394 u->timestamp = pa_rtclock_now();
399 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
402 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
403 if (process_render(u))
407 pa_log_info("Starting playback.");
408 pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
413 /* Hmm, nothing to do. Let's sleep */
414 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
420 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
421 struct pollfd *pollfd;
422 if (u->rtpoll_item) {
423 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
424 revents = pollfd->revents;
425 if (revents & ~POLLOUT) {
426 pa_log_debug("Poll error 0x%x occured, try recover.", revents);
427 pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
436 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
437 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
440 pa_log_debug("Thread shutting down");
443 static int parse_to_get_card(const char *modarg_device, char *card) {
447 if (!strchr(modarg_device, ',')) {
448 pa_log_error("Failed to parse device argument : no comma");
452 name_p = modarg_device;
454 while (*name_p != ',')
455 *(card_p++) = *(name_p++);
461 static int parse_to_get_device(const char *modarg_device, char *device) {
465 if (!(comma_p = strchr(modarg_device, ','))) {
466 pa_log_error("Failed to parse device argument : no comma");
472 while (*comma_p != '\0')
473 *(device_p++) = *(comma_p++);
479 int pa__init(pa_module*m) {
480 struct userdata *u = NULL;
483 pa_modargs *ma = NULL;
484 pa_sink_new_data data;
485 uint32_t alternate_sample_rate;
486 const char *modarg_device;
487 char card[DEVICE_NAME_MAX];
488 char device[DEVICE_NAME_MAX];
489 size_t frame_size, buffer_size, period_frames, buffer_frames;
493 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
494 pa_log_error("Failed to parse module arguments.");
498 ss = m->core->default_sample_spec;
499 map = m->core->default_channel_map;
500 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
501 pa_log_error("Invalid sample format specification or channel map");
505 alternate_sample_rate = m->core->alternate_sample_rate;
506 if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
507 pa_log_error("Failed to parse alternate sample rate");
511 m->userdata = u = pa_xnew0(struct userdata, 1);
516 u->hal_interface = pa_hal_interface_get(u->core);
517 u->rtpoll = pa_rtpoll_new();
518 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
520 if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
521 pa_log_error("device is invalid");
525 if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
526 pa_log_error("failed to parse device module argument, %s", modarg_device);
530 u->card = pa_xstrdup(card);
531 u->device = pa_xstrdup(device);
533 u->frag_size = (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGMENT_MSEC * PA_USEC_PER_MSEC, &ss);
534 u->nfrags = DEFAULT_FRAGMENTS;
535 if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
536 pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
537 pa_log_error("fragment_size or fragments are invalid.");
540 pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
542 pa_sink_new_data_init(&data);
543 data.driver = __FILE__;
545 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
546 pa_sink_new_data_set_sample_spec(&data, &ss);
547 pa_sink_new_data_set_channel_map(&data, &map);
548 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio sink"));
549 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
550 pa_proplist_sets(data.proplist, "tizen.card", u->card);
551 pa_proplist_sets(data.proplist, "tizen.device", u->device);
552 pa_proplist_sets(data.proplist, "tizen.version", "2");
553 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
555 frame_size = pa_frame_size(&ss);
556 buffer_size = u->frag_size * u->nfrags;
557 buffer_frames = buffer_size / frame_size;
558 period_frames = u->frag_size / frame_size;
559 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
560 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
562 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
563 pa_log_error("Invalid properties.");
564 pa_sink_new_data_done(&data);
568 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
569 pa_sink_new_data_done(&data);
572 pa_log_error("Failed to create sink object.");
576 u->sink->parent.process_msg = sink_process_msg;
577 u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
578 u->sink->userdata = u;
580 if (pa_hal_interface_pcm_open(u->hal_interface,
584 &u->sink->sample_spec,
585 u->frag_size / pa_frame_size(&u->sink->sample_spec),
587 (void **)&u->pcm_handle)) {
588 pa_log_error("Error opening PCM device");
592 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
593 pa_sink_set_rtpoll(u->sink, u->rtpoll);
595 pa_sink_set_max_request(u->sink, buffer_size);
596 pa_sink_set_max_rewind(u->sink, buffer_size);
598 if (!(u->thread = pa_thread_new("tizenaudio-sink2", thread_func, u))) {
599 pa_log_error("Failed to create thread.");
602 pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(buffer_size, &ss));
603 pa_sink_put(u->sink);
616 int pa__get_n_used(pa_module *m) {
620 pa_assert_se((u = m->userdata));
622 return pa_sink_linked_by(u->sink);
625 void pa__done(pa_module*m) {
630 if (!(u = m->userdata))
634 pa_sink_unlink(u->sink);
637 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
638 pa_thread_free(u->thread);
641 pa_thread_mq_done(&u->thread_mq);
644 pa_sink_unref(u->sink);
650 pa_rtpoll_free(u->rtpoll);
653 pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
654 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
657 if (u->hal_interface)
658 pa_hal_interface_unref(u->hal_interface);