File: channel.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (418 lines) | stat: -rw-r--r-- 12,850 bytes parent folder | download | duplicates (5)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * FILE:      channel.c
 * AUTHOR(S): Orion Hodson 
 *	
 *
 * Copyright (c) 1999-2001 University College London
 * All rights reserved.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: channel.c,v 1.64 2001/01/08 20:29:53 ucaccsp Exp $";
#endif /* HIDE_SOURCE_STRINGS */
#include "config_unix.h"
#include "config_win32.h"
#include "audio_types.h"
#include "codec_types.h"
#include "channel.h"
#include "playout.h"
#include "ts.h"
#include "memory.h"
#include "debug.h"

typedef struct s_channel_state {
        uint16_t coder;              /* Index of coder in coder table      */
        u_char  *state;              /* Pointer to state relevant to coder */
        uint32_t state_len;          /* The size of that state             */
        uint8_t  units_per_packet:7; /* The number of units per packet     */
        uint8_t  is_encoder:1;       /* For debugging                      */
} channel_state_t;

typedef struct {
        cc_details_t details;
        uint8_t  pt;
        uint8_t  layers;
        int     (*enc_create_state)   (u_char                **state,
                                       uint32_t                *len);
        void    (*enc_destroy_state)  (u_char                **state, 
                                       uint32_t                 len);
        int     (*enc_set_parameters) (u_char                 *state, 
                                       char                   *cmd);
        int     (*enc_get_parameters) (u_char                 *state, 
                                       char                   *cmd, 
                                       uint32_t                 cmd_len);
        int     (*enc_reset)          (u_char                  *state);
        int     (*enc_encode)         (u_char                  *state, 
                                       struct s_pb *in, 
                                       struct s_pb *out, 
                                       uint32_t                  units_per_packet);
        int     (*dec_create_state)   (u_char                 **state,
                                       uint32_t                 *len);
        void    (*dec_destroy_state)  (u_char                 **state, 
                                       uint32_t                  len);
        int     (*dec_reset)          (u_char                  *state);
        int     (*dec_decode)         (u_char                  *state, 
                                       struct s_pb *in, 
                                       struct s_pb *out,
                                       timestamp_t                     now);
        int     (*dec_peek)           (uint8_t                   ccpt,
                                       u_char                  *data,
                                       uint32_t                  len,
                                       uint16_t                 *upp,
                                       uint8_t                  *pt);
        int     (*dec_describe)       (uint8_t  pktpt,
                                       u_char *data,
                                       uint32_t data_len,
                                       char   *outstr,
                                       uint32_t out_len);
} channel_coder_t;

#include "cc_vanilla.h"
#include "cc_rdncy.h"
#include "cc_layered.h"

#define CC_REDUNDANCY_PT 121
#define CC_VANILLA_PT    255
#define CC_LAYERED_PT    125

#define CC_IDX_TO_ID(x) (((x)+1) | 0x0e00)
#define CC_ID_TO_IDX(x) (((x)-1) & 0x000f)

static const channel_coder_t table[] = {
        /* The vanilla coder goes first. Update channel_get_null_coder
         * and channel_coder_get_by_payload if it moves.
         */
        {
                {
                        CC_IDX_TO_ID(0), 
                        "None"
                },
                CC_VANILLA_PT,
                1,
                vanilla_encoder_create, 
                vanilla_encoder_destroy,
                NULL,                   /* No parameters to set ...*/
                NULL,                   /* ... or get. */
                vanilla_encoder_reset,
                vanilla_encoder_encode,
                NULL,
                NULL,
                NULL,
                vanilla_decoder_decode,
                vanilla_decoder_peek,
                vanilla_decoder_describe
        },
        {
                { 
                        CC_IDX_TO_ID(1), 
                        "Redundancy"
                },
                CC_REDUNDANCY_PT,
                1,
                redundancy_encoder_create,
                redundancy_encoder_destroy,
                redundancy_encoder_set_parameters,
                redundancy_encoder_get_parameters,
                redundancy_encoder_reset,
                redundancy_encoder_encode,
                NULL,
                NULL,
                NULL,
                redundancy_decoder_decode,
                redundancy_decoder_peek,
                redundancy_decoder_describe
        },
        {
                {
                        CC_IDX_TO_ID(2),
                        "Layering"
                }, 
                CC_LAYERED_PT,
                LAY_MAX_LAYERS,
                layered_encoder_create,
                layered_encoder_destroy,
                layered_encoder_set_parameters,
                layered_encoder_get_parameters,
                layered_encoder_reset,
                layered_encoder_encode,
                NULL,
                NULL,
                NULL,
                layered_decoder_decode,
                layered_decoder_peek,
                layered_decoder_describe
        }
};

