content/media/MediaRecorder.h

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

mercurial