1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/MediaRecorder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef MediaRecorder_h 1.11 +#define MediaRecorder_h 1.12 + 1.13 +#include "mozilla/dom/MediaRecorderBinding.h" 1.14 +#include "mozilla/DOMEventTargetHelper.h" 1.15 + 1.16 +// Max size for allowing queue encoded data in memory 1.17 +#define MAX_ALLOW_MEMORY_BUFFER 1024000 1.18 +namespace mozilla { 1.19 + 1.20 +class ErrorResult; 1.21 +class DOMMediaStream; 1.22 +class EncodedBufferCache; 1.23 +class MediaEncoder; 1.24 +class ProcessedMediaStream; 1.25 +class MediaInputPort; 1.26 +struct MediaRecorderOptions; 1.27 + 1.28 +namespace dom { 1.29 + 1.30 +/** 1.31 + * Implementation of https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-capture/MediaRecorder.html 1.32 + * The MediaRecorder accepts a mediaStream as input source passed from UA. When recorder starts, 1.33 + * a MediaEncoder will be created and accept the mediaStream as input source. 1.34 + * Encoder will get the raw data by track data changes, encode it by selected MIME Type, then store the encoded in EncodedBufferCache object. 1.35 + * The encoded data will be extracted on every timeslice passed from Start function call or by RequestData function. 1.36 + * Thread model: 1.37 + * When the recorder starts, it creates a "Media Encoder" thread to read data from MediaEncoder object and store buffer in EncodedBufferCache object. 1.38 + * Also extract the encoded data and create blobs on every timeslice passed from start function or RequestData function called by UA. 1.39 + */ 1.40 + 1.41 +class MediaRecorder : public DOMEventTargetHelper 1.42 +{ 1.43 + class Session; 1.44 + friend class CreateAndDispatchBlobEventRunnable; 1.45 + 1.46 +public: 1.47 + MediaRecorder(DOMMediaStream&, nsPIDOMWindow* aOwnerWindow); 1.48 + virtual ~MediaRecorder(); 1.49 + 1.50 + // nsWrapperCache 1.51 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.52 + 1.53 + nsPIDOMWindow* GetParentObject() { return GetOwner(); } 1.54 + 1.55 + NS_DECL_ISUPPORTS_INHERITED 1.56 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaRecorder, 1.57 + DOMEventTargetHelper) 1.58 + 1.59 + // WebIDL 1.60 + // Start recording. If timeSlice has been provided, mediaRecorder will 1.61 + // raise a dataavailable event containing the Blob of collected data on every timeSlice milliseconds. 1.62 + // If timeSlice isn't provided, UA should call the RequestData to obtain the Blob data, also set the mTimeSlice to zero. 1.63 + void Start(const Optional<int32_t>& timeSlice, ErrorResult & aResult); 1.64 + // Stop the recording activiy. Including stop the Media Encoder thread, un-hook the mediaStreamListener to encoder. 1.65 + void Stop(ErrorResult& aResult); 1.66 + // Pause the mTrackUnionStream 1.67 + void Pause(ErrorResult& aResult); 1.68 + 1.69 + void Resume(ErrorResult& aResult); 1.70 + // Extract encoded data Blob from EncodedBufferCache. 1.71 + void RequestData(ErrorResult& aResult); 1.72 + // Return the The DOMMediaStream passed from UA. 1.73 + DOMMediaStream* Stream() const { return mStream; } 1.74 + // The current state of the MediaRecorder object. 1.75 + RecordingState State() const { return mState; } 1.76 + // Return the current encoding MIME type selected by the MediaEncoder. 1.77 + void GetMimeType(nsString &aMimeType); 1.78 + 1.79 + static already_AddRefed<MediaRecorder> 1.80 + Constructor(const GlobalObject& aGlobal, 1.81 + DOMMediaStream& aStream, 1.82 + const MediaRecorderOptions& aInitDict, 1.83 + ErrorResult& aRv); 1.84 + 1.85 + // EventHandler 1.86 + IMPL_EVENT_HANDLER(dataavailable) 1.87 + IMPL_EVENT_HANDLER(error) 1.88 + IMPL_EVENT_HANDLER(stop) 1.89 + IMPL_EVENT_HANDLER(warning) 1.90 + 1.91 +protected: 1.92 + MediaRecorder& operator = (const MediaRecorder& x) MOZ_DELETE; 1.93 + // Create dataavailable event with Blob data and it runs in main thread 1.94 + nsresult CreateAndDispatchBlobEvent(already_AddRefed<nsIDOMBlob>&& aBlob); 1.95 + // Creating a simple event to notify UA simple event. 1.96 + void DispatchSimpleEvent(const nsAString & aStr); 1.97 + // Creating a error event with message. 1.98 + void NotifyError(nsresult aRv); 1.99 + // Check if the recorder's principal is the subsume of mediaStream 1.100 + bool CheckPrincipal(); 1.101 + // Set encoded MIME type. 1.102 + void SetMimeType(const nsString &aMimeType); 1.103 + 1.104 + MediaRecorder(const MediaRecorder& x) MOZ_DELETE; // prevent bad usage 1.105 + // Remove session pointer. 1.106 + void RemoveSession(Session* aSession); 1.107 + // MediaStream passed from js context 1.108 + nsRefPtr<DOMMediaStream> mStream; 1.109 + // The current state of the MediaRecorder object. 1.110 + RecordingState mState; 1.111 + // Hold the sessions pointer in media recorder and clean in the destructor of recorder. 1.112 + nsTArray<Session*> mSessions; 1.113 + // Thread safe for mMimeType. 1.114 + Mutex mMutex; 1.115 + // It specifies the container format as well as the audio and video capture formats. 1.116 + nsString mMimeType; 1.117 +}; 1.118 + 1.119 +} 1.120 +} 1.121 + 1.122 +#endif