/* read data from source, determine underlying protocol * (if not already known); and process the packet * if needed (for RTP - register packet) * * return the number of octets read from the source * into the buffer */ static ssize_t read_packet( struct dstream_ctx* spc, int fd, char* buf, size_t len ) { ssize_t n = -1; size_t chunk_len = len; assert( spc && buf && len ); assert( fd > 0 ); /* if *RAW* data specified - read AS IS * and exit */ if( UPXDT_RAW == spc->stype ) { return read_buf( fd, buf, len, g_flog ); } /* if it is (or *could* be) RTP, read only MTU bytes */ if( (spc->stype == UPXDT_RTP_TS) || (spc->flags & F_CHECK_FMT) ) chunk_len = (len > spc->mtu) ? spc->mtu : len; #ifdef UDPXY_FILEIO if( spc->flags & F_FILE_INPUT ) { assert( !buf_overrun( buf, len, 0, chunk_len, g_flog ) ); n = read_frecord( fd, buf, chunk_len, &(spc->stype), g_flog ); if( n <= 0 ) return n; } else #endif /* UDPXY_FILEIO */ { assert( !buf_overrun(buf, len, 0, chunk_len, g_flog) ); n = read_buf( fd, buf, chunk_len, g_flog ); if( n <= 0 ) return n; } if( spc->flags & F_CHECK_FMT ) { spc->stype = get_mstream_type( buf, n, g_flog ); switch (spc->stype) { case UPXDT_RTP_TS: /* scattered: exclude RTP headers */ spc->flags |= F_SCATTERED; break; case UPXDT_TS: spc->flags &= ~F_SCATTERED; break; default: spc->stype = UPXDT_RAW; TRACE( (void)tmfputs( "Unrecognized stream type\n", g_flog ) ); break; } /* switch */ TRACE( (void)tmfprintf( g_flog, "Established stream as [%s]\n", fmt2str( spc->stype ) ) ); spc->flags &= ~F_CHECK_FMT; } if( spc->flags & F_SCATTERED ) if( -1 == register_packet( spc, buf, n ) ) return -1; return n; }
/* convert source file into destination format */ static int convert2( const char* srcfile, const char* dstfile, upxfmt_t dfmt, FILE* log ) { char data[ MAX_REC_SIZE ]; ssize_t nrd = -1, nwr = -1; int sfd = -1, dfd = -1; upxfmt_t sfmt = DT_UNKNOWN; double rrec = 0, wrec = 0, rtotal = 0, wtotal = 0; assert( srcfile && log ); assert( ETHERNET_MTU < MAX_REC_SIZE ); sfd = open( srcfile, O_RDONLY ); if( -1 == sfd ) { mperror( log, errno, "srcfile - open" ); return -1; } if( dstfile ) { dfd = creat( dstfile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); if( -1 == dfd ) { mperror( log, errno, "dstfile - open" ); return -1; } } if( dfmt == sfmt ) { (void) tmfprintf( log, "%s: Same format for source and destination: " "writing will be skipped\n", appname); nwr = 0; /* set to 0 not to confuse with an error condition */ } do { nrd = read_frecord( sfd, data, ETHERNET_MTU, &sfmt, log ); /* TRACE( (void)tmfprintf( log, "%s:R nrd=[%ld], nwr=[%ld]\n", __func__, (long)nrd, (long)nwr) ); */ if( nrd <= 0 ) break; ++rrec; rtotal += (double)nrd; TRACE( (void) tmfprintf( log, "Read record [%ld] of [%ld] bytes\n", (long)rrec, (long)nrd ) ); if( (dfd <= 0) || (sfmt == dfmt) ) continue; nwr = write_frecord( dfd, data, nrd, sfmt, dfmt, log ); if( nwr <= 0 ) break; ++wrec; wtotal += (double)nwr; TRACE( (void) tmfprintf( log, "Wrote record [%ld] of [%ld] bytes\n", (long)wrec, (long)nwr ) ); } while( 1 ); if( dfd > 0 ) (void) close( dfd ); if( sfd > 0 ) (void) close( sfd ); (void) tmfprintf( log, "Read records=[%.f], bytes=[%.f], " "wrote records=[%.f], bytes=[%.f]\n", rrec, rtotal, wrec, wtotal ); if( (nrd < 0) || (nwr < 0)) { TRACE( (void)tmfprintf( log, "%s: nrd=[%ld], nwr=[%ld]\n", __func__, (long)nrd, (long)nwr) ); return -1; } return 0; }