基于linux下libvlc的二次开发

hTs.h

/* Project by qiuliangbin copyright 2015-2018*/
/* Just for learning;                        */
/* Function:Use Rtp Transmition send Ts to VLC Media player*/

#include 
#include 
#include 
#include  
#include  
#include  
#include  


#define PACKET_BUFFER_END            (unsigned int)0x00000000

#define DEST_IP                "10.1.1.124"
#define DEST_PORT            1234

static const char * test_defaults_args[] = { 
	"-v", 
	"--ignore-config", 
	"-I", "dummy", 
	"--no-media-library" 
};
  
static const int test_defaults_nargs = sizeof (test_defaults_args) / sizeof (test_defaults_args[0]); 

Ts_vlc.cpp

#include 
#include 
#include 
#include 
#include 

#include "hTs.h"

FILE *bits = NULL;                //!< the bit stream file
FILE *bitsout = NULL;
#define PACKET_SIZE 188

int max_size = 800000;
int count = 0;
char buffer[200000] = {0};

libvlc_time_t m_TotalTime;

void OpenBitstreamFile (char *fn)
{
	if (NULL == (bits=fopen(fn, "rb")))
	{
		printf("open file error\n");
		exit(0);
	}
}
void OpenBitstreamoutFile (char *fn)
{
	if (NULL == (bitsout=fopen(fn, "wb")))
	{
		printf("open out file error\n");
		exit(0);
	}
}

int GetAnnexbTS()
{	
	int n = 0;
	int FrameNum = 0;
	unsigned char *Buf;

	if ((Buf = (unsigned char*)calloc (max_size , sizeof(char))) == NULL) 
	  printf ("GetAnnexbNALU: Could not allocate Buf memory\n");

	/* Sync */
	while (1) 
	{
		if (1 != fread (Buf, 1, 1, bits))//从码流中读3个字节
		{
			free(Buf);
			return 0;
		}
		if(Buf[0] == 0x47)
		{
			int ncount =0;
			if ((n=fread(&Buf[1],1,PACKET_SIZE-1,bits)) == 187)
			{
				int readonebit = fgetc(bits);
				if ( readonebit== 0x47) 
				{
					FrameNum ++;
					if (0 != fseek (bits, -1 , SEEK_CUR))//把文件指针向后退开始字节的字节数
					{
						free(Buf);
						printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
					}
					break;
				}
				if(readonebit == feof(bits))
				{
					//Frame 1~6 表示文件传输
				//	FrameNum = 0;//表示文件传输结束 
					return -1; //到文件末尾了
				}
				else
				{
					return -2;//忽略这一帧,在文件中有帧被破坏不需要对他做特别处理,直接存储
				}
			}    
				
		}
	}
	memcpy (buffer, &Buf[0],188);
	free(Buf);
	return FrameNum;
}
void *ThreadFun_play(void *)
{
	
	libvlc_instance_t *instance; 
	libvlc_media_t *media; 
	libvlc_media_player_t *player;

	int width,height;
	libvlc_time_t length; 
	const char * file = "./666666.ts";  

	instance = libvlc_new (test_defaults_nargs, test_defaults_args); 
	assert (instance != NULL);  
	
	media = libvlc_media_new_path (instance, file); 
	assert (media != NULL);
	  
	player = libvlc_media_player_new_from_media (media); 
	assert (player != NULL); 
	 
	float fPos = libvlc_media_player_get_position(player);
	m_TotalTime = libvlc_media_player_get_length(player);//
	printf("--------------------%ld--------------------\n",m_TotalTime);

//	libvlc_set_fullscreen(player,1);
	libvlc_media_player_next_frame(player);
	libvlc_media_player_play (player);
	//sleep(m_TotalTime); 
	/*int libvlc_media_player_is_playing( libvlc_media_player_t *p_mi )
	{
    		libvlc_state_t state = libvlc_media_player_get_state( p_mi );
    		return (libvlc_Playing == state) || (libvlc_Buffering == state);
	}*/ 
//	while(1 == libvlc_media_player_is_playing(player))//playing...
	while(1)
	{
		if(libvlc_media_player_get_state(player)==4)//"libvlc_Paused"
		{
			libvlc_media_release (media);  
			libvlc_media_player_stop (player); 
			libvlc_media_player_release (player); 
			libvlc_release (instance); 
			printf("libvlc_Paused!\n");
			return (void *)1;
		}
		if(libvlc_media_player_get_state(player)==6)//"libvlc_Stopped"
		{
			libvlc_media_release (media);  
			libvlc_media_player_stop (player); 
			libvlc_media_player_release (player); 
			libvlc_release (instance); 
			printf("libvlc_Stopped!\n");
			return (void *)1;
		}
		if(libvlc_media_player_get_state(player)==7)//"libvlc_Error"
		{
			libvlc_media_release (media);  
			libvlc_media_player_stop (player); 
			libvlc_media_player_release (player); 
			libvlc_release (instance); 
			printf("libvlc_Error!\n");
			return (void *)1;
		}
		//printf("Get_Xwindow = %d\n",libvlc_media_player_get_xwindow(player));
		/*{
			libvlc_media_release (media);  
			libvlc_media_player_stop (player); 
			libvlc_media_player_release (player); 
			libvlc_release (instance); 
			printf("libvlc_Error!\n");
			return (void *)1;
		}*/
	//	XCB_Manage
	}
 
	libvlc_media_release (media);  
	libvlc_media_player_stop (player); 
	libvlc_media_player_release (player); 
	libvlc_release (instance); 
	return (void *)1;
}
int main(int argc, char* argv[])
{
	int Ts_RetVal=0;
	int Fmno = 0;
	int	bytes=0;
	int position=0;
	unsigned short seq_num =0;
	OpenBitstreamFile("./222.ts");
	OpenBitstreamoutFile("./666666.ts");
	char sendbuf[1500];

	int count = 0 ;

	memset(sendbuf,0,1500);

	/*create a thread to reduce the vlc palyer*/
	pthread_t vlc_thread;
	int handle_vlcplay = pthread_create(&vlc_thread,NULL,ThreadFun_play,NULL);
	if(handle_vlcplay == -1)
	{
		printf("Create Vlcplay thread Error\n");
		return -1;
	}
	while(!feof(bits)) {
		//printf("--------------------------------------\n");
		if ((Ts_RetVal=GetAnnexbTS()) == -1)
		{
			break;
		}
		Fmno += Ts_RetVal;
		memcpy(&sendbuf[12+(Fmno-1)*188],buffer,188);

		position = position + 188;
//		fwrite(&sendbuf[12+(Fmno-1)*188],1,PACKET_SIZE,bitsout);
		if ((Fmno == 7)&&(position >= 188*7))
		{			

			fwrite(&sendbuf[12],1,7*188,bitsout);
			position = 0;
			Fmno = 0;
			memset(sendbuf,0,1500);
		}	
	//	dump(buffer,Fmno);
		//usleep(1000);	
	}

	if((position>0)&&(Fmno>0))//发最后一帧
	{
		bytes=188*Fmno+ 12 ;
		
		fwrite(&sendbuf[12],1,188*Fmno,bitsout);
	}
	int wait_vlc=pthread_join(vlc_thread,NULL);
	if(wait_vlc == -1)
	{
		printf("pthread_join vlc _Error\n");
	}
//	free(buffer);
	fclose(bits);
	fclose(bitsout);
	return 0;
}

最后,一直想实现鼠标单击关闭按钮时,老是X windows failed,如何才能捕捉到这个事件呢??退出程序!! 望大神赐教!!

你可能感兴趣的:(音视频编解码)