2012-09-05 05:01:56 +04: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 "base/basictypes.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "VideoUtils.h"
|
|
|
|
#include "DOMCameraPreview.h"
|
|
|
|
#include "CameraCommon.h"
|
2013-01-30 14:03:17 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-03-22 04:05:20 +04:00
|
|
|
#include "nsIDocument.h"
|
2013-01-30 14:03:17 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-09-05 05:01:56 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2012-11-10 19:45:52 +04:00
|
|
|
using namespace mozilla::layers;
|
2012-09-05 05:01:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 'PreviewControl' is a helper class that dispatches preview control
|
|
|
|
* events from the main thread.
|
|
|
|
*
|
|
|
|
* NS_NewRunnableMethod() can't be used because it AddRef()s the method's
|
|
|
|
* object, which can't be done off the main thread for cycle collection
|
|
|
|
* participants.
|
|
|
|
*
|
|
|
|
* Before using this class, 'aDOMPreview' must be appropriately AddRef()ed.
|
|
|
|
*/
|
|
|
|
class PreviewControl : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
START,
|
|
|
|
STOP,
|
|
|
|
STARTED,
|
|
|
|
STOPPED
|
|
|
|
};
|
|
|
|
PreviewControl(DOMCameraPreview* aDOMPreview, uint32_t aControl)
|
|
|
|
: mDOMPreview(aDOMPreview)
|
|
|
|
, mControl(aControl)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "PreviewControl not run on main thread");
|
|
|
|
|
|
|
|
switch (mControl) {
|
|
|
|
case START:
|
|
|
|
mDOMPreview->Start();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOP:
|
2012-10-25 03:21:32 +04:00
|
|
|
mDOMPreview->StopPreview();
|
2012-09-05 05:01:56 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STARTED:
|
|
|
|
mDOMPreview->SetStateStarted();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STOPPED:
|
|
|
|
mDOMPreview->SetStateStopped();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DOM_CAMERA_LOGE("PreviewControl: invalid control %d\n", mControl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* This must be a raw pointer because this class is not created on the
|
|
|
|
* main thread, and DOMCameraPreview is not threadsafe. Prior to
|
|
|
|
* issuing a preview control event, the caller must ensure that
|
|
|
|
* mDOMPreview will not disappear.
|
|
|
|
*/
|
|
|
|
DOMCameraPreview* mDOMPreview;
|
|
|
|
uint32_t mControl;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DOMCameraPreviewListener : public MediaStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DOMCameraPreviewListener(DOMCameraPreview* aDOMPreview) :
|
|
|
|
mDOMPreview(aDOMPreview)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DOMCameraPreviewListener()
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyConsumptionChanged(MediaStreamGraph* aGraph, Consumption aConsuming)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
|
|
|
|
2012-11-30 20:06:37 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
const char* state;
|
|
|
|
|
2012-09-05 05:01:56 +04:00
|
|
|
switch (aConsuming) {
|
|
|
|
case NOT_CONSUMED:
|
|
|
|
state = "not consuming";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSUMED:
|
|
|
|
state = "consuming";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
state = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGA("camera viewfinder is %s\n", state);
|
2012-11-30 20:06:37 +04:00
|
|
|
#endif
|
2012-09-05 05:01:56 +04:00
|
|
|
nsCOMPtr<nsIRunnable> previewControl;
|
|
|
|
|
|
|
|
switch (aConsuming) {
|
|
|
|
case NOT_CONSUMED:
|
|
|
|
previewControl = new PreviewControl(mDOMPreview, PreviewControl::STOP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSUMED:
|
|
|
|
previewControl = new PreviewControl(mDOMPreview, PreviewControl::START);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_DispatchToMainThread(previewControl);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DOM_CAMERA_LOGE("Failed to dispatch preview control (%d)!\n", rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Raw pointer; if we exist, 'mDOMPreview' exists as well
|
|
|
|
DOMCameraPreview* mDOMPreview;
|
|
|
|
};
|
|
|
|
|
2013-02-15 12:04:11 +04:00
|
|
|
DOMCameraPreview::DOMCameraPreview(nsGlobalWindow* aWindow,
|
|
|
|
ICameraControl* aCameraControl,
|
|
|
|
uint32_t aWidth, uint32_t aHeight,
|
|
|
|
uint32_t aFrameRate)
|
2013-02-15 12:01:58 +04:00
|
|
|
: DOMMediaStream()
|
2012-09-05 05:01:56 +04:00
|
|
|
, mState(STOPPED)
|
|
|
|
, mWidth(aWidth)
|
|
|
|
, mHeight(aHeight)
|
|
|
|
, mFramesPerSecond(aFrameRate)
|
|
|
|
, mFrameCount(0)
|
|
|
|
, mCameraControl(aCameraControl)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p : mWidth=%d, mHeight=%d, mFramesPerSecond=%d\n", __func__, __LINE__, this, mWidth, mHeight, mFramesPerSecond);
|
|
|
|
|
|
|
|
mImageContainer = LayerManager::CreateImageContainer();
|
2013-02-15 12:04:11 +04:00
|
|
|
mWindow = aWindow;
|
2013-03-20 18:07:46 +04:00
|
|
|
mInput = new CameraPreviewMediaStream(this);
|
|
|
|
mStream = mInput;
|
2012-09-05 05:01:56 +04:00
|
|
|
|
|
|
|
mListener = new DOMCameraPreviewListener(this);
|
|
|
|
mInput->AddListener(mListener);
|
|
|
|
|
2013-02-15 12:04:11 +04:00
|
|
|
if (aWindow->GetExtantDoc()) {
|
|
|
|
CombineWithPrincipal(aWindow->GetExtantDoc()->NodePrincipal());
|
2013-01-30 14:03:17 +04:00
|
|
|
}
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMCameraPreview::~DOMCameraPreview()
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p, mListener=%p\n", __func__, __LINE__, this, mListener);
|
|
|
|
mInput->RemoveListener(mListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMCameraPreview::HaveEnoughBuffered()
|
|
|
|
{
|
2013-03-20 18:07:46 +04:00
|
|
|
return true;
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
|
2012-09-08 00:23:01 +04:00
|
|
|
bool
|
2012-09-05 05:01:56 +04:00
|
|
|
DOMCameraPreview::ReceiveFrame(void* aBuffer, ImageFormat aFormat, FrameBuilder aBuilder)
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
|
|
|
if (!aBuffer || !aBuilder) {
|
2012-09-08 00:23:01 +04:00
|
|
|
return false;
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
if (mState != STARTED) {
|
2012-09-08 00:23:01 +04:00
|
|
|
return false;
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageFormat format = aFormat;
|
|
|
|
nsRefPtr<Image> image = mImageContainer->CreateImage(&format, 1);
|
|
|
|
aBuilder(image, aBuffer, mWidth, mHeight);
|
|
|
|
|
2013-03-20 18:07:46 +04:00
|
|
|
mInput->SetCurrentFrame(gfxIntSize(mWidth, mHeight), image);
|
2012-09-08 00:23:01 +04:00
|
|
|
return true;
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::Start()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Start() not called from main thread");
|
|
|
|
if (mState != STOPPED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("Starting preview stream\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a reference to ourselves to make sure we stay alive while
|
|
|
|
* the preview is running, as the CameraControlImpl object holds a
|
|
|
|
* weak reference to us.
|
|
|
|
*
|
|
|
|
* This reference is removed in SetStateStopped().
|
|
|
|
*/
|
|
|
|
NS_ADDREF_THIS();
|
2012-12-23 19:54:54 +04:00
|
|
|
DOM_CAMERA_SETSTATE(STARTING);
|
2012-09-05 05:01:56 +04:00
|
|
|
mCameraControl->StartPreview(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::SetStateStarted()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "SetStateStarted() not called from main thread");
|
|
|
|
|
2012-12-23 19:54:54 +04:00
|
|
|
DOM_CAMERA_SETSTATE(STARTED);
|
2012-09-05 05:01:56 +04:00
|
|
|
DOM_CAMERA_LOGI("Preview stream started\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::Started()
|
|
|
|
{
|
|
|
|
if (mState != STARTING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("Dispatching preview stream started\n");
|
|
|
|
nsCOMPtr<nsIRunnable> started = new PreviewControl(this, PreviewControl::STARTED);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(started);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DOM_CAMERA_LOGE("failed to set statrted state (%d), POTENTIAL MEMORY LEAK!\n", rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-25 03:21:32 +04:00
|
|
|
DOMCameraPreview::StopPreview()
|
2012-09-05 05:01:56 +04:00
|
|
|
{
|
2012-10-25 03:21:32 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "StopPreview() not called from main thread");
|
2012-09-05 05:01:56 +04:00
|
|
|
if (mState != STARTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("Stopping preview stream\n");
|
2012-12-23 19:54:54 +04:00
|
|
|
DOM_CAMERA_SETSTATE(STOPPING);
|
2012-09-05 05:01:56 +04:00
|
|
|
mCameraControl->StopPreview();
|
2013-03-20 18:07:46 +04:00
|
|
|
//mInput->EndTrack(TRACK_VIDEO);
|
|
|
|
//mInput->Finish();
|
2012-09-05 05:01:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::SetStateStopped()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "SetStateStopped() not called from main thread");
|
|
|
|
|
2012-12-05 06:00:39 +04:00
|
|
|
// see bug 809259 and bug 817367.
|
|
|
|
if (mState != STOPPING) {
|
2013-03-20 18:07:46 +04:00
|
|
|
//mInput->EndTrack(TRACK_VIDEO);
|
|
|
|
//mInput->Finish();
|
2012-12-05 06:00:39 +04:00
|
|
|
}
|
2012-12-23 19:54:54 +04:00
|
|
|
DOM_CAMERA_SETSTATE(STOPPED);
|
2012-09-05 05:01:56 +04:00
|
|
|
DOM_CAMERA_LOGI("Preview stream stopped\n");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only remove the reference added in Start() once the preview
|
|
|
|
* has stopped completely.
|
|
|
|
*/
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::Stopped(bool aForced)
|
|
|
|
{
|
|
|
|
if (mState != STOPPING && !aForced) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("Dispatching preview stream stopped\n");
|
|
|
|
nsCOMPtr<nsIRunnable> stopped = new PreviewControl(this, PreviewControl::STOPPED);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(stopped);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DOM_CAMERA_LOGE("failed to decrement reference count (%d), MEMORY LEAK!\n", rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMCameraPreview::Error()
|
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGE("Error occurred changing preview state!\n");
|
|
|
|
Stopped(true);
|
|
|
|
}
|