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

The "Encode" media processing resource. More...

#include <MprEncode.h>

Inheritance diagram for MprEncode:
Inheritance graph
[legend]
Collaboration diagram for MprEncode:
Collaboration graph
[legend]

Public Member Functions

Creators
 MprEncode (const UtlString &rName)
 
virtual ~MprEncode ()
 
Accessors
UtlContainableType getContainableType () const
 
- Public Member Functions inherited from MpAudioResource
 MpAudioResource (const UtlString &rName, int minInputs, int maxInputs, int minOutputs, int maxOutputs)
 Constructor. More...
 
virtual ~MpAudioResource ()
 Destructor. More...
 
virtual UtlBoolean processFrame (void)
 Wrapper around doProcessFrame(). More...
 
- Public Member Functions inherited from MpResource
 MpResource (const UtlString &rName, int minInputs, int maxInputs, int minOutputs, int maxOutputs)
 Constructor. More...
 
virtual ~MpResource ()
 Destructor. More...
 
virtual UtlBoolean disable ()
 Disable this resource. More...
 
virtual UtlBoolean enable ()
 Enable this resource. More...
 
virtual void reset ()
 This method is invoked for resources that care about stream discontinuities. More...
 
UtlBoolean handleMessages (OsMsgQ &msgQ)
 Handles a queue full of incoming messages for this media processing object. More...
 
void setVisitState (int newState)
 Sets the visit state for this resource. More...
 
OsStatus sendNotification (MpResNotificationMsg::RNMsgType msgType)
 Send a notification with the given message type if notifications are enabled. More...
 
OsStatus sendNotification (MpResNotificationMsg &msg)
 Send the given notification message if notifications are enabled. More...
 
virtual void setConnectionId (MpConnectionID connectionId)
 Set the ID of a connection this resource belongs to. More...
 
virtual void setStreamId (int streamId)
 Set the ID of a stream inside of the connection this resource belongs to. More...
 
virtual int getStreamId (void)
 Get the ID of a stream within the connection this resource belongs to. More...
 
virtual OsStatus pushBuffer (int inputPort, MpBufPtr &inputBuffer)
 Receive buffer asynchronously from resource at given input port. More...
 
virtual void reassignSSRC (void)
 Broadcast announcement that we are changing our SSRC. More...
 
MpFlowGraphBasegetFlowGraph () const
 Returns parent flowgraph. More...
 
void getInputInfo (int inPortIdx, MpResource *&rpUpstreamResource, int &rUpstreamPortIdx)
 Returns information about the upstream end of a connection. More...
 
const UtlString & getName () const
 Returns the name associated with this resource. More...
 
void getOutputInfo (int outPortIdx, MpResource *&rpDownstreamResource, int &rDownstreamPortIdx)
 Returns information about the downstream end of a connection. More...
 
int getVisitState ()
 Returns the current visit state for this resource. More...
 
int maxInputs () const
 Returns the maximum number of inputs supported by this resource. More...
 
int maxOutputs () const
 Returns the maximum number of outputs supported by this resource. More...
 
int minInputs () const
 Returns the minimum number of inputs required by this resource. More...
 
int minOutputs () const
 Returns the minimum number of outputs required by this resource. More...
 
int numInputs () const
 Returns the number of resource inputs that are currently connected. More...
 
int numOutputs () const
 Returns the number of resource outputs that are currently connected. More...
 
virtual MpConnectionID getConnectionId (void) const
 Get the ID of a connection this resource belongs to. More...
 
int reserveFirstUnconnectedInput ()
 Find the first unconnected input port and reserve it. More...
 
int reserveFirstUnconnectedOutput ()
 Find the first unconnected output port and reserve it. More...
 
UtlContainableType getContainableType () const
 Get the ContainableType for a UtlContainable derived class. More...
 
virtual OsStatus getCurrentLatency (int &latency, int input=0, int output=0) const
 Get current input to output latency (in samples) More...
 
UtlBoolean isEnabled () const
 Returns TRUE is this resource is currently enabled, FALSE otherwise. More...
 
UtlBoolean isInputConnected (int portIdx)
 Returns TRUE if portIdx is valid and the indicated input is connected, FALSE otherwise. More...
 
UtlBoolean isInputUnconnected (int portIdx)
 Returns TRUE if portIdx is valid and the indicated input is not connected, FALSE otherwise. More...
 
UtlBoolean isOutputConnected (int portIdx)
 Returns TRUE if portIdx is valid and the indicated output is connected, FALSE otherwise. More...
 
