2013-03-20 18:07:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* 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 "CameraPreviewMediaStream.h"
|
2014-07-03 03:55:00 +04:00
|
|
|
#include "CameraCommon.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum number of outstanding invalidates before we start to drop frames;
|
|
|
|
* if we hit this threshold, it is an indicator that the main thread is
|
|
|
|
* either very busy or the device is busy elsewhere (e.g. encoding or
|
|
|
|
* persisting video data).
|
|
|
|
*/
|
|
|
|
#define MAX_INVALIDATE_PENDING 4
|
2013-03-20 18:07:46 +04:00
|
|
|
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-06-10 23:01:19 +04:00
|
|
|
CameraPreviewMediaStream::CameraPreviewMediaStream(DOMMediaStream* aWrapper)
|
|
|
|
: MediaStream(aWrapper)
|
|
|
|
, mMutex("mozilla::camera::CameraPreviewMediaStream")
|
2014-07-03 03:55:00 +04:00
|
|
|
, mInvalidatePending(0)
|
|
|
|
, mDiscardedFrames(0)
|
|
|
|
, mRateLimit(false)
|
2013-06-10 23:01:19 +04:00
|
|
|
{
|
2014-09-09 00:52:44 +04:00
|
|
|
SetGraphImpl(MediaStreamGraph::GetInstance());
|
2013-06-10 23:01:19 +04:00
|
|
|
mIsConsumed = false;
|
|
|
|
}
|
|
|
|
|
2013-03-20 18:07:46 +04:00
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::AddAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::SetAudioOutputVolume(void* aKey, float aVolume)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::RemoveAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::AddVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
nsRefPtr<VideoFrameContainer> container = aContainer;
|
|
|
|
AddVideoOutputImpl(container.forget());
|
|
|
|
|
|
|
|
if (mVideoOutputs.Length() > 1) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-09 00:52:44 +04:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
|
2013-03-20 18:07:46 +04:00
|
|
|
mIsConsumed = true;
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
2014-09-09 00:52:44 +04:00
|
|
|
l->NotifyConsumptionChanged(gm, MediaStreamListener::CONSUMED);
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::RemoveVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
RemoveVideoOutputImpl(aContainer);
|
|
|
|
|
|
|
|
if (!mVideoOutputs.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-09 00:52:44 +04:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
|
2013-03-20 18:07:46 +04:00
|
|
|
mIsConsumed = false;
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
2014-09-09 00:52:44 +04:00
|
|
|
l->NotifyConsumptionChanged(gm, MediaStreamListener::NOT_CONSUMED);
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::ChangeExplicitBlockerCount(int32_t aDelta)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::AddListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-09-09 00:52:44 +04:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
|
2013-03-20 18:07:46 +04:00
|
|
|
MediaStreamListener* listener = *mListeners.AppendElement() = aListener;
|
2014-09-09 00:52:44 +04:00
|
|
|
listener->NotifyBlockingChanged(gm, MediaStreamListener::UNBLOCKED);
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::RemoveListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-09-09 00:52:44 +04:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
|
2013-03-20 18:07:46 +04:00
|
|
|
nsRefPtr<MediaStreamListener> listener(aListener);
|
|
|
|
mListeners.RemoveElement(aListener);
|
2014-09-09 00:52:44 +04:00
|
|
|
listener->NotifyEvent(gm, MediaStreamListener::EVENT_REMOVED);
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::Destroy()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
DestroyImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-03 03:55:00 +04:00
|
|
|
CameraPreviewMediaStream::Invalidate()
|
2013-03-20 18:07:46 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2014-07-03 03:55:00 +04:00
|
|
|
--mInvalidatePending;
|
|
|
|
for (nsTArray<nsRefPtr<VideoFrameContainer> >::size_type i = 0; i < mVideoOutputs.Length(); ++i) {
|
2013-03-20 18:07:46 +04:00
|
|
|
VideoFrameContainer* output = mVideoOutputs[i];
|
2014-07-03 03:55:00 +04:00
|
|
|
output->Invalidate();
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
2014-07-03 03:55:00 +04:00
|
|
|
}
|
2013-05-02 15:59:58 +04:00
|
|
|
|
2014-07-03 03:55:00 +04:00
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::RateLimit(bool aLimit)
|
|
|
|
{
|
|
|
|
mRateLimit = aLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::SetCurrentFrame(const gfxIntSize& aIntrinsicSize, Image* aImage)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mInvalidatePending > 0) {
|
|
|
|
if (mRateLimit || mInvalidatePending > MAX_INVALIDATE_PENDING) {
|
|
|
|
++mDiscardedFrames;
|
|
|
|
DOM_CAMERA_LOGW("Discard preview frame %d, %d invalidation(s) pending",
|
|
|
|
mDiscardedFrames, mInvalidatePending);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("Update preview frame, %d invalidation(s) pending",
|
|
|
|
mInvalidatePending);
|
|
|
|
}
|
|
|
|
mDiscardedFrames = 0;
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
for (nsTArray<nsRefPtr<VideoFrameContainer> >::size_type i = 0; i < mVideoOutputs.Length(); ++i) {
|
|
|
|
VideoFrameContainer* output = mVideoOutputs[i];
|
|
|
|
output->SetCurrentFrame(aIntrinsicSize, aImage, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
++mInvalidatePending;
|
2013-05-02 15:59:58 +04:00
|
|
|
}
|
2014-07-03 03:55:00 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(this, &CameraPreviewMediaStream::Invalidate);
|
|
|
|
NS_DispatchToMainThread(event);
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:33:50 +04:00
|
|
|
void
|
|
|
|
CameraPreviewMediaStream::ClearCurrentFrame()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2014-07-03 03:55:00 +04:00
|
|
|
for (nsTArray<nsRefPtr<VideoFrameContainer> >::size_type i = 0; i < mVideoOutputs.Length(); ++i) {
|
2013-07-12 17:33:50 +04:00
|
|
|
VideoFrameContainer* output = mVideoOutputs[i];
|
|
|
|
output->ClearCurrentFrame();
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate);
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(event);
|
2013-07-12 17:33:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 18:07:46 +04:00
|
|
|
}
|