⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iso1394dataflowsource.c

📁 从 IEEE 1394总线接收传输流
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * MPEG2-TS over IEEE 1394 decoder - receive and decode MPEG-2 transport *                                   streams according to IEC 61883-4 * * Copyright (C) 2000-2007, Manfred Weihs <mweihs@users.sourceforge.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdlib.h>#include <string.h>#include <stdio.h>#include <libraw1394/raw1394.h>#include <errno.h>#include <sys/time.h> /* needed for select */#include <byteswap.h>#include <pthread.h>#include "config.h"#include "iso1394dataflowsource.h"/* These functions are needed for synchronisation. They are placed here, because only   the dataflowsource has exact knowledge of the arrival time of the TS packets */#include "synchronisation.h"#define PCR_DELAY 45000 /* amount of time, which PCRs are delayed (90000 = 1s) to compensate delay of decoders */extern int debug;static int last; /* current is in iso1394dataflowsource.h (externally available) */#define TS_BUFFER_MARGIN 10 /* number of free buffer places we try to perserve (for the next iso packet).                               If this space is not available, we will stop raw1394_loop_iterate and                               process the remaining iso packets later. *//** handle for the 1394 subsystem */static raw1394handle_t rawhandle_iso,rawhandle_ct; /* handle for iso reception and cycle time reading */static pthread_mutex_t rawhandle_ct_mutex;static int ch_number; /* iso channel number */static int fraction_number,data_block_size,next_block,next_source_packet; /* internal variables for CIP-resolution */static unsigned long isopackets; /* number of received iso packets *//* here we store the latest two pairs of pcr and 1394 cycle time */struct cycle_time1394 {	int second_count;	int cycle_count;	int cycle_offset;} ;static unsigned long long int pcr0,pcr1,pcr2,pcr3;static struct cycle_time1394 cycle_time0,cycle_time1,cycle_time2,cycle_time3,current_cycle_time;static int valid_pcrs; /* this is zero after disable_sync; when it reaches 2 (we got two valid pcrs) sync is enabled *//* mutexes for change of current_cycle_time and the pcr-ct-pairs */static pthread_mutex_t current_cycle_time_mutex, set_pcr_mutex; /* current_cycle_time is only used for reconstruction of second count */static int packets_since_last_update_ct; /* number of iso packets received since last update of current_cycle_time */#define MAX_PACKETS_BETWEEN_UPDATES 200  /* we update the cycle the current cycle time at lease every n packet */static int delivered_packet;static enum raw1394_iso_disposition iso_handler(raw1394handle_t handle, unsigned char *data, unsigned int length, unsigned char channel, unsigned char tag, unsigned char sy, unsigned int cycle, unsigned int dropped);static int getIsoPacket();int iso_init(int channel){	if (pthread_mutex_init(&current_cycle_time_mutex,NULL))	{		fprintf(stderr,"Iso1394DataFlowSource: cannot create current cycle time mutex\n");;		exit(1);	}	if (pthread_mutex_init(&set_pcr_mutex,NULL))	{		fprintf(stderr,"Iso1394DataFlowSource: cannot create pcr mutex\n");;		exit(1);	}	if (pthread_mutex_init(&rawhandle_ct_mutex,NULL))	{		fprintf(stderr,"Iso1394DataFlowSource: cannot create rawhandle_ct mutex\n");;		exit(1);	}	current=last=0;	isopackets=0;	bytecount=0;	fraction_number=data_block_size=0;	if (!(rawhandle_iso = raw1394_new_handle())) {		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		if (!errno) {			fprintf(stderr,"Iso1394DataFlowSource: libraw1394 not compatible with kernel\n");;			return 1;		}		else {			fprintf(stderr,"Iso1394DataFlowSource: cannot get 1394 handle\n");;			return 1;		}	} else {		if (debug) {			fprintf(stdout,"Iso1394DataFlowSource: successfully got 1394 handle for iso reception, 1394 subsystem initialized\n");		}	}	if (raw1394_set_port(rawhandle_iso, 0))	/* take first available 1394 adapter */	{		fprintf(stderr,"Iso1394DataFlowSource: couldn't set port\n");;		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		raw1394_destroy_handle(rawhandle_iso);		return 1;	}	if (debug) {		fprintf(stdout,"Iso1394DataFlowSource: using first card found: %d nodes on bus, local ID is %d\n", raw1394_get_nodecount(rawhandle_iso),(raw1394_get_local_id(rawhandle_iso) & 0x3f));	}	ch_number=channel; /* set channel */#if OLDLIBRAW1394	if (raw1394_iso_recv_init(rawhandle_iso, iso_handler, 2000, 1024, channel, 800) < 0)#else	if (raw1394_iso_recv_init(rawhandle_iso, iso_handler, 2000, 1024, channel, RAW1394_DMA_DEFAULT, 800) < 0)#endif	{		fprintf(stderr,"Iso1394DataFlowSource: cannot set iso handler\n");		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		raw1394_destroy_handle(rawhandle_iso);		return 1;	}	else	{		if (debug) {			fprintf(stdout,"Iso1394DataFlowSource: iso handler set\n");		}	}	if (raw1394_iso_recv_start(rawhandle_iso, -1, 2, -1) <0)	{		fprintf(stderr,"Iso1394DataFlowSource: cannot start iso listening\n");		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		raw1394_destroy_handle(rawhandle_iso);		return 1;	}	else	{		if (debug) {			fprintf(stdout,"Iso1394DataFlowSource: started listening to iso-channel\n");		}	}	/* init second handle for cycle time checks */	if (!(rawhandle_ct = raw1394_new_handle())) {		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		raw1394_destroy_handle(rawhandle_iso);		if (!errno) {			fprintf(stderr,"Iso1394DataFlowSource: libraw1394 not compatible with kernel\n");;			return 1;		}		else {			fprintf(stderr,"Iso1394DataFlowSource: cannot get 1394 handle\n");;			return 1;		}	} else {		if (debug) {			fprintf(stdout,"Iso1394DataFlowSource: successfully got 1394 handle for cycle time reading, 1394 subsystem initialized\n");		}	}	if (raw1394_set_port(rawhandle_ct, 0))	/* take first available 1394 adapter */	{		fprintf(stderr,"Iso1394DataFlowSource: couldn't set port\n");;		(void) pthread_mutex_destroy(&rawhandle_ct_mutex);		(void) pthread_mutex_destroy(&current_cycle_time_mutex);		(void) pthread_mutex_destroy(&set_pcr_mutex);		raw1394_destroy_handle(rawhandle_iso);		raw1394_destroy_handle(rawhandle_ct);		return 1;	}	packets_since_last_update_ct = MAX_PACKETS_BETWEEN_UPDATES; /* next packet should update current_cycle_time */	disable_sync(); /* start with sync is disabled (until we have enough pcrs */	delivered_packet = getIsoPacket(); /* retrieves first packet */	                                   /* to make sure that we start with a complete packet	                                      this first one, which might only be a fragment, will be skipped by mpeg2demux */	return 0;}void iso_done(){	if (debug) {		fprintf(stdout,"Stop listening for iso packets\n");	}	raw1394_iso_stop(rawhandle_iso);	raw1394_iso_shutdown(rawhandle_iso);	raw1394_destroy_handle(rawhandle_iso); /* destroy both raw-handles */	raw1394_destroy_handle(rawhandle_ct);	if (pthread_mutex_destroy(&rawhandle_ct_mutex))	{		fprintf(stderr,"Iso1394DataFlowSource: Cannot destroy rawhandle_ct mutex\n");		exit(1);	}	if (pthread_mutex_destroy(&current_cycle_time_mutex))	{		fprintf(stderr,"Iso1394DataFlowSource: Cannot destroy current cycle time mutex\n");		exit(1);	}	if (pthread_mutex_destroy(&set_pcr_mutex))	{		fprintf(stderr,"Iso1394DataFlowSource: Cannot destroy current cycle time mutex\n");;		exit(1);	}}static void getCurrentCycleTime(struct cycle_time1394 *cycle_time) {	unsigned char buf[4];	if (pthread_mutex_lock(&rawhandle_ct_mutex)) /* lock mutex */	{		fprintf(stderr,"IsoDataFlowSource: cannot lock current cycle time mutex\n");		exit(1);	}	while (raw1394_read(rawhandle_ct, 0xffc0 | raw1394_get_local_id(rawhandle_ct), 0xFFFFF0000200ull, 4, (quadlet_t*) buf) < 0) {		fprintf(stderr,"Iso1394DataFlowSource: cannot read cycle time -> retrying\n");	}	if (pthread_mutex_unlock(&rawhandle_ct_mutex)) /* unlock mutex */	{		fprintf(stderr,"IsoDataFlowSource: cannot unlock current cycle time mutex\n");		exit(1);	}	cycle_time->second_count=buf[0] >> 1;	cycle_time->cycle_count=((buf[0] & 0x01) << 12) | (buf[1] << 4) | (buf[2] >> 4);	cycle_time->cycle_offset=((buf[2] & 0x0f) << 8) | buf[3];}int copybytes(unsigned char* output,int length){	if (length > rest_of_ts_packet()){		fprintf(stderr,"Iso1394DataFlowSource: copying bytes beyond transport packet length\n");		length=rest_of_ts_packet();	}	memcpy(output,buffer[current]+4+bytecount,length);	bytecount+=length;	return length;}int skipbytes(int length){	if (length > rest_of_ts_packet())	{		fprintf(stderr,"Iso1394DataFlowSource: cannot skip bytes beyond current TS packet\n");;		length=rest_of_ts_packet();	}	if (length < 0)	{		fprintf(stderr,"Iso1394DataFlowSource: cannot skip negative number of bytes\n");;		length=0;	}	bytecount+=length;	return length;}int nextpacket(){	if (rest_of_ts_packet() < 0)	{		fprintf(stderr,"Iso1394DataFlowSource Error: we read beyond the length of an MPEG 2 TS packet\n");		exit(0); //FIXME: This is here for debugging puroses to ensure that we detect such errors	}	if (delivered_packet) {		current++;		current %= TS_BUFFER_ELEMENTS;	}	bytecount=0;	delivered_packet = getIsoPacket(); /* fetches packet if necessary; */	return delivered_packet;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -