File: transmit.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 (844 lines) | stat: -rw-r--r-- 32,782 bytes parent folder | download | duplicates (4)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
 * FILE:     transmit.c
 * PROGRAM:  RAT
 * AUTHOR:   Orion Hodson / Isidor Kouvelas
 * MODIFIED: Colin Perkins
 *
 * Copyright (c) 1995-2001 University College London
 * All rights reserved.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: transmit.c,v 1.131 2002/03/15 17:57:19 ucacoxh Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#include "config_unix.h"
#include "config_win32.h"
#include "memory.h"
#include "debug.h"
#include "audio_types.h"
#include "codec_types.h"
#include "codec.h"
#include "codec_state.h"
#include "playout.h"
#include "channel_types.h"
#include "channel.h"
#include "session.h"
#include "audio.h"
#include "audio_util.h"
#include "sndfile.h"
#include "converter.h"
#include "parameters.h"
#include "pdb.h"
#include "ui_send_rtp.h"
#include "ui_send_audio.h"
#include "ui_send_prefs.h"
#include "rtp.h"
#include "transmit.h"
#include "util.h"

#define TX_BUFFER_MAGIC 0x12345678

/* All this code can be greatly simplified and reduced by making
 * better use of the playout buffer structure in playout.h.
 */

typedef struct s_tx_unit {
        sample   	*data;		/* pointer to raw data in read_buf                     */
        uint32_t  	 dur_used;	/* number of time intervals filled                     */
        uint16_t 	 energy;	/*                                                     */
        char	 	 silence;	/* First pass                                          */
        u_char    	 send;		/* Silence second pass                                 */
	u_char		 encoded;	/* TRUE if this unit has been encoded for transmission */
	u_char		 live_capture;	/* TRUE if this unit came from a microphone, FALSE if it came from a file */
} tx_unit;

typedef struct s_tx_buffer {
        struct s_session     *sp;
        struct s_vad         *vad;
        struct s_agc         *agc;
        struct s_bias_ctl    *bc;
        struct s_pb          *media_buffer; 
        struct s_pb          *channel_buffer; 

        struct s_pb          *audio_buffer; /* Audio buffer and it's iterators... */
        struct s_pb_iterator *reading;      /* ...current read point iterator     */
        struct s_pb_iterator *silence;      /* ...silence classification iterator */
        struct s_pb_iterator *transmit;     /* ...transmission point iterator     */

        struct s_codec_state_store *state_store;    /* Encoder states        */
        uint32_t              sending_audio:1;
	uint32_t              sample_rate;
        uint16_t              channels;
        uint16_t              unit_dur; /* dur. in sampling intervals (excludes channels) */

        /* Statistics log */
        double          mean_read_dur;
        /* These are a hack because we use playout buffer
         * which expects time units of type timestamp_t so we need
         * to be able to map to and from 32 bit no for
         * packet timestamp */
        ts_sequencer    down_seq;  /* used for 32 -> timestamp_t */
        ts_sequencer    up_seq;    /* used for timestamp_t -> 32 */

        /* place for the samples */
        sample samples[DEVICE_REC_BUF];
        int    last_sample; /* Stores the index of the last read buffer */

        /* bandwidth estimate parameters */
        int    bps_bytes_sent;
        timestamp_t   bps_last_update;

	/* For debugging... */
	uint32_t	magic;
} tx_buffer;

static sample dummy_buf[DEVICE_REC_BUF];
static void tx_read_sndfile(session_t *sp, uint16_t tx_freq, uint16_t tx_channels, tx_unit *u);

static void
tx_buffer_validate(tx_buffer *tb)
{
#ifdef DEBUG
	session_validate(tb->sp);
	if (tb->sending_audio) {
        	assert(pb_iterator_count(tb->audio_buffer) == 3);
	} else {
        	assert(pb_iterator_count(tb->audio_buffer) == 0);
	}
        assert(tb->channels > 0 && tb->channels <= 2);
	assert(tb->last_sample < DEVICE_REC_BUF);
#endif
	assert(tb->magic == TX_BUFFER_MAGIC);
}

