2012-06-03 00:34:40 -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/. */
|
|
|
|
|
|
|
|
#include "MediaEngineDefault.h"
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDOMFile.h"
|
|
|
|
#include "nsILocalFile.h"
|
2012-08-19 12:33:25 -07:00
|
|
|
#include "Layers.h"
|
2012-08-20 21:06:46 -07:00
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "ImageTypes.h"
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-03 00:34:40 -07:00
|
|
|
#define CHANNELS 1
|
|
|
|
#define RATE USECS_PER_S
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineDefaultVideoSource, nsITimerCallback)
|
|
|
|
/**
|
|
|
|
* Default video source.
|
|
|
|
*/
|
2012-07-30 17:42:26 -07:00
|
|
|
|
2012-10-04 09:09:35 -07:00
|
|
|
// Cannot be initialized in the class definition
|
|
|
|
const MediaEngineVideoOptions MediaEngineDefaultVideoSource::mOpts = {
|
|
|
|
DEFAULT_WIDTH,
|
|
|
|
DEFAULT_HEIGHT,
|
|
|
|
DEFAULT_FPS,
|
|
|
|
kVideoCodecI420
|
|
|
|
};
|
|
|
|
|
2012-07-30 17:42:26 -07:00
|
|
|
MediaEngineDefaultVideoSource::MediaEngineDefaultVideoSource()
|
2012-10-16 17:53:55 -07:00
|
|
|
: mTimer(nullptr)
|
|
|
|
{
|
|
|
|
mState = kReleased;
|
|
|
|
}
|
2012-07-30 17:42:26 -07:00
|
|
|
|
|
|
|
MediaEngineDefaultVideoSource::~MediaEngineDefaultVideoSource()
|
|
|
|
{}
|
|
|
|
|
2012-06-03 00:34:40 -07:00
|
|
|
void
|
|
|
|
MediaEngineDefaultVideoSource::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
aName.Assign(NS_LITERAL_STRING("Default Video Device"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineDefaultVideoSource::GetUUID(nsAString& aUUID)
|
|
|
|
{
|
|
|
|
aUUID.Assign(NS_LITERAL_STRING("1041FCBD-3F12-4F7B-9E9B-1EC556DD5676"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-19 17:29:43 -07:00
|
|
|
nsresult
|
2012-06-03 00:34:40 -07:00
|
|
|
MediaEngineDefaultVideoSource::Allocate()
|
|
|
|
{
|
|
|
|
if (mState != kReleased) {
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mState = kAllocated;
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_OK;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultVideoSource::Deallocate()
|
|
|
|
{
|
|
|
|
if (mState != kStopped && mState != kAllocated) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mState = kReleased;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-04 09:09:35 -07:00
|
|
|
const MediaEngineVideoOptions *
|
2012-06-03 00:34:40 -07:00
|
|
|
MediaEngineDefaultVideoSource::GetOptions()
|
|
|
|
{
|
2012-10-04 09:09:35 -07:00
|
|
|
return &mOpts;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultVideoSource::Start(SourceMediaStream* aStream, TrackID aID)
|
|
|
|
{
|
|
|
|
if (mState != kAllocated) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!mTimer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSource = aStream;
|
|
|
|
|
|
|
|
// Allocate a single blank Image
|
2012-08-19 19:39:10 -07:00
|
|
|
ImageFormat format = PLANAR_YCBCR;
|
2012-06-03 00:34:40 -07:00
|
|
|
mImageContainer = layers::LayerManager::CreateImageContainer();
|
|
|
|
|
|
|
|
nsRefPtr<layers::Image> image = mImageContainer->CreateImage(&format, 1);
|
|
|
|
|
2012-10-04 09:09:35 -07:00
|
|
|
int len = ((DEFAULT_WIDTH * DEFAULT_HEIGHT) * 3 / 2);
|
2012-06-03 00:34:40 -07:00
|
|
|
mImage = static_cast<layers::PlanarYCbCrImage*>(image.get());
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* frame = (uint8_t*) PR_Malloc(len);
|
2012-06-03 00:34:40 -07:00
|
|
|
memset(frame, 0x80, len); // Gray
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint8_t lumaBpp = 8;
|
|
|
|
const uint8_t chromaBpp = 4;
|
2012-06-03 00:34:40 -07:00
|
|
|
|
|
|
|
layers::PlanarYCbCrImage::Data data;
|
|
|
|
data.mYChannel = frame;
|
2012-10-04 09:09:35 -07:00
|
|
|
data.mYSize = gfxIntSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
|
|
|
data.mYStride = DEFAULT_WIDTH * lumaBpp / 8.0;
|
|
|
|
data.mCbCrStride = DEFAULT_WIDTH * chromaBpp / 8.0;
|
|
|
|
data.mCbChannel = frame + DEFAULT_HEIGHT * data.mYStride;
|
|
|
|
data.mCrChannel = data.mCbChannel + DEFAULT_HEIGHT * data.mCbCrStride / 2;
|
|
|
|
data.mCbCrSize = gfxIntSize(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2);
|
2012-06-03 00:34:40 -07:00
|
|
|
data.mPicX = 0;
|
|
|
|
data.mPicY = 0;
|
2012-10-04 09:09:35 -07:00
|
|
|
data.mPicSize = gfxIntSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
2012-08-19 12:33:25 -07:00
|
|
|
data.mStereoMode = STEREO_MODE_MONO;
|
2012-06-03 00:34:40 -07:00
|
|
|
|
|
|
|
// SetData copies data, so we can free the frame
|
|
|
|
mImage->SetData(data);
|
|
|
|
PR_Free(frame);
|
|
|
|
|
|
|
|
// AddTrack takes ownership of segment
|
|
|
|
VideoSegment *segment = new VideoSegment();
|
2012-10-04 09:09:35 -07:00
|
|
|
segment->AppendFrame(image.forget(), USECS_PER_S / DEFAULT_FPS, gfxIntSize(DEFAULT_WIDTH, DEFAULT_HEIGHT));
|
2012-06-03 00:34:40 -07:00
|
|
|
mSource->AddTrack(aID, RATE, 0, segment);
|
|
|
|
|
|
|
|
// We aren't going to add any more tracks
|
|
|
|
mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
|
|
|
|
|
|
|
// Remember TrackID so we can end it later
|
|
|
|
mTrackID = aID;
|
|
|
|
|
|
|
|
// Start timer for subsequent frames
|
2012-10-04 09:09:35 -07:00
|
|
|
mTimer->InitWithCallback(this, 1000 / DEFAULT_FPS, nsITimer::TYPE_REPEATING_SLACK);
|
2012-06-03 00:34:40 -07:00
|
|
|
mState = kStarted;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultVideoSource::Stop()
|
|
|
|
{
|
|
|
|
if (mState != kStarted) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!mTimer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = NULL;
|
|
|
|
|
|
|
|
mSource->EndTrack(mTrackID);
|
|
|
|
mSource->Finish();
|
|
|
|
|
|
|
|
mState = kStopped;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
MediaEngineDefaultVideoSource::Snapshot(uint32_t aDuration, nsIDOMFile** aFile)
|
2012-06-03 00:34:40 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aFile = nullptr;
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
#ifndef MOZ_WIDGET_ANDROID
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#else
|
|
|
|
if (!AndroidBridge::Bridge()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString filePath;
|
|
|
|
AndroidBridge::Bridge()->ShowFilePickerForMimeType(filePath, NS_LITERAL_STRING("image/*"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = NS_NewLocalFile(filePath, false, getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*aFile = new nsDOMFileFile(file));
|
|
|
|
return NS_OK;
|
|
|
|
#endif
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaEngineDefaultVideoSource::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
VideoSegment segment;
|
|
|
|
|
|
|
|
nsRefPtr<layers::PlanarYCbCrImage> image = mImage;
|
2012-10-04 09:09:35 -07:00
|
|
|
segment.AppendFrame(image.forget(), USECS_PER_S / DEFAULT_FPS, gfxIntSize(DEFAULT_WIDTH, DEFAULT_HEIGHT));
|
2012-06-03 00:34:40 -07:00
|
|
|
mSource->AppendToTrack(mTrackID, &segment);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-17 02:46:40 -07:00
|
|
|
void
|
|
|
|
MediaEngineDefaultVideoSource::NotifyPull(MediaStreamGraph* aGraph,
|
|
|
|
StreamTime aDesiredTime)
|
|
|
|
{
|
|
|
|
// Ignore - we push video data
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-03 00:34:40 -07:00
|
|
|
/**
|
|
|
|
* Default audio source.
|
|
|
|
*/
|
2012-10-17 02:46:40 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineDefaultAudioSource, nsITimerCallback)
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
MediaEngineDefaultAudioSource::MediaEngineDefaultAudioSource()
|
|
|
|
: mTimer(nullptr)
|
|
|
|
{
|
|
|
|
mState = kReleased;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaEngineDefaultAudioSource::~MediaEngineDefaultAudioSource()
|
|
|
|
{}
|
|
|
|
|
2012-10-17 02:46:40 -07:00
|
|
|
void
|
|
|
|
MediaEngineDefaultAudioSource::NotifyPull(MediaStreamGraph* aGraph,
|
|
|
|
StreamTime aDesiredTime)
|
|
|
|
{
|
|
|
|
// Ignore - we push audio data
|
|
|
|
}
|
|
|
|
|
2012-06-03 00:34:40 -07:00
|
|
|
void
|
|
|
|
MediaEngineDefaultAudioSource::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
aName.Assign(NS_LITERAL_STRING("Default Audio Device"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineDefaultAudioSource::GetUUID(nsAString& aUUID)
|
|
|
|
{
|
|
|
|
aUUID.Assign(NS_LITERAL_STRING("B7CBD7C1-53EF-42F9-8353-73F61C70C092"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-19 17:29:43 -07:00
|
|
|
nsresult
|
2012-06-03 00:34:40 -07:00
|
|
|
MediaEngineDefaultAudioSource::Allocate()
|
|
|
|
{
|
|
|
|
if (mState != kReleased) {
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
2012-06-19 17:29:43 -07:00
|
|
|
|
2012-06-03 00:34:40 -07:00
|
|
|
mState = kAllocated;
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_OK;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultAudioSource::Deallocate()
|
|
|
|
{
|
|
|
|
if (mState != kStopped && mState != kAllocated) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mState = kReleased;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultAudioSource::Start(SourceMediaStream* aStream, TrackID aID)
|
|
|
|
{
|
|
|
|
if (mState != kAllocated) {
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!mTimer) {
|
2012-06-19 17:29:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-03 00:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mSource = aStream;
|
|
|
|
|
|
|
|
// AddTrack will take ownership of segment
|
|
|
|
AudioSegment* segment = new AudioSegment();
|
|
|
|
segment->Init(CHANNELS);
|
|
|
|
mSource->AddTrack(aID, RATE, 0, segment);
|
|
|
|
|
|
|
|
// We aren't going to add any more tracks
|
|
|
|
mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
|
|
|
|
|
|
|
// Remember TrackID so we can finish later
|
|
|
|
mTrackID = aID;
|
|
|
|
|
|
|
|
// 1 Audio frame per Video frame
|
2012-10-04 09:09:35 -07:00
|
|
|
mTimer->InitWithCallback(this, 1000 / MediaEngineDefaultVideoSource::DEFAULT_FPS, nsITimer::TYPE_REPEATING_SLACK);
|
2012-06-03 00:34:40 -07:00
|
|
|
mState = kStarted;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineDefaultAudioSource::Stop()
|
|
|
|
{
|
|
|
|
if (mState != kStarted) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!mTimer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = NULL;
|
|
|
|
|
|
|
|
mSource->EndTrack(mTrackID);
|
|
|
|
mSource->Finish();
|
|
|
|
|
|
|
|
mState = kStopped;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
MediaEngineDefaultAudioSource::Snapshot(uint32_t aDuration, nsIDOMFile** aFile)
|
2012-06-03 00:34:40 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaEngineDefaultAudioSource::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
AudioSegment segment;
|
|
|
|
segment.Init(CHANNELS);
|
|
|
|
segment.InsertNullDataAtStart(1);
|
|
|
|
|
|
|
|
mSource->AppendToTrack(mTrackID, &segment);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineDefault::EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >* aVSources) {
|
2012-10-19 15:06:18 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2012-10-16 17:53:55 -07:00
|
|
|
int32_t found = false;
|
|
|
|
int32_t len = mVSources.Length();
|
2012-10-19 15:06:18 -07:00
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
nsRefPtr<MediaEngineVideoSource> source = mVSources.ElementAt(i);
|
|
|
|
aVSources->AppendElement(source);
|
|
|
|
if (source->IsAvailable()) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All streams are currently busy, just make a new one.
|
|
|
|
if (!found) {
|
|
|
|
nsRefPtr<MediaEngineVideoSource> newSource =
|
|
|
|
new MediaEngineDefaultVideoSource();
|
|
|
|
mVSources.AppendElement(newSource);
|
|
|
|
aVSources->AppendElement(newSource);
|
|
|
|
}
|
2012-06-03 00:34:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineDefault::EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >* aASources) {
|
2012-10-19 15:06:18 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
int32_t len = mASources.Length();
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
nsRefPtr<MediaEngineAudioSource> source = mASources.ElementAt(i);
|
|
|
|
if (source->IsAvailable()) {
|
|
|
|
aASources->AppendElement(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All streams are currently busy, just make a new one.
|
|
|
|
if (aASources->Length() == 0) {
|
|
|
|
nsRefPtr<MediaEngineAudioSource> newSource =
|
|
|
|
new MediaEngineDefaultAudioSource();
|
|
|
|
mASources.AppendElement(newSource);
|
|
|
|
aASources->AppendElement(newSource);
|
|
|
|
}
|
2012-06-03 00:34:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-03 00:35:15 -07:00
|
|
|
} // namespace mozilla
|