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(true) |
| 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 exceptionState.throwDOMException(NotSupportedError, "No MediaRecorder ha
ndler can be created."); |
| 70 return; |
| 71 } |
| 72 |
| 73 if (!m_recorderHandler->initialize(this, stream->descriptor(), m_mimeType))
{ |
| 74 exceptionState.throwDOMException(NotSupportedError, "Failed to initializ
e native MediaRecorder."); |
| 75 return; |
| 76 } |
| 77 m_stopped = false; |
| 78 } |
| 79 |
| 80 String MediaRecorder::state() const |
| 81 { |
| 82 return stateToString(m_state); |
| 83 } |
| 84 |
| 85 void MediaRecorder::start(ExceptionState& exceptionState) |
| 86 { |
| 87 if (m_state != State::Inactive) { |
| 88 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 89 return; |
| 90 } |
| 91 m_state = State::Recording; |
| 92 |
| 93 m_recorderHandler->start(); |
| 94 } |
| 95 |
| 96 void MediaRecorder::start(int timeSlice, ExceptionState& exceptionState) |
| 97 { |
| 98 if (m_state != State::Inactive) { |
| 99 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 100 return; |
| 101 } |
| 102 m_state = State::Recording; |
| 103 |
| 104 m_recorderHandler->start(timeSlice); |
| 105 } |
| 106 |
| 107 void MediaRecorder::stop(ExceptionState& exceptionState) |
| 108 { |
| 109 if (m_state == State::Inactive) { |
| 110 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 111 return; |
| 112 } |
| 113 |
| 114 stopRecording(); |
| 115 } |
| 116 |
| 117 void MediaRecorder::pause(ExceptionState& exceptionState) |
| 118 { |
| 119 if (m_state == State::Inactive) { |
| 120 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 121 return; |
| 122 } |
| 123 if (m_state == State::Paused) |
| 124 return; |
| 125 |
| 126 m_state = State::Paused; |
| 127 |
| 128 m_recorderHandler->pause(); |
| 129 |
| 130 scheduleDispatchEvent(Event::create(EventTypeNames::pause)); |
| 131 } |
| 132 |
| 133 void MediaRecorder::resume(ExceptionState& exceptionState) |
| 134 { |
| 135 if (m_state == State::Inactive) { |
| 136 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 137 return; |
| 138 } |
| 139 if (m_state == State::Recording) |
| 140 return; |
| 141 |
| 142 m_state = State::Recording; |
| 143 |
| 144 m_recorderHandler->resume(); |
| 145 } |
| 146 |
| 147 void MediaRecorder::requestData(ExceptionState& exceptionState) |
| 148 { |
| 149 if (m_state != State::Recording) { |
| 150 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 151 return; |
| 152 } |
| 153 |
| 154 createBlobEvent(BlobData::create()); |
| 155 } |
| 156 |
| 157 String MediaRecorder::canRecordMimeType(const String& mimeType) |
| 158 { |
| 159 RawPtr<WebMediaRecorderHandler> handler = Platform::current()->createMediaRe
corderHandler(); |
| 160 if (!handler) |
| 161 return emptyString(); |
| 162 |
| 163 // MediaRecorder canRecordMimeType() MUST return 'probably' "if the UA is |
| 164 // confident that mimeType represents a type that it can record" [1], but a |
| 165 // number of reasons could prevent the recording from happening as expected, |
| 166 // so 'maybe' is a better option: "Implementors are encouraged to return |
| 167 // "maybe" unless the type can be confidently established as being supported |
| 168 // or not.". Hence this method returns '' or 'maybe', never 'probably'. |
| 169 // [1] http://w3c.github.io/mediacapture-record/MediaRecorder.html#methods |
| 170 return handler->canSupportMimeType(mimeType) ? "maybe" : emptyString(); |
| 171 } |
| 172 |
| 173 const AtomicString& MediaRecorder::interfaceName() const |
| 174 { |
| 175 return EventTargetNames::MediaRecorder; |
| 176 } |
| 177 |
| 178 ExecutionContext* MediaRecorder::executionContext() const |
| 179 { |
| 180 return ActiveDOMObject::executionContext(); |
| 181 } |
| 182 |
| 183 void MediaRecorder::suspend() |
| 184 { |
| 185 m_dispatchScheduledEventRunner.suspend(); |
| 186 } |
| 187 |
| 188 void MediaRecorder::resume() |
| 189 { |
| 190 m_dispatchScheduledEventRunner.resume(); |
| 191 } |
| 192 |
| 193 void MediaRecorder::stop() |
| 194 { |
| 195 if (m_stopped) |
| 196 return; |
| 197 |
| 198 m_stopped = true; |
| 199 m_stream.clear(); |
| 200 m_recorderHandler.clear(); |
| 201 |
| 202 scheduleDispatchEvent(Event::create(EventTypeNames::stop)); |
| 203 } |
| 204 |
| 205 void MediaRecorder::writeData(const char* data, size_t length, bool lastInSlice) |
| 206 { |
| 207 ASSERT(m_state == State::Recording); |
| 208 |
| 209 if (m_stopped) { |
| 210 m_stopped = false; |
| 211 scheduleDispatchEvent(Event::create(EventTypeNames::start)); |
| 212 } |
| 213 |
| 214 // TODO(mcasas): Use |lastInSlice| to indicate to JS that recording is done. |
| 215 |
| 216 OwnPtr<BlobData> blobData = BlobData::create(); |
| 217 blobData->appendBytes(data, length); |
| 218 createBlobEvent(blobData.release()); |
| 219 } |
| 220 |
| 221 void MediaRecorder::failOutOfMemory(const WebString& message) |
| 222 { |
| 223 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 224 EventTypeNames::error, false, false, "OutOfMemory", message)); |
| 225 |
| 226 if (m_state == State::Recording) |
| 227 stopRecording(); |
| 228 } |
| 229 |
| 230 void MediaRecorder::failIllegalStreamModification(const WebString& message) |
| 231 { |
| 232 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 233 EventTypeNames::error, false, false, "IllegalStreamModification", messag
e)); |
| 234 |
| 235 if (m_state == State::Recording) |
| 236 stopRecording(); |
| 237 } |
| 238 |
| 239 void MediaRecorder::failOtherRecordingError(const WebString& message) |
| 240 { |
| 241 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 242 EventTypeNames::error, false, false, "OtherRecordingError", message)); |
| 243 |
| 244 if (m_state == State::Recording) |
| 245 stopRecording(); |
| 246 } |
| 247 |
| 248 void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData) |
| 249 { |
| 250 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(blobData, blo
bData->length()); |
| 251 |
| 252 scheduleDispatchEvent(BlobEvent::create(EventTypeNames::dataavailable, |
| 253 false, false, Blob::create(blobDataHandle))); |
| 254 } |
| 255 |
| 256 void MediaRecorder::stopRecording() |
| 257 { |
| 258 ASSERT(m_state != State::Inactive); |
| 259 m_state = State::Inactive; |
| 260 |
| 261 m_recorderHandler->stop(); |
| 262 |
| 263 createBlobEvent(BlobData::create()); |
| 264 |
| 265 scheduleDispatchEvent(Event::create(EventTypeNames::stop)); |
| 266 } |
| 267 |
| 268 void MediaRecorder::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event) |
| 269 { |
| 270 m_scheduledEvents.append(event); |
| 271 |
| 272 m_dispatchScheduledEventRunner.runAsync(); |
| 273 } |
| 274 |
| 275 void MediaRecorder::dispatchScheduledEvent() |
| 276 { |
| 277 WillBeHeapVector<RefPtrWillBeMember<Event>> events; |
| 278 events.swap(m_scheduledEvents); |
| 279 |
| 280 WillBeHeapVector<RefPtrWillBeMember<Event>>::iterator it = events.begin(); |
| 281 for (; it != events.end(); ++it) |
| 282 dispatchEvent((*it).release()); |
| 283 |
| 284 events.clear(); |
| 285 } |
| 286 |
| 287 DEFINE_TRACE(MediaRecorder) |
| 288 { |
| 289 visitor->trace(m_stream); |
| 290 RefCountedGarbageCollectedEventTargetWithInlineData<MediaRecorder>::trace(vi
sitor); |
| 291 ActiveDOMObject::trace(visitor); |
| 292 } |
| 293 |
| 294 } // namespace blink |
OLD | NEW |