blob: ae8aa445410cc7ba3769e6be03e2838500499f66 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_DTMFSENDER_H_
12#define WEBRTC_API_DTMFSENDER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <string>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/dtmfsenderinterface.h"
17#include "webrtc/api/mediastreaminterface.h"
18#include "webrtc/api/proxy.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/common.h"
20#include "webrtc/base/messagehandler.h"
21#include "webrtc/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:
37 // Returns true if the audio track with given id (|track_id|) is capable
38 // of sending DTMF. Otherwise returns false.
39 virtual bool CanInsertDtmf(const std::string& track_id) = 0;
40 // Sends DTMF |code| via the audio track with given id (|track_id|).
41 // The |duration| indicates the length of the DTMF tone in ms.
42 // Returns true on success and false on failure.
43 virtual bool InsertDtmf(const std::string& track_id,
44 int code, int duration) = 0;
45 // 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
53class DtmfSender
54 : public DtmfSenderInterface,
55 public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000056 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058 static rtc::scoped_refptr<DtmfSender> Create(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 DtmfProviderInterface* provider);
62
63 // Implements DtmfSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 void RegisterObserver(DtmfSenderObserverInterface* observer) override;
65 void UnregisterObserver() override;
66 bool CanInsertDtmf() override;
67 bool InsertDtmf(const std::string& tones,
68 int duration,
69 int inter_tone_gap) override;
70 const AudioTrackInterface* track() const override;
71 std::string tones() const override;
72 int duration() const override;
73 int inter_tone_gap() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75 protected:
76 DtmfSender(AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 DtmfProviderInterface* provider);
79 virtual ~DtmfSender();
80
81 private:
82 DtmfSender();
83
84 // Implements MessageHandler.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
87 // The DTMF sending task.
88 void DoInsertDtmf();
89
90 void OnProviderDestroyed();
91
92 void StopSending();
93
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 rtc::scoped_refptr<AudioTrackInterface> track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 DtmfSenderObserverInterface* observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096 rtc::Thread* signaling_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 DtmfProviderInterface* provider_;
98 std::string tones_;
99 int duration_;
100 int inter_tone_gap_;
101
henrikg3c089d72015-09-16 05:37:44 -0700102 RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103};
104
105// Define proxy for DtmfSenderInterface.
106BEGIN_PROXY_MAP(DtmfSender)
107 PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
108 PROXY_METHOD0(void, UnregisterObserver)
109 PROXY_METHOD0(bool, CanInsertDtmf)
110 PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
111 PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
112 PROXY_CONSTMETHOD0(std::string, tones)
113 PROXY_CONSTMETHOD0(int, duration)
114 PROXY_CONSTMETHOD0(int, inter_tone_gap)
115END_PROXY()
116
117// Get DTMF code from the DTMF event character.
118bool GetDtmfCode(char tone, int* code);
119
120} // namespace webrtc
121
Henrik Kjellander15583c12016-02-10 10:53:12 +0100122#endif // WEBRTC_API_DTMFSENDER_H_