summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_client/Command_Handler.h
blob: 41f437b3ab5dfa1a30f8f4cc939798169292bdff (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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// $Id$

/* Copyright (c) 1995 Oregon Graduate Institute of Science and Technology
 * P.O.Box 91000-1000, Portland, OR 97291, USA;
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of O.G.I. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  O.G.I. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * O.G.I. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * O.G.I. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Shanwei Cen
 *         Department of Computer Science and Engineering
 *         email: scen@cse.ogi.edu
 */
#include "ace/OS.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

// ============================================================================
//
// = LIBRARY
//    mpeg_client
//
// = FILENAME
//    Command_Handler.h
//
// = DESCRIPTION
//     Defines the client side components of the A/V framework like
//     video stream endpoints ,video multi media device, video virtual device.
//
// = AUTHORS
//    Sumedh Mungee (sumedh@cs.wustl.edu)
//    Nagarajan Surendran (naga@cs.wustl.edu)
//
// ============================================================================


#ifndef AV_COMMAND_HANDLER_H
#define AV_COMMAND_HANDLER_H

#include "ace/Reactor.h"
#include "ace/Event_Handler.h"
#include "ace/ARGV.h"
#include "tao/TAO.h"
#include "mpeg_shared/Video_ControlC.h"
#include "orbsvcs/CosNamingC.h"
#include "ace/SOCK_Dgram.h"
#include "ace/SOCK_Connector.h"
#include "mpeg_shared/Audio_ControlC.h"
#include "orbsvcs/AV/AVStreams_i.h"
#include "ace/High_Res_Timer.h"
#include "ace/Acceptor.h"
#include "orbsvcs/Naming/Naming_Utils.h"
#include "mpeg_shared/ReceiverS.h"


class Command_Handler;


class Receiver_i
  :public POA_Receiver
{
 public:
  Receiver_i (Command_Handler *handler);
  // constructor.

  void push_mmdevice (CORBA::Object_ptr audio_mmdevice,
                      const char* audio_file,
                      CORBA::Object_ptr video_mmdevice,
                      const char *video_file,
                      CORBA::Environment&);

 private:
  Command_Handler *command_handler_;
  // Reference to command handler.
};

class Video_Client_StreamEndPoint
  :public virtual TAO_Client_StreamEndPoint
{
  // = TITLE
  //    Defines a video client StreamEndpoint class
  // = DESCRIPTION
  //    Makes use of a connected dgram to receive data from the server
  //    side stream endpoint.
public:
  Video_Client_StreamEndPoint (Command_Handler *command_handler =0,
                               char *host =0);
  // constructor

  virtual int handle_open (void);
  // called when streamendpoint is instantiated

  virtual int handle_close (void);
  // called when streamendpoint is being destructed

  virtual CORBA::Boolean handle_preconnect (AVStreams::flowSpec &the_spec);
  // called before connecting

  virtual CORBA::Boolean handle_postconnect (AVStreams::flowSpec &the_spec);
  // called after connecting

  virtual int handle_start (const AVStreams::flowSpec &the_spec,
                             CORBA::Environment &env) ;

  virtual int handle_stop (const AVStreams::flowSpec &the_spec,
                            CORBA::Environment &env) ;

  virtual int handle_destroy (const AVStreams::flowSpec &the_spec,
                               CORBA::Environment &env);

  virtual ACE_HANDLE get_handle (void);
  // gets the underlying socket descriptor from the SOCK_Dgram

private:
  ACE_SOCK_Dgram dgram_;
  // The datagram used for streaming.

  Command_Handler *command_handler_;
  // Reference to the command handler

  char *host_;
  // host interface to bind to.
};

class Audio_Client_StreamEndPoint
  :public virtual TAO_Client_StreamEndPoint
{
  // = TITLE
  //    Defines a audio client stream endpoint
  // = DESCRIPTION
  //    Uses a connected dgram to receive data from the audio server.
public:
  Audio_Client_StreamEndPoint (Command_Handler *command_handler = 0,
                               char *host = 0);
  // constructor

  virtual int handle_open (void);
  // called when streamendpoint is instantiated

  virtual int handle_close (void);
  // called when streamendpoint is being destructed

  virtual CORBA::Boolean handle_preconnect (AVStreams::flowSpec &the_spec);
  // called before connecting

  virtual CORBA::Boolean handle_postconnect (AVStreams::flowSpec &the_spec);
  // called after connecting

  virtual int handle_start (const AVStreams::flowSpec &the_spec,
                             CORBA::Environment &env) ;

  virtual int handle_stop (const AVStreams::flowSpec &the_spec,
                            CORBA::Environment &env) ;

  virtual int handle_destroy (const AVStreams::flowSpec &the_spec,
                               CORBA::Environment &env);

  virtual ACE_HANDLE get_handle (void);
  // gets the underlying socket descriptor from the SOCK_Dgram

private:
  ACE_SOCK_Dgram dgram_;
  // The datagram used for streaming.

  Command_Handler *command_handler_;
  // Reference to the command handler.

  char *host_;
  // host interface to bind to.
};


class Video_Client_VDev
  : public virtual TAO_VDev
{
  // = TITLE
  //    Defines a class that is a client side Virtual device for video.
  // = Description
  //    Needs a pointer to the command handler object so that it can
  //    set the video control pointer of the command handler.
public:
  Video_Client_VDev (void);
  Video_Client_VDev (Command_Handler *command_handler);

protected:
  CORBA::Boolean set_media_ctrl (CORBA::Object_ptr media_ctrl,
                                 CORBA::Environment& env);

private:
  Video_Control_ptr video_control_;
  // The video controller

  Command_Handler *command_handler_;
  // pointer to the command handler object
};


class Audio_Client_VDev
  : public virtual TAO_VDev
{
  // = TITLE
  //    Defines a client side audio virtual device
  // = DESCRIPTION
  //    Takes a pointer to a command handler object so that it can set
  //    the audio control object pointer of the command handler.
public:
  Audio_Client_VDev (void);
  Audio_Client_VDev (Command_Handler *command_handler);

protected:
  CORBA::Boolean set_media_ctrl (CORBA::Object_ptr media_ctrl,
                                 CORBA::Environment& env);

private:
  Audio_Control_ptr audio_control_;
  // The Audio controller

  Command_Handler *command_handler_;
  // pointer to the command handler object
};

class Video_Endpoint_Reactive_Strategy_A
  : public TAO_AV_Endpoint_Reactive_Strategy_A<Video_Client_StreamEndPoint,Video_Client_VDev,AV_Null_MediaCtrl>
{
  // = TITLE
  //    Implementation of the Reactive Strategy for the client
  //    video MMDevice.
  // = DESCRIPTION
  //    Takes a command handler object pointer. It overrides the
  //    make_vdev and make_stream_endpoint hooks to create the video
  //    vdev and video streamendpoint with command handler object pointers.
public:
  Video_Endpoint_Reactive_Strategy_A (TAO_ORB_Manager *orb_manager,
                                      Command_Handler *command_handler);
  // constructor . The orb manager is needed for the TAO_AV_Endpoint_Reactive_Strategy_A.

  virtual ~Video_Endpoint_Reactive_Strategy_A (void);
  // destructor.

  virtual int make_vdev (Video_Client_VDev *&vdev);
  // hook to make our Vdev with the pointer to command handler.
  virtual int make_stream_endpoint (Video_Client_StreamEndPoint *& endpoint);
  // hook to make our streamendpoint taking a command handler pointer
private:
  Command_Handler *command_handler_;
  // pointer to command handler object

  Video_Client_VDev *vdev_;
  // reference to the created vdev .

  Video_Client_StreamEndPoint *endpoint_;
  // reference to the created streamendpoint.
};

class Audio_Endpoint_Reactive_Strategy_A
  : public TAO_AV_Endpoint_Reactive_Strategy_A<Audio_Client_StreamEndPoint,Audio_Client_VDev,AV_Null_MediaCtrl>
{
  // = TITLE
  //    Implementation of the Reactive Strategy for the client audio
  //     MMDevice
  // = DESCRIPTION
  //    Takes a command handler object pointer. It overrides the
  //    make_vdev and make_stream_endpoint hooks to create the audio
  //    vdev and audio streamendpoint with command handler object pointers.
public:
  Audio_Endpoint_Reactive_Strategy_A (TAO_ORB_Manager *orb_manager,
                                      Command_Handler *command_handler);
  // constructor . The orb manager is needed for the TAO_AV_Endpoint_Reactive_Strategy_A.

  ~Audio_Endpoint_Reactive_Strategy_A (void);
  // Destructor.

  virtual int make_vdev (Audio_Client_VDev *&vdev);
  // hook to make our Vdev with the pointer to command handler.

  virtual int make_stream_endpoint (Audio_Client_StreamEndPoint *& endpoint);
  // hook to make our streamendpoint taking a command handler pointer

private:
  Command_Handler *command_handler_;
  // pointer to command handler object

  Audio_Client_VDev *vdev_;
  // reference to the vdev that was created.

  Audio_Client_StreamEndPoint *endpoint_;
  //reference to the created streamendpoint.

};

class Client_Sig_Handler
  : public virtual ACE_Event_Handler
  // = TITLE
  //    Defines the signal handler class for the client timer processing
  //
  // = DESCRIPTION
  //    Checks if the video is ready and refreshes the display and
  //    also plays the audio packets in the timer processing.
{
public:
  Client_Sig_Handler (Command_Handler *command_handler);
  // We need the command handler to call close ()

  ~Client_Sig_Handler (void);
  // Destructor

  virtual ACE_HANDLE get_handle (void) const;

  int register_handler (void);
  // this will register this sig_handler
  // with the reactor for SIGCHLD,SIGTERM,SIGINT

  virtual int shutdown (ACE_HANDLE,
                        ACE_Reactor_Mask);
  // handles the reactor shutdown

  virtual int handle_input (ACE_HANDLE);
  // handle input on the dummy handle.

  virtual int handle_signal (ACE_HANDLE signum,
                             siginfo_t * = 0,
                             ucontext_t* = 0);
  // handles SIGALRM

  void TimerHandler (int signum);

  void stop_timer (void);

  void DisplayPicture (void);
  // display the picture.

  void TimerProcessing (void);

  void PlayAudioOnly (void);

private:
  ACE_HANDLE handle_;
  // dummy handle for the sig handler.

  ACE_Sig_Set sig_set;
  // the signal set

  Command_Handler *command_handler_;
  // We need the command handler to call close ()

};

enum Suspended
{
  UNSUSPENDED = 0,INIT=1,STEP=2,POSITION=3
};

class Command_Handler
  : public virtual ACE_Event_Handler
    //    public virtual ACE_Svc_Handler <ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
  // = TITLE
  //   Defines the event handler class for the client commands
  //
  // = DESCRIPTION
  //   This class makes use of a UNIX socket, over which
  //   it will receive commands from the GUI

public:

  int command_suspended_;
  // flag to indicate whether the GUI handler has been suspended.

  int operation_tag_;
  // operation tag for release/press for position.

  int position_val_;
  // value of the position button.

  Command_Handler (ACE_HANDLE command_handle = ACE_INVALID_HANDLE);
  // Construct this handler with a control (UNIX) handle

  ~Command_Handler (void);
  // Destructor

  int init (int argc,char **argv);
  // initialize the ORB and NamingService

  int run (void);
  // Run the ORB event loop

  int handle_timeout (const ACE_Time_Value &,
                      const void *arg);
  // handle the timeout

  //  void stop_timer (void);
  // stop the internal timer

  void wait_for_display (Suspended reason);
  // suspends the gui socket handler in the reactor.

  TAO_ORB_Manager *orb_manager (void);
  // returns the orbmanager reference

  void set_mmdevice (CORBA::Object_ptr audio_mmdevice,
                     const char *audio_file,
                     CORBA::Object_ptr video_mmdevice,
                     const char *video_file);
  // sets the audio mmdevice object ptr.

  void set_video_data_handle (ACE_HANDLE data_fd);
  // sets the data handle (UDP) of the command handler

  void set_video_control (Video_Control_ptr video_control);
  // called to set the video control object pointer of the comand handler.

  void set_audio_data_handle (ACE_HANDLE data_fd);
  // sets the data handle (UDP) of the command handler

  void set_audio_control (Audio_Control_ptr video_control);
  // called to set the video control object pointer of the comand handler.

  int get_video_control (void);
  // Gets the video control reference thru the property service from
  // the video server virtual device

  char *get_data_host (void);
  // gets the data interface to be used.

  int resolve_video_reference (void);
  // Resolve the video control reference.

  int resolve_audio_reference (void);
  // Resolve the audio control reference.

  virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
  // Called when input events occur (e.g., connection or data).

  virtual ACE_HANDLE get_handle (void) const;
  // Returns the handle used by the event_handler.

  int init_av (void);
  // Initialize both the audio and video

  int init_java_av (char *audio_ior,
                    char *video_ior,
                    char *audio_file,
                    char *video_file);
  // initialize both the audio and video reading the iors from java GUI thru a socket.

  int init_video_channel (char *videofile);
  // Initializes the video channel by bind the client and server video
  // mmdevices together and gets the video control object.

  int init_audio_channel (char *videofile);
  // Initializes the audio channel by bind the client and server audio
  // mmdevices together and gets the video control object.

  int stat_stream (CORBA::Char_out ch,CORBA::Long_out size) ;
  // statistics of this stream.

  int  close (void) ;
  // close the audio and video connections

  int  stat_sent (void) ;

  int  fast_forward (void);

  int fast_backward (void);

  int step (void);

  int play (int flag,
            CORBA::Environment& env);

  int position_action (int operation_tag);

  int position (void);

  int position_release (void);

  int volume (void);

  int balance (void);

  int speed (void);

  int stop (void);

  int stop_playing (void);

  int connect_to_video_server (void);
  // resolves the server video mmdevice and binds the local and server
  // mmdevices together.

  int connect_to_audio_server (void);
  // resolves the server video mmdevice and binds the local and server
  // mmdevices together.

private:

  int parse_args (int argc,char **argv);
  // parses the arguments.

  int busy_;
  // flag to indicate the state of the command handler

  CORBA::String_var audio_mmdevice_ior_;
  CORBA::String_var video_mmdevice_ior_;

  ACE_SOCK_Dgram video_dgram_;
  // UDP socket on which to send/recv data

  ACE_SOCK_Dgram audio_dgram_;
  // Audio UDP socket

  ACE_HANDLE video_data_handle_;
  // UDP socket for video

  ACE_HANDLE audio_data_handle_;
  // UDP socket for audio

  ACE_HANDLE command_handle_;
  // The fd for the UNIX command socket

  TAO_ORB_Manager orb_manager_;
  // the ORB manager

  TAO_Naming_Client my_name_client_;
  // A Name_Client used to resolve the video_server_mmdevice

  Video_Control_ptr video_control_;
  // Video Control CORBA object

  AVStreams::MMDevice_var video_server_mmdevice_;
  // The video server multimedia device

  Video_Endpoint_Reactive_Strategy_A *video_reactive_strategy_;
  // Strategy for creating stream endpoints

  TAO_MMDevice *video_client_mmdevice_;
  // The video client multimedia device

  TAO_StreamCtrl *video_streamctrl_;
  // Video stream controller

  Audio_Control_ptr audio_control_;
  // Audio Control CORBA object

  AVStreams::MMDevice_var audio_server_mmdevice_;
  // The audio server multimedia device

  Audio_Endpoint_Reactive_Strategy_A *audio_reactive_strategy_;
  // Strategy for creating stream endpoints

  TAO_MMDevice *audio_client_mmdevice_;
  // The audio client multimedia device

  TAO_StreamCtrl *audio_streamctrl_;
  // audio stream controller

  //  ACE_High_Res_Timer timer_;
  // timer to record the time taken for the play sequence.

  Receiver_i receiver_;
  // Receive object,called by the  agent to give the mmdevice object
  // reference.

  char *data_host_;
  // interface name to bind to for data transfer.

  Client_Sig_Handler client_sig_handler_;
  // Handler for the signals.

  int argc_;
  char **argv_;
};



#endif /* AV_COMMAND_HANDLER_H */