gusucode.com > 嵌入式linux系统的网络编程源码程序 > 嵌入式linux系统的网络编程源码程序/视频会议源码/avi.cpp

    #include <qwidget.h>

#include "avi.h"
#include "video_player.h"
#include "audio_player.h"
#include "network_ctrl.h"
#include "signal_def.h"


///////////////////////////////////////////////////////
// Public Functions
///////////////////////////////////////////////////////

avi::avi( QWidget * parent, const char * name, int wFlags )
   	: QWidget( parent, name, wFlags ) // {{{
{

	// Video Part Public Variables
	video_player_ptr = NULL;
	video_opt.min_width = BASIC_WIDTH;
	video_opt.min_height = BASIC_HEIGHT;
	video_opt.factor = DEFAULT_FACTOR;
	video_opt.depth = DEFAULT_DEPTH;
	video_opt.palette = DEFAULT_PALETTE;
	video_opt.quality = DEFAULT_QUALITY;
	video_dev = DEFAULT_VIDEO_DEVICE;
	video_send_semaphore = NULL;
	video_cap_semaphore = NULL;

	// Audio Part Public Variables
	audio_player_ptr = NULL;
	audio_dev = DEFAULT_AUDIO_DEVICE;
	snd_buff_semaphore[0] = snd_buff_semaphore[1] = snd_buff_semaphore[2] = NULL;
	snd_buff[0] = snd_buff[1] = snd_buff[2] = NULL;
	use_g723 = DEFAULT_USE_G723;
	audio_cap_semaphore = NULL;
	g723_high_bit_rate = DEFAULT_G723_HIGH_BIT_RATE;
	g723_voice_detect = DEFAULT_G723_VOICE_DETECT;

	// Network Part Public Variables
	network_ctrl_ptr = NULL;
	net_mode = STANDALONE;
	ip = DEFAULT_IP;
	use_multicast = DEFAULT_USE_MULTICAST;
	mc_addr = DEFAULT_MC_ADDR;

	// Other Part Public Variables
	va_mode = VIDEO_AUDIO;
	send_image_semaphore = NULL;
	show_local_video = false;

	// Semaphore
	video_send_semaphore = new QSemaphore( 1 );
	video_cap_semaphore = new QSemaphore( 1 );
	snd_buff_semaphore[0] = new QSemaphore( 1 );
	snd_buff_semaphore[1] = new QSemaphore( 1 );
	snd_buff_semaphore[2] = new QSemaphore( 1 );
	audio_cap_semaphore = new QSemaphore( 1 );
	send_image_semaphore = new QSemaphore( 1 );

} // }}}

avi::~avi ( void ) // {{{
{

	// Video Part Public Variables
	delete video_player_ptr;
	delete video_send_semaphore;
	delete video_cap_semaphore;

	// Audio Part Public Variables
	delete audio_player_ptr;
	delete [] snd_buff_semaphore;
	delete [] snd_buff;
	delete audio_cap_semaphore;

	// Network Part Public Variables
	delete network_ctrl_ptr;

	// Other Part Public Variables
	delete send_image_semaphore;


} // }}}

void avi::init_video ( const char * video_dev_in, video_opt_t * video_opt_in/* ,
	   QSemaphore * video_send_semaphore_in, QSemaphore * video_cap_semaphore_in*/ ) // {{{{
{

	video_dev = video_dev_in;

	video_opt.min_width = video_opt_in->min_width;
	video_opt.min_height = video_opt_in->min_height;
	video_opt.factor = video_opt_in->factor;
	video_opt.depth = video_opt_in->depth;
	video_opt.palette = video_opt_in->palette;
	video_opt.quality = video_opt_in->quality;

	//video_send_semaphore = video_send_semaphore_in;
	//video_cap_semaphore = video_cap_semaphore_in;

	return;
} // }}}


