summaryrefslogtreecommitdiffstats
path: root/src/source.c
blob: deacfb3d69c95b9d17b7d087ef986558b97e260f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "source.h"
#include "sourceoutput.h"
#include "strbuf.h"
#include "namereg.h"

struct source* source_new(struct core *core, const char *name, int fail, const struct pa_sample_spec *spec) {
    struct source *s;
    int r;
    assert(core && spec);

    s = malloc(sizeof(struct source));
    assert(s);

    if (!(name = namereg_register(core, name, NAMEREG_SOURCE, s, fail))) {
        free(s);
        return NULL;
    }

    s->name = strdup(name);
    s->core = core;
    s->sample_spec = *spec;
    s->outputs = idxset_new(NULL, NULL);

    s->notify = NULL;
    s->userdata = NULL;

    r = idxset_put(core->sources, s, &s->index);
    assert(s->index != IDXSET_INVALID && r >= 0);

    fprintf(stderr, "source: created %u \"%s\"\n", s->index, s->name);
    
    return s;
}

void source_free(struct source *s) {
    struct source_output *o, *j = NULL;
    assert(s);

    namereg_unregister(s->core, s->name);
    
    while ((o = idxset_first(s->outputs, NULL))) {
        assert(o != j);
        source_output_kill(o);
        j = o;
    }
    idxset_free(s->outputs, NULL, NULL);
    
    idxset_remove_by_data(s->core->sources, s, NULL);

    fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name);

    free(s->name);
    free(s);
}

void source_notify(struct source*s) {
    assert(s);

    if (s->notify)
        s->notify(s);
}

static int do_post(void *p, uint32_t index, int *del, void*userdata) {
    struct memchunk *chunk = userdata;
    struct source_output *o = p;
    assert(o && o->push && del && chunk);

    o->push(o, chunk);
    return 0;
}

void source_post(struct source*s, struct memchunk *chunk) {
    assert(s && chunk);

    idxset_foreach(s->outputs, do_post, chunk);
}

struct source* source_get_default(struct core *c) {
    struct source *source;
    assert(c);

    if ((source = idxset_get_by_index(c->sources, c->default_source_index)))
        return source;

    if (!(source = idxset_first(c->sources, &c->default_source_index)))
        return NULL;

    fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index);
    return source;
}

char *source_list_to_string(struct core *c) {
    struct strbuf *s;
    struct source *source, *default_source;
    uint32_t index = IDXSET_INVALID;
    assert(c);

    s = strbuf_new();
    assert(s);

    strbuf_printf(s, "%u source(s) available.\n", idxset_ncontents(c->sources));

    default_source = source_get_default(c);
    
    for (source = idxset_first(c->sources, &index); source; source = idxset_next(c->sources, &index))
        strbuf_printf(s, "  %c index: %u, name: <%s>\n", source == default_source ? '*' : ' ', source->index, source->name);
    
    return strbuf_tostring_free(s);
}