sipxmedialib  Version 3.3
Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
MpJitterBuffer Class Reference

Class for decoding of incoming RTP, resampling it to target sample rate and slicing to frames of target size. More...

#include <MpJitterBuffer.h>

Collaboration diagram for MpJitterBuffer:
Collaboration graph
[legend]

Public Member Functions

Creators
 MpJitterBuffer (MpDecoderPayloadMap *pPayloadMap=NULL, const UtlString &resourceName="unknown")
 Constructor. More...
 
void init (unsigned int samplesPerSec, unsigned int samplesPerFrame)
 Initialize with given sample rate and frame size. More...
 
 ~MpJitterBuffer ()
 Destructor. More...
 
Manipulators
void reset ()
 Reset class to the initial state, preparing for handling new stream. More...
 
OsStatus pushPacket (const MpRtpBufPtr &rtpPacket, int minBufferSamples, int wantedBufferSamples, int &decodedSamples, int &adjustment, UtlBoolean &played)
 Push packet into decoder buffer. More...
 
void getFrame (MpAudioBufPtr &pFrame, int &numOriginalSamples)
 Get next frame from decoder buffer. More...
 
void setCodecList (MpDecoderPayloadMap *pPayloadMap)
 Update list of available decoders. More...
 
void setPlc (const UtlString &plcName)
 Change PLC algorithm to one provided. More...
 
Accessors
void setFlowGraph (MpFlowGraphBase *pFlowgraph)
 Set the pointer to the parent flowgraph for debug purposes. More...
 
int getSamplesNum () const
 Get number of samples, remaining in buffer. More...
 
OsStatus setVadParam (const UtlString &name, int value)
 Set parameter on the VAD component. More...
 

Protected Member Functions

int adjustStream (MpAudioSample *pBuffer, int bufferSize, unsigned numSamples, int wantedAdjustment)
 Reduce or extend audio fragment. More...
 
OsStatus sliceToFrames (int decodedSamples, int codecSampleRate, const MpSpeechParams &speechParams)
 

Private Types

enum  { FRAMES_TO_STORE = 32, DECODED_DATA_MAX_LENGTH = 10 * 160 }
 

Private Member Functions

 MpJitterBuffer (const MpJitterBuffer &rMpJitterBuffer)
 Copy constructor. More...
 
MpJitterBufferoperator= (const MpJitterBuffer &rhs)
 Assignment operator. More...
 

Private Attributes

UtlString mPlcName
 Packet Loss Concealer algorithm name. More...
 
MpPlcBasempPlc
 Packet Loss Concealer instance. More...
 
MpVadBasempVad
 Voice Activity Detector instance. More...
 
MpAgcBasempAgc
 Automatic Gain Calculator instance. More...
 
MpFlowGraphBasempFlowGraph
 Parent flowgraph for debugging. More...
 
Resampler variables.
int mStreamSampleRate
 Sample rate of incoming RTP stream. More...
 
int mOutputSampleRate
 
int mSamplesPerFrame
 Number of samples to put to output buffers. More...
 
MpAudioSample mDecodedData [DECODED_DATA_MAX_LENGTH]
 temporarily store decoded data. More...
 
MpResamplerBasempResampler
 
Audio buffers variables.
unsigned mCurFrameNum
 
unsigned mRemainingSamplesNum
 
MpAudioBufPtr mFrames [FRAMES_TO_STORE]
 Buffer for decoded, resampled and sliced audio. More...
 
int mOriginalSamples [FRAMES_TO_STORE]
 before resampling was done. More...
 
Decoding related variables.
UtlBoolean mIsFirstPacket
 Have we received our first packet or not. More...
 
RtpSeq mStreamSeq
 Sequence number of last played RTP packet. More...
 
RtpTimestamp mStreamTimestamp
 Timestamp of last played RTP packet. More...
 
uint8_t mStreamRtpPayload
 Payload type of last received RTP packet. More...
 
MpDecoderPayloadMapmpPayloadMap
 
unsigned mSamplesPerPacket
 Number of samples in RTP packet. More...
 

Detailed Description

Class for decoding of incoming RTP, resampling it to target sample rate and slicing to frames of target size.

This class is not thread-safe. For thread-safety it relies on external synchronization mechanisms in MprDecode.

Member Enumeration Documentation

anonymous enum
private
Enumerator
FRAMES_TO_STORE 

Number of frames to store in buffer. Should be a power of 2.

DECODED_DATA_MAX_LENGTH 

Size of mDecodedData temporary buffer.

Constructor & Destructor Documentation

MpJitterBuffer ( MpDecoderPayloadMap pPayloadMap = NULL,
const UtlString &  resourceName = "unknown" 
)

Constructor.

Parameters
[in]pPayloadMap- set of decoders, mapped to their RTP payload types.
[in]resourceName- name of the resource that this is part of. Used for debugging and logging purposes.

Destructor.

