File: auddev_atm.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 (566 lines) | stat: -rw-r--r-- 15,967 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 * FILE:     auddev_atm.c
 * PROGRAM:  RAT
 * AUTHORS:  Julian Cable, Dimitris Terzis
 * MODS:     Orion Hodson
 *
 * auddev_atm acts like an audio device, only it reads and writes raw
 * AAL1 ATM frames containing alaw coded samples.  It assumes that RAT
 * is launched from a process that handles the atm set up and writes
 * the atm file descriptor to a file called "atm_socket".  There are
 * obviously other ways to communicate this info, but this suffices.
 *
 * The ATM stack in question is the Linux one, it may work with other
 * stacks but we don't have access to any suitable ATM equipment at
 * UCL to investigate (or even test this file).
 *
 * The real work here was done by Julian and Dimitris, who not only
 * made initial version of this file, but also chased up ATM stack
 * bugs.  OH fixed atm_audio_supports, simplified read/writes, and
 * changed atm_audio_read to read as many frames as available.
 *
 * Copyright (c) 2000 Nortel Networks
 *           (c) 1995-2001 University College London 
 * All rights reserved.  
 */

#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: auddev_atm.c,v 1.5 2001/01/08 20:29:47 ucaccsp Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <atm.h>

#include <string.h>
#include "config_unix.h"
#include "debug.h"
#include "audio_types.h"
#include "audio_fmt.h"
#include "auddev_atm.h"
#include "codec_g711.h"

struct AUDIO
{
	int port;
};

static struct {
	unsigned long atmhdr;
	unsigned char rxresidue_buf[47];
        u_char       *rxresidue;
	unsigned char txresidue[47];
        int rxresidue_bytes; /* bytes available after rxresidue */
	int txresidue_bytes;
	int seqno;
	int monitor_gain;
	int output_muted;
	struct AUDIO play;
	struct AUDIO record;
}	dev_info;

/* AAL1-specific ATM header bytes */
static unsigned char aal1hdr[] = {0x00, 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74};

static int audio_fd = -1;

enum { AUDIO_LINE_OUT,  AUDIO_LINE_IN };

#define bat_to_device(x)	((x )
#define device_to_bat(x)	((x))

static int atm_socket;

int
atm_audio_device_count()
{
        return 1;
}

char*
atm_audio_device_name(audio_desc_t ad)
{
        UNUSED(ad);
        return "ATM Network Audio Device (PCM A-law)";
}

int
atm_audio_supports(audio_desc_t ad, audio_format *fmt)
{

        UNUSED(ad);

        /* Only check sample rate and channels */
	if ( fmt->sample_rate != 8000 || fmt->channels != 1) {
                return FALSE;
        }

        return TRUE;
}

/* Try to open the audio device.                        */
/* Essentially, we read an ATM socket file descriptor, */
/* passed to RAT from the command line, from a file */
/* Returns TRUE if ok, 0 otherwise. */
int
atm_audio_open(audio_desc_t ad, audio_format* ifmt, audio_format* ofmt)
{

        int len;
	struct sockaddr_atmpvc vcc_address;
	FILE *f = fopen("atm_socket", "r");

	if (f == NULL) {
                debug_msg("ATM socket file not found");
		return FALSE;
	}

        if (atm_audio_supports(ifmt) == FALSE) {
                /* Should never get here */
                debug_msg("ATM input format not supported\n");
                return FALSE;
        }

        if (atm_audio_supports(ifmt) == FALSE) {
                /* Should never get here */
                debug_msg("ATM output format not supported\n");
                return FALSE;
        }
        
	fread(&atm_socket, sizeof(atm_socket), 1, f);
	fclose(f);

        len = sizeof(vcc_address);

        if (audio_fd != -1) {
                debug_msg("Device already open!");
                atm_audio_close(ad);
                return FALSE;
        }

        if (getsockopt(atm_socket, SOL_ATM, SO_ATMPVC, (char *)&vcc_address, &len) >= 0) {
		unsigned gfc=0, vpi, vci, type=0, clp=0;
		vpi = vcc_address.sap_addr.vpi;
		vci = vcc_address.sap_addr.vci;
		audio_fd = atm_socket;
		dev_info.atmhdr = (gfc << ATM_HDR_GFC_SHIFT) | (vpi << ATM_HDR_VPI_SHIFT) |
                        (vci << ATM_HDR_VCI_SHIFT) |       (type << ATM_HDR_PTI_SHIFT) | clp;
                
		dev_info.txresidue_bytes    = 0;
		dev_info.rxresidue_bytes    = 0;
		dev_info.rxresidue          = dev_info.rxresidue_buf;
		dev_info.seqno              = 0;
		memset(dev_info.txresidue, 0, sizeof(dev_info.txresidue));
		memset(dev_info.rxresidue, 0, sizeof(dev_info.txresidue));
                
		dev_info.monitor_gain       = 0;
		dev_info.output_muted       = 0; /* 0==not muted */
		dev_info.play.port          = AUDIO_LINE_OUT;
		dev_info.record.port        = AUDIO_LINE_IN;

		audio_format_change_encoding(ifmt, DEV_PCMA);
		audio_format_change_encoding(ofmt, DEV_PCMA);

		debug_msg("ATM audio device open (fd=%d, vpi=%d, vci=%d)\n", audio_fd, vpi, vci);
	        atm_audio_drain(ad);

		return audio_fd;
	} else {
                debug_msg("ATM socket descriptor is invalid");
		return FALSE;
	}
}

