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 "RTCDTMFSender.h" |
28 | |
29 | #if ENABLE(WEB_RTC_DTMF) |
30 | |
31 | #include "MediaStreamTrack.h" |
32 | #include "RTCDTMFSenderHandler.h" |
33 | #include "RTCDTMFToneChangeEvent.h" |
34 | #include "ScriptExecutionContext.h" |
35 | #include <wtf/IsoMallocInlines.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | WTF_MAKE_ISO_ALLOCATED_IMPL(RTCDTMFSender); |
40 | |
41 | static const long minToneDurationMs = 40; |
42 | static const long defaultToneDurationMs = 100; |
43 | static const long maxToneDurationMs = 6000; |
44 | static const long minInterToneGapMs = 30; |
45 | static const long defaultInterToneGapMs = 70; |
46 | |
47 | RTCDTMFSender::RTCDTMFSender(ScriptExecutionContext& context, RefPtr<MediaStreamTrack>&& track) |
48 | : ActiveDOMObject(&context) |
49 | , m_track(WTFMove(track)) |
50 | , m_duration(defaultToneDurationMs) |
51 | , m_interToneGap(defaultInterToneGapMs) |
52 | , m_stopped(false) |
53 | , m_scheduledEventTimer(*this, &RTCDTMFSender::scheduledEventTimerFired) |
54 | { |
55 | } |
56 | |
57 | RTCDTMFSender::~RTCDTMFSender() = default; |
58 | |
59 | bool RTCDTMFSender::canInsertDTMF() const |
60 | { |
61 | return false; |
62 | } |
63 | |
64 | MediaStreamTrack* RTCDTMFSender::track() const |
65 | { |
66 | return m_track.get(); |
67 | } |
68 | |
69 | String RTCDTMFSender::toneBuffer() const |
70 | { |
71 | return { }; |
72 | } |
73 | |
74 | ExceptionOr<void> RTCDTMFSender::insertDTMF(const String&, Optional<int> duration, Optional<int> interToneGap) |
75 | { |
76 | if (!canInsertDTMF()) |
77 | return Exception { NotSupportedError }; |
78 | |
79 | if (duration && (duration.value() > maxToneDurationMs || duration.value() < minToneDurationMs)) |
80 | return Exception { SyntaxError }; |
81 | |
82 | if (interToneGap && interToneGap.value() < minInterToneGapMs) |
83 | return Exception { SyntaxError }; |
84 | |
85 | m_duration = duration.valueOr(defaultToneDurationMs); |
86 | m_interToneGap = interToneGap.valueOr(defaultInterToneGapMs); |
87 | |
88 | return Exception { SyntaxError }; |
89 | } |
90 | |
91 | void RTCDTMFSender::didPlayTone(const String& tone) |
92 | { |
93 | scheduleDispatchEvent(RTCDTMFToneChangeEvent::create(tone)); |
94 | } |
95 | |
96 | void RTCDTMFSender::stop() |
97 | { |
98 | m_stopped = true; |
99 | } |
100 | |
101 | const char* RTCDTMFSender::activeDOMObjectName() const |
102 | { |
103 | return "RTCDTMFSender" ; |
104 | } |
105 | |
106 | bool RTCDTMFSender::canSuspendForDocumentSuspension() const |
107 | { |
108 | // FIXME: We should try and do better here. |
109 | return false; |
110 | } |
111 | |
112 | void RTCDTMFSender::scheduleDispatchEvent(Ref<Event>&& event) |
113 | { |
114 | m_scheduledEvents.append(WTFMove(event)); |
115 | |
116 | if (!m_scheduledEventTimer.isActive()) |
117 | m_scheduledEventTimer.startOneShot(0_s); |
118 | } |
119 | |
120 | void RTCDTMFSender::scheduledEventTimerFired() |
121 | { |
122 | if (m_stopped) |
123 | return; |
124 | |
125 | Vector<Ref<Event>> events; |
126 | events.swap(m_scheduledEvents); |
127 | |
128 | for (auto& event : events) |
129 | dispatchEvent(event); |
130 | } |
131 | |
132 | } // namespace WebCore |
133 | |
134 | #endif // ENABLE(WEB_RTC) |
135 | |