static int
tx_unit_create(tx_buffer *tb, tx_unit  **ptu, int n_samples)
{
        tx_unit *tu;
	tx_buffer_validate(tb);
        tu = xmalloc(sizeof(tx_unit));
        if (tu) {
                *ptu = tu;
                /* Position sample pointer */
                if (tb->last_sample + n_samples >= DEVICE_REC_BUF) {
                        tb->last_sample = 0;
                }
                tu->data     = tb->samples + tb->last_sample;
		tu->dur_used = 0;
                tu->energy   = 555;
		tu->silence  = -1;	/* -1 == not yet performed silence detection */
		tu->send     = 0;
		tu->encoded  = FALSE;
		tu->live_capture = TRUE;
                tb->last_sample += n_samples;
		tx_buffer_validate(tb);
                return TRUE;
        }
        debug_msg("Failed to allocate tx_unit\n");
        return FALSE;
}

static void
tx_unit_destroy(tx_unit **ptu, uint32_t len)
{
        tx_unit *tu = *ptu;
        assert(tu != NULL);
        assert(len == sizeof(tx_unit));
        xfree(tu);
        *ptu = NULL;
}

int
tx_create(tx_buffer **ntb, 
          session_t  *sp,
	  uint32_t    sample_rate,     
          uint16_t    channels,
          uint16_t    unit_dur)
{
        tx_buffer *tb;

        tb = (tx_buffer*)xmalloc(sizeof(tx_buffer));
        if (tb) {
                memset(tb, 0, sizeof(tx_buffer));
                debug_msg("Unit duration %d channels %d\n", unit_dur, channels);
		tb->magic         = TX_BUFFER_MAGIC;
                tb->sp            = sp;
		tb->sending_audio = FALSE;
		tb->sample_rate   = sample_rate;
                tb->channels      = channels;
                tb->unit_dur      = unit_dur;
		tb->mean_read_dur = unit_dur;
                tb->bc            = bias_ctl_create(channels, sample_rate);
                tb->vad           = vad_create(unit_dur, sample_rate);
                tb->agc           = agc_create(sp);
                sp->auto_sd       = sd_init(unit_dur, sample_rate);
                sp->manual_sd     = manual_sd_init(unit_dur, sample_rate, sp->manual_sd_thresh);
                
                pb_create(&tb->audio_buffer, (playoutfreeproc)tx_unit_destroy);
                pb_create(&tb->media_buffer, (playoutfreeproc)media_data_destroy);
                pb_create(&tb->channel_buffer, (playoutfreeproc)channel_data_destroy);

		tx_buffer_validate(tb);

                *ntb = tb;
                return TRUE;
        }
        return FALSE;
}

void
tx_destroy(tx_buffer **ptb)
{
        tx_buffer *tb;

        assert(ptb != NULL);
        tb = *ptb;
        assert(tb != NULL);

	tx_buffer_validate(tb);
        bias_ctl_destroy(tb->bc);
        sd_destroy(tb->sp->auto_sd);
        manual_sd_destroy(tb->sp->manual_sd);
        vad_destroy(tb->vad);
        agc_destroy(tb->agc);

        pb_destroy(&tb->audio_buffer);
        pb_destroy(&tb->media_buffer);
        pb_destroy(&tb->channel_buffer);

        xfree(tb);
        *ptb = NULL;
}

