blob: 692c74bcef5449b7e74ef015b0bbc1b391592d7a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_DTMF_SENDER_H_
12#define PC_DTMF_SENDER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <string>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "api/dtmf_sender_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/proxy.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/async_invoker.h"
19#include "rtc_base/constructor_magic.h"
20#include "rtc_base/ref_count.h"
Steve Antonb983bae2018-06-20 11:16:53 -070021#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23// DtmfSender is the native implementation of the RTCDTMFSender defined by
24// the WebRTC W3C Editor's Draft.
Steve Anton9a4fd9b2018-08-31 15:20:10 -070025// https://w3c.github.io/webrtc-pc/#rtcdtmfsender
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027namespace webrtc {
28
29// This interface is called by DtmfSender to talk to the actual audio channel
30// to send DTMF.
31class DtmfProviderInterface {
32 public:
deadbeef20cb0c12017-02-01 20:27:00 -080033 // Returns true if the audio sender is capable of sending DTMF. Otherwise
34 // returns false.
35 virtual bool CanInsertDtmf() = 0;
36 // Sends DTMF |code|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037 // The |duration| indicates the length of the DTMF tone in ms.
38 // Returns true on success and false on failure.
deadbeef20cb0c12017-02-01 20:27:00 -080039 virtual bool InsertDtmf(int code, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 // Returns a |sigslot::signal0<>| signal. The signal should fire before
41 // the provider is destroyed.
42 virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
43
44 protected:
45 virtual ~DtmfProviderInterface() {}
46};
47
Steve Anton9a4fd9b2018-08-31 15:20:10 -070048class DtmfSender : public DtmfSenderInterface, public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 public:
Steve Antonb983bae2018-06-20 11:16:53 -070050 static rtc::scoped_refptr<DtmfSender> Create(rtc::Thread* signaling_thread,
Yves Gerey665174f2018-06-19 15:03:05 +020051 DtmfProviderInterface* provider);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53 // Implements DtmfSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000054 void RegisterObserver(DtmfSenderObserverInterface* observer) override;
55 void UnregisterObserver() override;
56 bool CanInsertDtmf() override;
57 bool InsertDtmf(const std::string& tones,
58 int duration,
59 int inter_tone_gap) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000060 std::string tones() const override;
61 int duration() const override;
62 int inter_tone_gap() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 protected:
Steve Antonb983bae2018-06-20 11:16:53 -070065 DtmfSender(rtc::Thread* signaling_thread, DtmfProviderInterface* provider);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 virtual ~DtmfSender();
67
68 private:
69 DtmfSender();
70
Steve Anton9a4fd9b2018-08-31 15:20:10 -070071 void QueueInsertDtmf(const rtc::Location& posted_from, uint32_t delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 // The DTMF sending task.
74 void DoInsertDtmf();
75
76 void OnProviderDestroyed();
77
78 void StopSending();
79
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 DtmfSenderObserverInterface* observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081 rtc::Thread* signaling_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 DtmfProviderInterface* provider_;
83 std::string tones_;
84 int duration_;
85 int inter_tone_gap_;
Steve Anton9a4fd9b2018-08-31 15:20:10 -070086 // Invoker for running delayed tasks which feed the DTMF provider one tone at
87 // a time.
88 rtc::AsyncInvoker dtmf_driver_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
henrikg3c089d72015-09-16 05:37:44 -070090 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091};
92
93// Define proxy for DtmfSenderInterface.
nisse72c8d2b2016-04-15 03:49:07 -070094BEGIN_SIGNALING_PROXY_MAP(DtmfSender)
Yves Gerey665174f2018-06-19 15:03:05 +020095PROXY_SIGNALING_THREAD_DESTRUCTOR()
96PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
97PROXY_METHOD0(void, UnregisterObserver)
98PROXY_METHOD0(bool, CanInsertDtmf)
99PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
Yves Gerey665174f2018-06-19 15:03:05 +0200100PROXY_CONSTMETHOD0(std::string, tones)
101PROXY_CONSTMETHOD0(int, duration)
102PROXY_CONSTMETHOD0(int, inter_tone_gap)
deadbeefd99a2002017-01-18 08:55:23 -0800103END_PROXY_MAP()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105// Get DTMF code from the DTMF event character.
106bool GetDtmfCode(char tone, int* code);
107
108} // namespace webrtc
109
Steve Anton10542f22019-01-11 09:11:00 -0800110#endif // PC_DTMF_SENDER_H_