c950b54d58638a0fe69f004aa9e10bc8e7709155
[profile/ivi/pulseaudio.git] / 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
9 struct source* source_new(struct core *core, const char *name, const struct sample_spec *spec) {
10     struct source *s;
11     int r;
12     assert(core && spec);
13
14     s = malloc(sizeof(struct source));
15     assert(s);
16
17     s->name = name ? strdup(name) : NULL;
18     s->core = core;
19     s->sample_spec = *spec;
20     s->outputs = idxset_new(NULL, NULL);
21
22     s->notify = NULL;
23     s->userdata = NULL;
24
25     r = idxset_put(core->sources, s, &s->index);
26     assert(s->index != IDXSET_INVALID && r >= 0);
27
28     fprintf(stderr, "source: created %u \"%s\"\n", s->index, s->name);
29     
30     return s;
31 }
32
33 void source_free(struct source *s) {
34     struct source_output *o, *j = NULL;
35     assert(s);
36
37     while ((o = idxset_first(s->outputs, NULL))) {
38         assert(o != j);
39         source_output_kill(o);
40         j = o;
41     }
42     idxset_free(s->outputs, NULL, NULL);
43     
44     idxset_remove_by_data(s->core->sources, s, NULL);
45
46     fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);
47
48     free(s->name);
49     free(s);
50 }
51
52 void source_notify(struct source*s) {
53     assert(s);
54
55     if (s->notify)
56         s->notify(s);
57 }
58
59 static int do_post(void *p, uint32_t index, int *del, void*userdata) {
60     struct memchunk *chunk = userdata;
61     struct source_output *o = p;
62     assert(o && o->push && del && chunk);
63
64     o->push(o, chunk);
65     return 0;
66 }
67
68 void source_post(struct source*s, struct memchunk *chunk) {
69     assert(s && chunk);
70
71     idxset_foreach(s->outputs, do_post, chunk);
72 }