/* These routines are called when the button on the interface is toggled */
void
tx_start(tx_buffer *tb)
{
        tx_unit *tu_new;
        timestamp_t     unit_start;

	debug_msg("Starting to transmit...\n");
	tx_buffer_validate(tb);
        if (tb->sending_audio) {
                debug_msg("Already sending... why? Fix me!");
                abort();
        }
        tb->sending_audio = TRUE;

        /* Turn off auto lecture */
        tb->sp->auto_lecture = 1;       

        /* Reset signal classification and auto-scaling */
        sd_reset(tb->sp->auto_sd);
        vad_reset(tb->vad);
        agc_reset(tb->agc);

        /* Attach iterator for silence classification */
        pb_iterator_create(tb->audio_buffer, &tb->transmit);
        pb_iterator_create(tb->audio_buffer, &tb->silence);
        pb_iterator_create(tb->audio_buffer, &tb->reading);
        assert(pb_iterator_count(tb->audio_buffer) == 3);

        /* Add one unit to media buffer to kick off audio reading */
        unit_start = tb->sp->cur_ts;
        tx_unit_create(tb, &tu_new, tb->unit_dur * tb->channels);
        assert(ts_valid(unit_start));
        pb_add(tb->audio_buffer, (u_char*)tu_new, sizeof(tx_unit), unit_start);

        /* And then put reading iterator on it */
        pb_iterator_advance(tb->reading);

        assert(tb->state_store == NULL);
        codec_state_store_create(&tb->state_store, ENCODER);
        tx_update_ui(tb);
        tb->bps_last_update = tb->sp->cur_ts;
}

void
tx_stop(tx_buffer *tb)
{
        struct timeval tv;

	tx_buffer_validate(tb);
        if (tb->sending_audio == FALSE) {
                return;
        }

        gettimeofday(&tv, NULL);
        tb->sp->auto_lecture  = tv.tv_sec;
        codec_state_store_destroy(&tb->state_store);
        channel_encoder_reset(tb->sp->channel_coder);
        ui_send_audio_input_powermeter(tb->sp, tb->sp->mbus_ui_addr, 0);
        tb->sending_audio = FALSE;
        /* Detach iterators      */
        assert(pb_iterator_count(tb->audio_buffer) == 3);
        pb_iterator_destroy(tb->audio_buffer, &tb->transmit);
        pb_iterator_destroy(tb->audio_buffer, &tb->silence);
        pb_iterator_destroy(tb->audio_buffer, &tb->reading);
        assert(pb_iterator_count(tb->audio_buffer) == 0);

        /* Drain playout buffers */
        pb_flush(tb->audio_buffer);
        pb_flush(tb->media_buffer);
        pb_flush(tb->channel_buffer);

        tb->bps_bytes_sent = 0;
	tx_buffer_validate(tb);
        tx_update_ui(tb);
}

int
tx_read_audio(tx_buffer *tb)
{
        session_t *sp;
        tx_unit   *u;
	timestamp_t       u_ts;
        uint32_t   read_dur = 0, this_read, ulen;

	tx_buffer_validate(tb);

        sp = tb->sp;
        if (tb->sending_audio) {
                int filled_unit;
                assert(pb_iterator_count(tb->audio_buffer) == 3);
                do {
                        if (pb_iterator_get_at(tb->reading, (u_char**)&u, &ulen, &u_ts) == FALSE) {
                                debug_msg("Reading iterator failed to get unit!\n");
                        }
                        assert(u != NULL);

                        this_read = audio_read(sp->audio_device, 
                                               u->data + u->dur_used * tb->channels,
                                               (tb->unit_dur - u->dur_used) * tb->channels) / tb->channels;
                        assert(this_read <= tb->unit_dur - u->dur_used);

                        filled_unit = FALSE;
                        u->dur_used += this_read;
                        if (u->dur_used == tb->unit_dur) {
                                read_dur += tb->unit_dur;
                                if (sp->in_file) {
					/* Reading from a file overwrites any audio we've captured... */
                                        tx_read_sndfile(sp, tb->sample_rate, tb->channels, u);
				}
				sp->cur_ts = ts_add(sp->cur_ts, ts_map32(tb->sample_rate, tb->unit_dur));
                                u_ts       = sp->cur_ts;
                                filled_unit = TRUE;
				/* We've filled one unit, so create the next one... */
				tx_unit_create(tb, &u, tb->unit_dur * tb->channels);
                                pb_add(tb->audio_buffer, (u_char*)u, ulen, u_ts);
                                pb_iterator_advance(tb->reading);
			}
                } while (filled_unit == TRUE);
                assert(pb_iterator_count(tb->audio_buffer) == 3);
        } else {
                int this_read = 0;
                /* We're not sending, but have access to the audio device. 
                 * Read the audio anyway to get exact values, and then 
                 * throw the data we've just read away...    
                 */
                do {
                        this_read = audio_read(sp->audio_device, dummy_buf, DEVICE_REC_BUF / 4) / sp->tb->channels;
                        read_dur += this_read;
                } while (this_read > 0);
		sp->cur_ts = ts_add(sp->cur_ts, ts_map32(tb->sample_rate, read_dur));
        }

        if (read_dur >= (uint32_t)(DEVICE_REC_BUF / (4 * tb->channels))) {
                debug_msg("Read a lot of audio %d\n", read_dur);
                if (tb->sending_audio) {
                        debug_msg("Resetting transmitter\n");
                        tx_stop(tb);
                        tx_start(tb);
                }
        }
        
        if (read_dur) {
                sp->tb->mean_read_dur += ((double)read_dur - sp->tb->mean_read_dur) / 8.0;
        }

        assert(read_dur < 0x7fffffff);
        return read_dur;
}

