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/mediarecorder/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/mediarecorder/MediaRecorderErrorEvent.h" |
| 13 #include "platform/blob/BlobData.h" |
| 14 #include "public/platform/Platform.h" |
| 15 #include "public/platform/WebMediaStream.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 namespace { |
| 20 |
| 21 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 |
| 69 // We deviate from the spec by not returning |UnsupportedOption|, see https:
//github.com/w3c/mediacapture-record/issues/18 |
| 70 if (!m_recorderHandler) { |
| 71 exceptionState.throwDOMException(NotSupportedError, "No MediaRecorder ha
ndler can be created."); |
| 72 return; |
| 73 } |
| 74 if (!m_recorderHandler->initialize(this, stream->descriptor(), m_mimeType))
{ |
| 75 exceptionState.throwDOMException(NotSupportedError, "Failed to initializ
e native MediaRecorder, the type provided " + m_mimeType + "is unsupported." ); |
| 76 return; |
| 77 } |
| 78 m_stopped = false; |
| 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 stopRecording(); |
| 116 } |
| 117 |
| 118 void MediaRecorder::pause(ExceptionState& exceptionState) |
| 119 { |
| 120 if (m_state == State::Inactive) { |
| 121 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 122 return; |
| 123 } |
| 124 if (m_state == State::Paused) |
| 125 return; |
| 126 |
| 127 m_state = State::Paused; |
| 128 |
| 129 m_recorderHandler->pause(); |
| 130 |
| 131 scheduleDispatchEvent(Event::create(EventTypeNames::pause)); |
| 132 } |
| 133 |
| 134 void MediaRecorder::resume(ExceptionState& exceptionState) |
| 135 { |
| 136 if (m_state == State::Inactive) { |
| 137 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 138 return; |
| 139 } |
| 140 if (m_state == State::Recording) |
| 141 return; |
| 142 |
| 143 m_state = State::Recording; |
| 144 |
| 145 m_recorderHandler->resume(); |
| 146 } |
| 147 |
| 148 void MediaRecorder::requestData(ExceptionState& exceptionState) |
| 149 { |
| 150 if (m_state != State::Recording) { |
| 151 exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's
state is '" + stateToString(m_state) + "'."); |
| 152 return; |
| 153 } |
| 154 |
| 155 createBlobEvent(BlobData::create()); |
| 156 } |
| 157 |
| 158 String MediaRecorder::canRecordMimeType(const String& mimeType) |
| 159 { |
| 160 RawPtr<WebMediaRecorderHandler> handler = Platform::current()->createMediaRe
corderHandler(); |
| 161 if (!handler) |
| 162 return emptyString(); |
| 163 |
| 164 // MediaRecorder canRecordMimeType() MUST return 'probably' "if the UA is |
| 165 // confident that mimeType represents a type that it can record" [1], but a |
| 166 // number of reasons could prevent the recording from happening as expected, |
| 167 // so 'maybe' is a better option: "Implementors are encouraged to return |
| 168 // "maybe" unless the type can be confidently established as being supported |
| 169 // or not.". Hence this method returns '' or 'maybe', never 'probably'. |
| 170 // [1] http://w3c.github.io/mediacapture-record/MediaRecorder.html#methods |
| 171 return handler->canSupportMimeType(mimeType) ? "maybe" : emptyString(); |
| 172 } |
| 173 |
| 174 const AtomicString& MediaRecorder::interfaceName() const |
| 175 { |
| 176 return EventTargetNames::MediaRecorder; |
| 177 } |
| 178 |
| 179 ExecutionContext* MediaRecorder::executionContext() const |
| 180 { |
| 181 return ActiveDOMObject::executionContext(); |
| 182 } |
| 183 |
| 184 void MediaRecorder::suspend() |
| 185 { |
| 186 m_dispatchScheduledEventRunner.suspend(); |
| 187 } |
| 188 |
| 189 void MediaRecorder::resume() |
| 190 { |
| 191 m_dispatchScheduledEventRunner.resume(); |
| 192 } |
| 193 |
| 194 void MediaRecorder::stop() |
| 195 { |
| 196 if (m_stopped) |
| 197 return; |
| 198 |
| 199 m_stopped = true; |
| 200 m_stream.clear(); |
| 201 m_recorderHandler.clear(); |
| 202 |
| 203 scheduleDispatchEvent(Event::create(EventTypeNames::stop)); |
| 204 } |
| 205 |
| 206 void MediaRecorder::writeData(const char* data, size_t length, bool lastInSlice) |
| 207 { |
| 208 ASSERT(m_state == State::Recording); |
| 209 |
| 210 if (m_stopped) { |
| 211 m_stopped = false; |
| 212 scheduleDispatchEvent(Event::create(EventTypeNames::start)); |
| 213 } |
| 214 |
| 215 // TODO(mcasas): Act as |m_ignoredMutedMedia| instructs if |m_stream| track(
s) is in muted() state. |
| 216 // TODO(mcasas): Use |lastInSlice| to indicate to JS that recording is done. |
| 217 |
| 218 OwnPtr<BlobData> blobData = BlobData::create(); |
| 219 blobData->appendBytes(data, length); |
| 220 createBlobEvent(blobData.release()); |
| 221 } |
| 222 |
| 223 void MediaRecorder::failOutOfMemory(const WebString& message) |
| 224 { |
| 225 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 226 EventTypeNames::error, false, false, "OutOfMemory", message)); |
| 227 |
| 228 if (m_state == State::Recording) |
| 229 stopRecording(); |
| 230 } |
| 231 |
| 232 void MediaRecorder::failIllegalStreamModification(const WebString& message) |
| 233 { |
| 234 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 235 EventTypeNames::error, false, false, "IllegalStreamModification", messag
e)); |
| 236 |
| 237 if (m_state == State::Recording) |
| 238 stopRecording(); |
| 239 } |
| 240 |
| 241 void MediaRecorder::failOtherRecordingError(const WebString& message) |
| 242 { |
| 243 scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
| 244 EventTypeNames::error, false, false, "OtherRecordingError", message)); |
| 245 |
| 246 if (m_state == State::Recording) |
| 247 stopRecording(); |
| 248 } |
| 249 |
| 250 void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData) |
| 251 { |
| 252 // TODO(mcasas): Launch a BlobEvent when that class is landed, but also see
https://github.com/w3c/mediacapture-record/issues/17. |
| 253 ASSERT_NOT_REACHED(); |
| 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 for (const auto& event : events) |
| 281 dispatchEvent(event); |
| 282 } |
| 283 |
| 284 DEFINE_TRACE(MediaRecorder) |
| 285 { |
| 286 visitor->trace(m_stream); |
| 287 RefCountedGarbageCollectedEventTargetWithInlineData<MediaRecorder>::trace(vi
sitor); |
| 288 ActiveDOMObject::trace(visitor); |
| 289 } |
| 290 |
| 291 } // namespace blink |
OLD | NEW |