/* Close the audio device */
void
atm_audio_close(audio_desc_t ad)
{
        UNUSED(ad);
        assert(audio_fd > 0);

	if (audio_fd <= 0) {
                debug_msg("Invalid desc");
		return;
        }

	close(audio_fd);
	audio_fd = -1;
}

/* Flush input buffer */
void
atm_audio_drain(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);

	ioctl(audio_fd, I_FLUSH, (caddr_t)FLUSHR);
}

/* Gain and volume values are in the range 0 - MAX_AMP */

void
atm_audio_set_igain(audio_desc_t ad, int gain)
{
        UNUSED(gain);
        UNUSED(ad); assert(audio_fd > 0);
}

int
atm_audio_get_igain(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);

        return 0;
}

void
atm_audio_set_ogain(audio_desc_t ad, int vol)
{
        UNUSED(vol);
        UNUSED(ad); assert(audio_fd > 0);
}

int
atm_audio_get_ogain(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);

	return 0;
}

void
atm_audio_loopback(audio_desc_t ad, int gain)
{
        UNUSED(gain);
        UNUSED(ad); assert(audio_fd > 0);

}

#define ADA_CELL_SZ  52
#define ADA_CELL_HEADER_SZ 5

/* atm_audio_read: modified to read as many atm frames as are available (oh) */

int
atm_audio_read(audio_desc_t ad, u_char *buf, int buf_bytes)
{
	int len, avail, done = 0;
	char cellbuf[ADA_CELL_SZ];

	UNUSED(ad);

        assert(dev_info.rxresidue_bytes >= 0);
        assert((uint32_t)dev_info.rxresidue_bytes < (uint32_t)sizeof(dev_info.rxresidue_buf));
	assert((uint32_t)dev_info.rxresidue - (uint32_t)dev_info.rxresidue_buf <= sizeof(dev_info.rxresidue_buf));
	assert(dev_info.rxresidue - dev_info.rxresidue_buf >= 0);

        if (dev_info.rxresidue_bytes > 0) {
                if (buf_bytes >= dev_info.rxresidue_bytes) {
                        /* big read that completely drains the residue */
                        memcpy(buf, dev_info.rxresidue, dev_info.rxresidue_bytes);
                        done                    += dev_info.rxresidue_bytes;
                        dev_info.rxresidue_bytes = 0;
                } else {
                        /* little read */
                        memcpy(buf, dev_info.rxresidue, buf_bytes);
                        dev_info.rxresidue_bytes -= buf_bytes;
                        dev_info.rxresidue       += buf_bytes;
                        done                     += buf_bytes;
			return done;
                }
        }

        assert(dev_info.rxresidue_bytes >= 0);
        assert((uint32_t)dev_info.rxresidue_bytes < (uint32_t)sizeof(dev_info.rxresidue_buf));
	assert(dev_info.txresidue_bytes >= 0);
	assert((uint32_t)dev_info.txresidue_bytes <= sizeof(dev_info.txresidue));

        /* Read as much audio as is available */
        len = 0;
        while ((ioctl(audio_fd, FIONREAD, &avail) >= 0) && 
               (buf_bytes - done) >= (ADA_CELL_SZ - ADA_CELL_HEADER_SZ)) {
                len = read(audio_fd, cellbuf, sizeof(cellbuf));
                if (len <= 0) {
                        break;
                }
                assert(len == ADA_CELL_SZ);
                memcpy(buf + done, cellbuf + ADA_CELL_HEADER_SZ, ADA_CELL_SZ - ADA_CELL_HEADER_SZ);
                done += ADA_CELL_SZ - ADA_CELL_HEADER_SZ;
		/*                debug_msg("read frame %d\n", (int)cellbuf[4]); */
		assert(dev_info.rxresidue_buf + dev_info.rxresidue_bytes == dev_info.rxresidue);
        }
        
        if (errno != 0) {
                debug_msg("atm audio read error (%d): len %d avail %d done %d of %d bytes\n", 
                          errno, len, avail, done, buf_bytes);
                /* avail =  0 ioctl failed          */
                /* len   = -1 read failed           */
                /* len   =  0 socket closed by peer */
                errno = 0;
                return done;
        }

        if (buf_bytes - done < ADA_CELL_SZ - ADA_CELL_HEADER_SZ && buf_bytes - done != 0) {
                /* Copy bytes left over into residue buffer */
                int over;
                over = buf_bytes - done;
		assert(dev_info.rxresidue == dev_info.rxresidue_buf);
		assert(dev_info.rxresidue_bytes == 0);
		memcpy(dev_info.rxresidue, 
                       cellbuf + sizeof(cellbuf) - over, 
                       sizeof(cellbuf) - over);
                dev_info.rxresidue_bytes += over;
		assert(dev_info.rxresidue_bytes <= ADA_CELL_SZ - ADA_CELL_HEADER_SZ);
		assert((uint32_t)dev_info.rxresidue - (uint32_t)dev_info.rxresidue_buf <= sizeof(dev_info.rxresidue_buf));
		assert(dev_info.rxresidue - dev_info.rxresidue_buf >= 0);
        }

        assert(dev_info.rxresidue_bytes >= 0);
        assert((uint32_t)dev_info.rxresidue_bytes < (uint32_t)sizeof(dev_info.rxresidue_buf));
	assert(dev_info.txresidue_bytes >= 0);
	assert((uint32_t)dev_info.txresidue_bytes <= sizeof(dev_info.txresidue));
	assert((uint32_t)dev_info.rxresidue - (uint32_t)dev_info.rxresidue_buf <= sizeof(dev_info.rxresidue_buf));
	assert(dev_info.rxresidue - dev_info.rxresidue_buf >= 0);

	if (done != 0) {
                debug_msg("Done %d bytes\n", done);
	}

        return done;
}


