2 This file is part of PulseAudio.
4 Copyright (c) 2022 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 #define DEFAULT_SOURCE_NAME "tizenaudio-source2"
53 #define DEVICE_NAME_MAX 30
54 #define DEFAULT_FRAGMENT_MSEC 20
55 #define DEFAULT_FRAGMENTS 4
63 pa_thread_mq thread_mq;
77 pa_rtpoll_item *rtpoll_item;
80 pa_usec_t latency_time;
81 pa_hal_interface *hal_interface;
84 pa_msgobject *ec_object;
85 pa_asyncmsgq *ec_asyncmsgq;
86 pa_rtpoll_item *ec_poll_item;
89 static void userdata_free(struct userdata *u);
91 static int build_pollfd(struct userdata *u) {
93 struct pollfd *pollfd;
97 pa_assert(u->pcm_handle);
101 pa_rtpoll_item_free(u->rtpoll_item);
103 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
104 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
105 ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
106 if (ret < 0 || fd < 0) {
107 pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
111 pollfd->events = POLLIN | POLLERR | POLLNVAL;
116 /* Called from IO context */
117 static int suspend(struct userdata *u) {
120 pa_assert(u->pcm_handle);
122 ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
124 pa_log_error("Error closing PCM device %x", ret);
126 u->pcm_handle = NULL;
128 if (u->rtpoll_item) {
129 pa_rtpoll_item_free(u->rtpoll_item);
130 u->rtpoll_item = NULL;
133 pa_log_info("Device suspended...");
138 /* Called from IO context */
139 static int unsuspend(struct userdata *u) {
140 pa_sample_spec sample_spec;
145 pa_assert(!u->pcm_handle);
147 pa_log_info("Trying resume...");
149 sample_spec = u->source->sample_spec;
150 frame_size = pa_frame_size(&sample_spec);
151 if (frame_size == 0) {
152 pa_log_error("Unexpected frame size zero!");
156 ret = pa_hal_interface_pcm_open(u->hal_interface,
161 u->frag_size / frame_size,
163 (void **)&u->pcm_handle);
165 pa_log_error("Error opening PCM device %x", ret);
169 if (build_pollfd(u) < 0)
174 u->timestamp = pa_rtclock_now();
176 pa_log_info("Resumed successfully...");
182 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
183 u->pcm_handle = NULL;
188 /* Called from the IO thread. */
189 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) {
194 pa_assert_se(u = s->userdata);
196 /* It may be that only the suspend cause is changing, in which case there's
197 * nothing more to do. */
198 if (new_state == s->thread_info.state)
202 case PA_SOURCE_SUSPENDED: {
203 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
204 if ((r = suspend(u)) < 0)
210 case PA_SOURCE_RUNNING: {
211 if (s->thread_info.state == PA_SOURCE_INIT) {
212 if (build_pollfd(u) < 0)
216 if (s->thread_info.state == PA_SOURCE_SUSPENDED) {
217 if ((r = unsuspend(u)) < 0)
223 case PA_SOURCE_UNLINKED:
225 case PA_SOURCE_INVALID_STATE:
232 static int source_process_msg(
237 pa_memchunk *chunk) {
239 struct userdata *u = PA_SOURCE(o)->userdata;
242 case PA_SOURCE_MESSAGE_SET_AEC_STATE: {
244 pa_log_info("EC state changed (%d)", u->echo_on);
247 case PA_SOURCE_MESSAGE_GET_LATENCY: {
251 r = pa_rtclock_now() - (u->timestamp + pa_bytes_to_usec(u->read_count, &u->source->sample_spec));
253 *((int64_t *) data) = r;
257 case PA_SOURCE_MESSAGE_REBUILD_RTPOLL: {
263 args = (struct arguments *)data;
266 u->ec_object = args->o;
267 u->ec_asyncmsgq = args->q;
268 u->ec_poll_item = pa_rtpoll_item_new_asyncmsgq_write(u->rtpoll, PA_RTPOLL_EARLY, args->q);
270 pa_rtpoll_item_free(u->ec_poll_item);
271 u->ec_poll_item = NULL;
279 return pa_source_process_msg(o, code, data, offset, chunk);
282 static int process_render(struct userdata *u) {
284 size_t frame_size = pa_frame_size(&u->source->sample_spec);
285 size_t frames_to_read = u->frag_size / frame_size;
291 /* Fill the buffer up the latency size */
293 pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
294 if (frames_to_read > avail) {
295 pa_log_debug("not enough avail size. frames_to_read(%zu), avail(%u)", frames_to_read, avail);
299 chunk.length = u->frag_size;
300 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
302 p = pa_memblock_acquire(chunk.memblock);
303 if (pa_hal_interface_pcm_read(u->hal_interface, u->pcm_handle, p, (uint32_t)frames_to_read)) {
304 pa_log_error("failed to read pcm. p(%p), size(%zu)", p, frames_to_read);
307 pa_memblock_release(chunk.memblock);
310 chunk.length = (size_t)frames_to_read * frame_size;
313 pa_asyncmsgq_post(u->ec_asyncmsgq, u->ec_object,
314 PA_ECHO_CANCEL_MESSAGE_PUSH_DATA, NULL, 0, &chunk, NULL);
316 pa_source_post(u->source, &chunk);
319 pa_memblock_unref(chunk.memblock);
321 u->read_count += chunk.length;
326 static void thread_func(void *userdata) {
327 struct userdata *u = userdata;
328 unsigned short revents = 0;
331 pa_log_debug("Thread starting up");
333 if (u->core->realtime_scheduling)
334 pa_thread_make_realtime(u->core->realtime_priority);
336 pa_thread_mq_install(&u->thread_mq);
338 u->timestamp = pa_rtclock_now();
343 /* Render some data and drop it immediately */
344 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
345 if (process_render(u))
349 pa_log_info("Starting capture.");
350 pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
355 /* Hmm, nothing to do. Let's sleep */
356 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
362 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
363 struct pollfd *pollfd;
364 if (u->rtpoll_item) {
365 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
366 revents = pollfd->revents;
367 if (revents & ~POLLIN) {
368 pa_log_debug("Poll error 0x%x occured, try recover.", revents);
369 pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
378 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
379 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
382 pa_log_debug("Thread shutting down");
385 static int parse_to_get_card(const char *modarg_device, char *card) {
389 if (!strchr(modarg_device, ',')) {
390 pa_log_error("Failed to parse device argument : no comma");
394 name_p = modarg_device;
396 while (*name_p != ',')
397 *(card_p++) = *(name_p++);
403 static int parse_to_get_device(const char *modarg_device, char *device) {
407 if (!(comma_p = strchr(modarg_device, ','))) {
408 pa_log_error("Failed to parse device argument : no comma");
414 while (*comma_p != '\0')
415 *(device_p++) = *(comma_p++);
421 pa_source *pa_tizenaudio_source2_new(pa_module *m, pa_modargs *ma, const char *driver) {
422 struct userdata *u = NULL;
425 pa_source_new_data data;
426 uint32_t alternate_sample_rate;
427 const char *modarg_device;
428 char card[DEVICE_NAME_MAX];
429 char device[DEVICE_NAME_MAX];
430 size_t frame_size, buffer_size, period_frames, buffer_frames;
434 ss = m->core->default_sample_spec;
435 map = m->core->default_channel_map;
436 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
437 pa_log_error("Invalid sample format specification or channel map");
441 alternate_sample_rate = m->core->alternate_sample_rate;
442 if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
443 pa_log_error("Failed to parse alternate sample rate");
447 u = pa_xnew0(struct userdata, 1);
451 u->hal_interface = pa_hal_interface_get(u->core);
452 u->rtpoll = pa_rtpoll_new();
453 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
455 if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
456 pa_log_error("device is invalid");
460 if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
461 pa_log_error("failed to parse device module argument, %s", modarg_device);
465 u->card = pa_xstrdup(card);
466 u->device = pa_xstrdup(device);
468 u->frag_size = (uint32_t) pa_usec_to_bytes(DEFAULT_FRAGMENT_MSEC * PA_USEC_PER_MSEC, &ss);
469 u->nfrags = DEFAULT_FRAGMENTS;
470 if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
471 pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
472 pa_log_error("fragment_size or fragments are invalid.");
475 pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
477 pa_source_new_data_init(&data);
478 data.driver = driver;
480 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
481 pa_source_new_data_set_sample_spec(&data, &ss);
482 pa_source_new_data_set_channel_map(&data, &map);
483 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio source2"));
484 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
485 pa_proplist_sets(data.proplist, "tizen.card", u->card);
486 pa_proplist_sets(data.proplist, "tizen.device", u->device);
487 pa_proplist_sets(data.proplist, "tizen.version", "2");
488 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
490 frame_size = pa_frame_size(&ss);
491 buffer_size = (size_t)(u->frag_size * u->nfrags);
492 buffer_frames = buffer_size / frame_size;
493 period_frames = u->frag_size / frame_size;
494 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
495 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
497 if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
498 pa_log_error("Invalid properties.");
499 pa_source_new_data_done(&data);
503 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
504 pa_source_new_data_done(&data);
507 pa_log_error("Failed to create source object.");
511 u->source->parent.process_msg = source_process_msg;
512 u->source->set_state_in_io_thread = source_set_state_in_io_thread_cb;
513 u->source->userdata = u;
515 if (pa_hal_interface_pcm_open(u->hal_interface,
519 &u->source->sample_spec,
520 u->frag_size / pa_frame_size(&u->source->sample_spec),
522 (void **)&u->pcm_handle)) {
523 pa_log_error("Error opening PCM device");
527 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
528 pa_source_set_rtpoll(u->source, u->rtpoll);
532 if (!(u->thread = pa_thread_new("tizenaudio-source2", thread_func, u))) {
533 pa_log_error("Failed to create thread.");
537 pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(buffer_size, &ss));
538 pa_source_put(u->source);
547 static void userdata_free(struct userdata *u) {
551 pa_source_unlink(u->source);
554 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
555 pa_thread_free(u->thread);
558 pa_thread_mq_done(&u->thread_mq);
561 pa_source_unref(u->source);
567 pa_rtpoll_free(u->rtpoll);
570 pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
571 pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
574 if (u->hal_interface)
575 pa_hal_interface_unref(u->hal_interface);
580 void pa_tizenaudio_source2_free(pa_source *s) {
583 pa_source_assert_ref(s);
584 pa_assert_se((u = s->userdata));