Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1165)

Unified Diff: Source/modules/mediarecorder/MediaRecorder.cpp

Issue 1255873002: MediaRecorder Blink part (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: peter@ comments. Removed BlobEvent, moved MediaRecorder* to Source/modules/mediarecorder Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/modules/mediarecorder/MediaRecorder.cpp
diff --git a/Source/modules/mediarecorder/MediaRecorder.cpp b/Source/modules/mediarecorder/MediaRecorder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..92f971e7c739a28cfcb4abc9bee6c0ef269b4245
--- /dev/null
+++ b/Source/modules/mediarecorder/MediaRecorder.cpp
@@ -0,0 +1,292 @@
+// 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/mediarecorder/MediaRecorder.h"
+
+#include "core/dom/DOMError.h"
+#include "core/fileapi/Blob.h"
+#include "modules/EventModules.h"
+#include "modules/EventTargetModules.h"
+#include "modules/mediarecorder/MediaRecorderErrorEvent.h"
+#include "platform/blob/BlobData.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(true)
+ , m_ignoreMutedMedia(true)
Peter Beverloo 2015/09/01 17:17:12 We currently don't use this property, and only ref
mcasas 2015/09/02 03:10:17 Done.
+ , 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) {
+ exceptionState.throwDOMException(NotSupportedError, "No MediaRecorder handler can be created.");
+ return;
+ }
+
+ if (!m_recorderHandler->initialize(this, stream->descriptor(), m_mimeType)) {
+ exceptionState.throwDOMException(NotSupportedError, "Failed to initialize native MediaRecorder.");
Peter Beverloo 2015/09/01 17:17:12 The specification states the following: "If the U
mcasas 2015/09/02 03:10:18 Done.
+ return;
+ }
+ m_stopped = false;
+}
+
+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;
+ }
+
+ stopRecording();
+}
+
+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 emptyString();
+
+ // 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" : emptyString();
+}
+
+const AtomicString& MediaRecorder::interfaceName() const
+{
+ return EventTargetNames::MediaRecorder;
+}
+
+ExecutionContext* MediaRecorder::executionContext() const
+{
+ return ActiveDOMObject::executionContext();
+}
+
+void MediaRecorder::suspend()
+{
+ m_dispatchScheduledEventRunner.suspend();
+}
+
+void MediaRecorder::resume()
+{
+ m_dispatchScheduledEventRunner.resume();
+}
+
+void MediaRecorder::stop()
+{
+ if (m_stopped)
+ return;
+
+ m_stopped = true;
+ m_stream.clear();
+ m_recorderHandler.clear();
+
+ scheduleDispatchEvent(Event::create(EventTypeNames::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(
Peter Beverloo 2015/09/01 17:17:12 nit: note that you don't have to wrap the line. I
mcasas 2015/09/02 03:10:17 Acknowledged.
+ EventTypeNames::error, false, false, "OutOfMemory", message));
+
+ if (m_state == State::Recording)
+ stopRecording();
+}
+
+void MediaRecorder::failIllegalStreamModification(const WebString& message)
+{
+ scheduleDispatchEvent(MediaRecorderErrorEvent::create(
+ EventTypeNames::error, false, false, "IllegalStreamModification", message));
+
+ if (m_state == State::Recording)
+ stopRecording();
+}
+
+void MediaRecorder::failOtherRecordingError(const WebString& message)
+{
+ scheduleDispatchEvent(MediaRecorderErrorEvent::create(
+ EventTypeNames::error, false, false, "OtherRecordingError", message));
+
+ if (m_state == State::Recording)
+ stopRecording();
+}
+
+void MediaRecorder::createBlobEvent(PassOwnPtr<BlobData> blobData)
+{
+ // TODO(mcasas): Launch a BlobEvent when that class is landed.
+ ASSERT_NOT_REACHED();
+}
+
+void MediaRecorder::stopRecording()
+{
+ 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()
+{
+ 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

Powered by Google App Engine
This is Rietveld 408576698