diff --git a/apps/isar_post_rend.c b/apps/isar_post_rend.c index a55ec0387a48e2b5b11cbd40397f33b192d51943..c3790facbc5ed9d918ff09698f0fa115c595987f 100644 --- a/apps/isar_post_rend.c +++ b/apps/isar_post_rend.c @@ -1180,11 +1180,7 @@ int main( num_in_channels = inBuffer.config.numChannels; numSamplesRead = 0; -#ifdef USE_RTPDUMP - if ( srRTP.dump && splitBinNeedsNewFrame ) -#else if ( srRTP.hRtpFile && splitBinNeedsNewFrame ) -#endif { IVAS_RTP_SR_INFO srInfo = { 0 }; uint32_t rtpTimeStamp = 0, nextPacketRcvTime_ms = 0; diff --git a/lib_util/ivas_rtp_file.c b/lib_util/ivas_rtp_file.c index db39f94673134e9ace8c4044d67db15845312009..99471e5d8876e188ccd75e84b90f5d6d37749627 100644 --- a/lib_util/ivas_rtp_file.c +++ b/lib_util/ivas_rtp_file.c @@ -29,7 +29,6 @@ the United Nations Convention on Contracts on the International Sales of Goods. *******************************************************************************************************/ - #include #include #include @@ -39,14 +38,21 @@ #include "ivas_error_utils.h" #ifdef USE_RTPDUMP #include "rtpdump.h" -#endif -#ifndef USE_RTPDUMP +struct IVAS_RTP_FILE +{ + bool isFileWriter; + RTPDUMP_HANDLE f_rtpstream; + uint32_t offset_ms; +}; + +#else // USE_RTPDUMP struct IVAS_RTP_FILE { bool isFileWriter; FILE *f_rtpstream; }; +#endif // USE_RTPDUMP static ivas_error IvasRtpFile_Open( const char *filePath, /* i : path to CA config file */ @@ -54,6 +60,22 @@ static ivas_error IvasRtpFile_Open( IVAS_RTP_FILE_HANDLE *phRtpFile /* o : pointer to an IVAS file reader handle */ ) { +#ifdef USE_RTPDUMP + RTPDUMP_HANDLE f_rtpstream; + RTPDUMP_ERROR rderr; + if ( isFileWriter ) + { + rderr = RTPDUMP_OpenForWriting( &f_rtpstream, filePath ); + } + else + { + rderr = RTPDUMP_OpenForReading( &f_rtpstream, filePath ); + } + if ( rderr != RTPDUMP_NO_ERROR ) + { + return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "rtpdump open error %d", rderr ); + } +#else // USE_RTPDUMP FILE *f_rtpstream; char *mode = isFileWriter ? "wb" : "rb"; @@ -63,6 +85,7 @@ static ivas_error IvasRtpFile_Open( return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "could not open: %s\n", filePath ); } +#endif // USE_RTPDUMP *phRtpFile = calloc( 1, sizeof( struct IVAS_RTP_FILE ) ); if ( *phRtpFile != NULL ) { @@ -81,7 +104,11 @@ static ivas_error IvasRtpFile_Close( { if ( ( *phReader )->f_rtpstream != NULL ) { +#ifdef USE_RTPDUMP + RTPDUMP_Close( &( *phReader )->f_rtpstream, 1 ); +#else // USE_RTPDUMP fclose( ( *phReader )->f_rtpstream ); +#endif // USE_RTPDUMP ( *phReader )->f_rtpstream = NULL; } free( *phReader ); @@ -99,9 +126,20 @@ static ivas_error IvasRtpFile_Write( ivas_error error = IVAS_ERR_OK; if ( hRtpFile->isFileWriter ) { +#ifdef USE_RTPDUMP + RTPDUMP_RTPPACKET pkt; + RTPDUMP_SetDefaultRtpPacketHeader( &pkt ); + numBytes = ( numBytes > sizeof( pkt.data ) ) ? sizeof( pkt.data ) : numBytes; + memcpy( pkt.data, packet, numBytes ); + RTPDUMP_ParseRTPHeader( &pkt ); + pkt.payloadSize = (unsigned short) ( numBytes - pkt.headerSize ); + /* compute time offset in ms from RTP timestamp (16kHz clock) */ + error = ( RTPDUMP_WritePacket( hRtpFile->f_rtpstream, &pkt, pkt.timeStamp / 16 ) != RTPDUMP_NO_ERROR ) ? IVAS_ERR_FAILED_FILE_WRITE : IVAS_ERR_OK; +#else // USE_RTPDUMP uint32_t length = (uint32_t) numBytes; /* Max packet length is < 32 bits*/ fwrite( &length, sizeof( uint32_t ), 1, hRtpFile->f_rtpstream ); fwrite( packet, sizeof( uint8_t ), numBytes, hRtpFile->f_rtpstream ); +#endif // USE_RTPDUMP } else { @@ -118,12 +156,35 @@ static ivas_error IvasRtpFile_Read( ) { size_t nread = 0; +#ifndef USE_RTPDUMP uint32_t length = 0; +#endif // USE_RTPDUMP if ( hRtpFile->isFileWriter ) { return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "File open for writing cannot be read" ); } +#ifdef USE_RTPDUMP + RTPDUMP_RTPPACKET pd; + RTPDUMP_ERROR rderr = RTPDUMP_ReadPacket( hRtpFile->f_rtpstream, &pd, &hRtpFile->offset_ms ); + if ( rderr == RTPDUMP_READ_ENDOFFILE ) + { + return IVAS_ERR_END_OF_FILE; + } + if ( rderr != RTPDUMP_NO_ERROR ) + { + return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "rtpdump read error %d", rderr ); + } + + /* copy raw data to unpack buffer */ + nread = pd.headerSize + pd.payloadSize; + if ( nread > capacity ) + { + return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE; + } + memcpy( packet, pd.data, nread ); + *numBytes = nread; +#else // USE_RTPDUMP nread = fread( &length, sizeof( uint32_t ), 1, hRtpFile->f_rtpstream ); /* Read Packet Length */ if ( nread == 0 ) { @@ -143,9 +204,10 @@ static ivas_error IvasRtpFile_Read( return IVAS_ERR_END_OF_FILE; } +#endif // USE_RTPDUMP return IVAS_ERR_OK; } -#endif + static const char *const PiDataNames[IVAS_PI_MAX_ID] = { "SCENE_ORIENTATION", "DEVICE_ORIENTATION_COMPENSATED", "DEVICE_ORIENTATION_UNCOMPENSATED", @@ -776,40 +838,6 @@ void IVAS_RTP_Term( { if ( rtp->hPack != NULL ) { -#ifdef USE_RTPDUMP - /* Flush any remaining RTP packets */ - while ( IVAS_RTP_PACK_GetNumFrames( rtp->hPack ) > 0 ) - { - ivas_error error; - uint32_t numFramesInPayload = 0; - error = IVAS_RTP_PACK_GetPacket( rtp->hPack, &rtp->rtpPacket, &numFramesInPayload ); - if ( error != IVAS_ERR_OK ) - { - fprintf( stderr, "\nError %s while packing RTP packet\n", ivas_error_to_string( error ) ); - break; - } - if ( numFramesInPayload > 0 ) - { - RTPDUMP_RTPPACKET pkt; - RTPDUMP_SetDefaultRtpPacketHeader( &pkt ); - - unsigned len = rtp->rtpPacket.length; - if ( len > sizeof( pkt.data ) ) - len = sizeof( pkt.data ); - memcpy( pkt.data, rtp->rtpPacket.buffer, len ); - RTPDUMP_ParseRTPHeader( &pkt ); - pkt.payloadSize = (unsigned short) ( len - pkt.headerSize ); - /* compute time offset in ms from RTP timestamp (16kHz clock) */ - uint32_t offset_ms = pkt.timeStamp / 16; - RTPDUMP_ERROR werr = RTPDUMP_WritePacket( rtp->dump, &pkt, offset_ms ); - if ( werr != RTPDUMP_NO_ERROR ) - { - fprintf( stderr, "\nError writing RTPDump packet: %d\n", werr ); - break; - } - } - } -#else /* Complete the last packet */ if ( IVAS_RTP_PACK_GetNumFrames( rtp->hPack ) != 0 ) { @@ -828,7 +856,6 @@ void IVAS_RTP_Term( } } } -#endif IVAS_RTP_PACK_Close( &rtp->hPack ); } @@ -850,17 +877,10 @@ void IVAS_RTP_Term( rtp->f_piExtOut = NULL; } -#ifdef USE_RTPDUMP - if ( rtp->dump ) - { - RTPDUMP_Close( &rtp->dump, 1 ); - } -#else if ( rtp->hRtpFile != NULL ) { IvasRtpFile_Close( &rtp->hRtpFile ); } -#endif } } @@ -895,19 +915,12 @@ ivas_error IVAS_RTP_WRITER_Init( if ( error == IVAS_ERR_OK ) { /* Open the output file for RTPDump writing */ -#ifdef USE_RTPDUMP - RTPDUMP_ERROR rderr = RTPDUMP_OpenForWriting( &rtp->dump, outputBitstreamFilename ); - if ( rderr != RTPDUMP_NO_ERROR ) - { - return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "rtpdump open error %d", rderr ); - } -#else error = IvasRtpFile_Open( outputBitstreamFilename, true, &rtp->hRtpFile ); if ( error != IVAS_ERR_OK ) { return error; } -#endif + /* initialize RTP packer header sequence only in file-based mode */ #ifdef FIX_1540_EXPOSE_PT_IN_RTP_HEADER_API error = IVAS_RTP_PACK_UpdateHeader( rtp->hPack, payloadType, seqNumInitVal, SSRC, 0, NULL, 0, 0, NULL ); @@ -943,21 +956,11 @@ ivas_error IVAS_RTP_READER_Init( if ( error == IVAS_ERR_OK ) { /* Open the output file for RTPDump writing */ -#ifdef USE_RTPDUMP - { - RTPDUMP_ERROR rderr = RTPDUMP_OpenForReading( &rtp->dump, inputBitstreamFilename ); - if ( rderr != RTPDUMP_NO_ERROR ) - { - return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "rtpdump open failed %d", rderr ); - } - } -#else error = IvasRtpFile_Open( inputBitstreamFilename, false, &rtp->hRtpFile ); if ( error != IVAS_ERR_OK ) { return error; } -#endif if ( piOutputFilename != NULL ) { @@ -1033,41 +1036,7 @@ ivas_error IVAS_RTP_WriteNextFrame( } rtp->nWrittenPiData--; } -#ifdef USE_RTPDUMP - /* flush packets when packet full or forced */ - if ( forcePacket || IVAS_RTP_PACK_GetNumFrames( rtp->hPack ) == rtp->packCfg.maxFramesPerPacket ) - { - while ( 1 ) - { - uint32_t numFramesInPayload = 0; - /* attempt to get a packet from packer */ - ivas_error pkErr = IVAS_RTP_PACK_GetPacket( rtp->hPack, &rtp->rtpPacket, &numFramesInPayload ); - if ( pkErr != IVAS_ERR_OK || numFramesInPayload == 0 ) - { - break; - } - /* write this packet via RTPDump */ - RTPDUMP_RTPPACKET pkt; - memset( &pkt, 0, sizeof( pkt ) ); - unsigned len = rtp->rtpPacket.length; - if ( len > sizeof( pkt.data ) ) - len = sizeof( pkt.data ); - memcpy( pkt.data, rtp->rtpPacket.buffer, len ); - RTPDUMP_ParseRTPHeader( &pkt ); - /* set proper header and payload sizes */ - pkt.headerSize = 12; - pkt.payloadSize = (unsigned short) ( len - pkt.headerSize ); - /* compute time offset in ms from RTP timestamp (16kHz clock) */ - uint32_t offset_ms = pkt.timeStamp / 16; - RTPDUMP_ERROR werr = RTPDUMP_WritePacket( rtp->dump, &pkt, offset_ms ); - if ( werr != RTPDUMP_NO_ERROR ) - { - fprintf( stderr, "\nError writing RTPDump packet: %d\n", werr ); - return IVAS_ERR_FAILED_FILE_OPEN; - } - } - } -#else + if ( forcePacket || IVAS_RTP_PACK_GetNumFrames( rtp->hPack ) == rtp->packCfg.maxFramesPerPacket ) { uint32_t numFramesInPayload = 0; @@ -1085,7 +1054,7 @@ ivas_error IVAS_RTP_WriteNextFrame( return error; } } -#endif + return error; } @@ -1110,36 +1079,11 @@ ivas_error IVAS_RTP_ReadNextFrame( packedFrame.capacity = ( IVAS_MAX_BITS_PER_FRAME / 8 ); if ( rtp->numFramesInPacket == 0 ) { -#ifdef USE_RTPDUMP - /* Read next packet via RTPDUMP library */ - { - RTPDUMP_RTPPACKET pd; - uint32_t offset_ms; - RTPDUMP_ERROR rderr = RTPDUMP_ReadPacket( rtp->dump, &pd, &offset_ms ); - if ( rderr == RTPDUMP_READ_ENDOFFILE ) - return IVAS_ERR_END_OF_FILE; - if ( rderr != RTPDUMP_NO_ERROR ) - { - return IVAS_ERROR( IVAS_ERR_FAILED_FILE_OPEN, "rtpdump read error %d", rderr ); - } - /* copy raw data to unpack buffer */ - unsigned total = pd.headerSize + pd.payloadSize; - if ( total > rtp->rtpPacket.capacity ) - { - return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE; - } - memcpy( rtp->rtpPacket.buffer, pd.data, total ); - rtp->rtpPacket.length = total; - if ( nextPacketRcvTime_ms ) - *nextPacketRcvTime_ms = offset_ms; - } -#else rtp->rtpPacket.length = 0; if ( ( error = IvasRtpFile_Read( rtp->hRtpFile, rtp->rtpPacket.buffer, &rtp->rtpPacket.length, rtp->rtpPacket.capacity ) ) != IVAS_ERR_OK ) { return error; } -#endif if ( ( error = IVAS_RTP_UNPACK_PushPacket( rtp->hUnpack, &rtp->rtpPacket, &rtp->numFramesInPacket, &rtp->numPiDataInPacket, @@ -1203,7 +1147,15 @@ ivas_error IVAS_RTP_ReadNextFrame( *qBit = !rtp->speechLostIndicated; rtp->numFramesInPacket--; rtp->numFramesProduced++; +#ifdef USE_RTPDUMP + if ( nextPacketRcvTime_ms ) + { + *nextPacketRcvTime_ms = rtp->hRtpFile->offset_ms; + } + rtp->hRtpFile->offset_ms += 20; +#else *nextPacketRcvTime_ms += 20; +#endif return IVAS_ERR_OK; } diff --git a/lib_util/ivas_rtp_file.h b/lib_util/ivas_rtp_file.h index ae5e851c181db5e29304f1f4ab2472d3e58aed8a..4483dea95b58926423fc9c8629a929f14ff5bdce 100644 --- a/lib_util/ivas_rtp_file.h +++ b/lib_util/ivas_rtp_file.h @@ -33,22 +33,18 @@ #ifndef IVAS_RTP_FILE_H #define IVAS_RTP_FILE_H -#include "options.h" #include "common_api_types.h" #include "ivas_rtp_api.h" #include "ivas_rtp_pi_data.h" -#ifdef USE_RTPDUMP -#include "rtpdump.h" -#endif + typedef struct IVAS_RTP_FILE *IVAS_RTP_FILE_HANDLE; typedef struct { uint8_t packet[NOMINAL_BUFFER_SIZE( IVAS_MAX_FRAMES_PER_RTP_PACKET )]; IVAS_PIDATA_TS piData[IVAS_PI_MAX_ID * IVAS_MAX_FRAMES_PER_RTP_PACKET]; -#ifndef USE_RTPDUMP + IVAS_RTP_FILE_HANDLE hRtpFile; -#endif FILE *f_piDataOut; FILE *f_piExtOut; char piExtFilename[FILENAME_MAX]; @@ -69,9 +65,6 @@ typedef struct IVAS_RTP_UNPACK_HANDLE hUnpack; IVAS_RTP_PACK_CONFIG packCfg; IVAS_RTP_UNPACK_CONFIG unpackCfg; -#ifdef USE_RTPDUMP - RTPDUMP_HANDLE dump; -#endif IVAS_RTP_SR_INFO srInfo; } IVAS_RTP; diff --git a/lib_util/rtpdump.c b/lib_util/rtpdump.c index 198f2089943292ae64efc19f65f4d4283930c92f..4e6b2e58d41d673d36a6a25227fae2861e893435 100644 --- a/lib_util/rtpdump.c +++ b/lib_util/rtpdump.c @@ -213,7 +213,7 @@ static int writeHeader( struct RTPDUMP *hRTPDUMP ) ip[2] = (unsigned char) ( ( hRTPDUMP->source >> 8 ) & 0xFF ); ip[3] = (unsigned char) ( hRTPDUMP->source & 0xFF ); - fprintf( hRTPDUMP->file, "#!rtpplay%s %03u.%03u.%03u.%03u/%05u\n", "1.0", ip[0], ip[1], ip[2], ip[3], hRTPDUMP->port ); + fprintf( hRTPDUMP->file, "#!rtpplay%s %u.%u.%u.%u/%u\n", "1.0", ip[0], ip[1], ip[2], ip[3], hRTPDUMP->port ); if ( !writeLong( hRTPDUMP->file, hRTPDUMP->startSeconds ) && !writeLong( hRTPDUMP->file, hRTPDUMP->startMicroSeconds ) && diff --git a/tests/rtp/ivasrtp.py b/tests/rtp/ivasrtp.py index b6a546fa4877f9ed792719792cafcf0391b3f996..04805b1096629093cd856393e263fdcaf5674f47 100644 --- a/tests/rtp/ivasrtp.py +++ b/tests/rtp/ivasrtp.py @@ -245,7 +245,7 @@ class RTPHDR: extension: bool = False csrcCount: int = 0 marker: bool = False - payloadType: int = 0 + payloadType: int = 96 sequenceNum: int = 0 timestamp: int = 0 ssrc: int = 0 diff --git a/tests/rtp/test_rtp.py b/tests/rtp/test_rtp.py index 76a97b03be6b3c127deed70135d658ac314ebdd2..05a0258200e1cca6c6c8e6391e39b3e15636406c 100644 --- a/tests/rtp/test_rtp.py +++ b/tests/rtp/test_rtp.py @@ -700,7 +700,7 @@ def run_rtp_bitstream_tests( g192Audio, Fs = readfile(pcmOutG192) decAudio /= 32768.0 # readfile reuturns 16 bit int g192Audio /= 32768.0 # readfile reuturns 16 bit int - decAudio = decAudio[4 * 960 :] + decAudio = decAudio[3 * 960 :] assert abs(decAudio.shape[0] - g192Audio.shape[0]) <= ( 4 * 960 ), "Decoded PCM Audio is not same length as input"