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::stop() |
| 187 { |
| 188 if (m_stopped) |
| 189 return; |
| 190 |
| 191 m_stopped = true; |
| 192 m_stream.clear(); |
| 193 m_recorderHandler.clear(); |
| 194 |
| 195 m_dispatchScheduledEventRunner.stop(); |
| 196 } |
| 197 |
| 198 void MediaRecorder::writeData(const char* data, size_t length, bool lastInSlice) |
| 199 { |
| 200 ASSERT(m_state == State::Recording); |
| 201 |
| 202 if (m_stopped) { |
| 203 m_stopped = false; |
| 204 scheduleDispatchEvent(Event::create(EventTypeNames::start)); |
| 205 } |
| 206 |
| 207 // TODO(mcasas): Use |lastInSlice| to indicate to JS that recording is done. |
| 208 |
| 209 OwnPtr<BlobData> blobData = BlobData::create(); |
| 210 blobData->appendBytes(data, length); |
| 211 createBlobEvent(blobData.release()); |
| 212 } |
| 213 |
| 214 void MediaRecorder::failOutOfMemory(const WebString& message) |
| 215 { |
| 216 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 217 EventTypeNames::error, false, false, "OutOfMemory", message)); |
| 218 |
| 219 if (m_state == State::Recording) |
| 220 doStop(); |
| 221 } |
| 222 |
| 223 void MediaRecorder::failIllegalStreamModification(const WebString& message) |
| 224 { |
| 225 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 226 EventTypeNames::error, false, false, "IllegalStreamModification", messag
e)); |
| 227 |
| 228 if (m_state == State::Recording) |
| 229 doStop(); |
| 230 } |
| 231 |
| 232 void MediaRecorder::failOtherRecordingError(const WebString& message) |
| 233 { |
| 234 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 235 EventTypeNames::error, false, false, "OtherRecordingError", message)); |
| 236 |
| 237 if (m_state == State::Recording) |
| 238 doStop(); |
| 239 } |
| 240 |
| 241 void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData) |
| 242 { |
| 243 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(blobData, blo
bData->length()); |
| 244 |
| 245 scheduleDispatchEvent(BlobEvent::create(EventTypeNames::dataavailable, |
| 246 false, false, Blob::create(blobDataHandle))); |
| 247 } |
| 248 |
| 249 void MediaRecorder::doStop() |
| 250 { |
| 251 ASSERT(m_state != State::Inactive); |
| 252 m_state = State::Inactive; |
| 253 |
| 254 m_recorderHandler->stop(); |
| 255 |
| 256 createBlobEvent(BlobData::create()); |
| 257 |
| 258 scheduleDispatchEvent(Event::create(EventTypeNames::stop)); |
| 259 } |
| 260 |
| 261 void MediaRecorder::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event) |
| 262 { |
| 263 m_scheduledEvents.append(event); |
| 264 |
| 265 m_dispatchScheduledEventRunner.runAsync(); |
| 266 } |
| 267 |
| 268 void MediaRecorder::dispatchScheduledEvent() |
| 269 { |
| 270 if (m_stopped) |
| 271 return; |
| 272 |
| 273 WillBeHeapVector<RefPtrWillBeMember<Event>> events; |
| 274 events.swap(m_scheduledEvents); |
| 275 |
| 276 WillBeHeapVector<RefPtrWillBeMember<Event>>::iterator it = events.begin(); |
| 277 for (; it != events.end(); ++it) |
| 278 dispatchEvent((*it).release()); |
| 279 |
| 280 events.clear(); |
| 281 } |
| 282 |
| 283 DEFINE_TRACE(MediaRecorder) |
| 284 { |
| 285 visitor->trace(m_stream); |
| 286 RefCountedGarbageCollectedEventTargetWithInlineData<MediaRecorder>::trace(vi
sitor); |
| 287 ActiveDOMObject::trace(visitor); |
| 288 } |
| 289 |
| 290 } // namespace blink |
OLD | NEW |