1 | //meta.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int stream_meta_set (int id, int type, char * name, char * val) { |
---|
6 | int i; |
---|
7 | struct roar_stream_server * s = g_streams[id]; |
---|
8 | |
---|
9 | if ( s == NULL ) |
---|
10 | return -1; |
---|
11 | |
---|
12 | for (i = 0; i < ROAR_META_MAX_PER_STREAM; i++) |
---|
13 | if ( s->meta[i].type == type ) { |
---|
14 | s->meta[i].type = ROAR_META_TYPE_NONE; |
---|
15 | if ( s->meta[i].value ) |
---|
16 | free(s->meta[i].value); |
---|
17 | s->meta[i].value = NULL; |
---|
18 | } |
---|
19 | |
---|
20 | return stream_meta_add(id, type, name, val); |
---|
21 | } |
---|
22 | |
---|
23 | int stream_meta_add (int id, int type, char * name, char * val) { |
---|
24 | int i; |
---|
25 | char * c; |
---|
26 | struct roar_stream_server * s = g_streams[id]; |
---|
27 | |
---|
28 | if ( s == NULL ) |
---|
29 | return -1; |
---|
30 | |
---|
31 | for (i = 0; i < ROAR_META_MAX_PER_STREAM; i++) { |
---|
32 | if ( s->meta[i].type == ROAR_META_TYPE_NONE ) { |
---|
33 | s->meta[i].type = type; |
---|
34 | |
---|
35 | if ( name == NULL ) { |
---|
36 | s->meta[i].key[0] = 0; |
---|
37 | } else { |
---|
38 | strncpy(s->meta[i].key, name, ROAR_META_MAX_NAMELEN); |
---|
39 | } |
---|
40 | |
---|
41 | if ( (c = malloc(strlen(val)+1)) == NULL ) { |
---|
42 | s->meta[i].type = ROAR_META_TYPE_NONE; |
---|
43 | s->meta[i].key[0] = 0; |
---|
44 | return -1; |
---|
45 | } |
---|
46 | |
---|
47 | strcpy(c, val); |
---|
48 | s->meta[i].value = c; |
---|
49 | |
---|
50 | ROAR_DBG("stream_meta_add(id=%i, type=%i, name='%s', val='%s') = 0", id, type, name, val); |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | return -1; |
---|
57 | } |
---|
58 | |
---|
59 | int stream_meta_get (int id, int type, char * name, char * val, size_t len) { |
---|
60 | int i, vallen; |
---|
61 | struct roar_stream_server * s = g_streams[id]; |
---|
62 | |
---|
63 | if ( s == NULL ) |
---|
64 | return -1; |
---|
65 | |
---|
66 | for (i = 0; i < ROAR_META_MAX_PER_STREAM; i++) { |
---|
67 | if ( s->meta[i].type == type ) { |
---|
68 | if ( name != NULL ) |
---|
69 | if ( strncmp(s->meta[i].key, name, ROAR_META_MAX_NAMELEN) != 0 ) |
---|
70 | continue; |
---|
71 | |
---|
72 | if ( s->meta[i].value == NULL ) |
---|
73 | return -1; |
---|
74 | |
---|
75 | if ( (vallen = strlen(s->meta[i].value)) > (len - 1) ) { |
---|
76 | ROAR_DBG("stream_meta_get(*): val too small: need %i have %i", vallen, len); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | strncpy(val, s->meta[i].value, vallen); |
---|
81 | val[vallen] = 0; |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | return -1; |
---|
88 | } |
---|
89 | |
---|
90 | int stream_meta_list (int id, int * types, size_t len) { |
---|
91 | int i, j; |
---|
92 | int have = 0; |
---|
93 | int found; |
---|
94 | struct roar_stream_server * s = g_streams[id]; |
---|
95 | |
---|
96 | if ( s == NULL ) |
---|
97 | return -1; |
---|
98 | |
---|
99 | if ( len < ROAR_META_MAX_PER_STREAM ) // TODO: find a way we do not need this |
---|
100 | return -1; |
---|
101 | |
---|
102 | types[0] = -1; |
---|
103 | |
---|
104 | for (i = 0; i < ROAR_META_MAX_PER_STREAM; i++) { |
---|
105 | found = 0; |
---|
106 | for (j = 0; j < have; j++) |
---|
107 | if ( types[j] == s->meta[i].type ) { |
---|
108 | found = 1; |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | if ( !found ) |
---|
113 | types[have++] = s->meta[i].type; |
---|
114 | } |
---|
115 | |
---|
116 | return have; |
---|
117 | } |
---|
118 | |
---|
119 | int stream_meta_clear (int id) { |
---|
120 | int i; |
---|
121 | struct roar_stream_server * s = NULL; |
---|
122 | |
---|
123 | if ( id < 0 || id > ROAR_STREAMS_MAX ) { |
---|
124 | ROAR_ERR("stream_meta_clear(id=%i): Can not clear meta data on stream: invalid stream ID", id); |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | s = g_streams[id]; |
---|
129 | |
---|
130 | if ( s == NULL ) |
---|
131 | return -1; |
---|
132 | |
---|
133 | for (i = 0; i < ROAR_META_MAX_PER_STREAM; i++) { |
---|
134 | s->meta[i].type = ROAR_META_TYPE_NONE; |
---|
135 | if ( s->meta[i].value ) |
---|
136 | free(s->meta[i].value); |
---|
137 | s->meta[i].value = NULL; |
---|
138 | s->meta[i].key[0] = 0; |
---|
139 | } |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | //ll |
---|