OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "modules/mediastream/MediaRecorder.h" | |
7 | |
8 #include "core/dom/DOMError.h" | |
9 #include "core/fileapi/Blob.h" | |
10 #include "modules/EventModules.h" | |
11 #include "modules/EventTargetModules.h" | |
12 #include "modules/mediastream/BlobEvent.h" | |
13 #include "modules/mediastream/MediaRecorderErrorEvent.h" | |
14 #include "public/platform/Platform.h" | |
15 #include "public/platform/WebMediaStream.h" | |
16 | |
17 namespace blink { | |
18 | |
19 namespace { | |
20 | |
21 static String StateToString(MediaRecorder::State state) | |
22 { | |
23 switch (state) { | |
24 case MediaRecorder::State::Inactive: | |
25 return "inactive"; | |
26 case MediaRecorder::State::Recording: | |
27 return "recording"; | |
28 case MediaRecorder::State::Paused: | |
29 return "paused"; | |
30 } | |
31 | |
32 ASSERT_NOT_REACHED(); | |
33 return String(); | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 MediaRecorder* MediaRecorder::create(ExecutionContext* context, MediaStream* str eam, ExceptionState& exceptionState) | |
39 { | |
40 MediaRecorder* recorder = new MediaRecorder(context, stream, String(), excep tionState); | |
41 recorder->suspendIfNeeded(); | |
42 | |
43 return recorder; | |
44 } | |
45 | |
46 MediaRecorder* MediaRecorder::create(ExecutionContext* context, MediaStream* str eam, const String& mimeType, ExceptionState& exceptionState) | |
47 { | |
48 MediaRecorder* recorder = new MediaRecorder(context, stream, mimeType, excep tionState); | |
49 recorder->suspendIfNeeded(); | |
50 | |
51 return recorder; | |
52 } | |
53 | |
54 MediaRecorder::MediaRecorder(ExecutionContext* context, MediaStream* stream, con st String& mimeType, ExceptionState& exceptionState) | |
55 : ActiveDOMObject(context) | |
56 , m_stream(stream) | |
57 , m_mimeType(mimeType) | |
58 , m_stopped(false) | |
59 , m_ignoreMutedMedia(true) | |
60 , m_state(State::Inactive) | |
61 , m_dispatchScheduledEventRunner(this, &MediaRecorder::dispatchScheduledEven t) | |
62 { | |
63 m_recorderHandler = adoptPtr(Platform::current()->createMediaRecorderHandler ()); | |
64 | |
65 // TODO(mcasas): Once http://crbug.com/262211 has landed the Chromium parts, | |
66 // and more concretetely the createMediaRecorderHandler() implementation, | |
67 // ASSERT() here for |m_recorderHandler|. | |
68 if (!m_recorderHandler) { | |
69 m_stopped = true; | |
70 exceptionState.throwDOMException(NotSupportedError, "No MediaRecorder ha ndler can be created."); | |
71 return; | |
72 } | |
73 | |
74 if (!m_recorderHandler->initialize(this, stream->descriptor(), m_mimeType)) { | |
75 m_stopped = true; | |
76 exceptionState.throwDOMException(NotSupportedError, "Failed to initializ e native MediaRecorder."); | |
77 return; | |
78 } | |
79 } | |
80 | |
81 String MediaRecorder::state() const | |
82 { | |
83 return StateToString(m_state); | |
84 } | |
85 | |
86 void MediaRecorder::start(ExceptionState& exceptionState) | |
87 { | |
88 if (m_state != State::Inactive) { | |
89 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
90 return; | |
91 } | |
92 m_state = State::Recording; | |
93 | |
94 m_recorderHandler->start(); | |
95 } | |
96 | |
97 void MediaRecorder::start(int timeSlice, ExceptionState& exceptionState) | |
98 { | |
99 if (m_state != State::Inactive) { | |
100 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
101 return; | |
102 } | |
103 m_state = State::Recording; | |
104 | |
105 m_recorderHandler->start(timeSlice); | |
106 } | |
107 | |
108 void MediaRecorder::stop(ExceptionState& exceptionState) | |
109 { | |
110 if (m_state == State::Inactive) { | |
111 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
112 return; | |
113 } | |
114 | |
115 // Raise a dataavailable event containing the Blob of data that has been gat hered. | |
116 // Raise a stop event. | |
117 doStop(); | |
118 } | |
119 | |
120 void MediaRecorder::pause(ExceptionState& exceptionState) | |
121 { | |
122 if (m_state == State::Inactive) { | |
123 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
124 return; | |
125 } | |
126 if (m_state == State::Paused) | |
127 return; | |
128 | |
129 m_state = State::Paused; | |
130 | |
131 m_recorderHandler->pause(); | |
132 | |
133 scheduleDispatchEvent(Event::create(EventTypeNames::pause)); | |
134 } | |
135 | |
136 void MediaRecorder::resume(ExceptionState& exceptionState) | |
137 { | |
138 if (m_state == State::Inactive) { | |
139 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
140 return; | |
141 } | |
142 if (m_state == State::Recording) | |
143 return; | |
144 | |
145 m_state = State::Recording; | |
146 | |
147 m_recorderHandler->resume(); | |
148 } | |
149 | |
150 void MediaRecorder::requestData(ExceptionState& exceptionState) | |
151 { | |
152 if (m_state != State::Recording) { | |
153 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); | |
154 return; | |
155 } | |
156 | |
157 createBlobEvent(BlobData::create()); | |
158 } | |
159 | |
160 String MediaRecorder::canRecordMimeType(const String& mimeType) | |
161 { | |
162 RawPtr<WebMediaRecorderHandler> handler = Platform::current()->createMediaRe corderHandler(); | |
163 if (!handler) | |
164 return ""; | |
165 | |
166 // MediaRecorder canRecordMimeType() MUST return 'probably' "if the UA is | |
167 // confident that mimeType represents a type that it can record" [1], but a | |
168 // number of reasons could prevent the recording from happening as expected, | |
169 // so 'maybe' is a better option: "Implementors are encouraged to return | |
170 // "maybe" unless the type can be confidently established as being supported | |
171 // or not.". Hence this method returns '' or 'maybe', never 'probably'. | |
172 // [1] http://w3c.github.io/mediacapture-record/MediaRecorder.html#methods | |
173 return handler->canSupportMimeType(mimeType) ? "maybe" : ""; | |
174 } | |
175 | |
176 const AtomicString& MediaRecorder::interfaceName() const | |
177 { | |
178 return EventTargetNames::MediaRecorder; | |
179 } | |
180 | |
181 ExecutionContext* MediaRecorder::executionContext() const | |
182 { | |
183 return ActiveDOMObject::executionContext(); | |
184 } | |
185 | |
186 void MediaRecorder::suspend() | |
187 { | |
188 m_dispatchScheduledEventRunner.suspend(); | |
Guido Urdaneta
2015/08/14 08:33:01
4-space indentation?
mcasas
2015/08/19 22:28:05
Done.
| |
189 } | |
190 | |
191 void MediaRecorder::resume() | |
192 { | |
193 m_dispatchScheduledEventRunner.resume(); | |
194 } | |
195 | |
196 void MediaRecorder::stop() | |
197 { | |
198 if (m_stopped) | |
199 return; | |
200 | |
201 m_stopped = true; | |
202 m_stream.clear(); | |
203 m_recorderHandler.clear(); | |
204 | |
205 m_dispatchScheduledEventRunner.stop(); | |
206 } | |
207 | |
208 void MediaRecorder::writeData(const char* data, size_t length, bool lastInSlice) | |
209 { | |
210 ASSERT(m_state == State::Recording); | |
211 | |
212 if (m_stopped) { | |
213 m_stopped = false; | |
214 scheduleDispatchEvent(Event::create(EventTypeNames::start)); | |
215 } | |
216 | |
217 // TODO(mcasas): Use |lastInSlice| to indicate to JS that recording is done. | |
218 | |
219 OwnPtr<BlobData> blobData = BlobData::create(); | |
220 blobData->appendBytes(data, length); | |
221 createBlobEvent(blobData.release()); | |
222 } | |
223 | |
224 void MediaRecorder::failOutOfMemory(const WebString& message) | |
225 { | |
226 scheduleDispatchEvent(MediaRecorderErrorEvent::create( | |
227 EventTypeNames::error, false, false, "OutOfMemory", message)); | |
228 | |
229 if (m_state == State::Recording) | |
230 doStop(); | |
231 } | |
232 | |
233 void MediaRecorder::failIllegalStreamModification(const WebString& message) | |
234 { | |
235 scheduleDispatchEvent(MediaRecorderErrorEvent::create( | |
236 EventTypeNames::error, false, false, "IllegalStreamModification", messag e)); | |
237 | |
238 if (m_state == State::Recording) | |
239 doStop(); | |
240 } | |
241 | |
242 void MediaRecorder::failOtherRecordingError(const WebString& message) | |
243 { | |
244 scheduleDispatchEvent(MediaRecorderErrorEvent::create( | |
245 EventTypeNames::error, false, false, "OtherRecordingError", message)); | |
246 | |
247 if (m_state == State::Recording) | |
248 doStop(); | |
249 } | |
250 | |
251 void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData) | |
252 { | |
253 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(blobData, blo bData->length()); | |
254 | |
255 scheduleDispatchEvent(BlobEvent::create(EventTypeNames::dataavailable, | |
256 false, false, Blob::create(blobDataHandle))); | |
257 } | |
258 | |
259 void MediaRecorder::doStop() | |
260 { | |
261 ASSERT(m_state != State::Inactive); | |
262 m_state = State::Inactive; | |
263 | |
264 m_recorderHandler->stop(); | |
265 | |
266 createBlobEvent(BlobData::create()); | |
267 | |
268 scheduleDispatchEvent(Event::create(EventTypeNames::stop)); | |
269 } | |
270 | |
271 void MediaRecorder::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event) | |
272 { | |
273 m_scheduledEvents.append(event); | |
274 | |
275 m_dispatchScheduledEventRunner.runAsync(); | |
276 } | |
277 | |
278 void MediaRecorder::dispatchScheduledEvent() | |
279 { | |
280 if (m_stopped) | |
281 return; | |
282 | |
283 WillBeHeapVector<RefPtrWillBeMember<Event>> events; | |
284 events.swap(m_scheduledEvents); | |
285 | |
286 WillBeHeapVector<RefPtrWillBeMember<Event>>::iterator it = events.begin(); | |
287 for (; it != events.end(); ++it) | |
288 dispatchEvent((*it).release()); | |
289 | |
290 events.clear(); | |
291 } | |
292 | |
293 DEFINE_TRACE(MediaRecorder) | |
294 { | |
295 visitor->trace(m_stream); | |
296 RefCountedGarbageCollectedEventTargetWithInlineData<MediaRecorder>::trace(vi sitor); | |
297 ActiveDOMObject::trace(visitor); | |
298 } | |
299 | |
300 } // namespace blink | |
OLD | NEW |