#define CC_NUM_CODERS (sizeof(table)/sizeof(table[0]))

uint32_t
channel_get_coder_count()
{
        return (int)CC_NUM_CODERS;
}

const cc_details_t*
channel_get_coder_details(uint32_t idx)
{
        if (idx < CC_NUM_CODERS) {
                return &table[idx].details;
        } 
        return NULL;
}

const cc_details_t*
channel_get_coder_identity(channel_state_t *cs)
{
        assert(cs->coder < CC_NUM_CODERS);
        return &table[cs->coder].details;
}

const cc_details_t*
channel_get_null_coder(void)
{
        return &table[0].details;
}

/* The create, destroy, and reset functions take the same arguments and so use
 * is_encoder to determine which function in the table to call.  It's dirty
 * but it saves typing.  This should be undone at some time [oh]
 */

int
_channel_coder_create(cc_id_t id, channel_state_t **ppcs, int is_encoder)
{
        channel_state_t *pcs;
        int (*create_state)(u_char**, uint32_t *len);

        pcs = (channel_state_t*)xmalloc(sizeof(channel_state_t));
        if (pcs == NULL) {
                return FALSE;
        }

        *ppcs = pcs;

        pcs->coder = (uint16_t)CC_ID_TO_IDX(id);
        assert(pcs->coder < CC_NUM_CODERS);

        pcs->units_per_packet = 2;
        pcs->is_encoder       = is_encoder;

        if (is_encoder) {
                create_state = table[pcs->coder].enc_create_state;
		debug_msg("Created encoder: \"%s\"\n", table[pcs->coder].details.name);
        } else {
                create_state = table[pcs->coder].dec_create_state;
		debug_msg("Created decoder: \"%s\"\n", table[pcs->coder].details.name);
        }

        if (create_state) {
                create_state(&pcs->state, &pcs->state_len);
        } else {
                pcs->state     = NULL;
		pcs->state_len = 0;
	}

        return TRUE;
}

void
_channel_coder_destroy(channel_state_t **ppcs, int is_encoder)
{
        channel_state_t *pcs = *ppcs;

        void (*destroy_state)(u_char**, uint32_t);

        assert(is_encoder == pcs->is_encoder);

        if (is_encoder) {
                destroy_state = table[pcs->coder].enc_destroy_state;
        } else {
                destroy_state = table[pcs->coder].dec_destroy_state;
        }

        if (destroy_state) {
                destroy_state(&pcs->state, pcs->state_len);
                pcs->state_len = 0;
        }

        assert(pcs->state     == NULL);
        assert(pcs->state_len == 0);

        xfree(pcs);
        *ppcs = NULL;
}

int
_channel_coder_reset(channel_state_t *pcs, int is_encoder)
{
        int (*reset) (u_char *state);
        
        assert(is_encoder == pcs->is_encoder);

        if (is_encoder) {
                reset = table[pcs->coder].enc_reset; 
        } else {
                reset = table[pcs->coder].dec_reset; 
        }
        
        return (reset != NULL) ? reset(pcs->state) : TRUE;
}

/* Encoder specifics */

int
channel_encoder_set_units_per_packet(channel_state_t *cs, uint16_t units)
{
        /* This should not be hardcoded, it should be based on packet 
         *size [oth] 
         */
        assert(cs->is_encoder);
        if (units != 0 && units <= MAX_UNITS_PER_PACKET) {
                cs->units_per_packet = (u_char)units;
                return TRUE;
        }
        return FALSE;
}