int
atm_audio_write(audio_desc_t ad, u_char *buf, int buf_bytes)
{
	int done;
	unsigned char cellbuf[ADA_CELL_SZ];

        UNUSED(ad); assert(audio_fd > 0);

        done = 0;
	debug_msg("atm_audio_write; got %d bytes b[0] = %d\n", buf_bytes, (int)buf[0]);

	/* if we have anything left from before put it in the output cell 
	 * first and then fill the cell from the buffer, fixing pointers and 
	 * counts
	 */

	assert(dev_info.txresidue_bytes >= 0);
	assert((uint32_t)dev_info.txresidue_bytes <= sizeof(dev_info.txresidue));
	if (dev_info.txresidue_bytes > 0 && 
            (buf_bytes + dev_info.txresidue_bytes) > (ADA_CELL_SZ - ADA_CELL_HEADER_SZ)) {
		int rem = ADA_CELL_SZ - ADA_CELL_HEADER_SZ - dev_info.txresidue_bytes;
                /* Fill in header */
		memcpy(cellbuf, &dev_info.atmhdr, 4);
		cellbuf[4] = aal1hdr[dev_info.seqno];
                dev_info.seqno = (dev_info.seqno + 1) & 0x07; /* values 0-7 valid */
                /* Fill in data   */
                assert(dev_info.txresidue_bytes <= ADA_CELL_SZ - ADA_CELL_HEADER_SZ);
		memcpy(cellbuf + ADA_CELL_HEADER_SZ, dev_info.txresidue, dev_info.txresidue_bytes);
		/* If not enough residue to fill cell start drawing audio from buf */
                if (rem > 0) {
                        memcpy(cellbuf + ADA_CELL_HEADER_SZ + dev_info.txresidue_bytes, buf, rem);
                }
                write(audio_fd, (char *)cellbuf, ADA_CELL_SZ);
		done += rem;
		dev_info.txresidue_bytes = 0;
		/*                debug_msg("wrote frame %d using residue\n", dev_info.seqno); */
	}

	assert(dev_info.txresidue_bytes >= 0);

        while(buf_bytes - done >= ADA_CELL_SZ - ADA_CELL_HEADER_SZ) {
		memcpy(cellbuf, &dev_info.atmhdr, 4);
		cellbuf[4] = aal1hdr[dev_info.seqno];
		memcpy(cellbuf + ADA_CELL_HEADER_SZ, buf + done, ADA_CELL_SZ - ADA_CELL_HEADER_SZ);
                write(audio_fd, (char *)cellbuf, ADA_CELL_SZ);
		done += ADA_CELL_SZ - ADA_CELL_HEADER_SZ;
                dev_info.seqno = (dev_info.seqno + 1) & 0x07; /* values 0-7 valid */
		/*                debug_msg("Wrote frame %d %d %d\n", dev_info.seqno, done, buf_bytes); */
	}

	assert(dev_info.txresidue_bytes >= 0);

	/* Save left over */
	if (buf_bytes - done > 0) {
		dev_info.txresidue_bytes = buf_bytes - done;
                assert(dev_info.txresidue_bytes <= ADA_CELL_SZ - ADA_CELL_HEADER_SZ);
		memcpy(dev_info.txresidue, buf + done, dev_info.txresidue_bytes);
		/*		debug_msg("write residue left over %d\n", buf_bytes - done); */
	}

	assert(dev_info.txresidue_bytes >= 0);

	return buf_bytes;
}