int
tx_process_audio(tx_buffer *tb)
{
        struct s_pb_iterator *marker;
        tx_unit              *u;
        uint32_t              u_len;
        timestamp_t                  u_ts;
        int                   to_send;
        
	tx_buffer_validate(tb);
        assert(tb->sending_audio);
        
        /* Do signal classification up until read point, that
         * is not a complete audio frame so cannot be done 
         */
        assert(pb_iterator_count(tb->audio_buffer) == 3);
        pb_iterator_get_at(tb->silence, (u_char**)&u, &u_len, &u_ts);
        while (pb_iterators_equal(tb->silence, tb->reading) == FALSE) {
		assert(u->dur_used == tb->unit_dur);
		if (u->live_capture) {
                	bias_remove(tb->bc, u->data, u->dur_used * tb->channels);
		} else {
			debug_msg("Unit came from a file, no need for bias removal\n");
		}
                u->energy = audio_avg_energy(u->data, u->dur_used * tb->channels, tb->channels);
                u->send   = FALSE;
                
                /* Silence classification on this block */
		assert(u->silence == -1);	/* We should only do this once per block... */
                switch(tb->sp->silence_detection) {
                case SILENCE_DETECTION_AUTO:
                        u->silence = sd(tb->sp->auto_sd, (uint16_t)u->energy);
                        break;
                case SILENCE_DETECTION_MANUAL:
                        u->silence = manual_sd(tb->sp->manual_sd, 
                                               (uint16_t)u->energy, 
                                               audio_abs_max(u->data, u->dur_used * tb->channels));
                        break;
		case SILENCE_DETECTION_OFF:
			u->silence = 0;
			break;
                }
		assert((u->silence == 0) || (u->silence == 1));
                                               
                /* Pass decision to voice activity detector (damps transients, etc) */
                to_send = vad_to_get(tb->vad, 
                                     (u_char)u->silence, 
                                     (u_char)((tb->sp->lecture) ? VAD_MODE_LECT : VAD_MODE_CONF));           
                agc_update(tb->agc, (uint16_t)u->energy, vad_talkspurt_no(tb->vad));
                
                if (tb->sp->silence_detection != SILENCE_DETECTION_OFF) {
                        if (to_send != 0) {
                                pb_iterator_dup(&marker, tb->silence);
                                while(u != NULL && to_send != 0) {
                                        u->send = TRUE;
                                        to_send --;
                                        pb_iterator_retreat(marker);
                                        pb_iterator_get_at(marker, (u_char**)&u, &u_len, &u_ts);
                                }
                                pb_iterator_destroy(tb->audio_buffer, &marker);
                        }
                        assert(pb_iterator_count(tb->audio_buffer) == 3);
                } else {
                        u->silence = FALSE;
                        u->send    = TRUE;
                }
                pb_iterator_advance(tb->silence);
                pb_iterator_get_at(tb->silence, (u_char**)&u, &u_len, &u_ts);
        }

        if (tb->sp->agc_on == TRUE && agc_apply_changes(tb->agc) == TRUE) {
                ui_send_audio_input_gain(tb->sp, tb->sp->mbus_ui_addr);
        }

	tx_buffer_validate(tb);
        return TRUE;
}

