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

#include "inputstream.h"

struct input_stream* input_stream_new(struct sink *s, struct sample_spec *spec, const char *name) {
    struct input_stream *i;
    int r;
    assert(s && spec);

    i = malloc(sizeof(struct input_stream));
    assert(i);
    i->name = name ? strdup(name) : NULL;
    i->sink = s;
    i->spec = *spec;
    i->kill = NULL;
    i->kill_userdata = NULL;

    i->memblockq = memblockq_new(bytes_per_second(spec)*5, sample_size(spec));
    assert(i->memblockq);
    
    assert(s->core);
    r = idxset_put(s->core->input_streams, i, &i->index);
    assert(r == 0 && i->index != IDXSET_INVALID);
    r = idxset_put(s->input_streams, i, NULL);
    assert(r == 0);
    
    return i;    
}

void input_stream_free(struct input_stream* i) {
    assert(i);

    memblockq_free(i->memblockq);

    assert(i->sink && i->sink->core);
    idxset_remove_by_data(i->sink->core->input_streams, i, NULL);
    idxset_remove_by_data(i->sink->input_streams, i, NULL);
    
    free(i->name);
    free(i);
}

void input_stream_notify_sink(struct input_stream *i) {
    assert(i);

    if (memblockq_is_empty(i->memblockq))
        return;
    
    sink_notify(i->sink);
}

void input_stream_set_kill_callback(struct input_stream *i, void (*kill)(struct input_stream*i, void *userdata), void *userdata) {
    assert(i && kill);
    i->kill = kill;
    i->kill_userdata = userdata;
}


void input_stream_kill(struct input_stream*i) {
    assert(i);

    if (i->kill)
        i->kill(i, i->kill_userdata);
}