Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / mediastream / RTCDTMFSender.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GOOGLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "modules/mediastream/RTCDTMFSender.h"
28
29 #include "bindings/v8/ExceptionMessages.h"
30 #include "bindings/v8/ExceptionState.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/dom/ExecutionContext.h"
33 #include "modules/mediastream/MediaStreamTrack.h"
34 #include "modules/mediastream/RTCDTMFToneChangeEvent.h"
35 #include "public/platform/WebMediaStreamTrack.h"
36 #include "public/platform/WebRTCDTMFSenderHandler.h"
37 #include "public/platform/WebRTCPeerConnectionHandler.h"
38
39 namespace WebCore {
40
41 static const long minToneDurationMs = 70;
42 static const long defaultToneDurationMs = 100;
43 static const long maxToneDurationMs = 6000;
44 static const long minInterToneGapMs = 50;
45 static const long defaultInterToneGapMs = 50;
46
47 PassRefPtr<RTCDTMFSender> RTCDTMFSender::create(ExecutionContext* context, blink::WebRTCPeerConnectionHandler* peerConnectionHandler, PassRefPtr<MediaStreamTrack> prpTrack, ExceptionState& exceptionState)
48 {
49     RefPtr<MediaStreamTrack> track = prpTrack;
50     OwnPtr<blink::WebRTCDTMFSenderHandler> handler = adoptPtr(peerConnectionHandler->createDTMFSender(track->component()));
51     if (!handler) {
52         exceptionState.throwDOMException(NotSupportedError, "The MediaStreamTrack provided is not an element of a MediaStream that's currently in the local streams set.");
53         return nullptr;
54     }
55
56     RefPtr<RTCDTMFSender> dtmfSender = adoptRef(new RTCDTMFSender(context, track, handler.release()));
57     dtmfSender->suspendIfNeeded();
58     return dtmfSender.release();
59 }
60
61 RTCDTMFSender::RTCDTMFSender(ExecutionContext* context, PassRefPtr<MediaStreamTrack> track, PassOwnPtr<blink::WebRTCDTMFSenderHandler> handler)
62     : ActiveDOMObject(context)
63     , m_track(track)
64     , m_duration(defaultToneDurationMs)
65     , m_interToneGap(defaultInterToneGapMs)
66     , m_handler(handler)
67     , m_stopped(false)
68     , m_scheduledEventTimer(this, &RTCDTMFSender::scheduledEventTimerFired)
69 {
70     ScriptWrappable::init(this);
71     m_handler->setClient(this);
72 }
73
74 RTCDTMFSender::~RTCDTMFSender()
75 {
76 }
77
78 bool RTCDTMFSender::canInsertDTMF() const
79 {
80     return m_handler->canInsertDTMF();
81 }
82
83 MediaStreamTrack* RTCDTMFSender::track() const
84 {
85     return m_track.get();
86 }
87
88 String RTCDTMFSender::toneBuffer() const
89 {
90     return m_handler->currentToneBuffer();
91 }
92
93 void RTCDTMFSender::insertDTMF(const String& tones, ExceptionState& exceptionState)
94 {
95     insertDTMF(tones, defaultToneDurationMs, defaultInterToneGapMs, exceptionState);
96 }
97
98 void RTCDTMFSender::insertDTMF(const String& tones, long duration, ExceptionState& exceptionState)
99 {
100     insertDTMF(tones, duration, defaultInterToneGapMs, exceptionState);
101 }
102
103 void RTCDTMFSender::insertDTMF(const String& tones, long duration, long interToneGap, ExceptionState& exceptionState)
104 {
105     if (!canInsertDTMF()) {
106         exceptionState.throwDOMException(NotSupportedError, "The 'canInsertDTMF' attribute is false: this sender cannot send DTMF.");
107         return;
108     }
109
110     if (duration > maxToneDurationMs || duration < minToneDurationMs) {
111         exceptionState.throwDOMException(SyntaxError, ExceptionMessages::indexOutsideRange("duration", duration, minToneDurationMs, ExceptionMessages::ExclusiveBound, maxToneDurationMs, ExceptionMessages::ExclusiveBound));
112         return;
113     }
114
115     if (interToneGap < minInterToneGapMs) {
116         exceptionState.throwDOMException(SyntaxError, ExceptionMessages::indexExceedsMinimumBound("intertone gap", interToneGap, minInterToneGapMs));
117         return;
118     }
119
120     m_duration = duration;
121     m_interToneGap = interToneGap;
122
123     if (!m_handler->insertDTMF(tones, m_duration, m_interToneGap))
124         exceptionState.throwDOMException(SyntaxError, "Could not send provided tones, '" + tones + "'.");
125 }
126
127 void RTCDTMFSender::didPlayTone(const blink::WebString& tone)
128 {
129     scheduleDispatchEvent(RTCDTMFToneChangeEvent::create(tone));
130 }
131
132 const AtomicString& RTCDTMFSender::interfaceName() const
133 {
134     return EventTargetNames::RTCDTMFSender;
135 }
136
137 ExecutionContext* RTCDTMFSender::executionContext() const
138 {
139     return ActiveDOMObject::executionContext();
140 }
141
142 void RTCDTMFSender::stop()
143 {
144     m_stopped = true;
145     m_handler->setClient(0);
146 }
147
148 void RTCDTMFSender::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
149 {
150     m_scheduledEvents.append(event);
151
152     if (!m_scheduledEventTimer.isActive())
153         m_scheduledEventTimer.startOneShot(0, FROM_HERE);
154 }
155
156 void RTCDTMFSender::scheduledEventTimerFired(Timer<RTCDTMFSender>*)
157 {
158     if (m_stopped)
159         return;
160
161     WillBeHeapVector<RefPtrWillBeMember<Event> > events;
162     events.swap(m_scheduledEvents);
163
164     WillBeHeapVector<RefPtrWillBeMember<Event> >::iterator it = events.begin();
165     for (; it != events.end(); ++it)
166         dispatchEvent((*it).release());
167 }
168
169 } // namespace WebCore