video_thread.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:5k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
  2.    Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
  3.    Software license is located in file "COPYING"
  4. */
  5. #ifndef _video_thread_h_
  6. #define _video_thread_h_
  7. #include "mutex.h"
  8. #include "../../pusher/src/sockaddr.h"
  9. #include "../../pusher/src/timeval.h"
  10. /** capturing thread must dump data to TCP sockets - usually to nearests
  11.     push servers, from where it will be pushed by UDP to clients. That's the
  12.     reference to push server
  13. */
  14. class broadcast_target {
  15.   Sockaddr broadcast_addr;
  16.   Sockaddr control_addr;
  17.   SOCKET broadcast_sock;
  18.   SOCKET control_sock;
  19.   unsigned sequence;
  20.   int push_serv_id;
  21.   
  22. public:
  23.   /** do not connect immediately */
  24.   broadcast_target( int _push_serv_id, const Sockaddr &control, 
  25.     const Sockaddr &broadcast );
  26.   ~broadcast_target();
  27.   bool compare_addr( const Sockaddr &a ) const {
  28.     return a == broadcast_addr;
  29.   }
  30.   bool compare( const broadcast_target &target ) const {
  31.     return ( push_serv_id == target.push_serv_id && 
  32.      broadcast_addr == target.broadcast_addr );
  33.   }
  34.   int get_push_serv_id() const { return push_serv_id; }
  35.   /** do not reconnect if connected. After a success, report to database 
  36.       server that this particular broadcast target is ok. This will force
  37.       the database server to configure its own targets - final clients
  38.    */
  39.   void connect();
  40.   /** Send to TCP stream the ready-to-resend by UDP "packet". Text may be 
  41.       incorporated as well. Defragment to not exceed maxPacketSize.
  42.       Of course text is attached only to the first packet.
  43.       raise exception if failed */
  44.   void send( const char *data, unsigned size, const std::string &text,
  45.      const Timeval &now );
  46.   /** get the channel description when needed for debug reasons */
  47.   std::string describe_itself() const;
  48. };
  49. struct text_data;
  50. /** video_thread is (maybe virtual) base class that hide actual implementation 
  51.     of video input. The first implementation will be Mpegator board under
  52.     NT OS. Hope to see the support for real OS's... */
  53. class video_thread 
  54. {
  55. protected:
  56.   /** set this flag from other threads to stop the job */
  57.   bool shutdown_now;
  58.   /** to never shutdown twice */
  59.   bool shutdown_performed;
  60.   /** mutex to lock shutdown procedure */
  61.   mutex shutdown_mutex;
  62.   typedef std::list< broadcast_target * > broadcast_targetsT;
  63.   /** list of all targets for broadcasting */
  64.   broadcast_targetsT broadcast_targets;
  65.   
  66.   typedef std::queue< broadcast_target * > add_target_queueT;
  67.   /** put in queue all hosts to set-up broadcasting */
  68.   add_target_queueT add_target_q;
  69.   /** when to reconnect if failed */
  70.   Timeval target_reconnect_time;
  71.   typedef std::queue< Sockaddr > target_queueT;
  72.   /** put in queue all hosts to drop broadcasting */
  73.   target_queueT drop_target_q;
  74.   /** lock the add/drop target operation */
  75.   mutex add_drop_target_mutex;
  76.   /** queue of text data to be sent with video packets */
  77.   std::queue< text_data * > new_text;
  78.   /** access to the queue is shared */
  79.   mutex lock_new_text;
  80. public:
  81.   video_thread( int argc, char **argv );
  82.   /** select proper derived class based on argv parameters. By default -
  83.       mpegator board */
  84.   static video_thread *create_instance( int argc, char **argv );
  85.   /** this is thread start function - single (of course) for all derived 
  86.       classes */
  87.   static void video_thread_entry_func( void *_instance );
  88.   /** clean-up; actually do nothing - child class must perform actual work  */
  89.   virtual ~video_thread();
  90.   virtual void shutdown() = 0;
  91.   /** the video hardware is sensetive to proper clean-up. In particular,
  92.       Mpegator driver may ( and usually do ) crash NT when it is initialised
  93.       after improper shutdown. The problem is that 3-rd party drivers are 
  94.       allowed to execute in core of Windows NT kernel
  95.   */
  96.   virtual bool is_shutdown_performed() = 0;
  97. protected:
  98.   /** raise exception if something wrong */
  99.   virtual void init() = 0;
  100.   /** this call loops forever until some exception, which must 
  101.       indicate what to do next
  102.   */
  103.   virtual void loop();
  104.   /** load data from video card and dump it to all broadcast targets */
  105.   virtual void check_for_new_data() = 0;
  106.   /** check for adding/dropping targets */
  107.   void check_for_add_drop_targets();
  108. public:
  109.   /** remove this target from the active queue and put it back into the 
  110.       queue
  111.   */
  112.   void reschedule_broadcast_target( broadcast_target *target );
  113.   // ----- all calls below are public to the outside
  114.   /** call from outside: all that calls must be sinchronized with mutexes
  115.    */
  116.   virtual void shutdown_requested();
  117.   /** start */
  118.   virtual void start_capture() = 0;
  119.   /** stop */
  120.   virtual void stop_capture() = 0;
  121.   /** get a broadcast target from outside thread and place into queue */
  122.   void add_broadcast_target( int pushserv_id, const Sockaddr &control_addr,
  123.      const Sockaddr &data_addr );
  124.   /** pass new text data to video thread. This call should never be 
  125.       blocked by any I/O - only place pointer into queue. The queue is
  126.       protected by mutex and mutex should never lock the queue while any
  127.       I/O on the way */
  128.   void new_text_data( text_data *data );
  129. };
  130. extern video_thread    *videoThread;
  131. #endif // _video_thread_h_