blob: ec9a36ef2d44037d3cca7a314d4e6b77d9bff7af [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_APP_WEBRTC_DTMFSENDER_H_
29#define TALK_APP_WEBRTC_DTMFSENDER_H_
30
31#include <string>
32
33#include "talk/app/webrtc/dtmfsenderinterface.h"
34#include "talk/app/webrtc/mediastreaminterface.h"
35#include "talk/app/webrtc/proxy.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/common.h"
37#include "webrtc/base/messagehandler.h"
38#include "webrtc/base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40// DtmfSender is the native implementation of the RTCDTMFSender defined by
41// the WebRTC W3C Editor's Draft.
42// http://dev.w3.org/2011/webrtc/editor/webrtc.html
43
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045class Thread;
46}
47
48namespace webrtc {
49
50// This interface is called by DtmfSender to talk to the actual audio channel
51// to send DTMF.
52class DtmfProviderInterface {
53 public:
54 // Returns true if the audio track with given id (|track_id|) is capable
55 // of sending DTMF. Otherwise returns false.
56 virtual bool CanInsertDtmf(const std::string& track_id) = 0;
57 // Sends DTMF |code| via the audio track with given id (|track_id|).
58 // The |duration| indicates the length of the DTMF tone in ms.
59 // Returns true on success and false on failure.
60 virtual bool InsertDtmf(const std::string& track_id,
61 int code, int duration) = 0;
62 // Returns a |sigslot::signal0<>| signal. The signal should fire before
63 // the provider is destroyed.
64 virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
65
66 protected:
67 virtual ~DtmfProviderInterface() {}
68};
69
70class DtmfSender
71 : public DtmfSenderInterface,
72 public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 static rtc::scoped_refptr<DtmfSender> Create(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 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
80 // Implements DtmfSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 void RegisterObserver(DtmfSenderObserverInterface* observer) override;
82 void UnregisterObserver() override;
83 bool CanInsertDtmf() override;
84 bool InsertDtmf(const std::string& tones,
85 int duration,
86 int inter_tone_gap) override;
87 const AudioTrackInterface* track() const override;
88 std::string tones() const override;
89 int duration() const override;
90 int inter_tone_gap() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
92 protected:
93 DtmfSender(AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 DtmfProviderInterface* provider);
96 virtual ~DtmfSender();
97
98 private:
99 DtmfSender();
100
101 // Implements MessageHandler.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
104 // The DTMF sending task.
105 void DoInsertDtmf();
106
107 void OnProviderDestroyed();
108
109 void StopSending();
110
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000111 rtc::scoped_refptr<AudioTrackInterface> track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 DtmfSenderObserverInterface* observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 rtc::Thread* signaling_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 DtmfProviderInterface* provider_;
115 std::string tones_;
116 int duration_;
117 int inter_tone_gap_;
118
119 DISALLOW_COPY_AND_ASSIGN(DtmfSender);
120};
121
122// Define proxy for DtmfSenderInterface.
123BEGIN_PROXY_MAP(DtmfSender)
124 PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
125 PROXY_METHOD0(void, UnregisterObserver)
126 PROXY_METHOD0(bool, CanInsertDtmf)
127 PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
128 PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
129 PROXY_CONSTMETHOD0(std::string, tones)
130 PROXY_CONSTMETHOD0(int, duration)
131 PROXY_CONSTMETHOD0(int, inter_tone_gap)
132END_PROXY()
133
134// Get DTMF code from the DTMF event character.
135bool GetDtmfCode(char tone, int* code);
136
137} // namespace webrtc
138
139#endif // TALK_APP_WEBRTC_DTMFSENDER_H_