UtlBoolean isOutputUnconnected (int portIdx)
 Returns TRUE if portIdx is valid and the indicated output is not connected, FALSE otherwise. More...
 
UtlBoolean areNotificationsEnabled () const
 Returns TRUE if notification sending is enabled on this resource. More...
 
virtual UtlBoolean isAsynchInput (int inputIndex)
 Takes asynchronous input (pushBuffer). More...
 

Static Public Attributes

static const UtlContainableType TYPE = "MprEncode"
 Class name, used for run-time checks. More...
 
- Static Public Attributes inherited from MpResource
static const UtlContainableType TYPE = "MpResource"
 Class name, used for run-time checks. More...
 

Protected Member Functions

virtual UtlBoolean doProcessFrame (MpBufPtr inBufs[], MpBufPtr outBufs[], int inBufsSize, int outBufsSize, UtlBoolean isEnabled, int samplesPerFrame, int samplesPerSecond)
 This method does the real work for the media processing resource and must be defined in each class derived from this one. More...
 
- Protected Member Functions inherited from MpResource
virtual UtlBoolean handleMessage (MpFlowGraphMsg &fgMsg)
 Handles an incoming flowgraph message for this media processing object. More...
 
virtual UtlBoolean handleEnable ()
 perform the enable operation on the resource More...
 
virtual UtlBoolean handleDisable ()
 perform the disable operation on the resource More...
 
void setInputBuffer (int inPortIdx, const MpBufPtr &pBuf)
 If there already is a buffer stored for this input port, delete it. Then store pBuf for the indicated input port. More...
 
OsStatus postMessage (MpFlowGraphMsg &rMsg)
 Post a message from this resource. More...
 
OsStatus postMessage (MpResourceMsg &rMsg)
 Post a message for this resource. More...
 
UtlBoolean pushBufferDownsream (int outPortIdx, const MpBufPtr &pBuf)
 Makes pBuf available to resource connected to the outPortIdx output port of this resource. More...
 
virtual OsStatus setFlowGraph (MpFlowGraphBase *pFlowGraph)
 Associates this resource with the indicated flow graph. More...
 
virtual OsStatus setNotificationsEnabled (UtlBoolean enable)
 Sets whether or not this resource should send notifications. More...
 
virtual UtlBoolean connectInput (MpResource &rFrom, int fromPortIdx, int toPortIdx)
 Connects the toPortIdx input port on this resource to the fromPortIdx output port of the rFrom resource. More...
 
virtual UtlBoolean connectOutput (MpResource &rTo, int toPortIdx, int fromPortIdx)
 Connects the fromPortIdx output port on this resource to the toPortIdx input port of the rTo resource. More...
 
virtual UtlBoolean disconnectInput (int inPortIdx)
 Removes the connection to the inPortIdx input port of this resource. More...
 
virtual UtlBoolean disconnectOutput (int outPortIdx)
 Removes the connection to the outPortIdx output port of this resource. More...
 
void setName (const UtlString &rName)
 Sets the name that is associated with this resource. More...
 

Private Types

enum  AddlResMsgTypes {
  MPRM_DESELECT_CODECS = MpResourceMsg::MPRM_EXTERNAL_MESSAGE_START, MPRM_START_TONE, MPRM_STOP_TONE, MPRM_SET_MAX_PACKET_TIME,
  MPRM_ENABLE_DTX, MPRM_DISABLE_DTX
}
 
enum  { TONE_STOP_PACKETS = 3, HANGOVER_PACKETS = 25 }
 

Private Member Functions

virtual UtlBoolean handleMessage (MpResourceMsg &rMsg)
 Handle resource messages for this resource. More...
 
OsStatus allocPacketBuffer (const MpEncoderBase &rEncoder, unsigned char *&rpPacketPayload, int &rPacketPayloadBytes)
 Allocate memory for RTP packet. More...
 
void handleSelectCodecs (int newCodecsCount, SdpCodec **newCodecs)
 
void handleDeselectCodecs (void)
 
int lookupTone (int toneId)
 Translate our tone ID into RFC2833 values. More...
 
void handleStartTone (int toneId)
 Handle message to send "begin tone" DTMF RTP packet. More...
 
void handleEnableDTX (UtlBoolean dtx)
 Handle message to enable or disable internal DTX. More...
 
void handleSetMaxPacketTime (unsigned maxPacketTime)
 Handle message to set maximum duration of one packet. More...
 
void handleStopTone (void)
 Handle message to send "stop tone" DTMF RTP packet. More...
 
void doPrimaryCodec (MpAudioBufPtr in)
 Encode audio buffer and send it. More...
 