uint16_t 
channel_encoder_get_units_per_packet(channel_state_t *cs)
{
        assert(cs->is_encoder);
        return cs->units_per_packet;
}

int
channel_encoder_set_parameters(channel_state_t *cs, char *cmd)
{
        if (table[cs->coder].enc_set_parameters) {
                return table[cs->coder].enc_set_parameters(cs->state, cmd);
        }
        return TRUE;
}

int
channel_encoder_get_parameters(channel_state_t *cs, char *cmd, int cmd_len)
{
        if (table[cs->coder].enc_get_parameters) {
                return table[cs->coder].enc_get_parameters(cs->state, cmd, cmd_len);
        } else {
                assert(cmd_len != 0);
                cmd[0] = 0;
        }
        return TRUE;
}

int
channel_encoder_encode(channel_state_t         *cs, 
                       struct s_pb *media_buffer, 
                       struct s_pb *channel_buffer)
{
        assert(table[cs->coder].enc_encode != NULL);
        return table[cs->coder].enc_encode(cs->state, media_buffer, channel_buffer, cs->units_per_packet);
}

int
channel_decoder_decode(channel_state_t         *cs, 
                       struct s_pb *media_buffer, 
                       struct s_pb *channel_buffer,
                       timestamp_t                     now)
{
        assert(table[cs->coder].dec_decode != NULL);
        return table[cs->coder].dec_decode(cs->state, media_buffer, channel_buffer, now);
}

int
channel_decoder_matches(cc_id_t          id,
                        channel_state_t *cs)
{
        uint32_t coder = CC_ID_TO_IDX(id);
        return (coder == cs->coder);
}

int
channel_verify_and_stat(cc_id_t  cid,
                        uint8_t   pktpt,
                        u_char  *data,
                        uint32_t  data_len,
                        uint16_t *units_per_packet,
                        u_char  *codec_pt)
{
        uint32_t idx = CC_ID_TO_IDX(cid);
        assert(idx < CC_NUM_CODERS);
        return table[idx].dec_peek(pktpt, data, data_len, units_per_packet, codec_pt);
}

int 
channel_describe_data(cc_id_t cid,
                      uint8_t  pktpt,
                      u_char *data,
                      uint32_t data_len,
                      char *outstr,
                      uint32_t out_len)
{
        uint32_t idx = CC_ID_TO_IDX(cid);
        assert(idx < CC_NUM_CODERS);

        assert(outstr  != NULL);
        assert(out_len != 0);

        if (table[idx].dec_describe) {
                return (table[idx].dec_describe(pktpt, data, data_len, outstr, out_len-1));
        } 

        strncpy(outstr, "Not implemented", out_len-1);
        outstr[out_len-1] = '\0'; /* Always zero terminated */
        return TRUE;
}
                                   
cc_id_t
channel_coder_get_by_payload(uint8_t payload)
{
	uint32_t i = 0;

        assert((payload & 0x80) == 0);

        for(i = 0; i < CC_NUM_CODERS; i++) {
                if (table[i].pt == payload) {
/*			debug_msg("Found channel coder for payload type %d \"%s\"\n", payload, table[i].details.name); */
                        return CC_IDX_TO_ID(i);
                }
        }
        /* Return vanilla if not found */
/*	debug_msg("No channel coder for payload type %d\n", payload); */
        return CC_IDX_TO_ID(0);        
}

uint8_t
channel_coder_get_payload(channel_state_t *st, uint8_t media_pt)
{
        assert(st->coder <= CC_NUM_CODERS);

        if (table[st->coder].pt == CC_VANILLA_PT) {
                return media_pt;
        }
        return table[st->coder].pt;
}

int
channel_coder_exist_payload(uint8_t pt)
{
        uint32_t i;
        for(i = 0; i < CC_NUM_CODERS; i++) {
                if (table[i].pt == pt) {
                        return TRUE;
                }
        }
        return FALSE;       
}

uint8_t
channel_coder_get_layers(cc_id_t cid)
{
        uint32_t idx = CC_ID_TO_IDX(cid);
        assert(idx < CC_NUM_CODERS);

        return (table[idx].layers);
}