static int
tx_encode(struct s_codec_state_store *css, 
          sample     *buf, 
          uint32_t     dur_used,
          uint32_t     encoding,
          u_char     *payloads, 
          coded_unit **coded)
{
        codec_id_t id;
        uint32_t    i;

        id = codec_get_by_payload(payloads[encoding]);
        assert(id);

        /* Look to see if we have already coded this unit,
         * i.e. we are using redundancy.  Don't want to code
         * twice since it screws up encoder state.
         */
        
        for (i = 0; i < encoding; i++) {
                if (coded[i]->id == id) {
                        break;
                }
        }

        if (i == encoding) {
                const codec_format_t *cf;
                coded_unit native;
                codec_state *cs;
                
                /* Unit does not exist already */
                cf = codec_get_format(id);
                
                /* native is a temporary coded_unit that we use to pass to
                 * codec_encode since this take a 'native' (raw) coded unit as
                 * input and fills in coded with the transformed data.
                 */
                native.id        = codec_get_native_coding((uint32_t)cf->format.sample_rate, 
                                                           (uint16_t)cf->format.channels);
                native.state     = NULL;
                native.state_len = 0;
                native.data      = (u_char*)buf;
                native.data_len  = (uint16_t)(dur_used * sizeof(sample) * cf->format.channels);
                
                /* Get codec state from those stored for us */
                cs = codec_state_store_get(css, id);
                return codec_encode(cs, &native, coded[encoding]);
        } else {
                /* duplicate coded unit */
                return coded_unit_dup(coded[encoding], coded[i]);
        }
}

