00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "BufferedImageMultiVideoSource.hh"
00021
00022 using namespace std;
00023
00024 BufferedImageMultiVideoSource::BufferedImageMultiVideoSource( unsigned int streams_num,
00025 on_parsable_image_cb *f,
00026 void* parsable_image_callback_data,
00027 BaseInputVideo *input_single_video ) : BaseInputVideo(),
00028 _streams_num( streams_num ),
00029 f_parsable_cb( f ),
00030 _parsable_image_callback_data( parsable_image_callback_data ),
00031 _input_single_video( input_single_video )
00032 {
00033 _initialized = false;
00034 _streams_vector = NULL;
00035
00036
00037 if ( _input_single_video->init_device () == true ) {
00038 DEBUG_PRINT("ERROR: _input_single_video->init_device () failed from BufferedImageMultiVideoSource ctor" );
00039 }
00040 }
00041
00042 BufferedImageMultiVideoSource::~BufferedImageMultiVideoSource()
00043 {
00044 list<IplImage*>* tmp_list;
00045
00046 while ( (tmp_list = _streams_vector->back()) != NULL ) {
00047
00048
00049 if ( !tmp_list->empty() ) {
00050 IplImage *tmp_image;
00051
00052 while ( (tmp_image = tmp_list->front ()) != NULL ) {
00053 cvReleaseImage( &tmp_image );
00054 tmp_list->pop_front ();
00055 }
00056 }
00057
00058 delete tmp_list;
00059 _streams_vector->pop_back ();
00060 }
00061
00062 delete _streams_vector;
00063
00064 _initialized = false;
00065 }
00066
00067 bool BufferedImageMultiVideoSource::init_device ()
00068 {
00069 if ( _initialized )
00070 return true;
00071
00072 _streams_vector = NULL;
00073
00074
00075 if ( _streams_vector == NULL ) {
00076 _streams_vector = new vector< list<IplImage*>* >;
00077 for ( unsigned int i=0; i < _streams_num; i++ ) {
00078 list<IplImage*> *tmp_stream = new list<IplImage*>;
00079 _streams_vector->push_back( tmp_stream );
00080 }
00081 }
00082
00083 _initialized = true;
00084 return true;
00085 }
00086
00087 void BufferedImageMultiVideoSource::add_image_by_stream_id( IplImage *image,
00088 int stream_id, char* __unused__, void* callback_data )
00089 {
00090 BufferedImageMultiVideoSource* klass;
00091
00092 if ( callback_data != NULL )
00093 klass = (BufferedImageMultiVideoSource*)callback_data;
00094 else
00095 return;
00096
00097 if ( !klass->_initialized ) {
00098 if ( klass->init_device () == false ) {
00099 return;
00100 }
00101 }
00102
00103
00104 klass->_streams_vector->at( stream_id )->push_back( image );
00105 return;
00106 }
00107
00108 IplImage *BufferedImageMultiVideoSource::get_next_frame_by_stream_id ( int stream_id )
00109 {
00110 IplImage *tmp_image, *res_image;
00111
00112 if ( !_initialized ) {
00113 if ( init_device () == false ) {
00114 DEBUG_PRINT ("device not initialized, returnning NULL from"
00115 "IplImage *BufferedImageMultiVideoSource::get_next_frame_by_stream_id\n");
00116 return NULL;
00117 }
00118 }
00119
00120
00121
00122 if ( _streams_vector->at( stream_id )->empty () == false ) {
00123 list<IplImage*> *tmp_stream;
00124
00125 tmp_stream = _streams_vector->at( stream_id );
00126
00127 tmp_image = tmp_stream->front ();
00128
00129 tmp_stream->pop_front ();
00130 return tmp_image;
00131 }
00132
00133
00134
00135
00136
00137 for ( int i = 0; i < (int)_streams_vector->size(); i++ ) {
00138 if ( _streams_vector->at(i)->size() > 5 )
00139 return NULL;
00140 }
00141
00142
00143
00144 res_image = _input_single_video->get_next_frame();
00145
00146 if ( res_image == NULL ) {
00147 DEBUG_PRINT ("ERROR: falied to retrieve frame from device.\n");
00148 return NULL;
00149 }
00150
00151
00152
00153 f_parsable_cb( res_image, this, _parsable_image_callback_data );
00154
00155 cvReleaseImage( &res_image );
00156
00157
00158
00159
00160
00161 if ( _streams_vector->at( stream_id )->empty () ) {
00162 return NULL;
00163 }
00164
00165 list<IplImage*> *tmp_stream;
00166 tmp_stream = _streams_vector->at( stream_id );
00167
00168
00169 tmp_image = tmp_stream->front ();
00170
00171 tmp_stream->pop_front ();
00172 return tmp_image;
00173 }
00174
00175 bool BufferedImageMultiVideoSource::is_multi_stream ()
00176 {
00177 return true;
00178 }
00179
00180 IplImage * BufferedImageMultiVideoSource::get_next_frame ()
00181 {
00182
00183 if ( _streams_num > 0 )
00184 return get_next_frame_by_stream_id( 0 );
00185 else
00186 return NULL;
00187 }
00188
00189