]> code.delx.au - pulseaudio/blob - src/source.c
c0eec3ead045b7dc960255551c40542c6a5747d2
[pulseaudio] / src / source.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "source.h"
7 #include "sourceoutput.h"
8 #include "namereg.h"
9
10 struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
11 struct pa_source *s;
12 char st[256];
13 int r;
14 assert(core && spec && name);
15
16 s = malloc(sizeof(struct pa_source));
17 assert(s);
18
19 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
20 free(s);
21 return NULL;
22 }
23
24 s->name = strdup(name);
25 s->description = NULL;
26
27 s->owner = NULL;
28 s->core = core;
29 s->sample_spec = *spec;
30 s->outputs = pa_idxset_new(NULL, NULL);
31 s->monitor_of = NULL;
32
33 s->notify = NULL;
34 s->userdata = NULL;
35
36 r = pa_idxset_put(core->sources, s, &s->index);
37 assert(s->index != PA_IDXSET_INVALID && r >= 0);
38
39 pa_sample_snprint(st, sizeof(st), spec);
40 fprintf(stderr, "source: created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
41
42 return s;
43 }
44
45 void pa_source_free(struct pa_source *s) {
46 struct pa_source_output *o, *j = NULL;
47 assert(s);
48
49 pa_namereg_unregister(s->core, s->name);
50
51 while ((o = pa_idxset_first(s->outputs, NULL))) {
52 assert(o != j);
53 pa_source_output_kill(o);
54 j = o;
55 }
56 pa_idxset_free(s->outputs, NULL, NULL);
57
58 pa_idxset_remove_by_data(s->core->sources, s, NULL);
59
60 fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
61
62 free(s->name);
63 free(s->description);
64 free(s);
65 }
66
67 void pa_source_notify(struct pa_source*s) {
68 assert(s);
69
70 if (s->notify)
71 s->notify(s);
72 }
73
74 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
75 struct pa_memchunk *chunk = userdata;
76 struct pa_source_output *o = p;
77 assert(o && o->push && del && chunk);
78
79 pa_source_output_push(o, chunk);
80 return 0;
81 }
82
83 void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) {
84 assert(s && chunk);
85
86 pa_idxset_foreach(s->outputs, do_post, chunk);
87 }
88
89 struct pa_source* pa_source_get_default(struct pa_core *c) {
90 struct pa_source *source;
91 assert(c);
92
93 if ((source = pa_idxset_get_by_index(c->sources, c->default_source_index)))
94 return source;
95
96 if (!(source = pa_idxset_first(c->sources, &c->default_source_index)))
97 return NULL;
98
99 fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
100 return source;
101 }
102
103 void pa_source_set_owner(struct pa_source *s, struct pa_module *m) {
104 assert(s);
105 s->owner = m;
106 }