void doDtmfCodec (int samplesPerFrame, int samplesPerSecond)
 Encode and send DTMF tone. More...
 
void notifyStartTx ()
 Send notification about TX start. More...
 
void notifyStopTx ()
 Send notification about TX stop. More...
 
 MprEncode (const MprEncode &rMprEncode)
 Copy constructor (not implemented for this class) More...
 
MprEncodeoperator= (const MprEncode &rhs)
 Assignment operator (not implemented for this class) More...
 

Private Attributes

Audio codec state variables
MpEncoderBasempPrimaryCodec
 
unsigned char * mpPacket1Payload
 Packet buffer for primary RTP stream. More...
 
int mMaxPacketSamples
 Maximum number of samples in RTP packet. More...
 
int mPacket1PayloadBytes
 Size of mpPacket1Payload buffer. More...
 
int mPayloadBytesUsed
 
unsigned int mSamplesPacked
 
unsigned int mStartTimestamp1
 Timestamp of packets being encoded. More...
 
UtlBoolean mActiveAudio1
 Does current RTP packet contain active voice? More...
 
UtlBoolean mMarkNext1
 Set Mark bit on next RTP packet. More...
 
int mConsecutiveInactive1
 Number of RTP packets with active voice data. More...
 
int mConsecutiveActive1
 
int mConsecutiveUnsentFrames1
 
UtlBoolean mDoesVad1
 Does codec its own VAD? More...
 
UtlBoolean mDisableDTX
 Disable internal DTX. More...
 
UtlBoolean mEnableG722Hack
 
UtlBoolean mDoG722Hack
 
Resampler-related variables.
UtlBoolean mNeedResample
 Is resampling needed? More...
 
MpResamplerBasempResampler
 
unsigned int mResampleBufLen
 Length of mpResampleBuf. More...
 
MpAudioSamplempResampleBuf
 
DTMF codec state variables
MpEncoderBasempDtmfCodec
 
unsigned char * mpPacket2Payload
 packet buffer for DTMF event RTP stream More...
 
int mPacket2PayloadBytes
 4 More...
 
unsigned int mStartTimestamp2
 sample time when tone starts More...
 
unsigned int mLastDtmfSendTimestamp
 
int mDtmfSampleInterval
 

samples between AVT packets

More...
 
int mCurrentTone
 AVT event code for current tone. More...
 
int mNumToneStops
 set to # of end packets to send when tone stops More...
 
int mTotalTime
 

samples tone was active, set when tone stops

More...
 
UtlBoolean mNewTone
 set when tone starts More...
 
General encoding state
unsigned int mCurrentTimestamp
 
unsigned int mMaxPacketTime
 Maximum duration of one packet in milliseconds. More...
 
MprToNetmpToNet
 

Static Private Attributes

static const int RTP_KEEP_ALIVE_FRAME_INTERVAL = 1000
 

Manipulators

void setMyToNet (MprToNet *myToNet)
 Set ToNet resource which will send generated RTP packets. More...
 
static OsStatus selectCodecs (const UtlString &namedResource, OsMsgQ &fgQ, SdpCodec *pPrimaryCodec, SdpCodec *pDtmfCodec)
 Tell encoder which codecs to use. More...
 
static OsStatus deselectCodecs (const UtlString &namedResource, OsMsgQ &fgQ)
 Free selected codecs. More...
 
static OsStatus startTone (const UtlString &namedResource, OsMsgQ &fgQ, int toneId)
 Send "begin tone" DTMF RTP packet. More...
 
static OsStatus stopTone (const UtlString &namedResource, OsMsgQ &fgQ)
 Send "stop tone" DTMF RTP packet. More...
 
static OsStatus enableDtx (const UtlString &namedResource, OsMsgQ &fgQ, UtlBoolean dtx)
 Enable or disable internal DTX. More...
 
static OsStatus setMaxPacketTime (const UtlString &namedResource, OsMsgQ &fgQ, unsigned int maxPacketTime)
 Set maximum duration of one packet in milliseconds. More...
 

Additional Inherited Members

- Public Types inherited from MpResource
enum  VisitState { NOT_VISITED, IN_PROGRESS, FINISHED }
 Graph traversal states that are used when running a topological sort to order resources within a flow graph. More...
 
enum  { ASSOCIATED_LATENCY =-1, INF_LATENCY =-1 }
 
- Static Public Member Functions inherited from MpResource
static OsStatus disable (const UtlString &namedResource, OsMsgQ &fgQ)
 Post a message to disable the resource named. More...
 
