2012-06-03 00:34:02 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MEDIAENGINE_H_
|
|
|
|
#define MEDIAENGINE_H_
|
|
|
|
|
2014-04-08 17:26:33 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-06-03 00:34:02 -07:00
|
|
|
#include "nsIDOMFile.h"
|
2013-02-15 00:01:58 -08:00
|
|
|
#include "DOMMediaStream.h"
|
2012-06-03 00:34:02 -07:00
|
|
|
#include "MediaStreamGraph.h"
|
2014-07-07 00:46:00 -07:00
|
|
|
#include "mozilla/dom/MediaStreamTrackBinding.h"
|
2012-06-03 00:34:02 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-18 17:57:51 -07:00
|
|
|
struct VideoTrackConstraintsN;
|
|
|
|
struct AudioTrackConstraintsN;
|
2014-04-18 12:15:10 -07:00
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
/**
|
|
|
|
* Abstract interface for managing audio and video devices. Each platform
|
|
|
|
* must implement a concrete class that will map these classes and methods
|
|
|
|
* to the appropriate backend. For example, on Desktop platforms, these will
|
|
|
|
* correspond to equivalent webrtc (GIPS) calls, and on B2G they will map to
|
|
|
|
* a Gonk interface.
|
|
|
|
*/
|
|
|
|
class MediaEngineVideoSource;
|
|
|
|
class MediaEngineAudioSource;
|
2014-06-18 17:57:51 -07:00
|
|
|
class MediaEnginePrefs;
|
2012-06-03 00:34:02 -07:00
|
|
|
|
2012-09-28 15:26:00 -07:00
|
|
|
enum MediaEngineState {
|
|
|
|
kAllocated,
|
|
|
|
kStarted,
|
|
|
|
kStopped,
|
|
|
|
kReleased
|
|
|
|
};
|
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
// We only support 1 audio and 1 video track for now.
|
|
|
|
enum {
|
|
|
|
kVideoTrack = 1,
|
|
|
|
kAudioTrack = 2
|
|
|
|
};
|
|
|
|
|
2014-07-17 19:23:00 -07:00
|
|
|
// includes everything from dom::MediaSourceEnum (really video sources), plus audio sources
|
|
|
|
enum MediaSourceType {
|
|
|
|
Camera = (int) dom::MediaSourceEnum::Camera,
|
|
|
|
Screen = (int) dom::MediaSourceEnum::Screen,
|
|
|
|
Application = (int) dom::MediaSourceEnum::Application,
|
|
|
|
Window, // = (int) dom::MediaSourceEnum::Window, // XXX bug 1038926
|
2014-07-21 05:31:31 -07:00
|
|
|
Browser = (int) dom::MediaSourceEnum::Browser, // proposed in WG, unclear if it's useful
|
2014-07-17 19:23:00 -07:00
|
|
|
Microphone
|
|
|
|
};
|
|
|
|
|
2014-04-11 08:35:34 -07:00
|
|
|
class MediaEngine
|
2012-06-03 00:34:02 -07:00
|
|
|
{
|
2014-04-08 16:37:05 -07:00
|
|
|
public:
|
2014-04-11 08:35:34 -07:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEngine)
|
2014-04-08 16:37:05 -07:00
|
|
|
|
2013-03-04 13:02:17 -08:00
|
|
|
static const int DEFAULT_VIDEO_FPS = 30;
|
|
|
|
static const int DEFAULT_VIDEO_MIN_FPS = 10;
|
2014-04-24 16:30:08 -07:00
|
|
|
static const int DEFAULT_43_VIDEO_WIDTH = 640;
|
|
|
|
static const int DEFAULT_43_VIDEO_HEIGHT = 480;
|
|
|
|
static const int DEFAULT_169_VIDEO_WIDTH = 1280;
|
|
|
|
static const int DEFAULT_169_VIDEO_HEIGHT = 720;
|
2013-06-20 10:14:04 -07:00
|
|
|
static const int DEFAULT_AUDIO_TIMER_MS = 10;
|
2013-03-04 13:02:17 -08:00
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
/* Populate an array of video sources in the nsTArray. Also include devices
|
|
|
|
* that are currently unavailable. */
|
2014-07-17 19:23:00 -07:00
|
|
|
virtual void EnumerateVideoDevices(MediaSourceType,
|
2014-07-07 00:46:00 -07:00
|
|
|
nsTArray<nsRefPtr<MediaEngineVideoSource> >*) = 0;
|
2012-06-03 00:34:02 -07:00
|
|
|
|
|
|
|
/* Populate an array of audio sources in the nsTArray. Also include devices
|
|
|
|
* that are currently unavailable. */
|
2014-07-17 19:23:00 -07:00
|
|
|
virtual void EnumerateAudioDevices(MediaSourceType,
|
2014-07-07 00:46:00 -07:00
|
|
|
nsTArray<nsRefPtr<MediaEngineAudioSource> >*) = 0;
|
2014-04-11 08:35:34 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~MediaEngine() {}
|
2012-06-03 00:34:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common abstract base class for audio and video sources.
|
|
|
|
*/
|
|
|
|
class MediaEngineSource : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
2014-09-02 01:52:45 -07:00
|
|
|
// code inside webrtc.org assumes these sizes; don't use anything smaller
|
|
|
|
// without verifying it's ok
|
|
|
|
static const unsigned int kMaxDeviceNameLength = 128;
|
|
|
|
static const unsigned int kMaxUniqueIdLength = 256;
|
|
|
|
|
2012-12-09 09:23:19 -08:00
|
|
|
virtual ~MediaEngineSource() {}
|
2012-06-03 00:34:02 -07:00
|
|
|
|
|
|
|
/* Populate the human readable name of this device in the nsAString */
|
|
|
|
virtual void GetName(nsAString&) = 0;
|
|
|
|
|
|
|
|
/* Populate the UUID of this device in the nsAString */
|
|
|
|
virtual void GetUUID(nsAString&) = 0;
|
|
|
|
|
|
|
|
/* Release the device back to the system. */
|
|
|
|
virtual nsresult Deallocate() = 0;
|
|
|
|
|
|
|
|
/* Start the device and add the track to the provided SourceMediaStream, with
|
|
|
|
* the provided TrackID. You may start appending data to the track
|
|
|
|
* immediately after. */
|
|
|
|
virtual nsresult Start(SourceMediaStream*, TrackID) = 0;
|
|
|
|
|
2014-07-13 22:48:02 -07:00
|
|
|
/* tell the source if there are any direct listeners attached */
|
|
|
|
virtual void SetDirectListeners(bool) = 0;
|
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
/* Take a snapshot from this source. In the case of video this is a single
|
|
|
|
* image, and for audio, it is a snippet lasting aDuration milliseconds. The
|
|
|
|
* duration argument is ignored for a MediaEngineVideoSource.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual nsresult Snapshot(uint32_t aDuration, nsIDOMFile** aFile) = 0;
|
2012-06-03 00:34:02 -07:00
|
|
|
|
2012-10-17 02:46:40 -07:00
|
|
|
/* Called when the stream wants more data */
|
2012-12-31 15:12:12 -08:00
|
|
|
virtual void NotifyPull(MediaStreamGraph* aGraph,
|
|
|
|
SourceMediaStream *aSource,
|
|
|
|
TrackID aId,
|
|
|
|
StreamTime aDesiredTime,
|
|
|
|
TrackTicks &aLastEndTime) = 0;
|
2012-10-17 02:46:40 -07:00
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
/* Stop the device and release the corresponding MediaStream */
|
2012-12-31 15:12:12 -08:00
|
|
|
virtual nsresult Stop(SourceMediaStream *aSource, TrackID aID) = 0;
|
2012-06-03 00:34:02 -07:00
|
|
|
|
2013-01-29 08:55:09 -08:00
|
|
|
/* Change device configuration. */
|
|
|
|
virtual nsresult Config(bool aEchoOn, uint32_t aEcho,
|
|
|
|
bool aAgcOn, uint32_t aAGC,
|
2014-04-02 10:58:19 -07:00
|
|
|
bool aNoiseOn, uint32_t aNoise,
|
|
|
|
int32_t aPlayoutDelay) = 0;
|
2013-01-29 08:55:09 -08:00
|
|
|
|
2013-07-04 22:53:10 -07:00
|
|
|
/* Returns true if a source represents a fake capture device and
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
virtual bool IsFake() = 0;
|
|
|
|
|
2014-07-17 19:23:00 -07:00
|
|
|
/* Returns the type of media source (camera, microphone, screen, window, etc) */
|
|
|
|
virtual const MediaSourceType GetMediaSource() = 0;
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
/* Return false if device is currently allocated or started */
|
|
|
|
bool IsAvailable() {
|
|
|
|
if (mState == kAllocated || mState == kStarted) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
/* It is an error to call Start() before an Allocate(), and Stop() before
|
|
|
|
* a Start(). Only Allocate() may be called after a Deallocate(). */
|
2012-10-16 17:53:55 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
MediaEngineState mState;
|
2012-06-03 00:34:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Video source and friends.
|
|
|
|
*/
|
2014-04-18 12:15:37 -07:00
|
|
|
class MediaEnginePrefs {
|
|
|
|
public:
|
2013-03-04 13:02:17 -08:00
|
|
|
int32_t mWidth;
|
|
|
|
int32_t mHeight;
|
|
|
|
int32_t mFPS;
|
|
|
|
int32_t mMinFPS;
|
2014-04-18 12:15:37 -07:00
|
|
|
|
|
|
|
// mWidth and/or mHeight may be zero (=adaptive default), so use functions.
|
|
|
|
|
|
|
|
int32_t GetWidth(bool aHD = false) const {
|
|
|
|
return mWidth? mWidth : (mHeight?
|
|
|
|
(mHeight * GetDefWidth(aHD)) / GetDefHeight(aHD) :
|
|
|
|
GetDefWidth(aHD));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t GetHeight(bool aHD = false) const {
|
|
|
|
return mHeight? mHeight : (mWidth?
|
|
|
|
(mWidth * GetDefHeight(aHD)) / GetDefWidth(aHD) :
|
|
|
|
GetDefHeight(aHD));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
static int32_t GetDefWidth(bool aHD = false) {
|
2014-04-30 23:48:05 -07:00
|
|
|
// It'd be nice if we could use the ternary operator here, but we can't
|
|
|
|
// because of bug 1002729.
|
|
|
|
if (aHD) {
|
|
|
|
return MediaEngine::DEFAULT_169_VIDEO_WIDTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MediaEngine::DEFAULT_43_VIDEO_WIDTH;
|
2014-04-18 12:15:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t GetDefHeight(bool aHD = false) {
|
2014-04-30 23:48:05 -07:00
|
|
|
// It'd be nice if we could use the ternary operator here, but we can't
|
|
|
|
// because of bug 1002729.
|
|
|
|
if (aHD) {
|
|
|
|
return MediaEngine::DEFAULT_169_VIDEO_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MediaEngine::DEFAULT_43_VIDEO_HEIGHT;
|
2014-04-18 12:15:37 -07:00
|
|
|
}
|
2012-06-03 00:34:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class MediaEngineVideoSource : public MediaEngineSource
|
|
|
|
{
|
|
|
|
public:
|
2012-12-09 09:23:19 -08:00
|
|
|
virtual ~MediaEngineVideoSource() {}
|
2014-04-18 12:15:10 -07:00
|
|
|
|
2014-07-17 19:23:00 -07:00
|
|
|
virtual const MediaSourceType GetMediaSource() {
|
|
|
|
return MediaSourceType::Camera;
|
2014-07-07 00:46:00 -07:00
|
|
|
}
|
2014-04-18 12:15:10 -07:00
|
|
|
/* This call reserves but does not start the device. */
|
|
|
|
virtual nsresult Allocate(const VideoTrackConstraintsN &aConstraints,
|
|
|
|
const MediaEnginePrefs &aPrefs) = 0;
|
2012-06-03 00:34:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Audio source and friends.
|
|
|
|
*/
|
|
|
|
class MediaEngineAudioSource : public MediaEngineSource
|
|
|
|
{
|
|
|
|
public:
|
2012-12-09 09:23:19 -08:00
|
|
|
virtual ~MediaEngineAudioSource() {}
|
2014-04-18 12:15:10 -07:00
|
|
|
|
|
|
|
/* This call reserves but does not start the device. */
|
|
|
|
virtual nsresult Allocate(const AudioTrackConstraintsN &aConstraints,
|
|
|
|
const MediaEnginePrefs &aPrefs) = 0;
|
|
|
|
|
2012-06-03 00:34:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MEDIAENGINE_H_ */
|