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 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | #include <list> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | struct DtmfEvent { |
| 22 | uint32_t timestamp; |
| 23 | int event_no; |
| 24 | int volume; |
| 25 | int duration; |
| 26 | bool end_bit; |
| 27 | |
| 28 | // Constructors |
| 29 | DtmfEvent() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | : timestamp(0), event_no(0), volume(0), duration(0), end_bit(false) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | DtmfEvent(uint32_t ts, int ev, int vol, int dur, bool end) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | : timestamp(ts), event_no(ev), volume(vol), duration(dur), end_bit(end) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | // This is the buffer holding DTMF events while waiting for them to be played. |
| 36 | class DtmfBuffer { |
| 37 | public: |
| 38 | enum BufferReturnCodes { |
| 39 | kOK = 0, |
| 40 | kInvalidPointer, |
| 41 | kPayloadTooShort, |
| 42 | kInvalidEventParameters, |
| 43 | kInvalidSampleRate |
| 44 | }; |
| 45 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 46 | // Set up the buffer for use at sample rate `fs_hz`. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 47 | explicit DtmfBuffer(int fs_hz); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 49 | virtual ~DtmfBuffer(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | |
Byoungchan Lee | 604fd2f | 2022-01-21 09:49:39 +0900 | [diff] [blame] | 51 | DtmfBuffer(const DtmfBuffer&) = delete; |
| 52 | DtmfBuffer& operator=(const DtmfBuffer&) = delete; |
| 53 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | // Flushes the buffer. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 55 | virtual void Flush(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 57 | // Static method to parse 4 bytes from `payload` as a DTMF event (RFC 4733) |
| 58 | // and write the parsed information into the struct `event`. Input variable |
| 59 | // `rtp_timestamp` is simply copied into the struct. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | static int ParseEvent(uint32_t rtp_timestamp, |
| 61 | const uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 62 | size_t payload_length_bytes, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 63 | DtmfEvent* event); |
| 64 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 65 | // Inserts `event` into the buffer. The method looks for a matching event and |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 66 | // merges the two if a match is found. |
| 67 | virtual int InsertEvent(const DtmfEvent& event); |
| 68 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 69 | // Checks if a DTMF event should be played at time `current_timestamp`. If so, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | // the method returns true; otherwise false. The parameters of the event to |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 71 | // play will be written to `event`. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event); |
| 73 | |
| 74 | // Number of events in the buffer. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 75 | virtual size_t Length() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 77 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 78 | |
| 79 | // Set a new sample rate. |
| 80 | virtual int SetSampleRate(int fs_hz); |
| 81 | |
| 82 | private: |
| 83 | typedef std::list<DtmfEvent> DtmfList; |
| 84 | |
| 85 | int max_extrapolation_samples_; |
| 86 | int frame_len_samples_; // TODO(hlundin): Remove this later. |
| 87 | |
| 88 | // Compares two events and returns true if they are the same. |
| 89 | static bool SameEvent(const DtmfEvent& a, const DtmfEvent& b); |
| 90 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 91 | // Merges `event` to the event pointed out by `it`. The method checks that |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 92 | // the two events are the same (using the SameEvent method), and merges them |
| 93 | // if that was the case, returning true. If the events are not the same, false |
| 94 | // is returned. |
| 95 | bool MergeEvents(DtmfList::iterator it, const DtmfEvent& event); |
| 96 | |
| 97 | // Method used by the sort algorithm to rank events in the buffer. |
| 98 | static bool CompareEvents(const DtmfEvent& a, const DtmfEvent& b); |
| 99 | |
| 100 | DtmfList buffer_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 104 | #endif // MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ |