File: source.h

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (418 lines) | stat: -rw-r--r-- 14,237 bytes parent folder | download
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
/*
 * Copyright (c) 1994 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and the Network Research Group at
 *      Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/source.h,v 1.5 1999/02/09 17:20:27 piers Exp $ (LBL)
 */

#ifndef vic_source_h
#define vic_source_h

#include <string.h>
#include "sys-time.h"
#include "config.h"
#include "timer.h"
#include "inet.h"
#include "Tcl.h"
#include "rtp.h"
#include "mbus.h"



/* Added as a variation of Isidor's approach 
 * for the elastic playout buffer. It actually
 * incorporates the playout.h & playout.cc files
 * in source.h adn source.cc p
 */


class MtuAlloc {
 public:
        MtuAlloc();
        ~MtuAlloc();
        inline char *new_blk();
        inline void delete_blk(char *bp);
 protected:
        static char *blk_list_;
};

inline char *MtuAlloc::new_blk()
{
        char    *bp;

        if (blk_list_) {
                bp = blk_list_;
                blk_list_ = *((char**)bp);
        } else
                bp = new char[2 * RTP_MTU];

        return (bp);
}

inline void MtuAlloc::delete_blk(char *bp)
{
        *((char**)bp) = blk_list_;
        blk_list_ = bp;
}

class PacketData : public MtuAlloc {
 public:
        PacketData(u_char *pkt, struct rtphdr* rh, u_char *vh,
                   int len, u_int playout);
        inline void construct(u_char *pkt, struct rtphdr* rh, u_char *vh,
                   int len, u_int playout);
        ~PacketData();
        inline const struct rtphdr *rtp_hdr() const { return (rh_); }
        inline const u_char *video_data() const { return (vh_); }
        inline const int data_length() const { return (len_); }
        inline const int playout() const { return (playout_); }
 private:
        u_char          *pkt_;
        struct rtphdr   *rh_;
        u_char          *vh_;
        int             len_;
        u_int           playout_;
 public:
        PacketData *next_;
};

inline void
PacketData::construct(u_char *pkt, struct rtphdr* rh, u_char *vh,
                      int len, u_int playout)
{
        pkt_ = pkt;
        rh_ = rh;
        vh_ = vh;
        len_ = len;
        playout_ = playout;
        next_ = 0;
}


#include "net.h"

class SourceManager;

#define SOURCE_HASH 1024

class PacketHandler : public TclObject {
    public:
	virtual ~PacketHandler();
	inline PacketHandler(int hdrlen) : hdrlen_(hdrlen), delvar_(0) { }
	inline int hdrlen() const { return (hdrlen_); }
	virtual void recv(const struct rtphdr*,
			  const u_char* data, int len) = 0;
	inline u_int32_t delvar() const { return (delvar_); }
	inline void delvar(u_int32_t v) { delvar_ = v; }
    protected:
	int hdrlen_;
	int delvar_;
};

class Source : public TclObject, public Timer {
    public:
	Source(u_int32_t srcid, u_int32_t ssrc, Address & addr);
	virtual ~Source();

	PacketHandler* activate(int format);
	PacketHandler* change_format(int format);
	void deactivate();

	virtual int command(int argc, const char*const* argv);
	inline PacketHandler* handler() const { return (handler_); }

	void lts_done(const timeval& now) { lts_done_ = now; }
	void lts_data(const timeval& now) { lts_data_ = now; }
	void action() { if (!busy_) set_busy(); }
	void lts_ctrl(const timeval& now) { lts_ctrl_ = now; }
	void sts_data(u_int32_t now) { sts_data_ = now; }
	void sts_ctrl(u_int32_t now) { sts_ctrl_ = now; }
	void rtp_ctrl(u_int32_t now) { rtp_ctrl_ = now; }
	void map_ntp_time(u_int32_t t) { map_ntp_time_ = t; }
	void map_rtp_time(u_int32_t t) { map_rtp_time_ = t; }
	void rtp2ntp(int v) { rtp2ntp_ = v; }

	inline const timeval& lts_ctrl() const { return (lts_ctrl_); }
	inline const timeval& lts_data() const { return (lts_data_); }
	inline const timeval& lts_done() const { return (lts_done_); }
	inline int sts_ctrl() const { return (sts_ctrl_); }
	inline int sts_data() const { return (sts_data_); }
	inline int rtp_ctrl() const { return (rtp_ctrl_); }
	inline int rtp2ntp() const { return (rtp2ntp_); }
	inline int apdelay() const { return (apdelay_); }

