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

    ///////////////////////////////////////////////////////
// FileName:	video_cap_thread.cpp
// Author:		b1gm0use
// Project:		myvideo

#include <iostream>

#include "network.h"
#include "video_cap_thread.h"
#include "video_cap.h"
#include "capture_event.h"
#include "video_send.h"
#include "network_video_send.h"
#include "video.h"
#include "avi.h"

using namespace std;


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

// 构造函数
// 传入参数为
// vp_in 上层VideoPlayer对象指针
// stackSize QThread所用的参数
video_cap_thread::video_cap_thread ( avi * avi_ptr_in, VideoPlayer * vp_in, 
		unsigned int stackSize )
		:QThread( stackSize ) // {{{
{
	verbose_output( 2, "create video_cap_thread." );

	vp = vp_in;

	ns = NULL;
	vs = NULL;

	avi_ptr = avi_ptr_in;

} // }}}


video_cap_thread::~video_cap_thread ( void ) // {{{
{
	delete vs;
	delete ns;
} // }}}


// 运行部分,线程代码在这里
void video_cap_thread::run ( void ) // {{{
{

	int size = 0;

	BUFF * image = NULL;

	video_cap * vc = NULL;

	verbose_output( 1, "running new thread." );

	// 如果是standalone会创建一个video_cap对象
	// 如果是listen会再创建一个network_send对象

	vc = new video_cap( avi_ptr );
	vc->init();

	if ( avi_ptr->net_mode == LISTEN )
	{
		verbose_output( 3, "net_mode LISTEN" );

		ns = new network_video_send( avi_ptr );
		ns->init();
	}

	// 创建video_send对象,用于向主体发送捕捉完毕信号
	if ( avi_ptr->net_mode == STANDALONE || 
		( avi_ptr->net_mode == LISTEN && avi_ptr->show_local_video ) )
	{
		vs = new video_send( avi_ptr );
		vs->init( vp );
	}

	verbose_output( 1, "begin video capture loop." );

	while ( 1 )
	{

		//sleep( 1 );
		// 取得一帧
		if ( vc->get_image ( image, &size ) == -1 )
		{
			cerr << "Warning! get image error" << endl;
			continue;
		}

		if ( image == NULL )
		{
			cerr << "Can't get new video frame" << endl;
			::exit( 1 );
		}

		// 发出信号
		if ( avi_ptr->net_mode == STANDALONE || 
			( avi_ptr->net_mode == LISTEN && avi_ptr->show_local_video ) )
		{
			vs->send_image( image, size );
		}

		// 如果做为listen方,则向其它connect方传送图像
		if ( avi_ptr->net_mode == LISTEN )
		{
			ns->send_image( image, size );
		}

	}

	return ;

} // }}}