summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/AV/RTCP.h
blob: 1ee7aa8b03f8848485d75ec222c3f68334f5aea2 (plain)
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
/* -*- C++ -*- */
// $Id$
/*-
 * Copyright (c) 1993-1994 The 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.
 */

// ============================================================================
//
// = LIBRARY
//   ORBSVCS AVStreams
//
// = FILENAME
//   RTCP.h
//
// = AUTHOR
//    Nagarajan Surendran <naga@cs.wustl.edu>
//
//
// ============================================================================

#ifndef TAO_AV_RTCP_H
#define TAO_AV_RTCP_H

#include "ace/OS.h"
#include "source.h"
#include "AVStreams_i.h"
#include "UDP.h"
#include <math.h>
#include <stdlib.h>
#include "RTP.h"

// TAO_AV_RTP_State
class TAO_AV_RTP_State
{
public:
  TAO_AV_RTP_State (void);
  int bad_version_;
  u_int badoptions_;
  u_int badfmt_;
  u_int badext_;
  u_int nrunt_;
  ACE_UINT32 last_np_;
  ACE_UINT32 sdes_seq_;
  double rtcp_inv_bw_;
  double rtcp_avg_size_;        /* (estimated) average size of rtcp packets */
  double rint_;         /* current session report rate (in ms) */
  int confid_;
  u_char* pktbuf_;
};

class TAO_AV_RTCP_Callback;
class TAO_AV_SourceManager;

class TAO_AV_RTCP
{
public:
  struct rtcphdr
  {
    ACE_UINT16 rh_flags;        /* T:2 P:1 CNT:5 PT:8 */
    ACE_UINT16 rh_len;  /* length of message (in bytes) */
    ACE_UINT32 rh_ssrc; /* synchronization src id */
  };

  struct ntp64
  {
    ACE_UINT32 upper;   /* more significant 32 bits */
    ACE_UINT32 lower;   /* less significant 32 bits */
  }; 

  /*
 * Sender report.
 */
  struct rtcp_sr {
    ntp64 sr_ntp;               /* 64-bit ntp timestamp */
    ACE_UINT32 sr_ts;   /* reference media timestamp */
    ACE_UINT32 sr_np;   /* no. packets sent */
    ACE_UINT32 sr_nb;   /* no. bytes sent */
  };

  /*
 * Receiver report.
 * Time stamps are middle 32-bits of ntp timestamp.
 */
  struct rtcp_rr {
    ACE_UINT32 rr_srcid;        /* sender being reported */
    ACE_UINT32 rr_loss; /* loss stats (8:fraction, 24:cumulative)*/
    ACE_UINT32 rr_ehsr; /* ext. highest seqno received */
    ACE_UINT32 rr_dv;   /* jitter (delay variance) */
    ACE_UINT32 rr_lsr;  /* orig. ts from last rr from this src  */
    ACE_UINT32 rr_dlsr; /* time from recpt of last rr to xmit time */
  };

  static int handle_input (ACE_Message_Block *data,
                           const ACE_Addr &peer_address,
                           rtcphdr &header,
                           TAO_AV_SourceManager *source_manager,
                           TAO_AV_RTP_State *state);

  static int build_bye (rtcphdr* rh,
                        TAO_AV_Source& local);

  static int build_sdes (rtcphdr* rh,
                         TAO_AV_Source& s,
                         TAO_AV_RTP_State *state);

  static u_char* build_sdes_item (u_char* p,
                                  int code,
                                  TAO_AV_Source&);

  static void parse_sr (rtcphdr* rh,
                        int flags,
                        u_char* ep,
                        TAO_AV_Source* ps,
                        ACE_UINT32 addr,
                        TAO_AV_SourceManager *source_manager);

  static void parse_rr (rtcphdr* rh,
                        int flags,
                        u_char* ep,
                        TAO_AV_Source* ps,
                        ACE_UINT32 addr,
                        TAO_AV_SourceManager *source_manager);

  static void parse_rr_records (ACE_UINT32 ssrc,
                                rtcp_rr* r,
                                int cnt,
                                const u_char* ep,
                                ACE_UINT32 addr);

  static int sdesbody (ACE_UINT32* p,
                       u_char* ep,
                       TAO_AV_Source* ps,
                       ACE_UINT32 addr,
                       ACE_UINT32 ssrc,
                       TAO_AV_SourceManager *source_manager);

  static void parse_sdes (rtcphdr* rh,
                          int flags,
                          u_char* ep,
                          TAO_AV_Source* ps,
                          ACE_UINT32 addr,
                          ACE_UINT32 ssrc,
                          TAO_AV_SourceManager *source_manager);

