streamer_ctrl.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) Massimo Cora' 2005 <maxcvs@email.it>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 //
00020 // This class will be an interface between the gui and the Streaming part
00021 // of the project.
00022 // It will be a thread, using wxWidgets thread features
00023 //
00024 
00025 
00026 #include <wx/wx.h>
00027 #include "streamer_ctrl.h"
00028 
00029 //--------------------------------------------------------------------------
00030 //
00031 
00032 StreamerCtrlThread::StreamerCtrlThread( MainFrame * main_frame_instance ) :
00033                                         _main_frame_pointer( main_frame_instance ),
00034                                         _streaming_status( false ),
00035                                         _sub_session( NULL ),
00036                                         _streamerUCast( NULL ),
00037                                         _algo_simple( false ),
00038                                         _file_path( NULL ),
00039                                         _ctrl_lookup( NULL ),
00040                                         _ctrl_gcard( NULL )
00041 {
00042 
00043 }
00044 
00045 //--------------------------------------------------------------------------
00046 //
00047 
00048 StreamerCtrlThread::~StreamerCtrlThread() {
00049 
00050         if ( _sub_session )
00051                 delete _sub_session;
00052 
00053         if ( _streamerUCast )
00054                 delete _streamerUCast;
00055 
00056         if ( _file_path )
00057                 free( _file_path );
00058 }
00059 
00060 //--------------------------------------------------------------------------
00061 // create the thread and initialize some useful data
00062 
00063 void StreamerCtrlThread::Create(bool gcard_use, 
00064                                                                 const wxString proto,
00065                                                                 const wxString file_path,
00066                                                                 int cam_num,
00067                                                                 int num_of_streams /* = 1 */,
00068                                                                 int audio_num /* = -1 */,
00069                                                                 const wxString sub_session /* = "testStream" */, 
00070                                                                 unsigned short port_rtsp /* = 8554 */,
00071                                                                 unsigned int fps /* = 25 */) 
00072 {
00073         // let's create the thread
00074         wxThread::Create();
00075 
00076         _cam_num = cam_num;
00077         _gcard_use = gcard_use;
00078         _num_of_streams = num_of_streams;
00079         _audio_num = audio_num;
00080         
00081 
00082         if ( proto.CompareTo( wxT("H263+") ) == 0) {
00083                 _enc_proto = OSTREAM_ENCODING_H263P;
00084         }
00085         else if ( proto.CompareTo( wxT("MJPEG") ) == 0) {
00086                 _enc_proto = OSTREAM_ENCODING_MJPEG;
00087         }
00088         else {
00089                 _enc_proto = OSTREAM_ENCODING_NONE;
00090         }
00091 
00092         if ( file_path.IsEmpty () ) {
00093                 _file_path = NULL;
00094         }
00095         else {
00096                 _file_path = strdup( wxConvCurrent->cWX2MB(file_path) );
00097         }
00098 
00099         _sub_session = new wxString( sub_session );
00100         _port_rtsp = port_rtsp;
00101         _fps = fps;
00102 
00103         
00104         _streamerUCast = new OStreamUCast( StreamerCtrlThread::onParsableImage, this, num_of_streams );
00105         _ctrl_initialized = false;
00106 }
00107 
00108 //--------------------------------------------------------------------------
00109 //
00110 
00111 bool StreamerCtrlThread::isStreaming() 
00112 {
00113         return _streaming_status;
00114 }
00115 
00116 
00117 //--------------------------------------------------------------------------
00118 //
00119 
00120 void StreamerCtrlThread::stopStreaming() 
00121 {
00122         _streamerUCast->stopStreaming();
00123 }
00124 
00125 
00126 //--------------------------------------------------------------------------
00127 // Thread entry: will be called after a Run() on this object
00128 
00129 void *StreamerCtrlThread::Entry() {
00130         
00131         _streaming_status = true;
00132         _streamerUCast->startStreaming ( _enc_proto, 
00133                                                                          _file_path,
00134                                                                          _cam_num,
00135                                                                          _audio_num,
00136                                                                          wxConvCurrent->cWX2MB(*(_sub_session)), 
00137                                                                          _port_rtsp, 
00138                                                                          _fps );
00139         
00140         _streaming_status = false;
00141 
00142         // let's notify main_frame class that we are exiting.
00143         _main_frame_pointer->OnStreamerCtrlThreadEnd();
00144 
00145         return 0;
00146 }
00147 
00148 void StreamerCtrlThread::initializeOmniStuff( IplImage *image_to_parse,
00149                                                                                          void* buffered_image_multi_vstream_callback_data ) 
00150 {
00151         if ( _gcard_use == false ) {
00152                 OmniConversion<OmniFastLookupTable, int, IplImage, CvPoint> *conv;
00153                 OmniFastLookupTable *fast_lookup;
00154                                         
00155                 DEBUG_PRINT ("creating OmniFastLookupTable\n");
00156 
00157                 // create the fastlookup table
00158                 if ( !_main_frame_pointer->is_face_detect_options_set () ) {
00159                         DEBUG_PRINT("WARNING! !_main_frame_pointer->is_face_detect_options_set () \n");
00160                         return;
00161                 }
00162 
00163                 fast_lookup = new OmniFastLookupTable( _main_frame_pointer->GetMinRadius (),
00164                                                                                            _main_frame_pointer->GetMaxRadius (),
00165                                                                                            _main_frame_pointer->GetCenterX (),
00166                                                                                            _main_frame_pointer->GetCenterY (),
00167                                                                                            0,
00168                                                                                            _main_frame_pointer->GetFlippedModeStatus() );
00169 
00170                 // conversion
00171                 conv = new OmniConversion<OmniFastLookupTable, int, IplImage, CvPoint>( fast_lookup );
00172                 if ( !conv->is_initialized () ) {
00173                         wxMessageDialog dlg1( NULL, 
00174                         wxT("Error initializing OmniFastLookupTable.") , wxT( "Error" ), wxOK );
00175                         dlg1.ShowModal();
00176                         return;
00177                 }
00178 
00179                 if ( _main_frame_pointer->GetAlgoSimpleStatus() ) {
00180                         const char **c_array;
00181                         wxArrayString *wx_array = _main_frame_pointer->GetCascadeArray ();
00182                         int c_size = _main_frame_pointer->GetCascadeSize ();
00183 
00184                         if ( c_size <= 0 ) {
00185                                 wxMessageDialog dlg1( NULL, 
00186                                 wxT("No cascade selected. You need at least one") , wxT( "Error" ), wxOK );
00187                                 dlg1.ShowModal();
00188                                 return;
00189                         }
00190                         c_array = (const char**)calloc(c_size, sizeof(char*));
00191 
00192                         for ( int i = 0; i < c_size; i++ ) {
00193                                 c_array[i] = strdup( wxConvCurrent->cWX2MB( wx_array->Item(i) ) );
00194                         }
00195 
00196 
00197                         // instantiate the algorithm
00198                         _ctrl_lookup = new OmniAlgoSimpleDetection<OmniFastLookupTable, int, IplImage, CvPoint>( conv, 
00199                                                                                                 image_to_parse, 
00200                                                                                                 (const char**)c_array,
00201                                                                                                 c_size,
00202                                                                                                 _main_frame_pointer->GetFreezeFrameBound (),
00203                                                                                                 _main_frame_pointer->GetTrackWindowHitrate ()
00204                                                                                                 );
00205 
00206                         _ctrl_lookup->register_all_image_callbacks( StreamerCtrlThread::fake_create_image_context, 
00207                                                                                                           BufferedImageMultiVideoSource::add_image_by_stream_id,
00208                                                                                                           StreamerCtrlThread::fake_destroy_image_context,
00209                                                                                                           buffered_image_multi_vstream_callback_data
00210                                                                                                           );
00211                 }
00212                 else {
00213                         _ctrl_vr_lookup = new OmniAlgoVRFilterDetection
00214                                                                 <OmniFastLookupTable, int, IplImage, CvPoint> ( conv, image_to_parse );
00215 
00216                         _ctrl_vr_lookup->register_all_image_callbacks( StreamerCtrlThread::fake_create_image_context,
00217                                                                                                                    BufferedImageMultiVideoSource::add_image_by_stream_id,
00218                                                                                                                    StreamerCtrlThread::fake_destroy_image_context,
00219                                                                                                                buffered_image_multi_vstream_callback_data
00220                                                                                                                    );
00221                 }
00222 
00223                 _ctrl_initialized = true;
00224         }
00225         else {          // we have a compatible graphic card and we want to use it
00226                 OmniConversion<OmniGCardConverter, double, IplImage, CvPoint> *conv;
00227                 OmniGCardConverter *gcard_converter;
00228                 
00229                 DEBUG_PRINT ("creating OmniGCardConverter\n");
00230                 
00231                 // create the gcard_converter class
00232                 gcard_converter = new OmniGCardConverter( _main_frame_pointer->GetMinRadius(), 
00233                                                                                           _main_frame_pointer->GetMaxRadius(), 
00234                                                                                           _main_frame_pointer->GetCenterX(), 
00235                                                                                           _main_frame_pointer->GetCenterY(), 
00236                                                                                           image_to_parse->width,
00237                                                                                           image_to_parse->height,
00238                                                                                           "omnigcard",
00239                                                                                           "omnistuff/data/shaders/vertex_shader.cg",
00240                                                                                           "vertex_shader_main",
00241                                                                                           "omnistuff/data/shaders/pixel_shader.cg",
00242                                                                                           "pixel_shader_main",
00243                                                                                           _main_frame_pointer->GetFlippedModeStatus());
00244 
00245                 // conversion
00246                 conv = new OmniConversion<OmniGCardConverter, double, IplImage, CvPoint>( gcard_converter );
00247                 if ( !conv->is_initialized () ) {
00248                         DEBUG_PRINT("Error initializing OmniGCardConverter");
00249                         delete conv;
00250                         gcard_converter = NULL;
00251                         return;
00252                 }
00253 
00254                 if ( _main_frame_pointer->GetAlgoSimpleStatus() ) {
00255                         const char **c_array;
00256                         wxArrayString *wx_array = _main_frame_pointer->GetCascadeArray ();
00257                         int c_size = _main_frame_pointer->GetCascadeSize ();
00258 
00259                         if ( c_size <= 0 ) {
00260                                 DEBUG_PRINT("ERROR: bad number of cascaded selected");
00261                                 return;
00262                         }
00263                         c_array = (const char**)calloc(c_size, sizeof(char*));
00264 
00265                         for ( int i = 0; i < c_size; i++ ) 
00266                                 c_array[i] = strdup( wxConvCurrent->cWX2MB( wx_array->Item(i) ) );
00267 
00268                         // instantiate the algorithm
00269                         _ctrl_gcard = new OmniAlgoSimpleDetection<OmniGCardConverter, double, IplImage, CvPoint>( conv, 
00270                                                                                                         image_to_parse, 
00271                                                                                                         (const char**)c_array,
00272                                                                                                         c_size,
00273                                                                                                         _main_frame_pointer->GetFreezeFrameBound(),
00274                                                                                                         _main_frame_pointer->GetTrackWindowHitrate());
00275 
00276                         _ctrl_gcard->register_all_image_callbacks( StreamerCtrlThread::fake_create_image_context,
00277                                                                                                            BufferedImageMultiVideoSource::add_image_by_stream_id,
00278                                                                                                            StreamerCtrlThread::fake_destroy_image_context,
00279                                                                                                            buffered_image_multi_vstream_callback_data
00280                                                                                                           );
00281                 }
00282                 else {
00283                         _ctrl_vr_gcard = new OmniAlgoVRFilterDetection
00284                                 <OmniGCardConverter, double, IplImage, CvPoint>( conv, image_to_parse );
00285 
00286                         _ctrl_vr_gcard->register_all_image_callbacks( StreamerCtrlThread::fake_create_image_context, 
00287                                                                                                                   BufferedImageMultiVideoSource::add_image_by_stream_id,
00288                                                                                                                   StreamerCtrlThread::fake_destroy_image_context,
00289                                                                                                               buffered_image_multi_vstream_callback_data
00290                                                                                                                  );
00291                 }
00292                 _ctrl_initialized = true;
00293         }
00294 }
00295 
00296 void StreamerCtrlThread::onParsableImage( IplImage* image_to_parse,
00297                                                                                 void* buffered_image_multi_vstream_callback_data,
00298                                                                                 void* callback_data )
00299 {
00300         StreamerCtrlThread* klass;
00301         
00302         if ( image_to_parse == NULL ) {
00303                 DEBUG_PRINT ("image_to_parse from void StreamerCtrlThread::onParsableImage is NULL\n");
00304                 return;
00305         }
00306         
00307         if ( callback_data == NULL ) {
00308                 DEBUG_PRINT ("WARNING: callback_data data is NULL into StreamerCtrlThread::onParsableImage\n");
00309                 return;
00310         }
00311 
00312         klass = (StreamerCtrlThread*)callback_data;
00313         
00314         if ( klass->_ctrl_initialized == false ) {
00315                 klass->initializeOmniStuff( image_to_parse, 
00316                                                                    buffered_image_multi_vstream_callback_data );
00317         }
00318         
00319         if ( klass->_gcard_use == true ) {
00320                 if ( klass->_main_frame_pointer->GetAlgoSimpleStatus() ) {
00321             klass->_ctrl_gcard->loop_next_frame( image_to_parse );
00322                 }
00323                 else {
00324                         klass->_ctrl_vr_gcard->loop_next_frame( image_to_parse );
00325                 }
00326         }
00327         else {
00328                 if ( klass->_main_frame_pointer->GetAlgoSimpleStatus() ) {
00329                         klass->_ctrl_lookup->loop_next_frame( image_to_parse );
00330                 }
00331                 else {
00332                         klass->_ctrl_vr_lookup->loop_next_frame( image_to_parse );
00333                 }
00334         }
00335 }
00336 
00337 
00338 void StreamerCtrlThread::fake_destroy_image_context( char* win_id, void* cb_data )
00339 {
00340 }
00341 
00342 void StreamerCtrlThread::fake_create_image_context( char* win_id, void* cb_data )
00343 {
00344 }
00345 

Generated on Tue Dec 26 10:32:39 2006 for Omnimeeting by  doxygen 1.4.7