]> code.delx.au - pulseaudio/blob - polyp/source.c
2c0caca077b884e8f39acf050e96225543cb01fc
[pulseaudio] / polyp / source.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; 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 <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "source.h"
32 #include "source-output.h"
33 #include "namereg.h"
34 #include "xmalloc.h"
35 #include "subscribe.h"
36 #include "log.h"
37
38 struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
39 struct pa_source *s;
40 char st[256];
41 int r;
42 assert(core && spec && name && *name);
43
44 s = pa_xmalloc(sizeof(struct pa_source));
45
46 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
47 pa_xfree(s);
48 return NULL;
49 }
50
51 s->ref = 1;
52 s->state = PA_SOURCE_RUNNING;
53
54 s->name = pa_xstrdup(name);
55 s->description = NULL;
56
57 s->owner = NULL;
58 s->core = core;
59 s->sample_spec = *spec;
60 s->outputs = pa_idxset_new(NULL, NULL);
61 s->monitor_of = NULL;
62
63 s->notify = NULL;
64 s->userdata = NULL;
65
66 r = pa_idxset_put(core->sources, s, &s->index);
67 assert(s->index != PA_IDXSET_INVALID && r >= 0);
68
69 pa_sample_spec_snprint(st, sizeof(st), spec);
70 pa_log(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
71
72 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
73
74 return s;
75 }
76
77 void pa_source_disconnect(struct pa_source *s) {
78 struct pa_source_output *o, *j = NULL;
79 assert(s && s->state == PA_SOURCE_RUNNING);
80
81 pa_namereg_unregister(s->core, s->name);
82
83 while ((o = pa_idxset_first(s->outputs, NULL))) {
84 assert(o != j);
85 pa_source_output_kill(o);
86 j = o;
87 }
88
89 pa_idxset_remove_by_data(s->core->sources, s, NULL);
90 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
91
92 s->notify = NULL;
93
94 s->state = PA_SOURCE_DISCONNECTED;
95 }
96
97 static void source_free(struct pa_source *s) {
98 assert(s && !s->ref);
99
100 if (s->state != PA_SOURCE_DISCONNECTED)
101 pa_source_disconnect(s);
102
103 pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name);
104
105 pa_idxset_free(s->outputs, NULL, NULL);
106
107 pa_xfree(s->name);
108 pa_xfree(s->description);
109 pa_xfree(s);
110 }
111
112 void pa_source_unref(struct pa_source *s) {
113 assert(s && s->ref >= 1);
114
115 if (!(--s->ref))
116 source_free(s);
117 }
118
119 struct pa_source* pa_source_ref(struct pa_source *s) {
120 assert(s && s->ref >= 1);
121 s->ref++;
122 return s;
123 }
124
125 void pa_source_notify(struct pa_source*s) {
126 assert(s && s->ref >= 1);
127
128 if (s->notify)
129 s->notify(s);
130 }
131
132 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
133 struct pa_memchunk *chunk = userdata;
134 struct pa_source_output *o = p;
135 assert(o && o->push && del && chunk);
136
137 pa_source_output_push(o, chunk);
138 return 0;
139 }
140
141 void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) {
142 assert(s && s->ref >= 1 && chunk);
143
144 pa_source_ref(s);
145 pa_idxset_foreach(s->outputs, do_post, chunk);
146 pa_source_unref(s);
147 }
148
149 void pa_source_set_owner(struct pa_source *s, struct pa_module *m) {
150 assert(s);
151 s->owner = m;
152 }