]> code.delx.au - pulseaudio/blob - src/pulsecore/play-memblockq.c
allow hooking into the process of creating playback streams. To implement this I...
[pulseaudio] / src / pulsecore / play-memblockq.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <assert.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/sink-input.h>
34 #include <pulsecore/gccmacro.h>
35
36 #include "play-memblockq.h"
37
38 static void sink_input_kill(pa_sink_input *i) {
39 pa_memblockq *q;
40 assert(i);
41 assert(i->userdata);
42
43 q = i->userdata;
44
45 pa_sink_input_disconnect(i);
46 pa_sink_input_unref(i);
47
48 pa_memblockq_free(q);
49 }
50
51 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
52 pa_memblockq *q;
53 assert(i);
54 assert(chunk);
55 assert(i->userdata);
56
57 q = i->userdata;
58
59 return pa_memblockq_peek(q, chunk);
60 }
61
62 static void si_kill(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
63 sink_input_kill(i);
64 }
65
66 static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
67 pa_memblockq *q;
68
69 assert(i);
70 assert(length > 0);
71 assert( i->userdata);
72
73 q = i->userdata;
74
75 pa_memblockq_drop(q, chunk, length);
76
77 if (pa_memblockq_get_length(q) <= 0)
78 pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
79 }
80
81 int pa_play_memblockq(
82 pa_sink *sink,
83 const char *name,
84 const pa_sample_spec *ss,
85 const pa_channel_map *map,
86 pa_memblockq *q,
87 pa_cvolume *volume) {
88
89 pa_sink_input *si;
90 pa_sink_input_new_data data;
91
92 assert(sink);
93 assert(ss);
94 assert(q);
95
96 if (pa_memblockq_get_length(q) <= 0) {
97 pa_memblockq_free(q);
98 return 0;
99 }
100
101 if (volume && pa_cvolume_is_muted(volume)) {
102 pa_memblockq_free(q);
103 return 0;
104 }
105
106 pa_sink_input_new_data_init(&data);
107 data.sink = sink;
108 data.name = name;
109 data.driver = __FILE__;
110 pa_sink_input_new_data_set_channel_map(&data, map);
111 pa_sink_input_new_data_set_sample_spec(&data, ss);
112 pa_sink_input_new_data_set_volume(&data, volume);
113
114 if (!(si = pa_sink_input_new(sink->core, &data, 0)))
115 return -1;
116
117 si->peek = sink_input_peek;
118 si->drop = sink_input_drop;
119 si->kill = sink_input_kill;
120
121 si->userdata = q;
122
123 pa_sink_notify(si->sink);
124
125 return 0;
126 }