blob: ff7f0ac049071f707e6d973499b6f495b9b43193 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_DTMFSENDER_H_
12#define PC_DTMFSENDER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/dtmfsenderinterface.h"
17#include "api/mediastreaminterface.h"
18#include "api/proxy.h"
19#include "rtc_base/constructormagic.h"
20#include "rtc_base/messagehandler.h"
21#include "rtc_base/refcount.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.
25// http://dev.w3.org/2011/webrtc/editor/webrtc.html
26
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000027namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028class Thread;
29}
30
31namespace webrtc {
32
33// This interface is called by DtmfSender to talk to the actual audio channel
34// to send DTMF.
35class DtmfProviderInterface {
36 public:
deadbeef20cb0c12017-02-01 20:27:00 -080037 // Returns true if the audio sender is capable of sending DTMF. Otherwise
38 // returns false.
39 virtual bool CanInsertDtmf() = 0;
40 // Sends DTMF |code|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 // The |duration| indicates the length of the DTMF tone in ms.
42 // Returns true on success and false on failure.
deadbeef20cb0c12017-02-01 20:27:00 -080043 virtual bool InsertDtmf(int code, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 // Returns a |sigslot::signal0<>| signal. The signal should fire before
45 // the provider is destroyed.
46 virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
47
48 protected:
49 virtual ~DtmfProviderInterface() {}
50};
51
52class DtmfSender
53 : public DtmfSenderInterface,
54 public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public:
deadbeef20cb0c12017-02-01 20:27:00 -080057 // |track| is only there for backwards compatibility, since there's a track
58 // accessor method.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059 static rtc::scoped_refptr<DtmfSender> Create(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 DtmfProviderInterface* provider);
63
64 // Implements DtmfSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 void RegisterObserver(DtmfSenderObserverInterface* observer) override;
66 void UnregisterObserver() override;
67 bool CanInsertDtmf() override;
68 bool InsertDtmf(const std::string& tones,
69 int duration,
70 int inter_tone_gap) override;
71 const AudioTrackInterface* track() const override;
72 std::string tones() const override;
73 int duration() const override;
74 int inter_tone_gap() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
76 protected:
77 DtmfSender(AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 DtmfProviderInterface* provider);
80 virtual ~DtmfSender();
81
82 private:
83 DtmfSender();
84
85 // Implements MessageHandler.
nisseef8b61e2016-04-29 06:09:15 -070086 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
88 // The DTMF sending task.
89 void DoInsertDtmf();
90
91 void OnProviderDestroyed();
92
93 void StopSending();
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 rtc::scoped_refptr<AudioTrackInterface> track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 DtmfSenderObserverInterface* observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 rtc::Thread* signaling_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 DtmfProviderInterface* provider_;
99 std::string tones_;
100 int duration_;
101 int inter_tone_gap_;
102
henrikg3c089d72015-09-16 05:37:44 -0700103 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104};
105
106// Define proxy for DtmfSenderInterface.
nisse72c8d2b2016-04-15 03:49:07 -0700107BEGIN_SIGNALING_PROXY_MAP(DtmfSender)
deadbeefd99a2002017-01-18 08:55:23 -0800108 PROXY_SIGNALING_THREAD_DESTRUCTOR()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
110 PROXY_METHOD0(void, UnregisterObserver)
111 PROXY_METHOD0(bool, CanInsertDtmf)
112 PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
113 PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
114 PROXY_CONSTMETHOD0(std::string, tones)
115 PROXY_CONSTMETHOD0(int, duration)
116 PROXY_CONSTMETHOD0(int, inter_tone_gap)
deadbeefd99a2002017-01-18 08:55:23 -0800117END_PROXY_MAP()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119// Get DTMF code from the DTMF event character.
120bool GetDtmfCode(char tone, int* code);
121
122} // namespace webrtc
123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // PC_DTMFSENDER_H_