dtmf_buffer.h 3.2 KB

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