MpJitterBuffer ( const MpJitterBuffer rMpJitterBuffer)
private

Copy constructor.

Member Function Documentation

void init ( unsigned int  samplesPerSec,
unsigned int  samplesPerFrame 
)

Initialize with given sample rate and frame size.

Should be called only once upon construction of an object.

void reset ( )

Reset class to the initial state, preparing for handling new stream.

OsStatus pushPacket ( const MpRtpBufPtr rtpPacket,
int  minBufferSamples,
int  wantedBufferSamples,
int &  decodedSamples,
int &  adjustment,
UtlBoolean &  played 
)

Push packet into decoder buffer.

Packet will be decoded and decoded data will be copied to internal buffer. If no decoder is available for this packet's payload type packet will be ignored.

Parameters
[in]rtpPacket- RTP packet to be decoded.
[in]minBufferSamples- minimum number of samples to remain in buffer after decoding and applying adjustment. This is useful, when we do not want to decode more packets at this moment, but need a frame of audio for processing.
[in]wantedBufferSamples- number of samples we want to have in buffer after decoding packet. Note, this value may be negative - this means we want to shorten our buffer as much as possible.
[out]decodedSamples- number of samples, decoded from packet.
[out]adjustment- how many samples were added or removed from stream to fulfil wantedBufferSamples request.
[out]played- was passed RTP packet decoded and added to decoder buffer, or it was dropped or used just to update PLC history. I.e. if played=FALSE then number of samples in buffer was not increased.
Note
This implementation behave unpredictable if packets come reordered.
Valid RTP packet MUST be passed with first call to this function.
Return values
OS_SUCCESSif RTP packet was successfully decoded.
OS_FAILEDin case of any problems.
void getFrame ( MpAudioBufPtr pFrame,
int &  numOriginalSamples 
)

Get next frame from decoder buffer.

Note
pFrame must be NULL before passing to this method!
Parameters
[out]pFrame- pointer to returned frame.
[out]numOriginalSamples- number of samples in returned frame would be without resampling.
void setCodecList ( MpDecoderPayloadMap pPayloadMap)

Update list of available decoders.

void setPlc ( const UtlString &  plcName)

Change PLC algorithm to one provided.

void setFlowGraph ( MpFlowGraphBase pFlowgraph)

Set the pointer to the parent flowgraph for debug purposes.

int getSamplesNum ( ) const
inline

Get number of samples, remaining in buffer.

OsStatus setVadParam ( const UtlString &  name,
int  value 
)

Set parameter on the VAD component.

int adjustStream ( MpAudioSample pBuffer,
int  bufferSize,
unsigned  numSamples,
int  wantedAdjustment 
)
protected

Reduce or extend audio fragment.

OsStatus sliceToFrames ( int  decodedSamples,
int  codecSampleRate,
const MpSpeechParams speechParams 
)
protected
MpJitterBuffer& operator= ( const MpJitterBuffer rhs)
private

Assignment operator.

Member Data Documentation

int mStreamSampleRate
private

Sample rate of incoming RTP stream.

int mOutputSampleRate
private

Output sample rate for decoded data. Samples from codecs with different sample rates will be resampled to this sample rate.

int mSamplesPerFrame
private

Number of samples to put to output buffers.

MpAudioSample mDecodedData[DECODED_DATA_MAX_LENGTH]
private

temporarily store decoded data.

Buffer, used to

MpResamplerBase* mpResampler
private

Resampler instance to convert codec sample rate to flowgraph sample rate.

unsigned mCurFrameNum
private

Internal sequence number of oldest frame in mFrames[]. It is also used as a base for frames index calculation.

unsigned mRemainingSamplesNum
private

Total number of samples still residing in mFrames[].

MpAudioBufPtr mFrames[FRAMES_TO_STORE]
private

Buffer for decoded, resampled and sliced audio.

int mOriginalSamples[FRAMES_TO_STORE]
private

before resampling was done.

Numbers of samples in frames

UtlBoolean mIsFirstPacket
private

Have we received our first packet or not.

RtpSeq mStreamSeq
private

Sequence number of last played RTP packet.

RtpTimestamp mStreamTimestamp
private

Timestamp of last played RTP packet.

uint8_t mStreamRtpPayload
private

Payload type of last received RTP packet.

MpDecoderPayloadMap* mpPayloadMap
private

Map of RTP payload types to decoders. Note, we do not own instance of this map, we only store pointer to it.

unsigned mSamplesPerPacket
private

Number of samples in RTP packet.

UtlString mPlcName
private

Packet Loss Concealer algorithm name.

MpPlcBase* mpPlc
private

Packet Loss Concealer instance.

MpVadBase* mpVad
private

Voice Activity Detector instance.

MpAgcBase* mpAgc
private

Automatic Gain Calculator instance.

MpFlowGraphBase* mpFlowGraph
private

Parent flowgraph for debugging.