dtmf_sender.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 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 PC_DTMF_SENDER_H_
  11. #define PC_DTMF_SENDER_H_
  12. #include <string>
  13. #include "api/dtmf_sender_interface.h"
  14. #include "api/proxy.h"
  15. #include "rtc_base/async_invoker.h"
  16. #include "rtc_base/constructor_magic.h"
  17. #include "rtc_base/ref_count.h"
  18. #include "rtc_base/thread.h"
  19. // DtmfSender is the native implementation of the RTCDTMFSender defined by
  20. // the WebRTC W3C Editor's Draft.
  21. // https://w3c.github.io/webrtc-pc/#rtcdtmfsender
  22. namespace webrtc {
  23. // This interface is called by DtmfSender to talk to the actual audio channel
  24. // to send DTMF.
  25. class DtmfProviderInterface {
  26. public:
  27. // Returns true if the audio sender is capable of sending DTMF. Otherwise
  28. // returns false.
  29. virtual bool CanInsertDtmf() = 0;
  30. // Sends DTMF |code|.
  31. // The |duration| indicates the length of the DTMF tone in ms.
  32. // Returns true on success and false on failure.
  33. virtual bool InsertDtmf(int code, int duration) = 0;
  34. // Returns a |sigslot::signal0<>| signal. The signal should fire before
  35. // the provider is destroyed.
  36. virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
  37. protected:
  38. virtual ~DtmfProviderInterface() {}
  39. };
  40. class DtmfSender : public DtmfSenderInterface, public sigslot::has_slots<> {
  41. public:
  42. static rtc::scoped_refptr<DtmfSender> Create(rtc::Thread* signaling_thread,
  43. DtmfProviderInterface* provider);
  44. // Implements DtmfSenderInterface.
  45. void RegisterObserver(DtmfSenderObserverInterface* observer) override;
  46. void UnregisterObserver() override;
  47. bool CanInsertDtmf() override;
  48. bool InsertDtmf(const std::string& tones,
  49. int duration,
  50. int inter_tone_gap,
  51. int comma_delay = kDtmfDefaultCommaDelayMs) override;
  52. std::string tones() const override;
  53. int duration() const override;
  54. int inter_tone_gap() const override;
  55. int comma_delay() const override;
  56. protected:
  57. DtmfSender(rtc::Thread* signaling_thread, DtmfProviderInterface* provider);
  58. virtual ~DtmfSender();
  59. private:
  60. DtmfSender();
  61. void QueueInsertDtmf(const rtc::Location& posted_from, uint32_t delay_ms);
  62. // The DTMF sending task.
  63. void DoInsertDtmf();
  64. void OnProviderDestroyed();
  65. void StopSending();
  66. DtmfSenderObserverInterface* observer_;
  67. rtc::Thread* signaling_thread_;
  68. DtmfProviderInterface* provider_;
  69. std::string tones_;
  70. int duration_;
  71. int inter_tone_gap_;
  72. int comma_delay_;
  73. // Invoker for running delayed tasks which feed the DTMF provider one tone at
  74. // a time.
  75. rtc::AsyncInvoker dtmf_driver_;
  76. RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
  77. };
  78. // Define proxy for DtmfSenderInterface.
  79. BEGIN_SIGNALING_PROXY_MAP(DtmfSender)
  80. PROXY_SIGNALING_THREAD_DESTRUCTOR()
  81. PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
  82. PROXY_METHOD0(void, UnregisterObserver)
  83. PROXY_METHOD0(bool, CanInsertDtmf)
  84. PROXY_METHOD4(bool, InsertDtmf, const std::string&, int, int, int)
  85. PROXY_CONSTMETHOD0(std::string, tones)
  86. PROXY_CONSTMETHOD0(int, duration)
  87. PROXY_CONSTMETHOD0(int, inter_tone_gap)
  88. PROXY_CONSTMETHOD0(int, comma_delay)
  89. END_PROXY_MAP()
  90. // Get DTMF code from the DTMF event character.
  91. bool GetDtmfCode(char tone, int* code);
  92. } // namespace webrtc
  93. #endif // PC_DTMF_SENDER_H_