aec-manager: Add audio AEC manager
[platform/core/multimedia/pulseaudio-modules-tizen.git] / src / module-tizenaudio-sink.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30
31 #include <pulse/rtclock.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35
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>
47
48 #include "hal-interface.h"
49
50 #ifdef SUPPORT_AEC
51 #include "aec-manager.h"
52 #endif
53
54 PA_MODULE_AUTHOR("Tizen");
55 PA_MODULE_DESCRIPTION("Tizen Audio Sink");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(false);
58 PA_MODULE_USAGE(
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> "
63         "rate=<sample rate> "
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> ");
70
71 #define DEFAULT_SINK_NAME "tizenaudio-sink"
72
73 /* block_usec should be less than amount of fragment_size * fragments */
74 #define DEFAULT_BLOCK_USEC (PA_USEC_PER_SEC * 0.05)
75
76 /* Sink device consumes that amount of maximum buffer at every request */
77 #define DEFAULT_MAX_REQUEST_USEC (PA_USEC_PER_SEC * 0.032)
78
79 #define DEVICE_NAME_MAX                     30
80
81 #ifdef SUPPORT_AEC
82 enum {
83     PA_SINK_MESSAGE_SET_AEC_STATE = PA_SINK_MESSAGE_MAX,
84     PA_SINK_MESSAGE_GET_AEC_PARAMS,
85 };
86 #endif
87
88 struct userdata {
89     pa_core *core;
90     pa_module *module;
91     pa_sink *sink;
92
93     pa_thread *thread;
94     pa_thread_mq thread_mq;
95     pa_rtpoll *rtpoll;
96
97     void *pcm_handle;
98     uint32_t nfrags;
99     uint32_t frag_size;
100
101     pa_usec_t block_usec;
102     pa_usec_t max_request_usec;
103     pa_usec_t timestamp;
104     pa_usec_t timestamp_written;
105
106     char* card;
107     char* device;
108     bool first;
109
110     pa_rtpoll_item *rtpoll_item;
111
112     uint64_t write_count;
113     pa_hal_interface *hal_interface;
114
115 #ifdef SUPPORT_AEC
116     bool aec_enable;
117     pa_sample_spec ss;
118 #endif
119 };
120
121 static const char* const valid_modargs[] = {
122     "sink_name",
123     "sink_properties",
124     "device",
125     "format",
126     "rate",
127     "channels",
128     "channel_map",
129     "fragments",
130     "fragment_size",
131     "block_msec",
132     "max_request_msec",
133     NULL
134 };
135
136 static int build_pollfd(struct userdata *u) {
137     int32_t ret;
138     struct pollfd *pollfd;
139     int fd = -1;
140
141     pa_assert(u);
142     pa_assert(u->pcm_handle);
143     pa_assert(u->rtpoll);
144
145     if (u->rtpoll_item)
146         pa_rtpoll_item_free(u->rtpoll_item);
147
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);
153         return -1;
154     }
155     pollfd->fd = fd;
156     pollfd->events = /* POLLOUT | */ POLLERR | POLLNVAL;
157
158     return 0;
159 }
160
161 /* Called from IO context */
162 static int suspend(struct userdata *u) {
163     int32_t ret;
164     pa_assert(u);
165     pa_assert(u->pcm_handle);
166
167     ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
168     if (ret) {
169         pa_log_error("Error closing PCM device %x", ret);
170     }
171     u->pcm_handle = NULL;
172
173     if (u->rtpoll_item) {
174         pa_rtpoll_item_free(u->rtpoll_item);
175         u->rtpoll_item = NULL;
176     }
177
178     pa_log_info("Device suspended...");
179
180     return 0;
181 }
182
183 /* Called from IO context */
184 static int unsuspend(struct userdata *u) {
185     pa_sample_spec sample_spec;
186     int32_t ret;
187     size_t frame_size;
188
189     char *card = u->card;
190     char *device = u->device;
191
192     pa_assert(u);
193     pa_assert(!u->pcm_handle);
194
195     pa_log_info("Trying resume...");
196
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!");
201         goto fail;
202     }
203
204 #ifdef SUPPORT_AEC
205     if (u->aec_enable) {
206         card = "Loopback";
207         device = "0,0";
208     }
209 #endif
210
211     ret = pa_hal_interface_pcm_open(u->hal_interface,
212               card,
213               device,
214               DIRECTION_OUT,
215               &sample_spec,
216               u->frag_size / frame_size,
217               u->nfrags,
218               (void **)&u->pcm_handle);
219     if (ret) {
220         pa_log_error("Error opening PCM device %x", ret);
221         goto fail;
222     }
223
224     if (build_pollfd(u) < 0)
225         goto fail;
226
227     u->write_count = 0;
228     u->first = true;
229
230     pa_log_info("Resumed successfully...device(%s:%s)", card, device);
231
232     return 0;
233
234 fail:
235     if (u->pcm_handle) {
236         pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
237         u->pcm_handle = NULL;
238     }
239     return -PA_ERR_IO;
240 }
241
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) {
244     struct userdata *u;
245     int r;
246
247     pa_assert(s);
248     pa_assert_se(u = s->userdata);
249
250     /* It may be that only the suspend cause is changing, in which case there's
251      * nothing to do. */
252     if (new_state == s->thread_info.state)
253         return 0;
254
255     switch (new_state) {
256         case PA_SINK_SUSPENDED: {
257             pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
258             if ((r = suspend(u)) < 0)
259                 return r;
260             break;
261         }
262
263         case PA_SINK_IDLE:
264         case PA_SINK_RUNNING: {
265             if (s->thread_info.state == PA_SINK_INIT) {
266                 if (build_pollfd(u) < 0)
267                     return -PA_ERR_IO;
268             }
269
270             if (s->thread_info.state == PA_SINK_SUSPENDED) {
271                 if ((r = unsuspend(u)) < 0)
272                     return r;
273             }
274             break;
275         }
276
277         case PA_SINK_UNLINKED:
278         case PA_SINK_INIT:
279         case PA_SINK_INVALID_STATE:
280             break;
281     }
282
283     return 0;
284 }
285
286 static int sink_process_msg(
287         pa_msgobject *o,
288         int code,
289         void *data,
290         int64_t offset,
291         pa_memchunk *chunk) {
292
293     struct userdata *u = PA_SINK(o)->userdata;
294
295     switch (code) {
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);
302                 }
303             }
304             *((pa_usec_t*)data) = latency;
305             return 0;
306         }
307 #ifdef SUPPORT_AEC
308         case PA_SINK_MESSAGE_SET_AEC_STATE: {
309             pa_sink *s = PA_SINK(o);
310             bool enable = (bool)data;
311
312             if (u->aec_enable == enable)
313                 return 0;
314
315             pa_log_info("AEC enable(%d)", enable);
316
317             u->aec_enable = enable;
318             if (s->thread_info.state == PA_SINK_RUNNING) {
319                 suspend(u);
320                 unsuspend(u);
321             }
322
323             return 0;
324         }
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);
334             return 0;
335         }
336 #endif
337     }
338
339     return pa_sink_process_msg(o, code, data, offset, chunk);
340 }
341
342 static void sink_update_requested_latency_cb(pa_sink *s) {
343     struct userdata *u;
344
345     pa_sink_assert_ref(s);
346     pa_assert_se((u = s->userdata));
347
348     u->block_usec = pa_sink_get_requested_latency_within_thread(s);
349
350     if (u->block_usec == (pa_usec_t)-1)
351         u->block_usec = s->thread_info.max_latency;
352
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));
355 }
356
357 static void process_rewind(struct userdata *u, pa_usec_t now) {
358 #if 1
359     /* Rewind not supported */
360     pa_sink_process_rewind(u->sink, 0);
361 #else
362     size_t rewind_nbytes, in_buffer;
363     pa_usec_t delay;
364
365     pa_assert(u);
366
367     rewind_nbytes = u->sink->thread_info.rewind_nbytes;
368
369     if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
370         goto do_nothing;
371
372     pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
373
374     if (u->timestamp <= now)
375         goto do_nothing;
376
377     delay = u->timestamp - now;
378     in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
379
380     if (in_buffer <= 0)
381         goto do_nothing;
382
383     if (rewind_nbytes > in_buffer)
384         rewind_nbytes = in_buffer;
385
386     pa_sink_process_rewind(u->sink, rewind_nbytes);
387     u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
388
389     pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
390     return;
391
392 do_nothing:
393     pa_sink_process_rewind(u->sink, 0);
394 #endif
395 }
396
397 static int process_render(struct userdata *u, pa_usec_t now) {
398     int work_done = 0;
399     size_t ate = 0;
400     void *p;
401     size_t frames_to_write, frame_size;
402     uint32_t avail = 0;
403
404     pa_assert(u);
405
406     /* Fill the buffer up the latency size */
407     while (u->timestamp < now + u->block_usec) {
408         pa_memchunk chunk;
409         frame_size = pa_frame_size(&u->sink->sample_spec);
410         if (frame_size == 0) {
411             pa_log_error("Unexpected frame size zero!");
412             break;
413         }
414
415         pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
416         if ((avail == 0) && !(u->first)) {
417             break;
418         }
419
420         if (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;
424         } else {
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)
428                 break;
429         }
430
431         pa_sink_render_full(u->sink, frames_to_write * frame_size, &chunk);
432         p = pa_memblock_acquire(chunk.memblock);
433
434         pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write);
435
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();
440
441         work_done = 1;
442
443         ate += chunk.length;
444         if (ate >= u->sink->thread_info.max_request) {
445             break;
446         }
447     }
448
449     return work_done;
450 }
451
452 static void thread_func(void *userdata) {
453     struct userdata *u = userdata;
454     unsigned short revents = 0;
455
456     pa_assert(u);
457
458     pa_log_debug("Thread starting up");
459
460     if (u->core->realtime_scheduling)
461         pa_thread_make_realtime(u->core->realtime_priority);
462
463     pa_thread_mq_install(&u->thread_mq);
464
465     for (;;) {
466         pa_usec_t now = 0;
467         int ret;
468
469         if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
470             now = pa_rtclock_now();
471
472         if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
473             process_rewind(u, now);
474
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);
478
479             if (work_done < 0)
480                 goto fail;
481
482             if (work_done == 0) {
483                 pa_rtpoll_set_timer_relative(u->rtpoll, (10 * PA_USEC_PER_MSEC));
484             } else {
485                 if (u->first) {
486                     pa_log_info("Starting playback.");
487                     pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
488                     u->first = false;
489                 }
490                 pa_rtpoll_set_timer_relative(u->rtpoll, (10 * PA_USEC_PER_MSEC));
491             }
492         } else {
493             pa_rtpoll_set_timer_disabled(u->rtpoll);
494         }
495
496         /* Hmm, nothing to do. Let's sleep */
497         if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
498             goto fail;
499
500         if (ret == 0)
501             goto finish;
502
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);
511                     u->first = true;
512                     revents = 0;
513                 } else {
514                     //pa_log_debug("Poll wakeup.", revents);
515                 }
516             }
517         }
518     }
519
520 fail:
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);
525
526 finish:
527     pa_log_debug("Thread shutting down");
528 }
529
530 static int parse_to_get_card(const char *modarg_device, char *card) {
531     const char *name_p;
532     char *card_p;
533
534     if (!strchr(modarg_device, ',')) {
535         pa_log_error("Failed to parse device argument : no comma");
536         return -1;
537     }
538
539     name_p = modarg_device;
540     card_p = card;
541     while (*name_p != ',')
542         *(card_p++) = *(name_p++);
543     *card_p = '\0';
544
545     return 0;
546 }
547
548 static int parse_to_get_device(const char *modarg_device, char *device) {
549     const char *comma_p;
550     char *device_p;
551
552     if (!(comma_p = strchr(modarg_device, ','))) {
553         pa_log_error("Failed to parse device argument : no comma");
554         return -1;
555     }
556
557     comma_p++;
558     device_p = device;
559     while (*comma_p != '\0')
560         *(device_p++) = *(comma_p++);
561     *device_p = '\0';
562
563     return 0;
564 }
565
566 int pa__init(pa_module*m) {
567     struct userdata *u = NULL;
568     pa_sample_spec ss;
569     pa_channel_map map;
570     pa_modargs *ma = NULL;
571     pa_sink_new_data data;
572     uint32_t alternate_sample_rate;
573     uint32_t block_msec;
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;
579
580     pa_assert(m);
581
582     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
583         pa_log_error("Failed to parse module arguments.");
584         goto fail;
585     }
586
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");
591         goto fail;
592     }
593
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");
597         goto fail;
598     }
599
600     m->userdata = u = pa_xnew0(struct userdata, 1);
601     u->core = m->core;
602     u->module = m;
603     u->first = true;
604     u->hal_interface = pa_hal_interface_get(u->core);
605     u->rtpoll = pa_rtpoll_new();
606 #ifdef SUPPORT_AEC
607     u->ss = ss;
608 #endif
609     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
610
611     if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
612         pa_log_error("device is invalid");
613         goto fail;
614     }
615
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);
618         goto fail;
619     }
620
621     u->card = pa_xstrdup(card);
622     u->device = pa_xstrdup(device);
623
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.");
629         goto fail;
630     }
631     pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
632
633     /* Optional */
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;
637
638     /* Optional */
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;
642
643     u->timestamp = 0ULL;
644     u->timestamp_written = 0ULL;
645
646     pa_sink_new_data_init(&data);
647     data.driver = __FILE__;
648     data.module = m;
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");
657
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);
664
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);
668         goto fail;
669     }
670
671     u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
672     pa_sink_new_data_done(&data);
673
674     if (!u->sink) {
675         pa_log_error("Failed to create sink object.");
676         goto fail;
677     }
678
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;
683
684     if (pa_hal_interface_pcm_open(u->hal_interface,
685               u->card,
686               u->device,
687               DIRECTION_OUT,
688               &u->sink->sample_spec,
689               u->frag_size / pa_frame_size(&u->sink->sample_spec),
690               u->nfrags,
691               (void **)&u->pcm_handle)) {
692         pa_log_error("Error opening PCM device");
693         goto fail;
694     }
695
696     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
697     pa_sink_set_rtpoll(u->sink, u->rtpoll);
698
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));
701
702     if (!(u->thread = pa_thread_new("tizenaudio-sink", thread_func, u))) {
703         pa_log_error("Failed to create thread.");
704         goto fail;
705     }
706     pa_sink_set_fixed_latency(u->sink, u->block_usec);
707     pa_sink_put(u->sink);
708     pa_modargs_free(ma);
709
710     return 0;
711
712 fail:
713     if (ma)
714         pa_modargs_free(ma);
715
716     pa__done(m);
717     return -1;
718 }
719
720 int pa__get_n_used(pa_module *m) {
721     struct userdata *u;
722
723     pa_assert(m);
724     pa_assert_se((u = m->userdata));
725
726     return pa_sink_linked_by(u->sink);
727 }
728
729 void pa__done(pa_module*m) {
730     struct userdata *u;
731
732     pa_assert(m);
733
734     if (!(u = m->userdata))
735         return;
736
737     if (u->sink)
738         pa_sink_unlink(u->sink);
739
740     if (u->thread) {
741         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
742         pa_thread_free(u->thread);
743     }
744
745     pa_thread_mq_done(&u->thread_mq);
746
747     if (u->sink)
748         pa_sink_unref(u->sink);
749
750     pa_xfree(u->card);
751     pa_xfree(u->device);
752
753     if (u->rtpoll)
754         pa_rtpoll_free(u->rtpoll);
755
756     if (u->pcm_handle) {
757         pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
758         pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
759     }
760
761     pa_xfree(u);
762 }