Index: Source/modules/mediastream/MediaRecorder.cpp |
diff --git a/Source/modules/mediastream/MediaRecorder.cpp b/Source/modules/mediastream/MediaRecorder.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..85f2eaa37675141481c520bdc220674cd1db92e7 |
--- /dev/null |
+++ b/Source/modules/mediastream/MediaRecorder.cpp |
@@ -0,0 +1,290 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "modules/mediastream/MediaRecorder.h" |
+ |
+#include "core/dom/DOMError.h" |
+#include "core/fileapi/Blob.h" |
+#include "modules/EventModules.h" |
+#include "modules/EventTargetModules.h" |
+#include "modules/mediastream/BlobEvent.h" |
+#include "modules/mediastream/MediaRecorderErrorEvent.h" |
+#include "public/platform/Platform.h" |
+#include "public/platform/WebMediaStream.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+static String StateToString(MediaRecorder::State state) |
+{ |
+ switch (state) { |
+ case MediaRecorder::State::Inactive: |
+ return "inactive"; |
+ case MediaRecorder::State::Recording: |
+ return "recording"; |
+ case MediaRecorder::State::Paused: |
+ return "paused"; |
+ } |
+ |
+ ASSERT_NOT_REACHED(); |
+ return String(); |
+} |
+ |
+} // namespace |
+ |
+MediaRecorder* MediaRecorder::create(ExecutionContext* context, MediaStream* stream, ExceptionState& exceptionState) |
+{ |
+ MediaRecorder* recorder = new MediaRecorder(context, stream, String(), exceptionState); |
+ recorder->suspendIfNeeded(); |
+ |
+ return recorder; |
+} |
+ |
+MediaRecorder* MediaRecorder::create(ExecutionContext* context, MediaStream* stream, const String& mimeType, ExceptionState& exceptionState) |
+{ |
+ MediaRecorder* recorder = new MediaRecorder(context, stream, mimeType, exceptionState); |
+ recorder->suspendIfNeeded(); |
+ |
+ return recorder; |
+} |
+ |
+MediaRecorder::MediaRecorder(ExecutionContext* context, MediaStream* stream, const String& mimeType, ExceptionState& exceptionState) |
+ : ActiveDOMObject(context) |
+ , m_stream(stream) |
+ , m_mimeType(mimeType) |
+ , m_stopped(false) |
+ , m_ignoreMutedMedia(true) |
+ , m_state(State::Inactive) |
+ , m_dispatchScheduledEventRunner(this, &MediaRecorder::dispatchScheduledEvent) |
+{ |
+ m_recorderHandler = adoptPtr(Platform::current()->createMediaRecorderHandler()); |
+ |
+ // TODO(mcasas): Once http://crbug.com/262211 has landed the Chromium parts, |
+ // and more concretetely the createMediaRecorderHandler() implementation, |
+ // ASSERT() here for |m_recorderHandler|. |
+ if (!m_recorderHandler) { |
+ m_stopped = true; |
+ exceptionState.throwDOMException(NotSupportedError, "No MediaRecorder handler can be created."); |
+ return; |
+ } |
+ |
+ if (!m_recorderHandler->initialize(this, stream->descriptor(), m_mimeType)) { |
+ m_stopped = true; |
+ exceptionState.throwDOMException(NotSupportedError, "Failed to initialize native MediaRecorder."); |
+ return; |
+ } |
+} |
+ |
+String MediaRecorder::state() const |
+{ |
+ return StateToString(m_state); |
+} |
+ |
+void MediaRecorder::start(ExceptionState& exceptionState) |
+{ |
+ if (m_state != State::Inactive) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ m_state = State::Recording; |
+ |
+ m_recorderHandler->start(); |
+} |
+ |
+void MediaRecorder::start(int timeSlice, ExceptionState& exceptionState) |
+{ |
+ if (m_state != State::Inactive) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ m_state = State::Recording; |
+ |
+ m_recorderHandler->start(timeSlice); |
+} |
+ |
+void MediaRecorder::stop(ExceptionState& exceptionState) |
+{ |
+ if (m_state == State::Inactive) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ |
+ // Raise a dataavailable event containing the Blob of data that has been gathered. |
+ // Raise a stop event. |
+ doStop(); |
+} |
+ |
+void MediaRecorder::pause(ExceptionState& exceptionState) |
+{ |
+ if (m_state == State::Inactive) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ if (m_state == State::Paused) |
+ return; |
+ |
+ m_state = State::Paused; |
+ |
+ m_recorderHandler->pause(); |
+ |
+ scheduleDispatchEvent(Event::create(EventTypeNames::pause)); |
+} |
+ |
+void MediaRecorder::resume(ExceptionState& exceptionState) |
+{ |
+ if (m_state == State::Inactive) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ if (m_state == State::Recording) |
+ return; |
+ |
+ m_state = State::Recording; |
+ |
+ m_recorderHandler->resume(); |
+} |
+ |
+void MediaRecorder::requestData(ExceptionState& exceptionState) |
+{ |
+ if (m_state != State::Recording) { |
+ exceptionState.throwDOMException(InvalidStateError, "The MediaRecorder's state is '" + StateToString(m_state) + "'."); |
+ return; |
+ } |
+ |
+ createBlobEvent(BlobData::create()); |
+} |
+ |
+String MediaRecorder::canRecordMimeType(const String& mimeType) |
+{ |
+ RawPtr<WebMediaRecorderHandler> handler = Platform::current()->createMediaRecorderHandler(); |
+ if (!handler) |
+ return ""; |
+ |
+ // MediaRecorder canRecordMimeType() MUST return 'probably' "if the UA is |
+ // confident that mimeType represents a type that it can record" [1], but a |
+ // number of reasons could prevent the recording from happening as expected, |
+ // so 'maybe' is a better option: "Implementors are encouraged to return |
+ // "maybe" unless the type can be confidently established as being supported |
+ // or not.". Hence this method returns '' or 'maybe', never 'probably'. |
+ // [1] http://w3c.github.io/mediacapture-record/MediaRecorder.html#methods |
+ return handler->canSupportMimeType(mimeType) ? "maybe" : ""; |
+} |
+ |
+const AtomicString& MediaRecorder::interfaceName() const |
+{ |
+ return EventTargetNames::MediaRecorder; |
+} |
+ |
+ExecutionContext* MediaRecorder::executionContext() const |
+{ |
+ return ActiveDOMObject::executionContext(); |
+} |
+ |
+void MediaRecorder::stop() |
+{ |
+ if (m_stopped) |
+ return; |
+ |
+ m_stopped = true; |
+ m_stream.clear(); |
+ m_recorderHandler.clear(); |
+ |
+ m_dispatchScheduledEventRunner.stop(); |
+} |
+ |
+void MediaRecorder::writeData(const char* data, size_t length, bool lastInSlice) |
+{ |
+ ASSERT(m_state == State::Recording); |
+ |
+ if (m_stopped) { |
+ m_stopped = false; |
+ scheduleDispatchEvent(Event::create(EventTypeNames::start)); |
+ } |
+ |
+ // TODO(mcasas): Use |lastInSlice| to indicate to JS that recording is done. |
+ |
+ OwnPtr<BlobData> blobData = BlobData::create(); |
+ blobData->appendBytes(data, length); |
+ createBlobEvent(blobData.release()); |
+} |
+ |
+void MediaRecorder::failOutOfMemory(const WebString& message) |
+{ |
+ scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
+ EventTypeNames::error, false, false, "OutOfMemory", message)); |
+ |
+ if (m_state == State::Recording) |
+ doStop(); |
+} |
+ |
+void MediaRecorder::failIllegalStreamModification(const WebString& message) |
+{ |
+ scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
+ EventTypeNames::error, false, false, "IllegalStreamModification", message)); |
+ |
+ if (m_state == State::Recording) |
+ doStop(); |
+} |
+ |
+void MediaRecorder::failOtherRecordingError(const WebString& message) |
+{ |
+ scheduleDispatchEvent(MediaRecorderErrorEvent::create( |
+ EventTypeNames::error, false, false, "OtherRecordingError", message)); |
+ |
+ if (m_state == State::Recording) |
+ doStop(); |
+} |
+ |
+void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData) |
+{ |
+ RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(blobData, blobData->length()); |
+ |
+ scheduleDispatchEvent(BlobEvent::create(EventTypeNames::dataavailable, |
+ false, false, Blob::create(blobDataHandle))); |
+} |
+ |
+void MediaRecorder::doStop() |
+{ |
+ ASSERT(m_state != State::Inactive); |
+ m_state = State::Inactive; |
+ |
+ m_recorderHandler->stop(); |
+ |
+ createBlobEvent(BlobData::create()); |
+ |
+ scheduleDispatchEvent(Event::create(EventTypeNames::stop)); |
+} |
+ |
+void MediaRecorder::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event) |
+{ |
+ m_scheduledEvents.append(event); |
+ |
+ m_dispatchScheduledEventRunner.runAsync(); |
+} |
+ |
+void MediaRecorder::dispatchScheduledEvent() |
+{ |
+ if (m_stopped) |
+ return; |
+ |
+ WillBeHeapVector<RefPtrWillBeMember<Event>> events; |
+ events.swap(m_scheduledEvents); |
+ |
+ WillBeHeapVector<RefPtrWillBeMember<Event>>::iterator it = events.begin(); |
+ for (; it != events.end(); ++it) |
+ dispatchEvent((*it).release()); |
+ |
+ events.clear(); |
+} |
+ |
+DEFINE_TRACE(MediaRecorder) |
+{ |
+ visitor->trace(m_stream); |
+ RefCountedGarbageCollectedEventTargetWithInlineData<MediaRecorder>::trace(visitor); |
+ ActiveDOMObject::trace(visitor); |
+} |
+ |
+} // namespace blink |