blob: 5f200545e99ced4b75024d035f46033639fc9c4c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:369 */
10
Steve Anton10542f22019-01-11 17:11:0011#ifndef PC_DTMF_SENDER_H_
12#define PC_DTMF_SENDER_H_
henrike@webrtc.org28e20752013-07-10 00:45:3613
Harald Alvestrand5761e7b2021-01-29 14:45:0814#include <stdint.h>
15
henrike@webrtc.org28e20752013-07-10 00:45:3616#include <string>
17
Steve Anton10542f22019-01-11 17:11:0018#include "api/dtmf_sender_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0819#include "api/scoped_refptr.h"
Markus Handella1b82012021-05-26 16:56:3020#include "pc/proxy.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/constructor_magic.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0822#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/ref_count.h"
Niels Möller08489942021-03-18 08:18:4824#include "rtc_base/task_utils/pending_task_safety_flag.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0825#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Antonb983bae2018-06-20 18:16:5326#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:3627
28// DtmfSender is the native implementation of the RTCDTMFSender defined by
29// the WebRTC W3C Editor's Draft.
Steve Anton9a4fd9b2018-08-31 22:20:1030// https://w3c.github.io/webrtc-pc/#rtcdtmfsender
henrike@webrtc.org28e20752013-07-10 00:45:3631
henrike@webrtc.org28e20752013-07-10 00:45:3632namespace webrtc {
33
34// This interface is called by DtmfSender to talk to the actual audio channel
35// to send DTMF.
36class DtmfProviderInterface {
37 public:
deadbeef20cb0c12017-02-02 04:27:0038 // Returns true if the audio sender is capable of sending DTMF. Otherwise
39 // returns false.
40 virtual bool CanInsertDtmf() = 0;
Artem Titov880fa812021-07-30 20:30:2341 // Sends DTMF `code`.
42 // The `duration` indicates the length of the DTMF tone in ms.
henrike@webrtc.org28e20752013-07-10 00:45:3643 // Returns true on success and false on failure.
deadbeef20cb0c12017-02-02 04:27:0044 virtual bool InsertDtmf(int code, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:3645 // Returns a |sigslot::signal0<>| signal. The signal should fire before
46 // the provider is destroyed.
47 virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
48
49 protected:
50 virtual ~DtmfProviderInterface() {}
51};
52
Steve Anton9a4fd9b2018-08-31 22:20:1053class DtmfSender : public DtmfSenderInterface, public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:3654 public:
Steve Antonb983bae2018-06-20 18:16:5355 static rtc::scoped_refptr<DtmfSender> Create(rtc::Thread* signaling_thread,
Yves Gerey665174f2018-06-19 13:03:0556 DtmfProviderInterface* provider);
henrike@webrtc.org28e20752013-07-10 00:45:3657
58 // Implements DtmfSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:3559 void RegisterObserver(DtmfSenderObserverInterface* observer) override;
60 void UnregisterObserver() override;
61 bool CanInsertDtmf() override;
62 bool InsertDtmf(const std::string& tones,
63 int duration,
Aaron Alaniz529d8862020-01-21 03:09:4764 int inter_tone_gap,
65 int comma_delay = kDtmfDefaultCommaDelayMs) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:3566 std::string tones() const override;
67 int duration() const override;
68 int inter_tone_gap() const override;
Aaron Alaniz529d8862020-01-21 03:09:4769 int comma_delay() const override;
henrike@webrtc.org28e20752013-07-10 00:45:3670
71 protected:
Steve Antonb983bae2018-06-20 18:16:5372 DtmfSender(rtc::Thread* signaling_thread, DtmfProviderInterface* provider);
henrike@webrtc.org28e20752013-07-10 00:45:3673 virtual ~DtmfSender();
74
75 private:
76 DtmfSender();
77
Niels Möller08489942021-03-18 08:18:4878 void QueueInsertDtmf(const rtc::Location& posted_from, uint32_t delay_ms)
79 RTC_RUN_ON(signaling_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:3680
81 // The DTMF sending task.
Niels Möller08489942021-03-18 08:18:4882 void DoInsertDtmf() RTC_RUN_ON(signaling_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:3683
84 void OnProviderDestroyed();
85
Niels Möller08489942021-03-18 08:18:4886 void StopSending() RTC_RUN_ON(signaling_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:3687
Niels Möller08489942021-03-18 08:18:4888 DtmfSenderObserverInterface* observer_ RTC_GUARDED_BY(signaling_thread_);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5289 rtc::Thread* signaling_thread_;
Niels Möller08489942021-03-18 08:18:4890 DtmfProviderInterface* provider_ RTC_GUARDED_BY(signaling_thread_);
91 std::string tones_ RTC_GUARDED_BY(signaling_thread_);
92 int duration_ RTC_GUARDED_BY(signaling_thread_);
93 int inter_tone_gap_ RTC_GUARDED_BY(signaling_thread_);
94 int comma_delay_ RTC_GUARDED_BY(signaling_thread_);
95
96 // For cancelling the tasks which feed the DTMF provider one tone at a time.
97 rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_ RTC_GUARDED_BY(
98 signaling_thread_) RTC_PT_GUARDED_BY(signaling_thread_) = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:3699
henrikg3c089d72015-09-16 12:37:44100 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
henrike@webrtc.org28e20752013-07-10 00:45:36101};
102
103// Define proxy for DtmfSenderInterface.
Mirko Bonadei9d9b8de2021-02-26 08:51:26104BEGIN_PRIMARY_PROXY_MAP(DtmfSender)
Mirko Bonadei9d9b8de2021-02-26 08:51:26105PROXY_PRIMARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:05106PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
107PROXY_METHOD0(void, UnregisterObserver)
108PROXY_METHOD0(bool, CanInsertDtmf)
Aaron Alaniz529d8862020-01-21 03:09:47109PROXY_METHOD4(bool, InsertDtmf, const std::string&, int, int, int)
Yves Gerey665174f2018-06-19 13:03:05110PROXY_CONSTMETHOD0(std::string, tones)
111PROXY_CONSTMETHOD0(int, duration)
112PROXY_CONSTMETHOD0(int, inter_tone_gap)
Aaron Alaniz529d8862020-01-21 03:09:47113PROXY_CONSTMETHOD0(int, comma_delay)
Markus Handell3d46d0b2021-05-27 19:42:57114END_PROXY_MAP(DtmfSender)
henrike@webrtc.org28e20752013-07-10 00:45:36115
116// Get DTMF code from the DTMF event character.
117bool GetDtmfCode(char tone, int* code);
118
119} // namespace webrtc
120
Steve Anton10542f22019-01-11 17:11:00121#endif // PC_DTMF_SENDER_H_