void avi::init_audio ( const char * audio_dev_in, bool use_g723_in,
	bool g723_high_bit_rate_in, bool g723_voice_detect_in/* ,
	QSemaphore * audio_can_play_semaphore_in, 
	QSemaphore * audio_cap_semaphore_in,
	QSemaphore * snd_buff_semaphore_in[3]*/ ) // {{{
{

	audio_dev = audio_dev_in;

	use_g723 = use_g723_in;

	g723_high_bit_rate = g723_high_bit_rate_in;
	g723_voice_detect = g723_voice_detect_in;

	snd_buff[0] = new BUFF [ FULL_AUDIO_BUFF_SIZE ];
	snd_buff[1] = new BUFF [ FULL_AUDIO_BUFF_SIZE ];
	snd_buff[2] = new BUFF [ FULL_AUDIO_BUFF_SIZE ];

	//audio_can_play_semaphore = audio_can_play_semaphore_in;
	//audio_cap_semaphore = audio_cap_semaphore_in;
	//snd_buff_semaphore[0] = snd_buff_semaphore_in[0];
	//snd_buff_semaphore[1] = snd_buff_semaphore_in[1];
	//snd_buff_semaphore[2] = snd_buff_semaphore_in[2];

	return;
} // }}}


void avi::init_network ( network_mode_t net_mode_in, const char * ip_in,
	   bool use_multicast_in, const char * mc_addr_in ) // {{{
{

	net_mode = net_mode_in;

	ip  = ip_in;

	use_multicast = use_multicast_in;

	mc_addr = mc_addr_in;

	return;
} // }}}


void avi::init_other ( va_mode_t va_mode_in/* , QSemaphore * send_image_semaphore_in*/,
	   bool show_local_video_in ) // {{{
{

	va_mode = va_mode_in;

	show_local_video = show_local_video_in;

	//send_image_semaphore = send_image_semaphore_in;

	return;
} // }}}


void avi::init ( void ) // {{{
{


	bool zoom = false;

	// 显示视频部件
	// 创建主widget
	video_player_ptr = new VideoPlayer( this, true, zoom, this, "Video Player", 
			Qt::WDestructiveClose | Qt::WResizeNoErase );

	if ( NULL == video_player_ptr )
	{
		cerr << "Can't create video widget, exit..." << endl;
		exit( 1 );
	}

	// 设置程序大小
	setFixedSize( video_opt.min_width * video_opt.factor, 
			video_opt.min_height * video_opt.factor );

	if ( va_mode != AUDIO_ONLY )
	{
		video_player_ptr->init();
		video_player_ptr->show();
	}

	// 显示音频部件
	if ( va_mode != VIDEO_ONLY )
	{
		audio_player_ptr = new AudioPlayer( this, this, "Audio Player" );

		if ( NULL == audio_player_ptr )
		{
			cerr << "Can't create audio widget, exit..." << endl;
			exit( 1 );
		}

		audio_player_ptr->init();
		audio_player_ptr->hide();
	}

	if ( net_mode != STANDALONE )
	{
		network_ctrl_ptr = new network_ctrl( this, this, "Network Ctrl" );

		if ( NULL == network_ctrl_ptr )
		{
			cerr << "Can't create network widget, exit..." << endl;
			exit( 1 );
		}

		network_ctrl_ptr->init();
		network_ctrl_ptr->hide();
		
	}


} // }}}



void avi::get_video ( const char *& video_dev_out, video_opt_t *& video_opt_out) // {{{{
{

	video_dev_out = video_dev;

	video_opt_out->min_width = video_opt.min_width;
	video_opt_out->min_height = video_opt.min_height;
	video_opt_out->factor = video_opt.factor;
	video_opt_out->depth = video_opt.depth;
	video_opt_out->palette = video_opt.palette;
	video_opt_out->quality = video_opt.quality;

	return;

} // }}}


