blob: 9209cae864ab9d8b5292d44e9746e25a72ed58e9 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_
12#define MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include <list>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
23struct DtmfEvent {
24 uint32_t timestamp;
25 int event_no;
26 int volume;
27 int duration;
28 bool end_bit;
29
30 // Constructors
31 DtmfEvent()
Yves Gerey665174f2018-06-19 15:03:05 +020032 : timestamp(0), event_no(0), volume(0), duration(0), end_bit(false) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033 DtmfEvent(uint32_t ts, int ev, int vol, int dur, bool end)
Yves Gerey665174f2018-06-19 15:03:05 +020034 : timestamp(ts), event_no(ev), volume(vol), duration(dur), end_bit(end) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035};
36
37// This is the buffer holding DTMF events while waiting for them to be played.
38class DtmfBuffer {
39 public:
40 enum BufferReturnCodes {
41 kOK = 0,
42 kInvalidPointer,
43 kPayloadTooShort,
44 kInvalidEventParameters,
45 kInvalidSampleRate
46 };
47
Artem Titovd00ce742021-07-28 20:00:17 +020048 // Set up the buffer for use at sample rate `fs_hz`.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020049 explicit DtmfBuffer(int fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020051 virtual ~DtmfBuffer();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
53 // Flushes the buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020054 virtual void Flush();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
Artem Titovd00ce742021-07-28 20:00:17 +020056 // 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.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 static int ParseEvent(uint32_t rtp_timestamp,
60 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000061 size_t payload_length_bytes,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 DtmfEvent* event);
63
Artem Titovd00ce742021-07-28 20:00:17 +020064 // Inserts `event` into the buffer. The method looks for a matching event and
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 // merges the two if a match is found.
66 virtual int InsertEvent(const DtmfEvent& event);
67
Artem Titovd00ce742021-07-28 20:00:17 +020068 // Checks if a DTMF event should be played at time `current_timestamp`. If so,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 // the method returns true; otherwise false. The parameters of the event to
Artem Titovd00ce742021-07-28 20:00:17 +020070 // play will be written to `event`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event);
72
73 // Number of events in the buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020074 virtual size_t Length() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020076 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077
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
Artem Titovd00ce742021-07-28 20:00:17 +020090 // Merges `event` to the event pointed out by `it`. The method checks that
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 // 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
henrikg3c089d72015-09-16 05:37:44 -0700101 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102};
103
104} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_