linux 기반libvlc 2차 개발

6399 단어 비디오 코딩
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
"); exit(0); } } void OpenBitstreamoutFile (char *fn) { if (NULL == (bitsout=fopen(fn, "wb"))) { printf("open out file error
"); 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
"); /* 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--------------------
",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!
"); 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!
"); 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!
"); return (void *)1; } //printf("Get_Xwindow = %d
",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!
"); 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
"); return -1; } while(!feof(bits)) { //printf("--------------------------------------
"); 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
"); } // free(buffer); fclose(bits); fclose(bitsout); return 0; }

마지막으로, 마우스로 닫기 단추를 눌렀을 때, 항상 X 윈도우즈failed를 실현하려고 했는데, 어떻게 해야만 이 사건을 포착할 수 있습니까?프로그램 종료!!신께서 가르침을 주시기를 바랍니다!!

좋은 웹페이지 즐겨찾기