www.pudn.com > rtp_src.rar > ts.c


/*
 * ts.c: tool to send MPEG-2 TS over {uni.multi,broad}cast RTP
 * Author: David Podeur for Convergence integrated media GmbH
 *
 * 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.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 *
 * The author can be reached at 
 */
#include 
#include 
#include "sendrtp_ts.c"

#define PACKET_SIZE 188
#define GIVE_UP 1024
#define MTU 1590

long getmsec() {
        struct timeval tv;
        gettimeofday(&tv,(struct timezone*) NULL);
        return(tv.tv_sec%1000000)*1000 + tv.tv_usec/1000;
}

int main(int argc, char** argv) {

	unsigned char c;
	unsigned char buf[MTU];

	int count = 0;

	int bytes_filled;
	int bytes_read;
	unsigned char* free_bytes;

	/* Output: {uni,multi,broad}cast socket */
	char ipOut[20];
	int portOut;
	int ttl;
	struct sockaddr_in sOut;
	int socketOut, s;

	/* rtp */
	struct rtpheader hdr;

	fprintf(stderr,"Very first hack to \"rtpize\" mpeg-2 ts streams\n");
	fprintf(stderr,"      by \n");

	/* options */
	if (argc == 1) {
		strcpy(ipOut,"224.0.1.2");
		portOut = 5004;
		ttl = 1;
	}
	else if (argc == 4) {
		strcpy(ipOut,argv[1]);
		portOut = atoi(argv[2]);
		ttl = atoi(argv[3]);
	}
	else {
		fprintf(stderr,"Usage %s ip port ttl\n",argv[0]);
		exit(1);
	}
	fprintf(stderr,"Using %s:%d:%d\n",ipOut,portOut,ttl);



	/* Sync */
	while (1) {
		c = getchar();
		if (count++ == GIVE_UP) {
			fprintf(stderr,"I can hardly believe this is MPEG-2 TS...\n");
			exit(-1);
		}
		if (c==0x47) {
			fread(buf,1,PACKET_SIZE-1,stdin);
			if (getchar()==0x47) {
				fread(buf,1,PACKET_SIZE-1,stdin);
				break;
			}
		}
	}

	/* Init RTP */
	socketOut = makesocket("224.0.1.2",5004,2,&sOut);
	initrtp(&hdr);
	fprintf(stderr,"version=%X\n",hdr.b.v);

	/* Read packets */
	free_bytes = buf;
	while (1) {
		bytes_read = fread(free_bytes,1,PACKET_SIZE,stdin);
		fprintf(stderr,"[%06d-%03d] %02x %02x %02x\n",count,bytes_read,free_bytes[0],free_bytes[1],free_bytes[2]);
		if (bytes_read!=188) {
			hdr.timestamp = getmsec()*90;
			sendrtp2(socketOut,&sOut,&hdr,buf,free_bytes-PACKET_SIZE-buf);
			break;
		}
		if ((free_bytes+PACKET_SIZE-buf)>MTU) {
			hdr.timestamp = getmsec()*90;
			sendrtp2(socketOut,&sOut,&hdr,buf,free_bytes-buf);
			free_bytes = buf;
/*			sleep(1);*/
		}
		free_bytes+=PACKET_SIZE;
		count++;
	}

	close(socketOut);

	exit(0);
}