Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef MEDIAENGINE_H_
6 #define MEDIAENGINE_H_
8 #include "mozilla/RefPtr.h"
9 #include "nsIDOMFile.h"
10 #include "DOMMediaStream.h"
11 #include "MediaStreamGraph.h"
13 namespace mozilla {
15 class VideoTrackConstraintsN;
16 class AudioTrackConstraintsN;
18 /**
19 * Abstract interface for managing audio and video devices. Each platform
20 * must implement a concrete class that will map these classes and methods
21 * to the appropriate backend. For example, on Desktop platforms, these will
22 * correspond to equivalent webrtc (GIPS) calls, and on B2G they will map to
23 * a Gonk interface.
24 */
25 class MediaEngineVideoSource;
26 class MediaEngineAudioSource;
27 struct MediaEnginePrefs;
29 enum MediaEngineState {
30 kAllocated,
31 kStarted,
32 kStopped,
33 kReleased
34 };
36 // We only support 1 audio and 1 video track for now.
37 enum {
38 kVideoTrack = 1,
39 kAudioTrack = 2
40 };
42 class MediaEngine
43 {
44 public:
45 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEngine)
47 static const int DEFAULT_VIDEO_FPS = 30;
48 static const int DEFAULT_VIDEO_MIN_FPS = 10;
49 static const int DEFAULT_43_VIDEO_WIDTH = 640;
50 static const int DEFAULT_43_VIDEO_HEIGHT = 480;
51 static const int DEFAULT_169_VIDEO_WIDTH = 1280;
52 static const int DEFAULT_169_VIDEO_HEIGHT = 720;
53 static const int DEFAULT_AUDIO_TIMER_MS = 10;
55 /* Populate an array of video sources in the nsTArray. Also include devices
56 * that are currently unavailable. */
57 virtual void EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >*) = 0;
59 /* Populate an array of audio sources in the nsTArray. Also include devices
60 * that are currently unavailable. */
61 virtual void EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >*) = 0;
63 protected:
64 virtual ~MediaEngine() {}
65 };
67 /**
68 * Common abstract base class for audio and video sources.
69 */
70 class MediaEngineSource : public nsISupports
71 {
72 public:
73 virtual ~MediaEngineSource() {}
75 /* Populate the human readable name of this device in the nsAString */
76 virtual void GetName(nsAString&) = 0;
78 /* Populate the UUID of this device in the nsAString */
79 virtual void GetUUID(nsAString&) = 0;
81 /* Release the device back to the system. */
82 virtual nsresult Deallocate() = 0;
84 /* Start the device and add the track to the provided SourceMediaStream, with
85 * the provided TrackID. You may start appending data to the track
86 * immediately after. */
87 virtual nsresult Start(SourceMediaStream*, TrackID) = 0;
89 /* Take a snapshot from this source. In the case of video this is a single
90 * image, and for audio, it is a snippet lasting aDuration milliseconds. The
91 * duration argument is ignored for a MediaEngineVideoSource.
92 */
93 virtual nsresult Snapshot(uint32_t aDuration, nsIDOMFile** aFile) = 0;
95 /* Called when the stream wants more data */
96 virtual void NotifyPull(MediaStreamGraph* aGraph,
97 SourceMediaStream *aSource,
98 TrackID aId,
99 StreamTime aDesiredTime,
100 TrackTicks &aLastEndTime) = 0;
102 /* Stop the device and release the corresponding MediaStream */
103 virtual nsresult Stop(SourceMediaStream *aSource, TrackID aID) = 0;
105 /* Change device configuration. */
106 virtual nsresult Config(bool aEchoOn, uint32_t aEcho,
107 bool aAgcOn, uint32_t aAGC,
108 bool aNoiseOn, uint32_t aNoise,
109 int32_t aPlayoutDelay) = 0;
111 /* Returns true if a source represents a fake capture device and
112 * false otherwise
113 */
114 virtual bool IsFake() = 0;
116 /* Return false if device is currently allocated or started */
117 bool IsAvailable() {
118 if (mState == kAllocated || mState == kStarted) {
119 return false;
120 } else {
121 return true;
122 }
123 }
125 /* It is an error to call Start() before an Allocate(), and Stop() before
126 * a Start(). Only Allocate() may be called after a Deallocate(). */
128 protected:
129 MediaEngineState mState;
130 };
132 /**
133 * Video source and friends.
134 */
135 class MediaEnginePrefs {
136 public:
137 int32_t mWidth;
138 int32_t mHeight;
139 int32_t mFPS;
140 int32_t mMinFPS;
142 // mWidth and/or mHeight may be zero (=adaptive default), so use functions.
144 int32_t GetWidth(bool aHD = false) const {
145 return mWidth? mWidth : (mHeight?
146 (mHeight * GetDefWidth(aHD)) / GetDefHeight(aHD) :
147 GetDefWidth(aHD));
148 }
150 int32_t GetHeight(bool aHD = false) const {
151 return mHeight? mHeight : (mWidth?
152 (mWidth * GetDefHeight(aHD)) / GetDefWidth(aHD) :
153 GetDefHeight(aHD));
154 }
155 private:
156 static int32_t GetDefWidth(bool aHD = false) {
157 return aHD ? MediaEngine::DEFAULT_169_VIDEO_WIDTH :
158 MediaEngine::DEFAULT_43_VIDEO_WIDTH;
159 }
161 static int32_t GetDefHeight(bool aHD = false) {
162 return aHD ? MediaEngine::DEFAULT_169_VIDEO_HEIGHT :
163 MediaEngine::DEFAULT_43_VIDEO_HEIGHT;
164 }
165 };
167 class MediaEngineVideoSource : public MediaEngineSource
168 {
169 public:
170 virtual ~MediaEngineVideoSource() {}
172 /* This call reserves but does not start the device. */
173 virtual nsresult Allocate(const VideoTrackConstraintsN &aConstraints,
174 const MediaEnginePrefs &aPrefs) = 0;
175 };
177 /**
178 * Audio source and friends.
179 */
180 class MediaEngineAudioSource : public MediaEngineSource
181 {
182 public:
183 virtual ~MediaEngineAudioSource() {}
185 /* This call reserves but does not start the device. */
186 virtual nsresult Allocate(const AudioTrackConstraintsN &aConstraints,
187 const MediaEnginePrefs &aPrefs) = 0;
189 };
191 }
193 #endif /* MEDIAENGINE_H_ */