static OsStatus enable (const UtlString &namedResource, OsMsgQ &fgQ)
 Post a message to enable the resource named. More...
 
static OsStatus setNotificationsEnabled (UtlBoolean enable, const UtlString &namedResource, OsMsgQ &fgQ)
 Post a message to enable or disable resource notifications on the named resource. More...
 
static void resourceInfo (MpResource *pResource, int index)
 Displays information on the console about the specified resource. More...
 
- Protected Attributes inherited from MpResource
MpFlowGraphBasempFlowGraph
 flow graph this resource belongs to More...
 
MpConnectionID mConnectionId
 The ID of connection this resource belongs to. More...
 
int mStreamId
 
UtlBoolean mIsEnabled
 TRUE if resource is enabled, FALSE otherwise. More...
 
OsRWMutex mRWMutex
 reader/writer lock for synchronization More...
 
MpBufPtrmpInBufs
 input buffers for this resource More...
 
MpBufPtrmpOutBufs
 output buffers for this resource More...
 
ConnmpInConns
 input connections for this resource More...
 
ConnmpOutConns
 output connections for this resource More...
 
int mMaxInputs
 maximum number of inputs More...
 
int mMaxOutputs
 maximum number of outputs More...
 
int mMinInputs
 number of required inputs More...
 
int mMinOutputs
 number of required outputs More...
 
int mNumActualInputs
 actual number of connected inputs More...
 
int mNumActualOutputs
 actual number of connected outputs More...
 
int mVisitState
 (used by flow graph topological sort alg.) More...
 
UtlBoolean mNotificationsEnabled
 Whether we should send notifications or not. More...
 
OsBSem mLock
 used mainly to make safe changes to ports More...
 
- Static Protected Attributes inherited from MpResource
static const OsTime sOperationQueueTimeout = OsTime::OS_INFINITY
 The timeout for message operations for all resources when posting to the flowgraph queue. More...
 

Detailed Description

The "Encode" media processing resource.

Member Enumeration Documentation

enum AddlResMsgTypes
private
Enumerator
MPRM_DESELECT_CODECS 
MPRM_START_TONE 
MPRM_STOP_TONE 
MPRM_SET_MAX_PACKET_TIME 
MPRM_ENABLE_DTX 
MPRM_DISABLE_DTX 
anonymous enum
private
Enumerator
TONE_STOP_PACKETS 

MUST BE > 0.

HANGOVER_PACKETS 

At 20 ms each, 500 ms.

Constructor & Destructor Documentation

MprEncode ( const UtlString &  rName)
~MprEncode ( )
virtual
MprEncode ( const MprEncode rMprEncode)
private

Copy constructor (not implemented for this class)

Member Function Documentation

OsStatus selectCodecs ( const UtlString &  namedResource,
OsMsgQ &  fgQ,
SdpCodec *  pPrimaryCodec,
SdpCodec *  pDtmfCodec 
)
static

Tell encoder which codecs to use.

OsStatus deselectCodecs ( const UtlString &  namedResource,
OsMsgQ &  fgQ 
)
static

Free selected codecs.

void setMyToNet ( MprToNet myToNet)

Set ToNet resource which will send generated RTP packets.

Warning
This method is not synchronous! I.e. it directly modifies resource structure without message passing.
OsStatus startTone ( const UtlString &  namedResource,
OsMsgQ &  fgQ,
int  toneId 
)
static

Send "begin tone" DTMF RTP packet.

OsStatus stopTone ( const UtlString &  namedResource,
OsMsgQ &  fgQ 
)
static

Send "stop tone" DTMF RTP packet.

OsStatus enableDtx ( const UtlString &  namedResource,
OsMsgQ &  fgQ,
UtlBoolean  dtx 
)
static

Enable or disable internal DTX.

Note
Codec still may use its DTX features.
OsStatus setMaxPacketTime ( const UtlString &  namedResource,
OsMsgQ &  fgQ,
unsigned int  maxPacketTime 
)
static

Set maximum duration of one packet in milliseconds.

UtlContainableType getContainableType ( ) const

UtlBoolean doProcessFrame ( MpBufPtr  inBufs[],
MpBufPtr  outBufs[],
int  inBufsSize,
int  outBufsSize,
UtlBoolean  isEnabled,
int  samplesPerFrame,
int  samplesPerSecond 
)
protectedvirtual

This method does the real work for the media processing resource and must be defined in each class derived from this one.

