From 9a8bc3886a901939264996e701ee587715e6ef3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rafa=C3=ABl=20Carr=C3=A9?=
 <rafael.carre@savoirfairelinux.com>
Date: Thu, 18 Aug 2011 16:35:54 -0400
Subject: [PATCH] * #6675 : send RTP dtmf events only once

---
 .../audio/audiortp/AudioRtpRecordHandler.cpp  | 10 +----
 .../audio/audiortp/AudioRtpRecordHandler.h    | 19 ++------
 daemon/src/audio/audiortp/AudioRtpSession.cpp | 45 +++++++------------
 daemon/src/audio/audiortp/AudioRtpSession.h   |  2 +-
 .../audiortp/AudioSymmetricRtpSession.cpp     |  7 ++-
 .../src/audio/audiortp/AudioZrtpSession.cpp   |  7 ++-
 6 files changed, 26 insertions(+), 64 deletions(-)

diff --git a/daemon/src/audio/audiortp/AudioRtpRecordHandler.cpp b/daemon/src/audio/audiortp/AudioRtpRecordHandler.cpp
index 795bebb4e0..21a44927f7 100644
--- a/daemon/src/audio/audiortp/AudioRtpRecordHandler.cpp
+++ b/daemon/src/audio/audiortp/AudioRtpRecordHandler.cpp
@@ -114,15 +114,7 @@ void AudioRtpRecordHandler::initNoiseSuppress()
 
 void AudioRtpRecordHandler::putDtmfEvent (int digit)
 {
-    sfl::DtmfEvent *dtmf = new sfl::DtmfEvent();
-    dtmf->payload.event = digit;
-    dtmf->payload.ebit = false; // end of event bit
-    dtmf->payload.rbit = false; // reserved bit
-    dtmf->payload.duration = 1; // duration for this event
-    dtmf->newevent = true;
-    dtmf->length = 1000;
-    getEventQueue()->push_back (dtmf);
-    _debug ("AudioRtpSession: Put Dtmf Event %d", digit);
+	_audioRtpRecord._dtmfQueue.push_back(digit);
 }
 
 #ifdef DUMP_PROCESS_DATA_ENCODE
diff --git a/daemon/src/audio/audiortp/AudioRtpRecordHandler.h b/daemon/src/audio/audiortp/AudioRtpRecordHandler.h
index 4709930a6c..7d7343d9f9 100644
--- a/daemon/src/audio/audiortp/AudioRtpRecordHandler.h
+++ b/daemon/src/audio/audiortp/AudioRtpRecordHandler.h
@@ -71,15 +71,6 @@ timeval2microtimeout (const timeval& t)
     return ( (t.tv_sec * 1000000ul) + t.tv_usec);
 }
 