	inline const char* sdes(int t) const { return (sdes_[t]); }
	void sdes(int t, const char* value);
	inline Address const & addr() const { return (addr_); }
	inline void addr(const Address & a) { addr_ = a; }
	inline u_int32_t srcid() const { return (srcid_); }
	inline void srcid(u_int32_t s) { srcid_ = s; }
	inline u_int32_t ssrc() const { return (ssrc_); }
	inline void ssrc(u_int32_t s) { ssrc_ = s; }
	inline void format(int v) { format_ = v; }
	inline int format() const { return (format_); }
	inline void mute(int v) { mute_ = v; }
	inline int mute() const { return (mute_); }
	inline void ismixer(int v) { ismixer_ = v; }
	inline int ismixer() const { return (ismixer_); }
	void clear_counters();

	/*XXX should start at random values*/
	inline u_int32_t nb() const { return (nb_); }
	inline u_int32_t nf() const { return (nf_); }
	inline u_int32_t np() const { return (np_); }
	inline u_int32_t nm() const { return (nm_); }
	inline u_int32_t ns() const { return (cs_ - fs_); } /* no. expected */
	inline u_int32_t ehs() const { return (-(fs_ & 0xffff0000) | cs_); }
	inline u_int32_t cs() const { return (cs_); }
	inline u_int32_t snp() const { return (snp_); }
	inline u_int32_t sns() const { return (sns_); }
	inline u_int32_t runt() const { return (nrunt_); }
	inline u_int32_t dups() const { return (ndup_); }
	inline int late() const { return (late_); } 
	inline u_int32_t badsesslen() const { return (badsesslen_); }
	inline u_int32_t badsessver() const { return (badsessver_); }
	inline u_int32_t badsessopt() const { return (badsessopt_); }
	inline u_int32_t badsdes() const { return (badsdes_); }
	inline u_int32_t badbye() const { return (badbye_); }
	inline void nb(int v) { nb_ += v; }
	inline void nf(int v) { nf_ += v; }
	inline void np(int v) { np_ += v; }
	inline void nm(int v) { nm_ += v; }
	inline void snp(int v) { snp_ = v; }
	inline void sns(int v) { sns_ = v; }
	inline void fs(int v) { fs_ = v; }
	inline void runt(int v) { nrunt_ += v; }
	inline void late(int v) { late_ += v; }
	inline void apdelay(int v) { apdelay_ = v; }
	inline void pending(int v) { pending_ = v; }
	inline void badsesslen(int v) { badsesslen_ += v; }
	inline void badsessver(int v) { badsessver_ += v; }
	inline void badsessopt(int v) { badsessopt_ += v; }
	inline void badsdes(int v) { badsdes_ += v; }
	inline void badbye(int v) { badbye_ += v; }
	int cs(u_int16_t v);
	int checkseq(u_int16_t v);
	void lost(int);

	inline void sync(int v) { sync_ = v; }
	inline int sync() const { return (sync_); }

	Source* next_;		/* link for SourceManager source list */
	Source* hlink_;		/* link for SourceManager hash table */

	// DMIRAS: This bit for the elastic buffer
	u_int32_t convert_time(u_int32_t ts);
	
	void add(u_char *pckt, struct rtphdr* rh, u_char *vh,
                      int len, u_int playout);
        void process(struct rtphdr* rh, u_char *vh, int len);
        inline void elastic(int v) { elastic_ = v; }
        inline int elastic() { return (elastic_); }
	
	// DMIRAS: This bit related to elastic buffer
        void adapt(u_int32_t arr_ts, u_int32_t src_ts, int flag);
        inline void skew(u_int s) { skew_ = s; }
        inline u_int skew() const { return (skew_); }
        inline double dvar() const { return (dvar_); }
        void recv(u_char *pkt, struct rtphdr* rh, u_char *vh, int cc);

	inline void mbus(MBusHandler *m) { mbus_ = m; }

    protected:
	char* stats(char* cp) const;
	void set_busy();

	PacketHandler* handler_;

	u_int32_t srcid_;	/* rtp global src id (CSRC), net order */
	u_int32_t ssrc_;	/* rtp global sync src id (SSRC), net order) */
	Address & addr_;	/* address of sender (net order) */

	int rtp2ntp_;		/* true if we've received a SR report */
	u_int32_t sts_data_;	/* sndr ts from last data packet (net order) */
	u_int32_t sts_ctrl_;	/* sndr ts from last control packet */
	u_int32_t rtp_ctrl_;	/* sndr rtp ts from last control packet */
	u_int32_t map_rtp_time_;/* local rtp time of last control packet */
	u_int32_t map_ntp_time_;/* local ntp time of last control packet */
	timeval lts_data_;	/* local unix time for last data packet */
	timeval lts_ctrl_;	/* local unix time for last control packet */
	timeval lts_done_;	/* local unix time for bye packet */