  static void parse_bye (rtcphdr* rh,
                         int flags,
                         u_char* ep,
                         TAO_AV_Source* ps,
                         TAO_AV_SourceManager *source_manager);

  static void send_report (int bye,
                           TAO_AV_Protocol_Object *object,
                           TAO_AV_SourceManager *source_manager,
                           TAO_AV_RTP_State *state,
                           TAO_AV_RTCP_Callback *callback);

  static ACE_UINT32 alloc_srcid (ACE_UINT32 addr);

  static double fmod (double dividend, double divisor);
};

class TAO_AV_Callback;

class TAO_AV_Export TAO_AV_RTCP_Object 
  : public TAO_AV_Protocol_Object
{
public:
  TAO_AV_RTCP_Object (TAO_AV_Callback *callback,
                      TAO_AV_Transport *transport = 0);
  // constructor.

  virtual ~TAO_AV_RTCP_Object (void);
  // Destructor

  virtual int handle_input (void);
  virtual int handle_control_input (ACE_Message_Block *frame,
                                    const ACE_Addr &peer_address);

  virtual int set_policies (const TAO_AV_PolicyList &policy_list);
  // set/get policies.

  virtual int start (void);
  virtual int stop (void);
  // start/stop the flow.

  virtual int send_frame (ACE_Message_Block *frame,
                          TAO_AV_frame_info *frame_info = 0);
  // send a data frame.

  virtual int send_frame (const iovec *iov,
                          int iovcnt,
                          TAO_AV_frame_info *frame_info = 0);
  // send a frame in iovecs.

  virtual int destroy (void);
  // end the stream.
};


class TAO_AV_Export TAO_AV_RTCP_Flow_Factory
  :public TAO_AV_Flow_Protocol_Factory
{
public:
  TAO_AV_RTCP_Flow_Factory (void);
  virtual ~TAO_AV_RTCP_Flow_Factory (void);
  virtual int init (int argc, char *argv[]);
  virtual int match_protocol (const char *flow_string);
  virtual TAO_AV_Protocol_Object* make_protocol_object (TAO_FlowSpec_Entry *entry,
                                                        TAO_Base_StreamEndPoint *endpoint,
                                                        TAO_AV_Flow_Handler *handler,
                                                        TAO_AV_Transport *transport);
};

class TAO_AV_Export TAO_AV_RTCP_Callback : public TAO_AV_Callback
{
public:
  TAO_AV_RTCP_Callback (void);
  // RTCP callback.

  virtual ~TAO_AV_RTCP_Callback (void);
  // virtual destructor.

  virtual int handle_start (void);
  // Called during Streamctrl->start.

  virtual int handle_stop (void);
  // Called during Streamctrl->stop.

  virtual int handle_timeout (void *arg);
  // Called during timeout for Flow Producers.

  virtual int receive_frame (ACE_Message_Block *frame,
                             TAO_AV_frame_info *frame_info = 0,
                             const ACE_Addr &peer_address = ACE_Addr::sap_any);
  // Called when a frame arrives for a FlowConsumer.

  virtual int receive_control_frame (ACE_Message_Block *frame,
                                     const ACE_Addr &peer_address = ACE_Addr::sap_any);

  virtual int handle_destroy (void);
  // Called during Streamctrl->destroy i.e tear_down  of the stream
  // @@coryan:Call it handle_destroy or handle_close.

  virtual void get_timeout (ACE_Time_Value *&tv,
                            void *&arg);
  // Called to get the timeout. If tv is 0 then the framework stop
  // calling this.

  int demux (TAO_AV_RTP::rtphdr* rh,
             ACE_Message_Block *data,
            const ACE_Addr &peer_address);

  virtual int get_rtp_source (TAO_AV_Source *&source,
                              ACE_UINT32 srcid,
                              ACE_UINT32 ssrc,
                              ACE_UINT32 addr);
  void schedule (int ms);
  TAO_AV_SourceManager *source_manager (void);
  TAO_AV_RTP_State *state (void);
protected:
  TAO_AV_SourceManager *source_manager_;
  TAO_AV_RTP_State *state_;
  int timeout_;
};

ACE_STATIC_SVC_DECLARE (TAO_AV_RTCP_Flow_Factory)
ACE_FACTORY_DECLARE (TAO_ORBSVCS, TAO_AV_RTCP_Flow_Factory)

#endif /* TAO_AV_RTCP_H */