f8a6059d47b070769536e8929b46a9e66fe36220
[platform/core/multimedia/pulseaudio-modules-tizen.git] / src / module-tizenaudio-sink2.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright (c) 2021 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 PA_MODULE_AUTHOR("Tizen");
51 PA_MODULE_DESCRIPTION("Tizen Audio Sink");
52 PA_MODULE_VERSION(PACKAGE_VERSION);
53 PA_MODULE_LOAD_ONCE(false);
54 PA_MODULE_USAGE(
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> "
59         "rate=<sample rate> "
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> ");
66
67 #define DEFAULT_SINK_NAME "tizenaudio-sink"
68
69 /* block_usec should be less than amount of fragment_size * fragments */
70 #define DEFAULT_BLOCK_USEC (PA_USEC_PER_SEC * 0.05)
71
72 /* Sink device consumes that amount of maximum buffer at every request */
73 #define DEFAULT_MAX_REQUEST_USEC (PA_USEC_PER_SEC * 0.032)
74
75 #define DEVICE_NAME_MAX                     30
76
77 struct userdata {
78     pa_core *core;
79     pa_module *module;
80     pa_sink *sink;
81
82     pa_thread *thread;
83     pa_thread_mq thread_mq;
84     pa_rtpoll *rtpoll;
85
86     void *pcm_handle;
87     uint32_t nfrags;
88     uint32_t frag_size;
89
90     pa_usec_t block_usec;
91     pa_usec_t max_request_usec;
92     pa_usec_t timestamp;
93     pa_usec_t timestamp_written;
94
95     char* card;
96     char* device;
97     bool first;
98
99     pa_rtpoll_item *rtpoll_item;
100
101     uint64_t write_count;
102     pa_hal_interface *hal_interface;
103 };
104
105 static const char* const valid_modargs[] = {
106     "sink_name",
107     "sink_properties",
108     "device",
109     "format",
110     "rate",
111     "channels",
112     "channel_map",
113     "fragments",
114     "fragment_size",
115     "block_msec",
116     "max_request_msec",
117     NULL
118 };
119
120 static int build_pollfd(struct userdata *u) {
121     int32_t ret;
122     struct pollfd *pollfd;
123     int fd = -1;
124
125     pa_assert(u);
126     pa_assert(u->pcm_handle);
127     pa_assert(u->rtpoll);
128
129     if (u->rtpoll_item)
130         pa_rtpoll_item_free(u->rtpoll_item);
131
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);
137         return -1;
138     }
139     pollfd->fd = fd;
140     pollfd->events = /* POLLOUT | */ POLLERR | POLLNVAL;
141
142     return 0;
143 }
144
145 /* Called from IO context */
146 static int suspend(struct userdata *u) {
147     int32_t ret;
148     pa_assert(u);
149     pa_assert(u->pcm_handle);
150
151     ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
152     if (ret) {
153         pa_log_error("Error closing PCM device %x", ret);
154     }
155     u->pcm_handle = NULL;
156
157     if (u->rtpoll_item) {
158         pa_rtpoll_item_free(u->rtpoll_item);
159         u->rtpoll_item = NULL;
160     }
161
162     pa_log_info("Device suspended...");
163
164     return 0;
165 }
166
167 /* Called from IO context */
168 static int unsuspend(struct userdata *u) {
169     pa_sample_spec sample_spec;
170     int32_t ret;
171     size_t frame_size;
172
173     pa_assert(u);
174     pa_assert(!u->pcm_handle);
175
176     pa_log_info("Trying resume...");
177
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!");
182         goto fail;
183     }
184
185     ret = pa_hal_interface_pcm_open(u->hal_interface,
186               u->card,
187               u->device,
188               DIRECTION_OUT,
189               &sample_spec,
190               u->frag_size / frame_size,
191               u->nfrags,
192               (void **)&u->pcm_handle);
193     if (ret) {
194         pa_log_error("Error opening PCM device %x", ret);
195         goto fail;
196     }
197
198     if (build_pollfd(u) < 0)
199         goto fail;
200
201     u->write_count = 0;
202     u->first = true;
203
204     pa_log_info("Resumed successfully...");
205
206     return 0;
207
208 fail:
209     if (u->pcm_handle) {
210         pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
211         u->pcm_handle = NULL;
212     }
213     return -PA_ERR_IO;
214 }
215
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) {
218     struct userdata *u;
219     int r;
220
221     pa_assert(s);
222     pa_assert_se(u = s->userdata);
223
224     /* It may be that only the suspend cause is changing, in which case there's
225      * nothing to do. */
226     if (new_state == s->thread_info.state)
227         return 0;
228
229     switch (new_state) {
230         case PA_SINK_SUSPENDED: {
231             pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
232             if ((r = suspend(u)) < 0)
233                 return r;
234             break;
235         }
236
237         case PA_SINK_IDLE:
238         case PA_SINK_RUNNING: {
239             if (s->thread_info.state == PA_SINK_INIT) {
240                 if (build_pollfd(u) < 0)
241                     return -PA_ERR_IO;
242             }
243
244             if (s->thread_info.state == PA_SINK_SUSPENDED) {
245                 if ((r = unsuspend(u)) < 0)
246                     return r;
247             }
248             break;
249         }
250
251         case PA_SINK_UNLINKED:
252         case PA_SINK_INIT:
253         case PA_SINK_INVALID_STATE:
254             break;
255     }
256
257     return 0;
258 }
259
260 static int sink_process_msg(
261         pa_msgobject *o,
262         int code,
263         void *data,
264         int64_t offset,
265         pa_memchunk *chunk) {
266
267     struct userdata *u = PA_SINK(o)->userdata;
268
269     switch (code) {
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);
276                 }
277             }
278             *((pa_usec_t*)data) = latency;
279             return 0;
280         }
281     }
282
283     return pa_sink_process_msg(o, code, data, offset, chunk);
284 }
285
286 static void sink_update_requested_latency_cb(pa_sink *s) {
287     struct userdata *u;
288
289     pa_sink_assert_ref(s);
290     pa_assert_se((u = s->userdata));
291
292     u->block_usec = pa_sink_get_requested_latency_within_thread(s);
293
294     if (u->block_usec == (pa_usec_t)-1)
295         u->block_usec = s->thread_info.max_latency;
296
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));
299 }
300
301 static void process_rewind(struct userdata *u, pa_usec_t now) {
302 #if 1
303     /* Rewind not supported */
304     pa_sink_process_rewind(u->sink, 0);
305 #else
306     size_t rewind_nbytes, in_buffer;
307     pa_usec_t delay;
308
309     pa_assert(u);
310
311     rewind_nbytes = u->sink->thread_info.rewind_nbytes;
312
313     if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
314         goto do_nothing;
315
316     pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
317
318     if (u->timestamp <= now)
319         goto do_nothing;
320
321     delay = u->timestamp - now;
322     in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
323
324     if (in_buffer <= 0)
325         goto do_nothing;
326
327     if (rewind_nbytes > in_buffer)
328         rewind_nbytes = in_buffer;
329
330     pa_sink_process_rewind(u->sink, rewind_nbytes);
331     u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
332
333     pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
334     return;
335
336 do_nothing:
337     pa_sink_process_rewind(u->sink, 0);
338 #endif
339 }
340
341 static int process_render(struct userdata *u, pa_usec_t now) {
342     int work_done = 0;
343     size_t ate = 0;
344     void *p;
345     size_t frames_to_write, frame_size;
346     uint32_t avail = 0;
347
348     pa_assert(u);
349
350     /* Fill the buffer up the latency size */
351     while (u->timestamp < now + u->block_usec) {
352         pa_memchunk chunk;
353         frame_size = pa_frame_size(&u->sink->sample_spec);
354         if (frame_size == 0) {
355             pa_log_error("Unexpected frame size zero!");
356             break;
357         }
358
359         pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
360         if ((avail == 0) && !(u->first)) {
361             break;
362         }
363
364         if (u->first) {
365             pa_log_debug("Fill initial buffer");
366
367             frames_to_write = u->frag_size / frame_size;
368             u->timestamp = u->timestamp_written = now;
369         } else {
370             frames_to_write = (u->frag_size / u->nfrags);
371
372             if (frames_to_write > avail)
373                 break;
374         }
375
376         pa_sink_render_full(u->sink, frames_to_write * frame_size, &chunk);
377         p = pa_memblock_acquire(chunk.memblock);
378
379         pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write);
380
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();
385
386         work_done = 1;
387
388         ate += chunk.length;
389         if (ate >= u->sink->thread_info.max_request) {
390             break;
391         }
392     }
393
394     return work_done;
395 }
396
397 static void thread_func(void *userdata) {
398     struct userdata *u = userdata;
399     unsigned short revents = 0;
400
401     pa_assert(u);
402
403     pa_log_debug("Thread starting up");
404
405     if (u->core->realtime_scheduling)
406         pa_thread_make_realtime(u->core->realtime_priority);
407
408     pa_thread_mq_install(&u->thread_mq);
409
410     for (;;) {
411         pa_usec_t now = 0;
412         int ret;
413
414         if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
415             now = pa_rtclock_now();
416
417         if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
418             process_rewind(u, now);
419
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);
423
424             if (work_done < 0)
425                 goto fail;
426
427             if (work_done == 0) {
428                 pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
429             } else {
430                 if (u->first) {
431                     pa_log_info("Starting playback.");
432                     pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
433                     u->first = false;
434                 }
435                 pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
436             }
437         } else {
438             pa_rtpoll_set_timer_disabled(u->rtpoll);
439         }
440
441         /* Hmm, nothing to do. Let's sleep */
442         if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
443             goto fail;
444
445         if (ret == 0)
446             goto finish;
447
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);
456                     u->first = true;
457                     revents = 0;
458                 } else {
459                     //pa_log_debug("Poll wakeup.", revents);
460                 }
461             }
462         }
463     }
464
465 fail:
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);
470
471 finish:
472     pa_log_debug("Thread shutting down");
473 }
474
475 static int parse_to_get_card(const char *modarg_device, char *card) {
476     const char *name_p;
477     char *card_p;
478
479     if (!strchr(modarg_device, ',')) {
480         pa_log_error("Failed to parse device argument : no comma");
481         return -1;
482     }
483
484     name_p = modarg_device;
485     card_p = card;
486     while (*name_p != ',')
487         *(card_p++) = *(name_p++);
488     *card_p = '\0';
489
490     return 0;
491 }
492
493 static int parse_to_get_device(const char *modarg_device, char *device) {
494     const char *comma_p;
495     char *device_p;
496
497     if (!(comma_p = strchr(modarg_device, ','))) {
498         pa_log_error("Failed to parse device argument : no comma");
499         return -1;
500     }
501
502     comma_p++;
503     device_p = device;
504     while (*comma_p != '\0')
505         *(device_p++) = *(comma_p++);
506     *device_p = '\0';
507
508     return 0;
509 }
510
511 int pa__init(pa_module*m) {
512     struct userdata *u = NULL;
513     pa_sample_spec ss;
514     pa_channel_map map;
515     pa_modargs *ma = NULL;
516     pa_sink_new_data data;
517     uint32_t alternate_sample_rate;
518     uint32_t block_msec;
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;
524
525     pa_assert(m);
526
527     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
528         pa_log_error("Failed to parse module arguments.");
529         goto fail;
530     }
531
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");
536         goto fail;
537     }
538
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");
542         goto fail;
543     }
544
545     m->userdata = u = pa_xnew0(struct userdata, 1);
546     u->core = m->core;
547     u->module = m;
548     u->first = true;
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);
552
553     if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
554         pa_log_error("device is invalid");
555         goto fail;
556     }
557
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);
560         goto fail;
561     }
562
563     u->card = pa_xstrdup(card);
564     u->device = pa_xstrdup(device);
565
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.");
571         goto fail;
572     }
573     pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
574
575     /* Optional */
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;
579
580     /* Optional */
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;
584
585     u->timestamp = 0ULL;
586     u->timestamp_written = 0ULL;
587
588     pa_sink_new_data_init(&data);
589     data.driver = __FILE__;
590     data.module = m;
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");
599
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);
606
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);
610         goto fail;
611     }
612
613     u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
614     pa_sink_new_data_done(&data);
615
616     if (!u->sink) {
617         pa_log_error("Failed to create sink object.");
618         goto fail;
619     }
620
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;
625
626     if (pa_hal_interface_pcm_open(u->hal_interface,
627               u->card,
628               u->device,
629               DIRECTION_OUT,
630               &u->sink->sample_spec,
631               u->frag_size / pa_frame_size(&u->sink->sample_spec),
632               u->nfrags,
633               (void **)&u->pcm_handle)) {
634         pa_log_error("Error opening PCM device");
635         goto fail;
636     }
637
638     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
639     pa_sink_set_rtpoll(u->sink, u->rtpoll);
640
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));
643
644     if (!(u->thread = pa_thread_new("tizenaudio-sink", thread_func, u))) {
645         pa_log_error("Failed to create thread.");
646         goto fail;
647     }
648     pa_sink_set_fixed_latency(u->sink, u->block_usec);
649     pa_sink_put(u->sink);
650     pa_modargs_free(ma);
651
652     return 0;
653
654 fail:
655     if (ma)
656         pa_modargs_free(ma);
657
658     pa__done(m);
659     return -1;
660 }
661
662 int pa__get_n_used(pa_module *m) {
663     struct userdata *u;
664
665     pa_assert(m);
666     pa_assert_se((u = m->userdata));
667
668     return pa_sink_linked_by(u->sink);
669 }
670
671 void pa__done(pa_module*m) {
672     struct userdata *u;
673
674     pa_assert(m);
675
676     if (!(u = m->userdata))
677         return;
678
679     if (u->sink)
680         pa_sink_unlink(u->sink);
681
682     if (u->thread) {
683         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
684         pa_thread_free(u->thread);
685     }
686
687     pa_thread_mq_done(&u->thread_mq);
688
689     if (u->sink)
690         pa_sink_unref(u->sink);
691
692     pa_xfree(u->card);
693     pa_xfree(u->device);
694
695     if (u->rtpoll)
696         pa_rtpoll_free(u->rtpoll);
697
698     if (u->pcm_handle) {
699         pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
700         pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
701     }
702
703     pa_xfree(u);
704 }