/* Set ops on audio device to be non-blocking */
void
atm_audio_non_block(audio_desc_t ad)
{
	int	on = 1;

        UNUSED(ad); assert(audio_fd > 0);

	if (ioctl(audio_fd, FIONBIO, (char *)&on) < 0) {
		debug_msg("Failed to set non blocking mode on audio device!\n");
        } else {
 		debug_msg("Non-blocking mode set on ATM audio device (fd=%d)\n", audio_fd);
        }
}

/* Set ops on audio device to block */
void
atm_audio_block(audio_desc_t ad)
{
	int	on = 0;

        UNUSED(ad); assert(audio_fd > 0);

	if (ioctl(audio_fd, FIONBIO, (char *)&on) < 0) {
		debug_msg("Failed to set blocking mode on audio device!\n");
        } else {
 		debug_msg("Blocking mode set on ATM audio device (fd=%d)\n", audio_fd);
        }
}


static const audio_port_details_t out_ports[] = {
        { AUDIO_LINE_OUT,  AUDIO_PORT_LINE_OUT }
};

#define NUM_OUT_PORTS (sizeof(out_ports)/sizeof(out_ports[0]))

void
atm_audio_oport_set(audio_desc_t ad, audio_port_t port)
{
        UNUSED(ad); assert(audio_fd > 0);

        if (port != AUDIO_LINE_OUT) {
                debug_msg("Port not recognized\n");
                port = AUDIO_LINE_OUT;
        }
        dev_info.play.port = port;
}

audio_port_t
atm_audio_oport_get(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);
	return (dev_info.play.port);
}

int
atm_audio_oport_count(audio_desc_t ad)
{
        UNUSED(ad);
        return (int)NUM_OUT_PORTS;
}

const audio_port_details_t*
atm_audio_oport_details(audio_desc_t ad, int idx)
{
        UNUSED(ad);
        if (idx >= 0 && idx < (int)NUM_OUT_PORTS) {
                return &out_ports[idx];
        }
        return NULL;
}

static const audio_port_details_t in_ports[] = {
        { AUDIO_LINE_IN,    AUDIO_PORT_LINE_IN}
};

#define NUM_IN_PORTS (sizeof(out_ports)/sizeof(out_ports[0]))

void
atm_audio_iport_set(audio_desc_t ad, audio_port_t port)
{
        UNUSED(ad); assert(audio_fd > 0);

        if (port != AUDIO_LINE_IN ) {
                port = AUDIO_LINE_IN;
        }
        
        dev_info.record.port = port;
}

audio_port_t
atm_audio_iport_get(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);
	return (dev_info.record.port);
}

int
atm_audio_iport_count(audio_desc_t ad)
{
        UNUSED(ad);
        return (int)NUM_IN_PORTS;
}

const audio_port_details_t*
atm_audio_iport_details(audio_desc_t ad, int idx)
{
        UNUSED(ad);
        if (idx >= 0 && idx < (int)NUM_IN_PORTS) {
                return &in_ports[idx];
        }
        return NULL;
}

int
atm_audio_duplex(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);

        return 1;
}

static int
atm_audio_select(audio_desc_t ad, int delay_us)
{
        fd_set rfds;
        struct timeval tv, s1, s2;

        UNUSED(ad); assert(audio_fd > 0);

        tv.tv_sec = 0;
        tv.tv_usec = delay_us;

        FD_ZERO(&rfds);
        FD_SET(audio_fd, &rfds);

        gettimeofday (&s1, 0);
        select(audio_fd+1, &rfds, NULL, NULL, &tv);
        gettimeofday (&s2, 0);

        s2.tv_usec -= s1.tv_usec;
        s2.tv_sec  -= s1.tv_sec;

        if (s2.tv_usec < 0) {
                s2.tv_usec += 1000000;
                s2.tv_sec  -= 1;
        }

        return FD_ISSET(audio_fd, &rfds);
}

void
atm_audio_wait_for(audio_desc_t ad, int delay_ms)
{
        UNUSED(ad); assert(audio_fd > 0);
        atm_audio_select(ad, delay_ms * 1000);
}

int
atm_audio_is_ready(audio_desc_t ad)
{
        UNUSED(ad); assert(audio_fd > 0);
        return atm_audio_select(ad, 0);
}