void avi::get_audio ( const char *& audio_dev_out, bool & use_g723_out,
	bool & g723_high_bit_rate_out, bool & g723_voice_detect_out ) // {{{
{

	audio_dev_out = audio_dev;

	use_g723_out = use_g723;

	g723_high_bit_rate_out = g723_high_bit_rate;
	g723_voice_detect_out = g723_voice_detect;

	return;

} // }}}


void avi::get_network ( network_mode_t & net_mode_out, const char *& ip_out, 
		bool use_multicast_out, const char *& mc_addr_out ) // {{{
{

	net_mode_out = net_mode;

	ip_out = ip ;

	use_multicast_out = use_multicast;

	mc_addr_out = mc_addr;

	return;

} // }}}


void avi::get_other ( va_mode_t & va_mode_out, bool & show_local_video_out ) // {{{
{

	va_mode_out = va_mode;

	show_local_video_out = show_local_video;

	return;

} // }}}


///////////////////////////////////////////////////////
// Protected Functions
///////////////////////////////////////////////////////


void avi::mousePressEvent ( QMouseEvent * e ) // {{{
{
	e = NULL;

	//if ( net_mode == CONNECT )
	{
		if ( network_ctrl_ptr->running() )
		{
			cout << "stop it" << endl;
			stop_running();
			//network_ctrl_ptr->stop_running();
		}
		else
		{
			cout << "connect it" << endl;
			begin_running();
			//network_ctrl_ptr->begin_running();
		}
	}

	return;

} // }}}


void avi::customEvent ( QCustomEvent * e ) // {{{
{
	verbose_output( 4, "avi event." );

	if ( net_mode == LISTEN )
	{
		switch ( e->type() )
		{
			// Signal for listen
			case SIG_ACCEPT_CONNECT:
				cout << "SIG_ACCEPT_CONNECT" << endl;
				break;

			case SIG_ACCEPT_DISCONNECT:
				cout << "SIG_ACCEPT_DISCONNECT" << endl;
				stop_running();
				break;

			case SIG_LOST_CLIENT:
				cout << "SIG_LOST_CLIENT" << endl;
				stop_running();
				break;

			case SIG_STOP_OK:
				cout << "SIG_STOP_OK" << endl;
				break;

			default:
				cerr << "Warning! Unknown signal." << endl;
				return;
				break;
		}

		emit error_signal( e->type() );
	}
	else
	{
		if ( net_mode == CONNECT )
		{
			switch ( e->type() )
			{
				// Signal for connect
				case SIG_CONNECT_OK:
					// Ok, correct!
					cout << "SIG_CONNECT_OK" << endl;
					break;

				case SIG_DISCONNECT_OK:
					cout << "SIG_DISCONNECT_OK" << endl;
					break;

				case SIG_CONNECT_TIMEOUT:
					cout << "SIG_CONNECT_TIMEOUT" << endl;
					break;

				case SIG_LOST_SERVER:
					// error in connect
					// delete the network thread first
					cout << "SIG_LOST_SERVER" << endl;
					stop_running();
					break;

				default:
					cerr << "Warning! Unknown signal." << endl;
					return;
					break;
			}

			emit error_signal( e->type() );
		}
		else
		{
			cerr << "Error! You shouldn't be here!" << endl;
			return;
		}
	}

	return;

} // }}}


///////////////////////////////////////////////////////
// Public Slots
///////////////////////////////////////////////////////


void avi::begin_running( void ) // {{{
{
	if ( NULL != video_player_ptr && !video_player_ptr->running() )
	{
		video_player_ptr->begin_running();
	}

	if ( NULL != audio_player_ptr && !audio_player_ptr->running() )
	{
		audio_player_ptr->begin_running();
	}

	if ( !network_ctrl_ptr->running() )
	{
		network_ctrl_ptr->begin_running();
	}

	return;
} // }}}


void avi::stop_running ( void ) // {{{
{

	if ( network_ctrl_ptr->running() )
	{
		cout << "stop" << endl;
		network_ctrl_ptr->stop_running();
	}

	return;

} // }}}