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/source.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 Source2");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(false);
56 "source_name=<name of source> "
57 "source_properties=<properties for the source> "
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_SOURCE_NAME "tizenaudio-source2"
68 #define DEVICE_NAME_MAX 30
69 #define DEFAULT_FRAGMENT_MSEC 20
70 #define DEFAULT_FRAGMENTS 4
78 pa_thread_mq thread_mq;
92 pa_rtpoll_item *rtpoll_item;
95 pa_usec_t latency_time;
96 pa_hal_interface *hal_interface;
98 pa_msgobject *ec_object;
99 pa_asyncmsgq *ec_asyncmsgq;
100 pa_rtpoll_item *ec_poll_item;
103 static const char* const valid_modargs[] = {
116 static int build_pollfd(struct userdata *u) {
118 struct pollfd *pollfd;
122 pa_assert(u->pcm_handle);
123 pa_assert(u->rtpoll);
126 pa_rtpoll_item_free(u->rtpoll_item);
128 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
129 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
130 ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
131 if (ret < 0 || fd < 0) {
132 pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
136 pollfd->events = POLLIN | POLLERR | POLLNVAL;
141 /* Called from IO context */
142 static int suspend(struct userdata *u) {
145 pa_assert(u->pcm_handle);
147 ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
149 pa_log_error("Error closing PCM device %x", ret);
151 u->pcm_handle = NULL;
153 if (u->rtpoll_item) {
154 pa_rtpoll_item_free(u->rtpoll_item);
155 u->rtpoll_item = NULL;
158 pa_log_info("Device suspended...");
163 /* Called from IO context */
164 static int unsuspend(struct userdata *u) {
165 pa_sample_spec sample_spec;
170 pa_assert(!u->pcm_handle);
172 pa_log_info("Trying resume...");
174 sample_spec = u->source->sample_spec;
175 frame_size = pa_frame_size(&sample_spec);
176 if (frame_size == 0) {
177 pa_log_error("Unexpected frame size zero!");
181 ret = pa_hal_interface_pcm_open(u->hal_interface,
186 u->frag_size / frame_size,
188 (void **)&u->pcm_handle);
190 pa_log_error("Error opening PCM device %x", ret);
194 if (build_pollfd(u) < 0)
199 u->timestamp = pa_rtclock_now();
201 pa_log_info("Resumed successfully...");
207 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
208 u->pcm_handle = NULL;
213 /* Called from the IO thread. */
214 static int source_set_state_in_io_thread_cb(pa_source *s, pa_source_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
219 pa_assert_se(u = s->userdata);
221 /* It may be that only the suspend cause is changing, in which case there's
222 * nothing more to do. */
223 if (new_state == s->thread_info.state)
227 case PA_SOURCE_SUSPENDED: {
228 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
229 if ((r = suspend(u)) < 0)
235 case PA_SOURCE_RUNNING: {
236 if (s->thread_info.state == PA_SOURCE_INIT) {
237 if (build_pollfd(u) < 0)
241 if (s->thread_info.state == PA_SOURCE_SUSPENDED) {
242 if ((r = unsuspend(u)) < 0)
248 case PA_SOURCE_UNLINKED:
250 case PA_SOURCE_INVALID_STATE:
257 static int source_process_msg(
262 pa_memchunk *chunk) {
264 struct userdata *u = PA_SOURCE(o)->userdata;
267 case PA_SOURCE_MESSAGE_SET_AEC_STATE: {
269 pa_log_info("EC state changed (%d)", u->echo_on);
272 case PA_SOURCE_MESSAGE_GET_LATENCY: {
276 r = pa_rtclock_now() - (u->timestamp + pa_bytes_to_usec(u->read_count, &u->source->sample_spec));
278 *((int64_t *) data) = r;
282 case PA_SOURCE_MESSAGE_REBUILD_RTPOLL: {
288 args = (struct arguments *)data;
291 u->ec_object = args->o;
292 u->ec_asyncmsgq = args->q;
293 u->ec_poll_item = pa_rtpoll_item_new_asyncmsgq_write(u->rtpoll, PA_RTPOLL_EARLY, args->q);
295 pa_rtpoll_item_free(u->ec_poll_item);
296 u->ec_poll_item = NULL;
304 return pa_source_process_msg(o, code, data, offset, chunk);
307 static int process_render(struct userdata *u) {
309 size_t frame_size = pa_frame_size(&u->source->sample_spec);
310 size_t frames_to_read = u->frag_size / frame_size;
316 /* Fill the buffer up the latency size */
318 pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
319 if (frames_to_read > avail) {
320 pa_log_debug("not enough avail size. frames_to_read(%zu), avail(%d)", frames_to_read, avail);
324 chunk.length = u->frag_size;
325 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
327 p = pa_memblock_acquire(chunk.memblock);
328 if (pa_hal_interface_pcm_read(u->hal_interface, u->pcm_handle, p, (uint32_t)frames_to_read)) {
329 pa_log_error("failed to read pcm. p(%p), size(%zu)", p, frames_to_read);
332 pa_memblock_release(chunk.memblock);
335 chunk.length = (size_t)frames_to_read * frame_size;
338 pa_asyncmsgq_post(u->ec_asyncmsgq, u->ec_object,
339 PA_ECHO_CANCEL_MESSAGE_PUSH_DATA, NULL, 0, &chunk, NULL);
341 pa_source_post(u->source, &chunk);
344 pa_memblock_unref(chunk.memblock);
346 u->read_count += chunk.length;
351 static void thread_func(void *userdata) {
352 struct userdata *u = userdata;
353 unsigned short revents = 0;
356 pa_log_debug("Thread starting up");
358 if (u->core->realtime_scheduling)
359 pa_thread_make_realtime(u->core->realtime_priority);
361 pa_thread_mq_install(&u->thread_mq);
363 u->timestamp = pa_rtclock_now();
368 /* Render some data and drop it immediately */
369 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
370 if (process_render(u))
374 pa_log_info("Starting capture.");
375 pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
380 /* Hmm, nothing to do. Let's sleep */
381 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
387 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
388 struct pollfd *pollfd;
389 if (u->rtpoll_item) {
390 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
391 revents = pollfd->revents;
392 if (revents & ~POLLIN) {
393 pa_log_debug("Poll error 0x%x occured, try recover.", revents);
394 pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
403 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
404 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
407 pa_log_debug("Thread shutting down");
410 static int parse_to_get_card(const char *modarg_device, char *card) {
414 if (!strchr(modarg_device, ',')) {
415 pa_log_error("Failed to parse device argument : no comma");
419 name_p = modarg_device;
421 while (*name_p != ',')
422 *(card_p++) = *(name_p++);
428 static int parse_to_get_device(const char *modarg_device, char *device) {
432 if (!(comma_p = strchr(modarg_device, ','))) {
433 pa_log_error("Failed to parse device argument : no comma");
439 while (*comma_p != '\0')
440 *(device_p++) = *(comma_p++);
446 int pa__init(pa_module*m) {
447 struct userdata *u = NULL;
450 pa_modargs *ma = NULL;
451 pa_source_new_data data;
452 uint32_t alternate_sample_rate;
453 const char *modarg_device;
454 char card[DEVICE_NAME_MAX];
455 char device[DEVICE_NAME_MAX];
456 size_t frame_size, buffer_size, period_frames, buffer_frames;
460 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
461 pa_log_error("Failed to parse module arguments.");
465 ss = m->core->default_sample_spec;
466 map = m->core->default_channel_map;
467 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
468 pa_log_error("Invalid sample format specification or channel map");
472 alternate_sample_rate = m->core->alternate_sample_rate;
473 if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
474 pa_log_error("Failed to parse alternate sample rate");
478 m->userdata = u = pa_xnew0(struct userdata, 1);
482 u->hal_interface = pa_hal_interface_get(u->core);
483 u->rtpoll = pa_rtpoll_new();
484 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
486 if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
487 pa_log_error("device is invalid");
491 if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
492 pa_log_error("failed to parse device module argument, %s", modarg_device);
496 u->card = pa_xstrdup(card);
497 u->device = pa_xstrdup(device);
499 u->frag_size = (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGMENT_MSEC * PA_USEC_PER_MSEC, &ss);
500 u->nfrags = DEFAULT_FRAGMENTS;
501 if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
502 pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
503 pa_log_error("fragment_size or fragments are invalid.");
506 pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
508 pa_source_new_data_init(&data);
509 data.driver = __FILE__;
511 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
512 pa_source_new_data_set_sample_spec(&data, &ss);
513 pa_source_new_data_set_channel_map(&data, &map);
514 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio source"));
515 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
516 pa_proplist_sets(data.proplist, "tizen.card", u->card);
517 pa_proplist_sets(data.proplist, "tizen.device", u->device);
518 pa_proplist_sets(data.proplist, "tizen.version", "2");
519 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
521 frame_size = pa_frame_size(&ss);
522 buffer_size = u->frag_size * u->nfrags;
523 buffer_frames = buffer_size / frame_size;
524 period_frames = u->frag_size / frame_size;
525 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
526 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
528 if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
529 pa_log_error("Invalid properties.");
530 pa_source_new_data_done(&data);
534 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
535 pa_source_new_data_done(&data);
538 pa_log_error("Failed to create source object.");
542 u->source->parent.process_msg = source_process_msg;
543 u->source->set_state_in_io_thread = source_set_state_in_io_thread_cb;
544 u->source->userdata = u;
546 if (pa_hal_interface_pcm_open(u->hal_interface,
550 &u->source->sample_spec,
551 u->frag_size / pa_frame_size(&u->source->sample_spec),
553 (void **)&u->pcm_handle)) {
554 pa_log_error("Error opening PCM device");
558 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
559 pa_source_set_rtpoll(u->source, u->rtpoll);
563 if (!(u->thread = pa_thread_new("tizenaudio-source2", thread_func, u))) {
564 pa_log_error("Failed to create thread.");
567 pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(buffer_size, &ss));
568 pa_source_put(u->source);
581 int pa__get_n_used(pa_module *m) {
585 pa_assert_se((u = m->userdata));
587 return pa_source_linked_by(u->source);
590 void pa__done(pa_module*m) {
595 if (!(u = m->userdata))
599 pa_source_unlink(u->source);
602 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
603 pa_thread_free(u->thread);
606 pa_thread_mq_done(&u->thread_mq);
609 pa_source_unref(u->source);
615 pa_rtpoll_free(u->rtpoll);
618 pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
619 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
622 if (u->hal_interface)
623 pa_hal_interface_unref(u->hal_interface);