henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | #include <string> // size_t |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/constructormagic.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 18 | #include "typedefs.h" // NOLINT(build/include) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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|. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 58 | explicit DtmfBuffer(int fs_hz); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 60 | virtual ~DtmfBuffer(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 61 | |
| 62 | // Flushes the buffer. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 63 | virtual void Flush(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | |
| 65 | // Static method to parse 4 bytes from |payload| as a DTMF event (RFC 4733) |
| 66 | // and write the parsed information into the struct |event|. Input variable |
| 67 | // |rtp_timestamp| is simply copied into the struct. |
| 68 | static int ParseEvent(uint32_t rtp_timestamp, |
| 69 | const uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 70 | size_t payload_length_bytes, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | DtmfEvent* event); |
| 72 | |
| 73 | // Inserts |event| into the buffer. The method looks for a matching event and |
| 74 | // merges the two if a match is found. |
| 75 | virtual int InsertEvent(const DtmfEvent& event); |
| 76 | |
| 77 | // Checks if a DTMF event should be played at time |current_timestamp|. If so, |
| 78 | // the method returns true; otherwise false. The parameters of the event to |
| 79 | // play will be written to |event|. |
| 80 | virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event); |
| 81 | |
| 82 | // Number of events in the buffer. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 83 | virtual size_t Length() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 85 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 86 | |
| 87 | // Set a new sample rate. |
| 88 | virtual int SetSampleRate(int fs_hz); |
| 89 | |
| 90 | private: |
| 91 | typedef std::list<DtmfEvent> DtmfList; |
| 92 | |
| 93 | int max_extrapolation_samples_; |
| 94 | int frame_len_samples_; // TODO(hlundin): Remove this later. |
| 95 | |
| 96 | // Compares two events and returns true if they are the same. |
| 97 | static bool SameEvent(const DtmfEvent& a, const DtmfEvent& b); |
| 98 | |
| 99 | // Merges |event| to the event pointed out by |it|. The method checks that |
| 100 | // the two events are the same (using the SameEvent method), and merges them |
| 101 | // if that was the case, returning true. If the events are not the same, false |
| 102 | // is returned. |
| 103 | bool MergeEvents(DtmfList::iterator it, const DtmfEvent& event); |
| 104 | |
| 105 | // Method used by the sort algorithm to rank events in the buffer. |
| 106 | static bool CompareEvents(const DtmfEvent& a, const DtmfEvent& b); |
| 107 | |
| 108 | DtmfList buffer_; |
| 109 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 110 | RTC_DISALLOW_COPY_AND_ASSIGN(DtmfBuffer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 114 | #endif // MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ |