Commit 6e25ed64 authored by Ripinder Singh's avatar Ripinder Singh
Browse files

Merge branch '1438-rtpdump-is-not-rtpdump_refactored' into '1438-rtpdump-is-not-rtpdump'

Refactor all rtpdump functionality inside RTP file wrapper

See merge request !2574
parents 5169f5ae 05a0b6a6
Loading
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -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;
+77 −125
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -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;
}
+2 −9
Original line number Diff line number Diff line
@@ -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;

+1 −1
Original line number Diff line number Diff line
@@ -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 ) &&
+1 −1
Original line number Diff line number Diff line
@@ -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
Loading