	u_int32_t fs_;		/* first seq. no received */
	u_int32_t cs_;		/* current (most recent) seq. no received */
	u_int32_t np_;		/* no. packets received  */
	u_int32_t nf_;		/* no. video frames received */
	u_int32_t nb_;		/* no. bytes received */
	u_int32_t nm_;		/* no. misordered packets detected */
	u_int32_t snp_;		/* last advertised no. pkts received */
	u_int32_t sns_;		/* last advertised no. pkts exptected */
	u_int32_t ndup_;	/* no. of duplicate packets (via RTP seqno) */
	u_int32_t nrunt_;	/* count of packets too small */
	
	/* following errors are from session (rtcp) processing */
	u_int32_t badsesslen_;	/* bad header length field */
	u_int32_t badsessver_;	/* bad header version number */
	u_int32_t badsessopt_;	/* unrecognized option */
	u_int32_t badsdes_;	/* sdes cnt > available data */
	u_int32_t badbye_;	/* bye cnt > available data */

	int format_;		/* RTP packet type */
	int mute_;		/* true if source muted */
	int lost_;		/* true when signal lost */
	int busy_;		/* nonzero. during talk spurt */
	int ismixer_;		/* true if source has acted as a 'mixer' */

#define SOURCE_NSEQ 64
	u_int16_t seqno_[SOURCE_NSEQ];
	char* sdes_[RTCP_SDES_MAX + 1];

	// DMIRAS: This bit the protected for elastic buffer
	PacketData *head_;
        PacketData *tail_;
        PacketData *free_;
        u_int32_t now_;
        float dtskew_;
        int elastic_;
	// DMIRAS: adaptive bit of the elastic buffer
	u_int skew_;            /* clock skew */
        int delay_;	        /* pkt delay */
        double dvar_;		/* delay variance */
        int delta_;		/* last delay time */
        int pdelay_;	        /* playout delay for this frame */
	int apdelay_;		/* audio playout delay as sent via mbus */
        int adapt_init_;        /* Initialised adapt? */
	int late_;		/* num of late pkts */
	int count_;
	int pending_;		/* if set, then have to send a mbus msg */
	int sync_;		/* set if audio-video synchronisation is set */

        void schedule();
        virtual void timeout();
	
	/* pointer to the mbus object, so that source can send to it */
	MBusHandler *mbus_;
};

class SourceManager : public TclObject {
    public:
	SourceManager();
	static inline SourceManager& instance() { return (instance_); }
	void init(u_int32_t localid, Address & localaddr);
	virtual int command(int argc, const char*const* argv);
	Source* lookup(u_int32_t addr);
	Source* lookup(u_int32_t srcid, u_int32_t ssrc, Address & addr);
	Source* demux(u_int32_t srcid, Address & addr, u_int16_t seq);
	Source* consult(u_int32_t srcid);
	inline int nsources() const { return (nsources_); }
	inline Source* sources() const { return (sources_); }

	void CheckActiveSources(double msgint);
	void ListSources();

	u_int32_t clock() const { return (clock_); }
	inline Source* localsrc() const { return (localsrc_); }

	void sortactive(char*) const;
	void remove(Source*);
    protected:
	static int compare(const void*, const void*);
	Source* enter(Source* s);
	void remove_from_hashtable(Source* s);

	Source* lookup_duplicate(u_int32_t srcid, Address & addr);

	int nsources_;
	Source* sources_;
	u_int32_t clock_;
	int keep_sites_;
	u_int site_drop_time_;
	Source* localsrc_;
	Source* generator_;
	Source* hashtab_[SOURCE_HASH];

	static SourceManager instance_;
};

inline int Source::checkseq(u_int16_t v)
{
	int k = v & (SOURCE_NSEQ-1);
	if (seqno_[k] != v) {
		seqno_[k] = v;
		return (0);
	} else {
		++ndup_;
		return (-1);
	}
}

inline int Source::cs(u_int16_t v)
{
	/*
	 * This routine updates a 32 bit sequence number based on
	 * the rtp packet's 16 bit seq. no.
	 */
	register int c = cs_;
	register int d = v - c;
	if (d < -1024 || d > 1024) {
		cs_ = v;
		if (v < 512 && c > 0x10000-512) {
			/*
			 * seq no. wrapped - subtract 64k from fs to
			 * account for it.
			 */
			fs_ -= 0x10000;
		} else {
			/*
			 * the seq. no made a very large jump.  assume
			 * that the other side restarted without telling
			 * us about it so just re-sync (i.e., pretend
			 * this was the first packet).
			 */
			fs_ = v - 1;
			np_ = 0;
			nf_ = 0;
			nb_ = 0;
			snp_ = 0;
			nm_ = 0;
		}
	} else if (d > 0) {
		/* d <= 0 means duplicate or reordered packet - ignore */
		cs_ = v;
		if (d < 0)
			/* out of order */
			++nm_;
	}
	return (checkseq(v));
}

#endif