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 MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <list> 18 19 #include "rtc_base/constructor_magic.h" 20 21 namespace webrtc { 22 23 struct DtmfEvent { 24 uint32_t timestamp; 25 int event_no; 26 int volume; 27 int duration; 28 bool end_bit; 29 30 // Constructors DtmfEventDtmfEvent31 DtmfEvent() 32 : timestamp(0), event_no(0), volume(0), duration(0), end_bit(false) {} DtmfEventDtmfEvent33 DtmfEvent(uint32_t ts, int ev, int vol, int dur, bool end) 34 : timestamp(ts), event_no(ev), volume(vol), duration(dur), end_bit(end) {} 35 }; 36 37 // This is the buffer holding DTMF events while waiting for them to be played. 38 class DtmfBuffer { 39 public: 40 enum BufferReturnCodes { 41 kOK = 0, 42 kInvalidPointer, 43 kPayloadTooShort, 44 kInvalidEventParameters, 45 kInvalidSampleRate 46 }; 47 48 // Set up the buffer for use at sample rate |fs_hz|. 49 explicit DtmfBuffer(int fs_hz); 50 51 virtual ~DtmfBuffer(); 52 53 // Flushes the buffer. 54 virtual void Flush(); 55 56 // Static method to parse 4 bytes from |payload| as a DTMF event (RFC 4733) 57 // and write the parsed information into the struct |event|. Input variable 58 // |rtp_timestamp| is simply copied into the struct. 59 static int ParseEvent(uint32_t rtp_timestamp, 60 const uint8_t* payload, 61 size_t payload_length_bytes, 62 DtmfEvent* event); 63 64 // Inserts |event| into the buffer. The method looks for a matching event and 65 // merges the two if a match is found. 66 virtual int InsertEvent(const DtmfEvent& event); 67 68 // Checks if a DTMF event should be played at time |current_timestamp|. If so, 69 // the method returns true; otherwise false. The parameters of the event to 70 // play will be written to |event|. 71 virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event); 72 73 // Number of events in the buffer. 74 virtual size_t Length() const; 75 76 virtual bool Empty() const; 77 78 // Set a new sample rate. 79 virtual int SetSampleRate(int fs_hz); 80 81 private: 82 typedef std::list<DtmfEvent> DtmfList; 83 84 int max_extrapolation_samples_; 85 int frame_len_samples_; // TODO(hlundin): Remove this later. 86 87 // Compares two events and returns true if they are the same. 88 static bool SameEvent(const DtmfEvent& a, const DtmfEvent& b); 89 90 // Merges |event| to the event pointed out by |it|. The method checks that 91 // the two events are the same (using the SameEvent method), and merges them 92 // if that was the case, returning true. If the events are not the same, false 93 // is returned. 94 bool MergeEvents(DtmfList::iterator it, const DtmfEvent& event); 95 96 // Method used by the sort algorithm to rank events in the buffer. 97 static bool CompareEvents(const DtmfEvent& a, const DtmfEvent& b); 98 99 DtmfList buffer_; 100 101 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfBuffer); 102 }; 103 104 } // namespace webrtc 105 #endif // MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 106