Home | History | Annotate | Download | only in neteq
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_
     12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_
     13 
     14 #include <list>
     15 #include <string>  // size_t
     16 
     17 #include "webrtc/base/constructormagic.h"
     18 #include "webrtc/typedefs.h"
     19 
     20 namespace webrtc {
     21 
     22 struct DtmfEvent {
     23   uint32_t timestamp;
     24   int event_no;
     25   int volume;
     26   int duration;
     27   bool end_bit;
     28 
     29   // Constructors
     30   DtmfEvent()
     31       : timestamp(0),
     32         event_no(0),
     33         volume(0),
     34         duration(0),
     35         end_bit(false) {
     36   }
     37   DtmfEvent(uint32_t ts, int ev, int vol, int dur, bool end)
     38       : timestamp(ts),
     39         event_no(ev),
     40         volume(vol),
     41         duration(dur),
     42         end_bit(end) {
     43   }
     44 };
     45 
     46 // This is the buffer holding DTMF events while waiting for them to be played.
     47 class DtmfBuffer {
     48  public:
     49   enum BufferReturnCodes {
     50     kOK = 0,
     51     kInvalidPointer,
     52     kPayloadTooShort,
     53     kInvalidEventParameters,
     54     kInvalidSampleRate
     55   };
     56 
     57   // Set up the buffer for use at sample rate |fs_hz|.
     58   explicit DtmfBuffer(int fs_hz) {
     59     SetSampleRate(fs_hz);
     60   }
     61 
     62   virtual ~DtmfBuffer() {}
     63 
     64   // Flushes the buffer.
     65   virtual void Flush() { buffer_.clear(); }
     66 
     67   // Static method to parse 4 bytes from |payload| as a DTMF event (RFC 4733)
     68   // and write the parsed information into the struct |event|. Input variable
     69   // |rtp_timestamp| is simply copied into the struct.
     70   static int ParseEvent(uint32_t rtp_timestamp,
     71                         const uint8_t* payload,
     72                         int payload_length_bytes,
     73                         DtmfEvent* event);
     74 
     75   // Inserts |event| into the buffer. The method looks for a matching event and
     76   // merges the two if a match is found.
     77   virtual int InsertEvent(const DtmfEvent& event);
     78 
     79   // Checks if a DTMF event should be played at time |current_timestamp|. If so,
     80   // the method returns true; otherwise false. The parameters of the event to
     81   // play will be written to |event|.
     82   virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event);
     83 
     84   // Number of events in the buffer.
     85   virtual size_t Length() const { return buffer_.size(); }
     86 
     87   virtual bool Empty() const { return buffer_.empty(); }
     88 
     89   // Set a new sample rate.
     90   virtual int SetSampleRate(int fs_hz);
     91 
     92  private:
     93   typedef std::list<DtmfEvent> DtmfList;
     94 
     95   int max_extrapolation_samples_;
     96   int frame_len_samples_;  // TODO(hlundin): Remove this later.
     97 
     98   // Compares two events and returns true if they are the same.
     99   static bool SameEvent(const DtmfEvent& a, const DtmfEvent& b);
    100 
    101   // Merges |event| to the event pointed out by |it|. The method checks that
    102   // the two events are the same (using the SameEvent method), and merges them
    103   // if that was the case, returning true. If the events are not the same, false
    104   // is returned.
    105   bool MergeEvents(DtmfList::iterator it, const DtmfEvent& event);
    106 
    107   // Method used by the sort algorithm to rank events in the buffer.
    108   static bool CompareEvents(const DtmfEvent& a, const DtmfEvent& b);
    109 
    110   DtmfList buffer_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(DtmfBuffer);
    113 };
    114 
    115 }  // namespace webrtc
    116 #endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_
    117