summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_client/vb.h
blob: 6c0e4e69a86b560ed0565ab0f66560a518e7c1ee (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    mpeg_client
//
// = FILENAME
//    vb.h
//
// = DESCRIPTION
//     Defines the video packet buffering process.
//
// = AUTHORS
//    Nagarajan Surendran (naga@cs.wustl.edu)
//
// ============================================================================

#if !defined (AV_VB_H)
#define AV_VB_H

#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Intrinsic.h>
#if defined(__svr4__) || defined(IRIX)
#include <stropts.h>
#include <sys/conf.h>
#endif
#include "include/common.h"
#include "newproto.h"
#include "global.h"
#include "mpeg_shared/filters.h"
#include "mpeg_shared/fileio.h"
#include "mpeg_shared/com.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h"

/* magic number -- deviation is considered
   caused by clock drift only if rate <= 1/MAX_CLOCK_DRIFT.
   */
#define MAX_CLOCK_DRIFT 50
#define SHCODE 0x000001b3
#define max(a,b) ((a)>(b) ? (a) : (b))

struct block
{
  unsigned shcode;
  struct block * next;
  int full;
};

struct header
{
  struct block *h, *t;
};

class Notification_Handler;

class VideoBuffer :public ACE_Event_Handler
{
public:
  VideoBuffer (void);
  // constructor.

  virtual ~VideoBuffer (void);
  // destructor.

  virtual int handle_input (ACE_HANDLE fd);
  // callback when data arrives on the video socket.

  virtual int handle_output (ACE_HANDLE fd);
  // callback when data can be written on the video socket.

  ACE_HANDLE get_handle (void) const;
  // gets the video socket.
 
  void VBinitBuf (int size);
  // size in byte.

  char * VBgetBuf (int size);
  // block version.

  int VBcheckBuf (int size);
  // non-block check, return True/False

  void VBputMsg (char * msgPtr);

  char * VBgetMsg ();
  // block version.

  int  VBcheckMsg();
  // non-block check, return Number of Msgs in buffer.

  int VBbufEmpty(void);

  void VBreclaimMsg(char * msgPtr);

  static void VBdeleteBuf(void);

  static void VBdeleteSem(void);

  static void exit_on_kill(void);

  int  VBprocess(int initSocket, int normalSocket);

private:

  int sync_feedback (void);
  int feedback_action (void);

  static block ** head;
  static block ** tail;
  static char * buf;
  static int bufsize;
  static int sid, countid;
  static int exit_tag;
  static int conn_tag;
  static int savedSocket;

  VideoMessage *msg, msghd;
  VideoPacket *packet;
  int len, bsize, msgsn;
  int psize, poffset;
  char * ptr, *ptr1;
  char *tmp_buf;
  int dataSocket;
  int initSocket,normalSocket;
  
  // following variables are for feedback 
  int pcmdsn, pcmd; 
  // to store the cmdsn and cmd of previous frame,
  // to detect new commands 

  int pfid, pgop;
  // frame-id of gopid of previous frame 

  int cmdsn;
  // cmdsn of previous command 

  int fb_state;
  // state of the control law/regulator 

  int startpos;
  // gopid or frameid when the regulator is in 'start' state 1 

  int delay;
  // #frames to delay after 'start' 

  int qosRecomputes;
  // indicate if qos has been recomputed recently 

  int action_delay;
  // microseconds to delay after feedback action 

  int action_time;
  // the time when feedback action is taken, used to enforce
  // action_delay 

  int upf, cupf;
  // upf -- currentUPF as carried in all frames,
  // cupf -- real current UPF, being adjusted by FB 

  double fv;
  // current buffer fill level in term of #frames 	

  double init_fv;
  // init_fv and init_pos record the fv value and position
  // (frameid or gopid) when fv is very close to med 

  double init_pos;
  int high, low, med;
  // median, high and low water marks, in term of #frames 

  int period;
  // interms of #frames, (high-med, period) defined the max
  // buffer fill drift which can be assumed as caused by
  // clock drift 

  Filter * f;
  // buffer fill level filter 

  int advance, min_advance;
  // in microseconds, advance holds the
  // current target buffer-fill-level as
  // determined by jitter level
  // estimate. min_advance sets a absolute
  // minimun level, no matter how low jitter
  // level is.'advance' does not change when
  // play-speed changes, but med/high/low
  // will be updated upon speed change 

  double fav;
  // #frames, jitter level output by the filter.
 
  Filter * fa;
  // jitter filter.
 
  int reach_limit;
  // indicate that the target 'advance' has reached client VB
  // buffer limit, and can not further increase, even if
  // jitter increases. 

  int not_action;
  // indicates entering state=3 is not caused by
  // feedback action. If not_action = 1 entering state=3
  // is caused either by 'start' or speed change, and
  // the jitter filter needs to be reset. 

#ifdef STAT
  int to_count;
  int gap_msgsn;
#endif

  Notification_Handler *handler_;
  // handler to be used for exit notification.

  enum state {INVALID = -1,
              READ_HEADER = 0,
              READ_NEXT_HEADER = 1,
              READ_MESSAGE = 2,
              SKIP_MESSAGE = 3,
              SKIP_NEXT_MESSAGE = 4,
              WRITE_FEEDBACK1 = 5,
              WRITE_FEEDBACK2 = 6};
  state state_;

  char *temp;
  // temporary pointer.
  int bytes;
  // temporary placeholder for no.of bytes to be read.

  int fb_addupf;
  int fb_addf;
  int fb_advance;
  
  int socket_flag_;
  // flag to indicate which socket to be used.
};

class Notification_Handler :public ACE_Event_Handler
{
public:
  virtual ACE_HANDLE get_handle (void) const;
  // Get the Notification handle.

  virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
  // called when input events occur.

};  

class Video_Notification_Handler :public Notification_Handler
{
public:
  virtual ACE_HANDLE get_handle (void) const;
  // Returns the video notification handle.
};
#endif /* AV_VB_H */