00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #if defined(__GNUG__) && !defined(__APPLE__)
00021 #pragma implementation "main_frame.h"
00022 #endif
00023
00024
00025 #include "wx/wxprec.h"
00026
00027 #ifdef __BORLANDC__
00028 #pragma hdrstop
00029 #endif
00030
00031 #ifndef WX_PRECOMP
00032 #include "wx/wx.h"
00033 #endif
00034
00036 #include "options_dialog.h"
00037 #include "about_dialog.h"
00038 #include "version_dialog.h"
00040
00041 #include <wx/dcbuffer.h>
00042 #include "main_frame.h"
00043
00044
00045 #include <cv.h>
00046 #include <highgui.h>
00047 #include <cvaux.h>
00048
00049 #include <stdio.h>
00050 #include <ctype.h>
00051 #include <time.h>
00052
00053
00054 static bool video_received_window = false;
00055
00058
00063 IMPLEMENT_CLASS( MainFrame, wxFrame )
00064
00065
00069 BEGIN_EVENT_TABLE( MainFrame, wxFrame )
00070
00072 EVT_CLOSE( MainFrame::OnCloseWindow )
00073
00074 EVT_MENU( ID_MENUITEM_FILE_EXIT, MainFrame::OnMenuitemFileExitClick )
00075
00076 EVT_MENU( ID_MENUITEM_ACTIONS_STOP_SERVER, MainFrame::OnMenuitemActionsStopServerClick )
00077
00078 EVT_MENU( ID_MENUITEM_ACTIONS_STOP_RECEIVING, MainFrame::OnMenuitemActionsStopReceivingClick )
00079
00080 EVT_MENU( ID_MENU_PREFERENCES, MainFrame::OnMenuPreferencesClick )
00081
00082 EVT_MENU( ID_MENU_HELP_AUTHORS, MainFrame::OnMenuHelpAuthorsClick )
00083
00084 EVT_MENU( ID_MENU_HELP_VERSION, MainFrame::OnMenuHelpVersionClick )
00085
00087
00088
00089 EVT_MENU( WORKER_EVENT, MainFrame::OnWorkerEvent )
00090
00091
00092 EVT_MENU( IMAGE_RECEIVED_EVENT, MainFrame::OnImageReceivedFromNet )
00093
00094
00095
00096 EVT_MENU( AUDIO_RECEIVED_EVENT, MainFrame::OnAudioFrameReceivedFromNet )
00097
00098 END_EVENT_TABLE()
00099
00104 MainFrame::MainFrame( )
00105 {
00106
00107 }
00108
00109 MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
00110 {
00111
00112 m_streamer_thread = new StreamerCtrlThread( this );
00113 m_receiver_thread = new ReceiverCtrlThread( this );
00114
00115 ctrl_lookup = NULL;
00116 ctrl_gcard = NULL;
00117 ctrl_vr_lookup = NULL;
00118 ctrl_vr_gcard = NULL;
00119 ctrl_initialized = false;
00120 face_detect = false;
00121
00122
00123 num_streams = -1;
00124 _displayed_frames = new vector<DisplayImageFrame *>;
00125 _gc_images = new vector<list<IplImage* > *>;
00126 _paint_mutex = new wxMutex();
00127
00128 Create( parent, id, caption, pos, size, style );
00129
00130 SetAutoLayout( true );
00131 }
00132
00133
00134
00135
00136
00137 MainFrame::~MainFrame( ){
00138
00139 if ( m_receiver_thread ) {
00140 if ( m_receiver_thread->isReceiving() ) {
00141 printf("stop receiving\n");
00142 m_receiver_thread->StopReceiving();
00143 m_receiver_thread = NULL;
00144 }
00145 else
00146 delete m_receiver_thread;
00147
00148 }
00149
00150 if ( m_streamer_thread ) {
00151 if ( m_streamer_thread->isStreaming() ) {
00152 printf("stop streaming\n");
00153 m_streamer_thread->stopStreaming();
00154 m_streamer_thread = NULL;
00155 }
00156 else
00157 delete m_streamer_thread;
00158 }
00159
00160 reset_displayed_image_vector ();
00161 delete _displayed_frames;
00162 }
00163
00168 bool MainFrame::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
00169 {
00171 m_status_bar = NULL;
00173
00175 wxFrame::Create( parent, id, caption, pos, size, style );
00176
00177 CreateControls();
00178 Centre();
00180 return TRUE;
00181 }
00182
00187 void MainFrame::CreateControls()
00188 {
00190 MainFrame* itemFrame1 = this;
00191
00192 wxMenuBar* menuBar = new wxMenuBar;
00193 wxMenu* itemMenu3 = new wxMenu;
00194 itemMenu3->Append(ID_MENUITEM_FILE_EXIT, _("Exit"), _T(""), wxITEM_NORMAL);
00195 menuBar->Append(itemMenu3, _("File"));
00196 wxMenu* itemMenu5 = new wxMenu;
00197 itemMenu5->Append(ID_MENUITEM_ACTIONS_STOP_SERVER, _("Stop Server"), _T(""), wxITEM_NORMAL);
00198 itemMenu5->Append(ID_MENUITEM_ACTIONS_STOP_RECEIVING, _("Stop Receiving"), _T(""), wxITEM_NORMAL);
00199 menuBar->Append(itemMenu5, _("Actions"));
00200 wxMenu* itemMenu8 = new wxMenu;
00201 itemMenu8->Append(ID_MENU_PREFERENCES, _("Preferences..."), _T(""), wxITEM_NORMAL);
00202 menuBar->Append(itemMenu8, _("Tools"));
00203 wxMenu* itemMenu10 = new wxMenu;
00204 itemMenu10->Append(ID_MENU_HELP_AUTHORS, _("Authors..."), _T(""), wxITEM_NORMAL);
00205 itemMenu10->Append(ID_MENU_HELP_VERSION, _("Version..."), _T(""), wxITEM_NORMAL);
00206 menuBar->Append(itemMenu10, _("Help"));
00207 itemFrame1->SetMenuBar(menuBar);
00208
00209 m_status_bar = new wxStatusBar( itemFrame1, ID_STATUSBAR, wxST_SIZEGRIP|wxNO_BORDER );
00210 m_status_bar->SetFieldsCount(2);
00211 itemFrame1->SetStatusBar(m_status_bar);
00212
00214
00215
00216 }
00217
00222 void MainFrame::OnMenuPreferencesClick( wxCommandEvent& event )
00223 {
00224
00225 if ( m_streamer_thread == NULL ) {
00226 m_streamer_thread = new StreamerCtrlThread( this );
00227 }
00228
00229 if ( m_receiver_thread == NULL ) {
00230 m_receiver_thread = new ReceiverCtrlThread( this );
00231 }
00232
00233
00234 DialogOptions* window = new DialogOptions(this, m_streamer_thread, m_receiver_thread, this,
00235 ID_OPTIONS, _("Preferences..."));
00236 window->Show(true);
00237
00238 }
00239
00240
00245 bool MainFrame::ShowToolTips()
00246 {
00247 return TRUE;
00248 }
00249
00254 wxBitmap MainFrame::GetBitmapResource( const wxString& name )
00255 {
00256
00258 return wxNullBitmap;
00260 }
00261
00266 wxIcon MainFrame::GetIconResource( const wxString& name )
00267 {
00268
00270 return wxNullIcon;
00272 }
00277 void MainFrame::OnCloseWindow( wxCloseEvent& event )
00278 {
00279
00280 if ( m_receiver_thread && m_receiver_thread->isReceiving() ) {
00281 wxMessageDialog dlg1( this,
00282 wxT("Please stop by hand the receiver, then close.") , wxT( "Warning" ), wxOK );
00283 dlg1.ShowModal();
00284 return;
00285 }
00286
00287 if ( m_streamer_thread && m_streamer_thread->isStreaming() ) {
00288 wxMessageDialog dlg1( this,
00289 wxT("Please stop by hand the server streaming, then close."), wxT( "Warning" ), wxOK );
00290 dlg1.ShowModal();
00291 return;
00292 }
00293
00294 destroy_garbage_collector_for_images ();
00295
00296 wxWindow* window = this;
00297 window->Destroy();
00298
00299 event.Skip();
00300 }
00301
00302
00307 void MainFrame::OnMenuitemFileExitClick( wxCommandEvent& event )
00308 {
00310
00311 Destroy();
00313 }
00314
00315
00320 void MainFrame::OnMenuitemActionsStopServerClick( wxCommandEvent& event )
00321 {
00322 if ( m_streamer_thread != NULL ) {
00323 if ( m_streamer_thread->isStreaming() ) {
00324 m_streamer_thread->stopStreaming();
00325
00326 PrintStatusBarMessage( wxT( "Streaming stopped" ), 0 );
00327
00328
00329
00330
00331 m_streamer_thread = new StreamerCtrlThread( this );
00332 }
00333 else {
00334 wxMessageDialog dlg1( this, wxT( "Server is not running" ), wxT( "Info" ), wxOK );
00335 dlg1.ShowModal();
00336
00337 delete m_streamer_thread;
00338 m_streamer_thread = new StreamerCtrlThread( this );
00339 }
00340 }
00341 else {
00342 wxMessageDialog dlg1( this, wxT( "Server is not running" ), wxT( "Info" ), wxOK );
00343 dlg1.ShowModal();
00344 }
00345 }
00346
00347
00352 void MainFrame::OnMenuitemActionsStopReceivingClick( wxCommandEvent& event )
00353 {
00354 if ( m_receiver_thread != NULL ) {
00355 if ( m_receiver_thread->isReceiving() ) {
00356 m_receiver_thread->StopReceiving();
00357
00358 PrintStatusBarMessage( wxT( "Receiving stopped" ), 1 );
00359 }
00360 else {
00361 wxMessageDialog dlg1( this, wxT( "Receiver is not running" ), wxT( "Info" ), wxOK );
00362 dlg1.ShowModal();
00363
00364 delete m_receiver_thread;
00365 m_receiver_thread = new ReceiverCtrlThread( this );
00366 }
00367 }
00368 else {
00369 wxMessageDialog dlg1( this, wxT( "Receiver is not running" ), wxT( "Info" ), wxOK );
00370 dlg1.ShowModal();
00371 }
00372 }
00373
00374
00375
00376
00377
00378 void MainFrame::PrintStatusBarMessage( const wxChar* message, int field )
00379 {
00380 m_status_bar->SetStatusText( message, field );
00381 }
00382
00383
00384
00385
00386 void MainFrame::OnStreamerCtrlThreadEnd()
00387 {
00388 PrintStatusBarMessage( wxT( "Streaming stopped" ), 0 );
00389
00390
00391 m_streamer_thread = NULL;
00392 }
00393
00394
00395 void MainFrame::reset_displayed_image_vector ()
00396 {
00397
00398 int vec_size = (int)_displayed_frames->size();
00399 for ( int i = 0; i < vec_size; i++ ) {
00400 _displayed_frames->at( i )->Close ();
00401 }
00402
00403
00404 _displayed_frames->clear();
00405 }
00406
00407
00408
00409
00410 void MainFrame::OnReceiverCtrlThreadEnd()
00411 {
00412
00413 if ( face_detect ) {
00414 if ( gcard_use == true ) {
00415 if ( ctrl_gcard ) {
00416 ctrl_gcard->loop_stop();
00417
00418 delete ctrl_gcard;
00419 ctrl_gcard = NULL;
00420 }
00421
00422 if ( ctrl_vr_gcard ) {
00423 ctrl_vr_gcard->loop_stop();
00424
00425 delete ctrl_vr_gcard;
00426 ctrl_vr_gcard = NULL;
00427 }
00428 }
00429 else {
00430 if ( ctrl_lookup ) {
00431 ctrl_lookup->loop_stop();
00432
00433 delete ctrl_lookup;
00434 ctrl_lookup = NULL;
00435 }
00436 if ( ctrl_vr_lookup ) {
00437 ctrl_vr_lookup->loop_stop();
00438
00439 delete ctrl_vr_lookup;
00440 ctrl_vr_lookup = NULL;
00441 }
00442
00443 }
00444
00445 ctrl_initialized = false;
00446 face_detect = false;
00447 }
00448
00449
00450 m_receiver_thread = NULL;
00451
00452 reset_displayed_image_vector();
00453
00454 clear_garbage_collector_for_images();
00455
00456 video_received_window = false;
00457 }
00458
00459
00460 void MainFrame::OnAudioFrameReceivedFromNet( wxCommandEvent& event )
00461 {
00462 }
00463
00464 void MainFrame::destroy_garbage_collector_for_images()
00465 {
00466
00467
00468 clear_garbage_collector_for_images ();
00469
00470
00471
00472
00473 int tmp_size = (int)_gc_images->size();
00474 for (int i=0; i < tmp_size; i++) {
00475 list<IplImage*>* tmp_list;
00476
00477 tmp_list = _gc_images->at(i);
00478 tmp_list->clear();
00479 delete tmp_list;
00480 }
00481
00482 delete _gc_images;
00483 _gc_images = NULL;
00484 }
00485
00486
00487 void MainFrame::clear_garbage_collector_for_images()
00488 {
00489
00490
00491 int tmp_size = (int)_gc_images->size();
00492 for (int i=0; i < tmp_size; i++) {
00493 list<IplImage*>* tmp_list;
00494
00495 tmp_list = _gc_images->at(i);
00496
00497 while (tmp_list->size() > 0) {
00498 cvReleaseImage( &tmp_list->front());
00499 tmp_list->pop_front();
00500 }
00501 }
00502 }
00503
00504 void MainFrame::manage_garbage_collector_for_images( IplImage *image, int stream_id )
00505 {
00506
00507 if ( (int)_gc_images->size() < (stream_id +1) ) {
00508 list<IplImage*>* tmp_node;
00509
00510 for ( int i = (int)_gc_images->size(); i < (stream_id +1); i++) {
00511 tmp_node = new list<IplImage*>;
00512
00513
00514 _gc_images->push_back( tmp_node );
00515 }
00516 }
00517
00518 list<IplImage*> *list_at_id;
00519 list_at_id = _gc_images->at( stream_id );
00520
00521
00522 list_at_id->push_back( image );
00523
00524
00525 if ( list_at_id->size() > 3 ) {
00526
00527 IplImage *tmp_image;
00528 tmp_image = list_at_id->front();
00529
00530 list_at_id->pop_front();
00531
00532 cvReleaseImage( &tmp_image );
00533 }
00534 }
00535
00536
00537 void MainFrame::on_display_image_cb ( IplImage* image_received, int stream_id,
00538 char* window_id, void* callback_data )
00539 {
00540 MainFrame * klass;
00541
00542 if ( callback_data == NULL )
00543 return;
00544
00545 klass = (MainFrame*)callback_data;
00546
00547 if ( (int)klass->_displayed_frames->size() < (stream_id +1) ) {
00548 DisplayImageFrame *df;
00549 for ( int i = (int)klass->_displayed_frames->size(); i < (stream_id +1); i++) {
00550 df = new DisplayImageFrame( klass, i, wxSize(image_received->width,
00551 image_received->height) );
00552
00553
00554 klass->_displayed_frames->push_back( df );
00555 }
00556 }
00557 klass->_displayed_frames->at(stream_id)->DisplayIplImage( image_received );
00558 klass->manage_garbage_collector_for_images( image_received, stream_id );
00559
00560 }
00561
00562 void MainFrame::on_destroy_image_cb ( char* window_id, void* callback_data )
00563 {
00564 }
00565
00566 void MainFrame::on_create_image_cb ( char* window_id, void* callback_data )
00567 {
00568 }
00569
00570
00571
00572
00573
00574 void MainFrame::OnImageReceivedFromNet( wxCommandEvent& event )
00575 {
00576 image_received_data_t *img_data_struct;
00577 IplImage *image_received;
00578 int stream_id;
00579
00580 #ifdef __LINUX__
00581 wxMutexLocker( *this->_paint_mutex );
00582 #endif
00583 img_data_struct = (image_received_data_t *)event.GetClientData();
00584
00585 if ( img_data_struct == NULL )
00586 return;
00587
00588
00589 image_received = img_data_struct->image;
00590 stream_id = img_data_struct->stream_id;
00591
00592
00593
00594
00595 if ( this->num_streams > 0 ) {
00596
00597 if ( (int)_displayed_frames->size() < (stream_id +1) ) {
00598 DisplayImageFrame *df;
00599 for ( int i = (int)_displayed_frames->size(); i < (stream_id +1); i++) {
00600 df = new DisplayImageFrame( this, i, wxSize(image_received->width,
00601 image_received->height) );
00602
00603
00604 _displayed_frames->push_back( df );
00605 }
00606 }
00607 _displayed_frames->at(stream_id)->DisplayIplImage( image_received );
00608 manage_garbage_collector_for_images( image_received, stream_id );
00609 delete img_data_struct;
00610 }
00611 else {
00612
00613
00614
00615
00616
00617 if ( !face_detect ) {
00618 if ( video_received_window == false ) {
00619 DEBUG_PRINT ( "creating Video received window\n" );
00620 DisplayImageFrame *df;
00621 if ( _displayed_frames->size() == 0 ) {
00622
00623 df = new DisplayImageFrame( this, stream_id, wxSize(image_received->width,
00624 image_received->height) );
00625
00626 df->Show();
00627
00628 _displayed_frames->push_back( df );
00629 }
00630 else {
00631
00632 _displayed_frames->at(0)->Show();
00633 }
00634
00635 video_received_window = true;
00636 }
00637 _displayed_frames->at(0)->DisplayIplImage( image_received );
00638
00639 manage_garbage_collector_for_images( image_received, stream_id );
00640 delete img_data_struct;
00641 }
00642 else {
00643
00644
00645 if ( (int)_displayed_frames->size() < (stream_id +1) ) {
00646 DisplayImageFrame *df;
00647 for ( int i = (int)_displayed_frames->size(); i < (stream_id +1); i++) {
00648 df = new DisplayImageFrame( this, i, wxSize(image_received->width,
00649 image_received->height) );
00650
00651
00652 _displayed_frames->push_back( df );
00653 }
00654 }
00655 _displayed_frames->at(stream_id)->DisplayIplImage( image_received );
00656 manage_garbage_collector_for_images( image_received, stream_id );
00657 delete img_data_struct;
00658 }
00659 }
00660 #ifdef __LINUX__
00661
00662 Update();
00663 #endif
00664 return;
00665 }
00666
00667
00668
00669
00670
00671 void MainFrame::SetFacetrackOptions( wxArrayString* cascade_array, int cascade_count, int radius_min,
00672 int radius_max, int center_x, int center_y, int freeze_frame_bound,
00673 double track_window_hitrate, bool gcard_use, bool flipped_mode,
00674 int num_streams , bool algo_simple )
00675 {
00676
00677 face_detect = true;
00678
00679 this->cascade_array = cascade_array;
00680 this->cascade_size = cascade_count;
00681 this->radius_min = radius_min;
00682 this->radius_max = radius_max;
00683 this->center_x = center_x;
00684 this->center_y = center_y;
00685 this->freeze_frame_bound = freeze_frame_bound;
00686 this->track_window_hitrate = track_window_hitrate;
00687 this->gcard_use = gcard_use;
00688 this->flipped_mode = flipped_mode;
00689 this->num_streams = num_streams;
00690 this->algo_simple = algo_simple;
00691 }
00692
00693
00694
00699 void MainFrame::OnMenuHelpAuthorsClick( wxCommandEvent& event )
00700 {
00702
00703 AboutDialog* window = new AboutDialog(NULL, ID_AUTHORS_DIALOG, _("Authors"));
00704 window->Show(true);
00706 }
00707
00708
00713 void MainFrame::OnMenuHelpVersionClick( wxCommandEvent& event )
00714 {
00716
00717 VersionDialog* window = new VersionDialog(NULL, ID_VERSION_DIALOG, _("Dialog"));
00718 window->Show(true);
00720 }
00721
00722
00723 void MainFrame::OnWorkerEvent( wxCommandEvent& event )
00724 {
00725
00726 wxString str = event.GetString();
00727
00728 wxMessageDialog dlg( this, str, wxT( "Info" ), wxOK );
00729
00730 dlg.ShowModal();
00731 }
00732
00733
00734 int MainFrame::GetMaxRadius()
00735 {
00736 return radius_max;
00737 }
00738
00739 int MainFrame::GetMinRadius()
00740 {
00741 return radius_min;
00742 }
00743
00744 int MainFrame::GetCenterX()
00745 {
00746 return center_x;
00747 }
00748
00749 int MainFrame::GetCenterY()
00750 {
00751 return center_y;
00752 }
00753
00754 wxArrayString* MainFrame::GetCascadeArray ()
00755 {
00756 return cascade_array;
00757 }
00758
00759 int MainFrame::GetFreezeFrameBound()
00760 {
00761 return freeze_frame_bound;
00762 }
00763
00764 double MainFrame::GetTrackWindowHitrate()
00765 {
00766 return track_window_hitrate;
00767 }
00768
00769 int MainFrame::GetCascadeSize ()
00770 {
00771 return cascade_size;
00772 }
00773
00774 bool MainFrame::GetAlgoSimpleStatus()
00775 {
00776 return algo_simple;
00777 }
00778
00779 bool MainFrame::is_face_detect_options_set ()
00780 {
00781 return face_detect;
00782 }
00783
00784 bool MainFrame::GetGCardUseStatus()
00785 {
00786 return gcard_use;
00787 }
00788
00789 bool MainFrame::GetFlippedModeStatus()
00790 {
00791 return flipped_mode;
00792 }