void
tx_send(tx_buffer *tb)
{
        struct s_pb_iterator    *cpos;
        channel_data            *cd;
        channel_unit            *cu;
        tx_unit        		*u;
        timestamp_t            	 u_ts, u_sil_ts, delta;
        timestamp_t            	 time_ts;
        uint32_t         	 time_32, cd_len;
        uint32_t         	 u_len, units, i, j, k, n, send, encoding;
        int 			 success;
	char			*extn;
	uint16_t		 extn_len, extn_type;
        
	tx_buffer_validate(tb);
        assert(pb_iterator_count(tb->audio_buffer) == 3);

        if (pb_iterators_equal(tb->silence, tb->transmit)) {
                return;
        } 

        pb_iterator_get_at(tb->silence,  (u_char**)&u, &u_len, &u_sil_ts);
        pb_iterator_get_at(tb->transmit, (u_char**)&u, &u_len, &u_ts);

        assert(ts_gt(u_sil_ts, u_ts));

        delta = ts_sub(u_sil_ts, u_ts);
        n     = delta.ticks / tb->unit_dur;
        units = channel_encoder_get_units_per_packet(tb->sp->channel_coder);
        
        while(n >= units) {
		/* We have accumulated at least enough audio to fill an RTP packet. */
		/* The following code processes one packet's worth of audio, and if */
		/* any of the units are marked `to send' then the entire packet is  */
		/* encoded and placed onto the transmission queue: tb->media_buffer */
                send = FALSE;
                for (i = 0; i < units; i++) {
                        pb_iterator_get_at(tb->transmit, (u_char**)&u, &u_len, &u_ts);
			assert((u->silence == 0) || (u->silence == 1)); /* We MUST have done silence detection before transmit */
                        if (u->send) {
                                send = TRUE;
                                break;
                        }
                        pb_iterator_advance(tb->transmit);
                }

                /* Rewind transmit point to where it was before we did last check */
                while(i > 0) {
                        pb_iterator_retreat(tb->transmit);
                        i--;
                }
                
                for (i = 0;i < units; i++) {
                        media_data *m;
                        success = pb_iterator_get_at(tb->transmit, (u_char**)&u, &u_len, &u_ts);
                        assert(success);
			assert((u->silence == 0) || (u->silence == 1)); /* We MUST have done silence detection before transmit */
                        if (send) {
				assert(u->encoded == FALSE); 
                                media_data_create(&m, tb->sp->num_encodings);
                                for(encoding = 0; encoding < (uint32_t)tb->sp->num_encodings; encoding ++) {
                                        tx_encode(tb->state_store, u->data, u->dur_used, encoding, tb->sp->encodings, m->rep);
                                }
				/* At this point, the uncompressed data `u' is no longer needed since its */
				/* coded form is in tb->media_buffer. The tb->transmit is audited later.  */
				/* We mark it as being encoded, as a debugging check to ensure that it is */
				/* not accidently re-sent later.                                          */
				u->encoded = TRUE;
                        } else {
                                media_data_create(&m, 0);
                        }
                        assert(m != NULL);
                        success = pb_add(tb->media_buffer, (u_char*)m, sizeof(media_data), u_ts);
                        assert(success);
                        success = pb_iterator_advance(tb->transmit);
                        assert(success);
                }
                n -= units;
	}

	/* This does any necessary channel coding... The channel coder takes units from */
	/* tb->media_buffer and moves then (eventually) onto tb->channel_buffer. They   */
	/* be delayed, reordered, aggregated, etc, in the process, but we don't worry   */
	/* about that here. tb->media_buffer is drained by the channel encoding stage.  */
        channel_encoder_encode(tb->sp->channel_coder, tb->media_buffer, tb->channel_buffer);

	/* Pull units out of tb->channel_buffer and transmit them... */
        pb_iterator_create(tb->channel_buffer, &cpos);
        pb_iterator_advance(cpos);
        while(pb_iterator_detach_at(cpos, (u_char**)&cd, &cd_len, &time_ts)) {
                uint32_t csrc[16];
                char *data, pt;
                int   data_len, done;
                int  marker;

                /* Set up fields for RTP header */
                cu = cd->elem[0];
                pt = channel_coder_get_payload(tb->sp->channel_coder, cu->pt);
                time_32 = ts_seq32_out(&tb->up_seq, tb->sample_rate, time_ts);
                if (time_32 - tb->sp->last_depart_ts != units * tb->unit_dur) {
                        marker = 1;
                        debug_msg("new talkspurt (%d - %d != %d)\n", time_32, tb->sp->last_depart_ts, units * tb->unit_dur);
                } else {
                        marker = 0;
                }   
                
                /* layer loop starts here */
                for(j = 0; j < (uint32_t)tb->sp->layers; j++) {
			assert(tb->sp->layers == 1); /* FIXME */
			assert(j == 0);		 /* FIXME */
#ifdef DEBUG_HEADER_EXTN
			extn = (char *) xmalloc(5);
			sprintf(extn, "test");
			extn_len  = 1;	/* 32 bit words of extn data */
			extn_type = 1;
#else
			extn      = NULL;
			extn_len  = 0;
			extn_type = 0;
#endif
                        data_len = 0;
                        /* determine data length for packet.  This is a   */  
                        /* little over complicated because of layering... */
                        for(i = j, k=0; i < cd->nelem; i += tb->sp->layers) {
                                data_len += (int) cd->elem[i]->data_len;
                                k++;
                        }

                        /* Copy all out going data into one block (no scatter) */
                        data = (char*)block_alloc(data_len);
                        done = 0;
                        for(i = j; i < cd->nelem; i += tb->sp->layers) {
                                memcpy(data + done, cd->elem[i]->data, cd->elem[i]->data_len);
                                done += cd->elem[i]->data_len;
                        }
                        rtp_send_data(tb->sp->rtp_session[j], time_32, pt, marker, 0, csrc, data, data_len, extn, extn_len, extn_type);
                        block_free(data, data_len);
                        tb->bps_bytes_sent += data_len;
			if (extn != NULL) {
				xfree(extn);
			}
                }
                /* layer loop ends here */
                
                tb->sp->last_depart_ts  = time_32;
                channel_data_destroy(&cd, sizeof(channel_data));
        }
        pb_iterator_destroy(tb->channel_buffer, &cpos);

        /* Drain tb->audio, remove every older than silence position
         * by two packets worth of audio.  Note tb->media_buffer is drained
         * by the channel encoding stage and tb->channel_buffer is drained
         * in the act of transmission with pbi_detach_at call.
         */
        u_ts = ts_map32(tb->sample_rate, 2 * units * tb->unit_dur);

        {
                struct s_pb *buf;
                buf = pb_iterator_get_playout_buffer(tb->transmit);
                assert(pb_iterator_count(buf) == 3);
        }

        assert(pb_iterator_count(tb->audio_buffer) == 3);        
        n = pb_iterator_audit(tb->transmit, u_ts);
}

