Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef MediaRecorder_h |
michael@0 | 8 | #define MediaRecorder_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/dom/MediaRecorderBinding.h" |
michael@0 | 11 | #include "mozilla/DOMEventTargetHelper.h" |
michael@0 | 12 | |
michael@0 | 13 | // Max size for allowing queue encoded data in memory |
michael@0 | 14 | #define MAX_ALLOW_MEMORY_BUFFER 1024000 |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | class ErrorResult; |
michael@0 | 18 | class DOMMediaStream; |
michael@0 | 19 | class EncodedBufferCache; |
michael@0 | 20 | class MediaEncoder; |
michael@0 | 21 | class ProcessedMediaStream; |
michael@0 | 22 | class MediaInputPort; |
michael@0 | 23 | struct MediaRecorderOptions; |
michael@0 | 24 | |
michael@0 | 25 | namespace dom { |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * Implementation of https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-capture/MediaRecorder.html |
michael@0 | 29 | * The MediaRecorder accepts a mediaStream as input source passed from UA. When recorder starts, |
michael@0 | 30 | * a MediaEncoder will be created and accept the mediaStream as input source. |
michael@0 | 31 | * Encoder will get the raw data by track data changes, encode it by selected MIME Type, then store the encoded in EncodedBufferCache object. |
michael@0 | 32 | * The encoded data will be extracted on every timeslice passed from Start function call or by RequestData function. |
michael@0 | 33 | * Thread model: |
michael@0 | 34 | * When the recorder starts, it creates a "Media Encoder" thread to read data from MediaEncoder object and store buffer in EncodedBufferCache object. |
michael@0 | 35 | * Also extract the encoded data and create blobs on every timeslice passed from start function or RequestData function called by UA. |
michael@0 | 36 | */ |
michael@0 | 37 | |
michael@0 | 38 | class MediaRecorder : public DOMEventTargetHelper |
michael@0 | 39 | { |
michael@0 | 40 | class Session; |
michael@0 | 41 | friend class CreateAndDispatchBlobEventRunnable; |
michael@0 | 42 | |
michael@0 | 43 | public: |
michael@0 | 44 | MediaRecorder(DOMMediaStream&, nsPIDOMWindow* aOwnerWindow); |
michael@0 | 45 | virtual ~MediaRecorder(); |
michael@0 | 46 | |
michael@0 | 47 | // nsWrapperCache |
michael@0 | 48 | virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 49 | |
michael@0 | 50 | nsPIDOMWindow* GetParentObject() { return GetOwner(); } |
michael@0 | 51 | |
michael@0 | 52 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 53 | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaRecorder, |
michael@0 | 54 | DOMEventTargetHelper) |
michael@0 | 55 | |
michael@0 | 56 | // WebIDL |
michael@0 | 57 | // Start recording. If timeSlice has been provided, mediaRecorder will |
michael@0 | 58 | // raise a dataavailable event containing the Blob of collected data on every timeSlice milliseconds. |
michael@0 | 59 | // If timeSlice isn't provided, UA should call the RequestData to obtain the Blob data, also set the mTimeSlice to zero. |
michael@0 | 60 | void Start(const Optional<int32_t>& timeSlice, ErrorResult & aResult); |
michael@0 | 61 | // Stop the recording activiy. Including stop the Media Encoder thread, un-hook the mediaStreamListener to encoder. |
michael@0 | 62 | void Stop(ErrorResult& aResult); |
michael@0 | 63 | // Pause the mTrackUnionStream |
michael@0 | 64 | void Pause(ErrorResult& aResult); |
michael@0 | 65 | |
michael@0 | 66 | void Resume(ErrorResult& aResult); |
michael@0 | 67 | // Extract encoded data Blob from EncodedBufferCache. |
michael@0 | 68 | void RequestData(ErrorResult& aResult); |
michael@0 | 69 | // Return the The DOMMediaStream passed from UA. |
michael@0 | 70 | DOMMediaStream* Stream() const { return mStream; } |
michael@0 | 71 | // The current state of the MediaRecorder object. |
michael@0 | 72 | RecordingState State() const { return mState; } |
michael@0 | 73 | // Return the current encoding MIME type selected by the MediaEncoder. |
michael@0 | 74 | void GetMimeType(nsString &aMimeType); |
michael@0 | 75 | |
michael@0 | 76 | static already_AddRefed<MediaRecorder> |
michael@0 | 77 | Constructor(const GlobalObject& aGlobal, |
michael@0 | 78 | DOMMediaStream& aStream, |
michael@0 | 79 | const MediaRecorderOptions& aInitDict, |
michael@0 | 80 | ErrorResult& aRv); |
michael@0 | 81 | |
michael@0 | 82 | // EventHandler |
michael@0 | 83 | IMPL_EVENT_HANDLER(dataavailable) |
michael@0 | 84 | IMPL_EVENT_HANDLER(error) |
michael@0 | 85 | IMPL_EVENT_HANDLER(stop) |
michael@0 | 86 | IMPL_EVENT_HANDLER(warning) |
michael@0 | 87 | |
michael@0 | 88 | protected: |
michael@0 | 89 | MediaRecorder& operator = (const MediaRecorder& x) MOZ_DELETE; |
michael@0 | 90 | // Create dataavailable event with Blob data and it runs in main thread |
michael@0 | 91 | nsresult CreateAndDispatchBlobEvent(already_AddRefed<nsIDOMBlob>&& aBlob); |
michael@0 | 92 | // Creating a simple event to notify UA simple event. |
michael@0 | 93 | void DispatchSimpleEvent(const nsAString & aStr); |
michael@0 | 94 | // Creating a error event with message. |
michael@0 | 95 | void NotifyError(nsresult aRv); |
michael@0 | 96 | // Check if the recorder's principal is the subsume of mediaStream |
michael@0 | 97 | bool CheckPrincipal(); |
michael@0 | 98 | // Set encoded MIME type. |
michael@0 | 99 | void SetMimeType(const nsString &aMimeType); |
michael@0 | 100 | |
michael@0 | 101 | MediaRecorder(const MediaRecorder& x) MOZ_DELETE; // prevent bad usage |
michael@0 | 102 | // Remove session pointer. |
michael@0 | 103 | void RemoveSession(Session* aSession); |
michael@0 | 104 | // MediaStream passed from js context |
michael@0 | 105 | nsRefPtr<DOMMediaStream> mStream; |
michael@0 | 106 | // The current state of the MediaRecorder object. |
michael@0 | 107 | RecordingState mState; |
michael@0 | 108 | // Hold the sessions pointer in media recorder and clean in the destructor of recorder. |
michael@0 | 109 | nsTArray<Session*> mSessions; |
michael@0 | 110 | // Thread safe for mMimeType. |
michael@0 | 111 | Mutex mMutex; |
michael@0 | 112 | // It specifies the container format as well as the audio and video capture formats. |
michael@0 | 113 | nsString mMimeType; |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | #endif |