1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webrtc/MediaEngine.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,193 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef MEDIAENGINE_H_ 1.9 +#define MEDIAENGINE_H_ 1.10 + 1.11 +#include "mozilla/RefPtr.h" 1.12 +#include "nsIDOMFile.h" 1.13 +#include "DOMMediaStream.h" 1.14 +#include "MediaStreamGraph.h" 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +class VideoTrackConstraintsN; 1.19 +class AudioTrackConstraintsN; 1.20 + 1.21 +/** 1.22 + * Abstract interface for managing audio and video devices. Each platform 1.23 + * must implement a concrete class that will map these classes and methods 1.24 + * to the appropriate backend. For example, on Desktop platforms, these will 1.25 + * correspond to equivalent webrtc (GIPS) calls, and on B2G they will map to 1.26 + * a Gonk interface. 1.27 + */ 1.28 +class MediaEngineVideoSource; 1.29 +class MediaEngineAudioSource; 1.30 +struct MediaEnginePrefs; 1.31 + 1.32 +enum MediaEngineState { 1.33 + kAllocated, 1.34 + kStarted, 1.35 + kStopped, 1.36 + kReleased 1.37 +}; 1.38 + 1.39 +// We only support 1 audio and 1 video track for now. 1.40 +enum { 1.41 + kVideoTrack = 1, 1.42 + kAudioTrack = 2 1.43 +}; 1.44 + 1.45 +class MediaEngine 1.46 +{ 1.47 +public: 1.48 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEngine) 1.49 + 1.50 + static const int DEFAULT_VIDEO_FPS = 30; 1.51 + static const int DEFAULT_VIDEO_MIN_FPS = 10; 1.52 + static const int DEFAULT_43_VIDEO_WIDTH = 640; 1.53 + static const int DEFAULT_43_VIDEO_HEIGHT = 480; 1.54 + static const int DEFAULT_169_VIDEO_WIDTH = 1280; 1.55 + static const int DEFAULT_169_VIDEO_HEIGHT = 720; 1.56 + static const int DEFAULT_AUDIO_TIMER_MS = 10; 1.57 + 1.58 + /* Populate an array of video sources in the nsTArray. Also include devices 1.59 + * that are currently unavailable. */ 1.60 + virtual void EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >*) = 0; 1.61 + 1.62 + /* Populate an array of audio sources in the nsTArray. Also include devices 1.63 + * that are currently unavailable. */ 1.64 + virtual void EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >*) = 0; 1.65 + 1.66 +protected: 1.67 + virtual ~MediaEngine() {} 1.68 +}; 1.69 + 1.70 +/** 1.71 + * Common abstract base class for audio and video sources. 1.72 + */ 1.73 +class MediaEngineSource : public nsISupports 1.74 +{ 1.75 +public: 1.76 + virtual ~MediaEngineSource() {} 1.77 + 1.78 + /* Populate the human readable name of this device in the nsAString */ 1.79 + virtual void GetName(nsAString&) = 0; 1.80 + 1.81 + /* Populate the UUID of this device in the nsAString */ 1.82 + virtual void GetUUID(nsAString&) = 0; 1.83 + 1.84 + /* Release the device back to the system. */ 1.85 + virtual nsresult Deallocate() = 0; 1.86 + 1.87 + /* Start the device and add the track to the provided SourceMediaStream, with 1.88 + * the provided TrackID. You may start appending data to the track 1.89 + * immediately after. */ 1.90 + virtual nsresult Start(SourceMediaStream*, TrackID) = 0; 1.91 + 1.92 + /* Take a snapshot from this source. In the case of video this is a single 1.93 + * image, and for audio, it is a snippet lasting aDuration milliseconds. The 1.94 + * duration argument is ignored for a MediaEngineVideoSource. 1.95 + */ 1.96 + virtual nsresult Snapshot(uint32_t aDuration, nsIDOMFile** aFile) = 0; 1.97 + 1.98 + /* Called when the stream wants more data */ 1.99 + virtual void NotifyPull(MediaStreamGraph* aGraph, 1.100 + SourceMediaStream *aSource, 1.101 + TrackID aId, 1.102 + StreamTime aDesiredTime, 1.103 + TrackTicks &aLastEndTime) = 0; 1.104 + 1.105 + /* Stop the device and release the corresponding MediaStream */ 1.106 + virtual nsresult Stop(SourceMediaStream *aSource, TrackID aID) = 0; 1.107 + 1.108 + /* Change device configuration. */ 1.109 + virtual nsresult Config(bool aEchoOn, uint32_t aEcho, 1.110 + bool aAgcOn, uint32_t aAGC, 1.111 + bool aNoiseOn, uint32_t aNoise, 1.112 + int32_t aPlayoutDelay) = 0; 1.113 + 1.114 + /* Returns true if a source represents a fake capture device and 1.115 + * false otherwise 1.116 + */ 1.117 + virtual bool IsFake() = 0; 1.118 + 1.119 + /* Return false if device is currently allocated or started */ 1.120 + bool IsAvailable() { 1.121 + if (mState == kAllocated || mState == kStarted) { 1.122 + return false; 1.123 + } else { 1.124 + return true; 1.125 + } 1.126 + } 1.127 + 1.128 + /* It is an error to call Start() before an Allocate(), and Stop() before 1.129 + * a Start(). Only Allocate() may be called after a Deallocate(). */ 1.130 + 1.131 +protected: 1.132 + MediaEngineState mState; 1.133 +}; 1.134 + 1.135 +/** 1.136 + * Video source and friends. 1.137 + */ 1.138 +class MediaEnginePrefs { 1.139 +public: 1.140 + int32_t mWidth; 1.141 + int32_t mHeight; 1.142 + int32_t mFPS; 1.143 + int32_t mMinFPS; 1.144 + 1.145 + // mWidth and/or mHeight may be zero (=adaptive default), so use functions. 1.146 + 1.147 + int32_t GetWidth(bool aHD = false) const { 1.148 + return mWidth? mWidth : (mHeight? 1.149 + (mHeight * GetDefWidth(aHD)) / GetDefHeight(aHD) : 1.150 + GetDefWidth(aHD)); 1.151 + } 1.152 + 1.153 + int32_t GetHeight(bool aHD = false) const { 1.154 + return mHeight? mHeight : (mWidth? 1.155 + (mWidth * GetDefHeight(aHD)) / GetDefWidth(aHD) : 1.156 + GetDefHeight(aHD)); 1.157 + } 1.158 +private: 1.159 + static int32_t GetDefWidth(bool aHD = false) { 1.160 + return aHD ? MediaEngine::DEFAULT_169_VIDEO_WIDTH : 1.161 + MediaEngine::DEFAULT_43_VIDEO_WIDTH; 1.162 + } 1.163 + 1.164 + static int32_t GetDefHeight(bool aHD = false) { 1.165 + return aHD ? MediaEngine::DEFAULT_169_VIDEO_HEIGHT : 1.166 + MediaEngine::DEFAULT_43_VIDEO_HEIGHT; 1.167 + } 1.168 +}; 1.169 + 1.170 +class MediaEngineVideoSource : public MediaEngineSource 1.171 +{ 1.172 +public: 1.173 + virtual ~MediaEngineVideoSource() {} 1.174 + 1.175 + /* This call reserves but does not start the device. */ 1.176 + virtual nsresult Allocate(const VideoTrackConstraintsN &aConstraints, 1.177 + const MediaEnginePrefs &aPrefs) = 0; 1.178 +}; 1.179 + 1.180 +/** 1.181 + * Audio source and friends. 1.182 + */ 1.183 +class MediaEngineAudioSource : public MediaEngineSource 1.184 +{ 1.185 +public: 1.186 + virtual ~MediaEngineAudioSource() {} 1.187 + 1.188 + /* This call reserves but does not start the device. */ 1.189 + virtual nsresult Allocate(const AudioTrackConstraintsN &aConstraints, 1.190 + const MediaEnginePrefs &aPrefs) = 0; 1.191 + 1.192 +}; 1.193 + 1.194 +} 1.195 + 1.196 +#endif /* MEDIAENGINE_H_ */