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

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

#if !defined (AV_AB_H)
#define AV_AB_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>
#ifdef __svr4__
#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/OS.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h"
#include "vb.h"

extern VideoBuffer *vbuffer;

/* magic number -- deviation is considered
   caused by clock drift only if rate <= 1/MAX_CLOCK_DRIFT.
   */
#define MAX_CLOCK_DRIFT 50

#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define PACKET_SIZE 8192
#define STARTUP_WAIT 10000000
#define ACTION_WAIT 5000000

typedef struct {
  int bufsize; /* number of bytes for the buffer pointed by 'buf' */
  char * buf;  /* pointer to the data buffer area */
  int bps; /* current byte-per-sample */
  int size;  /* number of samples the buffer can hold */
  int samples; /* number of samples in the buffer; */
  int stuff; /* number of stuff samples to be read by ABgetSamples() */
  int ts;  /* tail-sample: the next sample to be comsumed by CTR */
  int hs;  /* head-sample: the next sample to be expected from the network */
  int tind; /* index of the ts-sample in the buf */
} ABBuffer;


class Notification_Handler;

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

  int ABprocess (int dataSocket);
  // start the buffering process.

  ~AudioBuffer (void);
  // destructor.

  virtual ACE_HANDLE get_handle (void) const;
  // get the underlying I/O handle.

  virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
  // called when input arrives on the datasocket.

  virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);
  // Called to send feedback.

  void set_silence(char *buf, int samples);

  void ABinitBuf(int size);

  void ABflushBuf(int nextSample);
  // flush the whole buffer 

  int ABcheckSamples(void);
  // returns # of samples in ABbuf

  int ABgetSamples(char * buf, int samples);
  // read at most given number of samples from AB to buf, returns
  // number of sample actually read 

  int ABskipSamples(int samples);
  // if samples < 0; then stuff |samples| silient samples to ABgetSamples(),
  //   otherwise wipe out this number of samples from AB. 

  void ABdeleteBuf(void);

  void ABdeleteSem(void);

  static void exit_on_kill(void);

  static void usr1_handler(int sig);

  static void usr2_handler(int sig);

  void feedback (void);

private:
  char *temp;
  // temporary pointer.
  int bytes;
  // temporary placeholder for no.of bytes to be read.
  ABBuffer *abuf;
  int sid;
  int exit_tag;
  int savedSocket;
  AudioPacket *packet;
  char *pkt_data;
  int conn_tag;
  /* following are for feedback */
  int fbstate;
  unsigned waketime;
  int pcmdsn;

  int dataSocket;
  // data socket handle.

  Notification_Handler *handler_;
  
  enum MODE {INVALID = -1,READ_HEADER,READ_DATA,WRITE_FEEDBACK1,WRITE_FEEDBACK2};
  int mode_;
};

class Audio_Notification_Handler :public Notification_Handler
{
public:
  virtual ACE_HANDLE get_handle (void) const;
  // Returns the audio notification handle.
};

#endif /* AV_AB_H */