Parameters
inBufs(in) array of pointers to input buffers for the resource
outBufs(out) array of pointers to output buffers produce by the resource
inBufsSize(in) size of the inBufs array
outBufsSize(in) size of the outBufs array
isEnabled(in) indicates whether this resource has been enabled
samplesPerFrame(in) samples to produce per frame processing interval
samplesPerSecond(in) samples to produce per second
Returns
TRUE if successful, FALSE otherwise.

Implements MpAudioResource.

UtlBoolean handleMessage ( MpResourceMsg rMsg)
privatevirtual

Handle resource messages for this resource.

Reimplemented from MpResource.

OsStatus allocPacketBuffer ( const MpEncoderBase rEncoder,
unsigned char *&  rpPacketPayload,
int &  rPacketPayloadBytes 
)
private

Allocate memory for RTP packet.

void handleSelectCodecs ( int  newCodecsCount,
SdpCodec **  newCodecs 
)
private
void handleDeselectCodecs ( void  )
private
int lookupTone ( int  toneId)
private

Translate our tone ID into RFC2833 values.

void handleStartTone ( int  toneId)
private

Handle message to send "begin tone" DTMF RTP packet.

void handleEnableDTX ( UtlBoolean  dtx)
private

Handle message to enable or disable internal DTX.

void handleSetMaxPacketTime ( unsigned  maxPacketTime)
private

Handle message to set maximum duration of one packet.

void handleStopTone ( void  )
private

Handle message to send "stop tone" DTMF RTP packet.

void doPrimaryCodec ( MpAudioBufPtr  in)
private

Encode audio buffer and send it.

void doDtmfCodec ( int  samplesPerFrame,
int  samplesPerSecond 
)
private

Encode and send DTMF tone.

void notifyStartTx ( )
private

Send notification about TX start.

void notifyStopTx ( )
private

Send notification about TX stop.

MprEncode& operator= ( const MprEncode rhs)
private

Assignment operator (not implemented for this class)

Member Data Documentation

const UtlContainableType TYPE = "MprEncode"
static

Class name, used for run-time checks.

const int RTP_KEEP_ALIVE_FRAME_INTERVAL = 1000
staticprivate
MpEncoderBase* mpPrimaryCodec
private
unsigned char* mpPacket1Payload
private

Packet buffer for primary RTP stream.

int mMaxPacketSamples
private

Maximum number of samples in RTP packet.

int mPacket1PayloadBytes
private

Size of mpPacket1Payload buffer.

int mPayloadBytesUsed
private

Number of bytes in mpPacket1Payload, already filled with encoded data

unsigned int mSamplesPacked
private

Number of samples already encoded to current packet.

unsigned int mStartTimestamp1
private

Timestamp of packets being encoded.

UtlBoolean mActiveAudio1
private

Does current RTP packet contain active voice?

UtlBoolean mMarkNext1
private

Set Mark bit on next RTP packet.

int mConsecutiveInactive1
private

Number of RTP packets with active voice data.

int mConsecutiveActive1
private
int mConsecutiveUnsentFrames1
private
UtlBoolean mDoesVad1
private

Does codec its own VAD?

UtlBoolean mDisableDTX
private

Disable internal DTX.

UtlBoolean mEnableG722Hack
private

Should we cheat with RTP clock rate in case G.722 is selected. According to section 4.5.2 of RFC 3551 we should use 8K RTP clock rate instead of 16K for G.722 because of historical error in RFC 1890.

UtlBoolean mDoG722Hack
private

Should we apply RTP clock rate halving to workaround G.722 spec bug? See mEnableG722Hack for better description.

UtlBoolean mNeedResample
private

Is resampling needed?

MpResamplerBase* mpResampler
private

Resampler to convert flowgraph sample rate to codec's sample rate.

unsigned int mResampleBufLen
private

Length of mpResampleBuf.

MpAudioSample* mpResampleBuf
private

Temporary buffer used to store resampled audio samples before passing them to encoder.

MpEncoderBase* mpDtmfCodec
private
unsigned char* mpPacket2Payload
private

packet buffer for DTMF event RTP stream

int mPacket2PayloadBytes
private

4

unsigned int mStartTimestamp2
private

sample time when tone starts

unsigned int mLastDtmfSendTimestamp
private
int mDtmfSampleInterval
private

samples between AVT packets

int mCurrentTone
private

AVT event code for current tone.

int mNumToneStops
private

set to # of end packets to send when tone stops

int mTotalTime
private

samples tone was active, set when tone stops

UtlBoolean mNewTone
private

set when tone starts

unsigned int mCurrentTimestamp
private
unsigned int mMaxPacketTime
private

Maximum duration of one packet in milliseconds.

MprToNet* mpToNet
private

Pointer to ToNet resource, which will send generated RTP packets.