-typedef struct DtmfEvent {
-    ost::RTPPacket::RFC2833Payload payload;
-    int factor;
-    int length;
-    bool newevent;
-} DtmfEvent;
-
-typedef std::list<DtmfEvent *> EventQueue;
-
 /**
  * Class meant to store internal data in order to encode/decode,
  * resample, process, and packetize audio streams. This class should not be
@@ -102,7 +93,7 @@ class AudioRtpRecord
         int _codecSampleRate;
         int _codecFrameSize;
         int _converterSamplingRate;
-        EventQueue _eventQueue;
+        std::list<int> _dtmfQueue;
         SFLDataFormat _micAmplFactor;
         AudioProcessing *_audioProcess;
         NoiseSuppress *_noiseSuppress;
@@ -145,12 +136,8 @@ class AudioRtpRecordHandler
             return _audioRtpRecord._hasDynamicPayloadType;
         }
 
-        EventQueue *getEventQueue (void) {
-            return &_audioRtpRecord._eventQueue;
-        }
-
-        int getEventQueueSize (void) const {
-            return _audioRtpRecord._eventQueue.size();
+        int DtmfPending (void) const {
+            return _audioRtpRecord._dtmfQueue.size() > 0;
         }
 
         const unsigned char *getMicDataEncoded (void) const {
diff --git a/daemon/src/audio/audiortp/AudioRtpSession.cpp b/daemon/src/audio/audiortp/AudioRtpSession.cpp
index 9d7945791d..180ac52de0 100644
--- a/daemon/src/audio/audiortp/AudioRtpSession.cpp
+++ b/daemon/src/audio/audiortp/AudioRtpSession.cpp
@@ -124,12 +124,20 @@ void AudioRtpSession::setSessionMedia (AudioCodec *audioCodec)
 	}
 }
 
-void AudioRtpSession::sendDtmfEvent (sfl::DtmfEvent *dtmf)
+void AudioRtpSession::sendDtmfEvent ()
 {
-	const int increment = (_type == Zrtp) ? 160 : _timestampIncrement;
-    _debug ("AudioRtpSession: Send Dtmf");
+    ost::RTPPacket::RFC2833Payload payload;
 
-    _timestamp += increment;
+    payload.event = _audioRtpRecord._dtmfQueue.front();
+    payload.ebit = false; // end of event bit
+    payload.rbit = false; // reserved bit
+    payload.duration = 1; // duration for this event
+
+    _audioRtpRecord._dtmfQueue.pop_front();
+
+    _debug ("AudioRtpSession: Send RTP Dtmf (%d)", payload.event);
+
+    _timestamp += (_type == Zrtp) ? 160 : _timestampIncrement;
 
     // discard equivalent size of audio
     processDataEncode();
@@ -137,35 +145,12 @@ void AudioRtpSession::sendDtmfEvent (sfl::DtmfEvent *dtmf)
     // change Payload type for DTMF payload
     _queue->setPayloadFormat (ost::DynamicPayloadFormat ( (ost::PayloadType) getDtmfPayloadType(), 8000));
 
-    // Set marker in case this is a new Event
-    if (dtmf->newevent)
-        _queue->setMark (true);
-
-    // putData (_timestamp, (const unsigned char*) (& (dtmf->payload)), sizeof (ost::RTPPacket::RFC2833Payload));
-    _queue->sendImmediate (_timestamp, (const unsigned char *) (& (dtmf->payload)), sizeof (ost::RTPPacket::RFC2833Payload));
-
-    // This is no more a new event
-    if (dtmf->newevent) {
-        dtmf->newevent = false;
-        _queue->setMark (false);
-    }
+    _queue->setMark (true);
+    _queue->sendImmediate (_timestamp, (const unsigned char *) (&payload), sizeof (payload));
+    _queue->setMark (false);
 
     // get back the payload to audio
     _queue->setPayloadFormat (ost::StaticPayloadFormat ( (ost::StaticPayloadType) getCodecPayloadType()));
-
-    // decrease length remaining to process for this event
-    dtmf->length -= increment;
-
-    dtmf->payload.duration++;
-
-    // next packet is going to be the last one
-    if ( (dtmf->length - increment) < increment)
-        dtmf->payload.ebit = true;
-
-    if (dtmf->length < increment) {
-        delete dtmf;
-        getEventQueue()->pop_front();
-    }
 }
 
 
diff --git a/daemon/src/audio/audiortp/AudioRtpSession.h b/daemon/src/audio/audiortp/AudioRtpSession.h
index 1e737196d3..0a609646fc 100644
--- a/daemon/src/audio/audiortp/AudioRtpSession.h
+++ b/daemon/src/audio/audiortp/AudioRtpSession.h
@@ -84,7 +84,7 @@ class AudioRtpSession : public AudioRtpRecordHandler
          * send the appropriate DTMF digit using this payload, discard coresponding data from mainbuffer and get
          * back the codec payload for further audio processing.
          */
-        void sendDtmfEvent (sfl::DtmfEvent *dtmf);
+        void sendDtmfEvent ();
 
         /**
          * Send encoded data to peer
diff --git a/daemon/src/audio/audiortp/AudioSymmetricRtpSession.cpp b/daemon/src/audio/audiortp/AudioSymmetricRtpSession.cpp
index f3e6ea95f3..a2897ef844 100644
--- a/daemon/src/audio/audiortp/AudioSymmetricRtpSession.cpp
+++ b/daemon/src/audio/audiortp/AudioSymmetricRtpSession.cpp
@@ -87,11 +87,10 @@ void AudioSymmetricRtpSession::AudioRtpThread::run()
     while (running) {
 
         // Send session
-        if (rtpSession->getEventQueueSize() > 0) {
-            rtpSession->sendDtmfEvent (rtpSession->getEventQueue()->front());
-        } else {
+        if (rtpSession->DtmfPending())
+            rtpSession->sendDtmfEvent ();
+        else
             rtpSession->sendMicData ();
-        }
 
         Thread::sleep (TimerPort::getTimer());
 
diff --git a/daemon/src/audio/audiortp/AudioZrtpSession.cpp b/daemon/src/audio/audiortp/AudioZrtpSession.cpp
index 3da3ffd955..05c6b260a3 100644
--- a/daemon/src/audio/audiortp/AudioZrtpSession.cpp
+++ b/daemon/src/audio/audiortp/AudioZrtpSession.cpp
@@ -147,11 +147,10 @@ void AudioZrtpSession::run ()
         }
 
         // Send session
-        if (getEventQueueSize() > 0) {
-            sendDtmfEvent (getEventQueue()->front());
-        } else {
+        if (DtmfPending())
+            sendDtmfEvent ();
+        else
             sendMicData ();
-        }
 
         setCancel (cancelDeferred);
         controlReceptionService();
-- 
GitLab