summaryrefslogtreecommitdiffstats
path: root/src/source.c
blob: a7fc9a64f22970bc7edf9dd34eac8a3e73b69e4b (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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "source.h"
#include "outputstream.h"

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

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

    s->name = name ? strdup(name) : NULL;
    r = idxset_put(core->sources, s, &s->index);
    assert(s->index != IDXSET_INVALID && r >= 0);

    s->core = core;
    s->sample_spec = *spec;
    s->output_streams = idxset_new(NULL, NULL);

    s->link_change_callback = NULL;
    s->userdata = NULL;

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

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

    while ((o = idxset_first(s->output_streams, NULL))) {
        assert(o != j);
        output_stream_free(o);
        j = o;
    }
    idxset_free(s->output_streams, 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);
}

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

    memblockq_push(o->memblockq, chunk, 0);
    return 0;
}

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

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