summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_server/Video_Control_i.cpp
blob: 5db371090c1e6047e2233aa7a7b6d8e5fb03eb5d (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
// $Id$

#include "Video_Control_i.h"
#include "mpeg_server/Video_Server.h"

ACE_RCSID(mpeg_server, Video_Control_i, "$Id$")

Video_Control_i::Video_Control_i ()
  : reactor_ (TAO_ORB_Core_instance ()->reactor ()),
    state_ (0),
    data_handler_ (0),
    sig_handler_ (0)
{
}

int
Video_Control_i::create_handlers (void)
{
  ACE_NEW_RETURN (this->data_handler_,
                  Video_Data_Handler,
                  -1);

  ACE_NEW_RETURN (this->sig_handler_, 
                  Video_Sig_Handler,
                  -1);
  return 0;
}
CORBA::Boolean 
Video_Control_i::init_video (const Video_Control::INITvideoPara &para,
                             Video_Control::INITvideoReply_out reply,
                             CORBA::Environment& env)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Video_Control_i::init_video () called\n"));
  
  CORBA::Boolean result = this->state_->init_video (para,
                                                    reply);

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Video_Control_State::init_video returned %d\n", result));
  return result;

}


CORBA::Boolean 
Video_Control_i::stat_stream (CORBA::Char_out ch,
                              CORBA::Long_out size,
                              CORBA::Environment& env)
{
  return 0;
}


void
Video_Control_i::close (CORBA::Environment& env)
{
  ACE_DEBUG ((LM_DEBUG, 
              "(%P|%t) Video_Control_i::close:"
              "shutting down the ORB\n"));

  TAO_ORB_Core_instance ()-> orb ()-> shutdown ();
  return;
}


CORBA::Boolean 
Video_Control_i::stat_sent (CORBA::Environment& env)
{
  return 0;
}


CORBA::Boolean 
Video_Control_i::fast_forward (const Video_Control::FFpara &para,
                               CORBA::Environment& env)
{
  return this->state_->fast_forward (para);
}


CORBA::Boolean 
Video_Control_i::fast_backward (const Video_Control::FFpara &para,
                                CORBA::Environment& env)
{
  return this->state_->fast_backward (para);
}


CORBA::Boolean 
Video_Control_i::step (const Video_Control::STEPpara &para,
                       CORBA::Environment& env)
{
  return this->state_->step (para);
}


// this gets called by the client, when it wants
// to start playback
CORBA::Boolean 
Video_Control_i::play (const Video_Control::PLAYpara &para,
                       CORBA::Long_out vts,
                       CORBA::Environment& env)
{
  return this->state_->play (para,
                             vts);
}


CORBA::Boolean 
Video_Control_i::position (const Video_Control::POSITIONpara &para,
                           CORBA::Environment& env)
{
  return this->state_->position (para);
}


CORBA::Boolean 
Video_Control_i::speed (const Video_Control::SPEEDpara &para,
                        CORBA::Environment& env)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Video_Control_i::speed () called\n"));
  return this->state_->speed (para);
}


CORBA::Boolean 
Video_Control_i::stop (CORBA::Long cmdsn,
                       CORBA::Environment& env)
{
  return this->state_->stop (cmdsn);
}

CORBA::Boolean
Video_Control_i::set_peer (char * &peer,
                           CORBA::Environment &_tao_environment)
{
   ACE_INET_Addr client_data_addr (peer);
  // Data (UDP) Address of the client.

   ACE_DEBUG ((LM_DEBUG,
               "(%P|%t) set_peer called: %s,%s,%d\n",
               peer,
               client_data_addr.get_host_addr (),
               client_data_addr.get_port_number ()));

   char temp_addr [BUFSIZ];
   if (VIDEO_SINGLETON::instance ()->data_host != 0)
     ACE_OS::sprintf (temp_addr,"%s:%d",VIDEO_SINGLETON::instance ()->data_host,0);
   else
     ACE_OS::sprintf (temp_addr,":%d",0);
 
  if (VIDEO_SINGLETON::instance ()->dgram.open (client_data_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) UDP open failed: %p\n"),
                      0);

  // set the socket buffer sizes to 64k.
  int sndbufsize = ACE_DEFAULT_MAX_SOCKET_BUFSIZ;
  int rcvbufsize = ACE_DEFAULT_MAX_SOCKET_BUFSIZ;
  
  if (VIDEO_SINGLETON::instance ()->dgram.set_option (SOL_SOCKET,
                                                       SO_SNDBUF,
                                                       (void *) &sndbufsize,
                                                       sizeof (sndbufsize)) == -1
      && errno != ENOTSUP)
    return 0;
  else if (VIDEO_SINGLETON::instance ()->dgram.set_option (SOL_SOCKET,
                                                           SO_RCVBUF,
                                                           (void *) &rcvbufsize,
                                                           sizeof (rcvbufsize)) == -1
           && errno != ENOTSUP)
    return 0;


  ACE_INET_Addr server_data_addr;
  // Data (UDP) Address of this server.
  
  if (VIDEO_SINGLETON::instance ()-> dgram.get_local_addr 
      (server_data_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) UDP get_local_addr failed: %p\n"),
                      0);

  ACE_DEBUG ((LM_DEBUG, 
              "(%P|%t) Video_Server: My UDP port number is %d\n",
              server_data_addr.get_port_number ()));

  if (this->register_handlers () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) Video_Control_i::set_peer: register_handlers failed\n"),
                      0);

  VIDEO_SINGLETON::instance ()->videoSocket = VIDEO_SINGLETON::instance ()->dgram.get_handle ();

  ACE_DEBUG ((LM_DEBUG,"(%P|%t) set_peer: server port = %d\n",server_data_addr.get_port_number ()));
  ACE_NEW_RETURN (peer,
                  char [BUFSIZ],
                  0);
  // hack to set the ip address correctly.
  server_data_addr.set (server_data_addr.get_port_number (),
                        server_data_addr.get_host_name ());
  server_data_addr.addr_to_string (peer,
                                   BUFSIZ);

  return 1;
}

int
Video_Control_i::register_handlers (void)
{
  int result;


  // create the handlers

  if (this->create_handlers () == -1)
    return -1;

  this->change_state (VIDEO_CONTROL_WAITING_STATE::instance ());
  // sets the state to waiting.

  // Register the event handlers with the Reactor
  // first the data handler, i.e. UDP
  result = this->reactor_->register_handler (this->data_handler_, 
                                             ACE_Event_Handler::READ_MASK);
  if (result < 0)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P|%t) register_handler for data_handler failed\n"),
                        -1);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) registered fd for data handler = (%d)\n",
              this->data_handler_->get_handle ()));
  
  // finally, the signal handler, for periodic transmission
  // of packets
  result = this->sig_handler_->register_handler ();
  
  if (result < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) register_handler for sig_handler"
                       "failed!\n"),
                      -1);

  return 0;
}

// Changes the state of the video control handler from the current
// state to the state represented by the argument.
void
Video_Control_i::change_state (Video_Control_State *state)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Video_Control_i::Changing to state %d\n",
              state->get_state ()));
  this->state_ = state;
}

// Returns the current state object .
Video_Control_State *
Video_Control_i::get_state (void)
{
  return this->state_;
}


Video_Control_i::~Video_Control_i ()
{
  delete this->data_handler_;
  delete this->sig_handler_;
}