void
tx_update_ui(tx_buffer *tb)
{
        session_t	*sp           = tb->sp;

	tx_buffer_validate(tb);
        if (sp->meter && tb->sending_audio) {
                struct s_pb_iterator *prev;  
                tx_unit              *u;
                uint32_t               u_len;
                timestamp_t                  u_ts;

                /* Silence point should be upto read point here so use last
                 * completely read unit.
                 */
                assert(pb_iterator_count(tb->audio_buffer) == 3);
                pb_iterator_dup(&prev, tb->silence);
                pb_iterator_retreat(prev);
                if (pb_iterators_equal(tb->silence, prev)) {
                        pb_iterator_destroy(tb->audio_buffer, &prev);
                        return;
                }
                if (pb_iterator_get_at(prev, (u_char**)&u, &u_len, &u_ts) &&
                    (vad_in_talkspurt(sp->tb->vad) == TRUE || sp->silence_detection == SILENCE_DETECTION_OFF)) {
                        ui_send_audio_input_powermeter(sp, sp->mbus_ui_addr, lin2vu(u->energy, 100, VU_INPUT));
                } else {
                        ui_send_audio_input_powermeter(sp, sp->mbus_ui_addr, 0);
                }
                pb_iterator_destroy(tb->audio_buffer, &prev);
                assert(pb_iterator_count(tb->audio_buffer) == 3);
        }
	/* This next routine is really inefficient - we only need do ui_info_activate() */
	/* when the state changes, else we flood the mbus with redundant messages.      */
        if (sp->silence_detection != SILENCE_DETECTION_OFF) {
                if (vad_in_talkspurt(sp->tb->vad) == TRUE) {
                        if (sp->ui_activated == FALSE) {
                                ui_send_rtp_active(sp, sp->mbus_ui_addr, rtp_my_ssrc(sp->rtp_session[0]));
                                sp->ui_activated = TRUE;
                        }
                } else if (sp->ui_activated == TRUE) {
                        ui_send_rtp_inactive(sp, sp->mbus_ui_addr, rtp_my_ssrc(sp->rtp_session[0]));
                        sp->ui_activated = FALSE;
                }
		if (sp->lecture) {
			sp->lecture = FALSE;
			ui_send_lecture_mode(sp, sp->mbus_ui_addr);
		}
        } else if (sp->silence_detection == SILENCE_DETECTION_OFF) {
                if (tb->sending_audio == TRUE && sp->ui_activated == FALSE) {
                        ui_send_rtp_active(sp, sp->mbus_ui_addr, rtp_my_ssrc(sp->rtp_session[0]));
                        sp->ui_activated = TRUE;
                }
        }
        if (tb->sending_audio == FALSE && sp->ui_activated == TRUE) {
                ui_send_rtp_inactive(sp, sp->mbus_ui_addr, rtp_my_ssrc(sp->rtp_session[0]));
                sp->ui_activated = FALSE;
        }
}

void
tx_igain_update(tx_buffer *tb)
{
	tx_buffer_validate(tb);
        sd_reset(tb->sp->auto_sd);
        agc_reset(tb->agc);
}

