19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* 29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * 49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * Use of this source code is governed by a BSD-style license 59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * that can be found in the LICENSE file in the root of the source 69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * tree. An additional intellectual property rights grant can be found 79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * in the file PATENTS. All contributing project authors may 89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org * be found in the AUTHORS file in the root of the source tree. 99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */ 109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 12e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <list> 159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <string> // size_t 169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 17774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h" 189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/typedefs.h" 199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc { 219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstruct DtmfEvent { 239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org uint32_t timestamp; 249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int event_no; 259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int volume; 269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int duration; 279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org bool end_bit; 289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Constructors 309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org DtmfEvent() 319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org : timestamp(0), 329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org event_no(0), 339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org volume(0), 349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org duration(0), 359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org end_bit(false) { 369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org } 379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org DtmfEvent(uint32_t ts, int ev, int vol, int dur, bool end) 389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org : timestamp(ts), 399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org event_no(ev), 409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org volume(vol), 419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org duration(dur), 429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org end_bit(end) { 439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org } 449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}; 459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// This is the buffer holding DTMF events while waiting for them to be played. 479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass DtmfBuffer { 489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org public: 499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org enum BufferReturnCodes { 509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org kOK = 0, 519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org kInvalidPointer, 529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org kPayloadTooShort, 539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org kInvalidEventParameters, 549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org kInvalidSampleRate 559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org }; 569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Set up the buffer for use at sample rate |fs_hz|. 589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org explicit DtmfBuffer(int fs_hz) { 599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org SetSampleRate(fs_hz); 609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org } 619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual ~DtmfBuffer() {} 639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Flushes the buffer. 659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual void Flush() { buffer_.clear(); } 669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Static method to parse 4 bytes from |payload| as a DTMF event (RFC 4733) 689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // and write the parsed information into the struct |event|. Input variable 699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // |rtp_timestamp| is simply copied into the struct. 709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org static int ParseEvent(uint32_t rtp_timestamp, 719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org const uint8_t* payload, 729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int payload_length_bytes, 739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org DtmfEvent* event); 749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Inserts |event| into the buffer. The method looks for a matching event and 769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // merges the two if a match is found. 779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual int InsertEvent(const DtmfEvent& event); 789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Checks if a DTMF event should be played at time |current_timestamp|. If so, 809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // the method returns true; otherwise false. The parameters of the event to 819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // play will be written to |event|. 829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual bool GetEvent(uint32_t current_timestamp, DtmfEvent* event); 839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Number of events in the buffer. 859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual size_t Length() const { return buffer_.size(); } 869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual bool Empty() const { return buffer_.empty(); } 889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Set a new sample rate. 909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org virtual int SetSampleRate(int fs_hz); 919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org private: 939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org typedef std::list<DtmfEvent> DtmfList; 949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int max_extrapolation_samples_; 969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org int frame_len_samples_; // TODO(hlundin): Remove this later. 979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Compares two events and returns true if they are the same. 999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org static bool SameEvent(const DtmfEvent& a, const DtmfEvent& b); 1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 1019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Merges |event| to the event pointed out by |it|. The method checks that 1029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // the two events are the same (using the SameEvent method), and merges them 1039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // if that was the case, returning true. If the events are not the same, false 1049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // is returned. 1059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org bool MergeEvents(DtmfList::iterator it, const DtmfEvent& event); 1069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 1079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org // Method used by the sort algorithm to rank events in the buffer. 1089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org static bool CompareEvents(const DtmfEvent& a, const DtmfEvent& b); 1099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 1109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org DtmfList buffer_; 1119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 1129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org DISALLOW_COPY_AND_ASSIGN(DtmfBuffer); 1139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}; 1149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 1159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org} // namespace webrtc 116e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DTMF_BUFFER_H_ 117