video_thread.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:5k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef _video_thread_h_
- #define _video_thread_h_
- #include "mutex.h"
- #include "../../pusher/src/sockaddr.h"
- #include "../../pusher/src/timeval.h"
- /** capturing thread must dump data to TCP sockets - usually to nearests
- push servers, from where it will be pushed by UDP to clients. That's the
- reference to push server
- */
- class broadcast_target {
- Sockaddr broadcast_addr;
- Sockaddr control_addr;
- SOCKET broadcast_sock;
- SOCKET control_sock;
- unsigned sequence;
- int push_serv_id;
-
- public:
- /** do not connect immediately */
- broadcast_target( int _push_serv_id, const Sockaddr &control,
- const Sockaddr &broadcast );
- ~broadcast_target();
- bool compare_addr( const Sockaddr &a ) const {
- return a == broadcast_addr;
- }
- bool compare( const broadcast_target &target ) const {
- return ( push_serv_id == target.push_serv_id &&
- broadcast_addr == target.broadcast_addr );
- }
- int get_push_serv_id() const { return push_serv_id; }
- /** do not reconnect if connected. After a success, report to database
- server that this particular broadcast target is ok. This will force
- the database server to configure its own targets - final clients
- */
- void connect();
- /** Send to TCP stream the ready-to-resend by UDP "packet". Text may be
- incorporated as well. Defragment to not exceed maxPacketSize.
- Of course text is attached only to the first packet.
- raise exception if failed */
- void send( const char *data, unsigned size, const std::string &text,
- const Timeval &now );
- /** get the channel description when needed for debug reasons */
- std::string describe_itself() const;
- };
- struct text_data;
- /** video_thread is (maybe virtual) base class that hide actual implementation
- of video input. The first implementation will be Mpegator board under
- NT OS. Hope to see the support for real OS's... */
- class video_thread
- {
- protected:
- /** set this flag from other threads to stop the job */
- bool shutdown_now;
- /** to never shutdown twice */
- bool shutdown_performed;
- /** mutex to lock shutdown procedure */
- mutex shutdown_mutex;
- typedef std::list< broadcast_target * > broadcast_targetsT;
- /** list of all targets for broadcasting */
- broadcast_targetsT broadcast_targets;
-
- typedef std::queue< broadcast_target * > add_target_queueT;
- /** put in queue all hosts to set-up broadcasting */
- add_target_queueT add_target_q;
- /** when to reconnect if failed */
- Timeval target_reconnect_time;
- typedef std::queue< Sockaddr > target_queueT;
- /** put in queue all hosts to drop broadcasting */
- target_queueT drop_target_q;
- /** lock the add/drop target operation */
- mutex add_drop_target_mutex;
- /** queue of text data to be sent with video packets */
- std::queue< text_data * > new_text;
- /** access to the queue is shared */
- mutex lock_new_text;
- public:
- video_thread( int argc, char **argv );
- /** select proper derived class based on argv parameters. By default -
- mpegator board */
- static video_thread *create_instance( int argc, char **argv );
- /** this is thread start function - single (of course) for all derived
- classes */
- static void video_thread_entry_func( void *_instance );
- /** clean-up; actually do nothing - child class must perform actual work */
- virtual ~video_thread();
- virtual void shutdown() = 0;
- /** the video hardware is sensetive to proper clean-up. In particular,
- Mpegator driver may ( and usually do ) crash NT when it is initialised
- after improper shutdown. The problem is that 3-rd party drivers are
- allowed to execute in core of Windows NT kernel
- */
- virtual bool is_shutdown_performed() = 0;
- protected:
- /** raise exception if something wrong */
- virtual void init() = 0;
- /** this call loops forever until some exception, which must
- indicate what to do next
- */
- virtual void loop();
- /** load data from video card and dump it to all broadcast targets */
- virtual void check_for_new_data() = 0;
- /** check for adding/dropping targets */
- void check_for_add_drop_targets();
- public:
- /** remove this target from the active queue and put it back into the
- queue
- */
- void reschedule_broadcast_target( broadcast_target *target );
- // ----- all calls below are public to the outside
- /** call from outside: all that calls must be sinchronized with mutexes
- */
- virtual void shutdown_requested();
- /** start */
- virtual void start_capture() = 0;
- /** stop */
- virtual void stop_capture() = 0;
- /** get a broadcast target from outside thread and place into queue */
- void add_broadcast_target( int pushserv_id, const Sockaddr &control_addr,
- const Sockaddr &data_addr );
- /** pass new text data to video thread. This call should never be
- blocked by any I/O - only place pointer into queue. The queue is
- protected by mutex and mutex should never lock the queue while any
- I/O on the way */
- void new_text_data( text_data *data );
- };
- extern video_thread *videoThread;
- #endif // _video_thread_h_