int
tx_is_sending(tx_buffer *tb)
{
        return tb->sending_audio;
}

double
tx_get_bps(tx_buffer *tb)
{
	tx_buffer_validate(tb);
        if (tb->bps_bytes_sent == 0) {
                return 0.0;
        } else {
                uint32_t dms;
                double  bps;
                timestamp_t delta = ts_abs_diff(tb->bps_last_update, tb->sp->cur_ts);
                dms        = timestamp_to_us(delta);
                bps        = tb->bps_bytes_sent * 8e6 / (double)dms;
                tb->bps_bytes_sent  = 0;
                tb->bps_last_update = tb->sp->cur_ts;
                return bps;
        }
}

static void 
tx_read_sndfile(session_t *sp, uint16_t tx_freq, uint16_t tx_channels, tx_unit *u)
{
        sndfile_fmt_t sfmt;
        int samples_read, dst_samples;

        snd_get_format(sp->in_file, &sfmt);
        if (sfmt.channels != tx_channels || sfmt.sample_rate != tx_freq) {
                converter_fmt_t target;
                const converter_fmt_t *actual;
                coded_unit in, out;

                target.src_channels = (uint16_t)sfmt.channels;
                target.src_freq     = (uint32_t)sfmt.sample_rate;
                target.dst_channels = (uint16_t)tx_channels;
                target.dst_freq     = tx_freq;

                /* Check if existing converter exists and whether valid */
                if (sp->in_file_converter != NULL) {
                        actual = converter_get_format(sp->in_file_converter);
                        if (memcmp(actual, &target, sizeof(converter_fmt_t)) != 0) {
                                converter_destroy(&sp->in_file_converter);
                        }
                }
                /* Create relevent converter if necessary */
                if (sp->in_file_converter == NULL) {
                        const converter_details_t *details = NULL;
                        uint32_t i, n;
                        /* We iterate through available converters
                         * since they have different capabilities,
                         * specifically MS-ACM does m*8:n*11025 and
                         * the RAT ones don't at time of writing.
                         */
                        n = converter_get_count();
                        for(i = 0; i < n; i++) {
                                details = converter_get_details(i);
                                if (converter_create(details->id, &target, &sp->in_file_converter)) {
                                        debug_msg("Created converter %s for sound file conversion\n", details->name);
                                        break;
                                }
                        }
                        if (i == n) {
                                debug_msg("Could not create suitable converter for sound file\n");
                                snd_read_close(&sp->in_file);
                                return;
                        }
                }

                dst_samples = u->dur_used * tx_channels;

                /* Prepare block to read audio into */
                in.id        = codec_get_native_coding(target.src_freq, target.src_channels);
                in.state     = NULL;
                in.state_len = 0;
                in.data_len  = sizeof(sample) * dst_samples * 
                        (target.src_freq * target.src_channels) /
                        (target.dst_freq * target.dst_channels);
                        
                in.data      = (u_char*)block_alloc(in.data_len);

                /* Get the sound from file */
                samples_read = snd_read_audio(&sp->in_file, (sample*)in.data, (uint16_t)(in.data_len / sizeof(sample)));

                if (samples_read == 0) {
                        /* File is paused */
                        codec_clear_coded_unit(&in);
                        return;
                }

                /* Prepare output block */
                memset(&out, 0, sizeof(out));
                converter_process(sp->in_file_converter, &in, &out);
                assert((uint32_t)dst_samples == out.data_len / sizeof(sample));
                memcpy(u->data, out.data, dst_samples * sizeof(sample));
                /* Tidy up */
                codec_clear_coded_unit(&in);
                codec_clear_coded_unit(&out);
        } else {
                samples_read = snd_read_audio(&sp->in_file, u->data, (uint16_t)(u->dur_used * tx_channels));
        }
	if (samples_read > 0) {
		u->live_capture = FALSE;
	}
}

uint32_t
tx_get_rtp_time(session_t *sp)
{
	return ts_seq32_out(&sp->tb->up_seq, sp->tb->sample_rate, sp->cur_ts);
}