2012-09-04 18:01:56 -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/. */
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
#include "CameraControlImpl.h"
|
2012-09-04 18:01:56 -07:00
|
|
|
#include "base/basictypes.h"
|
2013-08-05 13:24:43 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-02-14 14:41:07 -08:00
|
|
|
#include "mozilla/unused.h"
|
2014-04-25 13:28:15 -07:00
|
|
|
#include "nsPrintfCString.h"
|
2014-02-14 14:41:07 -08:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2012-10-23 15:30:28 -07:00
|
|
|
#include "CameraRecorderProfiles.h"
|
2012-09-04 18:01:56 -07:00
|
|
|
#include "CameraCommon.h"
|
2013-02-15 00:04:11 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
2014-01-16 17:01:43 -08:00
|
|
|
#include "DeviceStorageFileDescriptor.h"
|
2014-02-14 14:41:07 -08:00
|
|
|
#include "CameraControlListener.h"
|
2012-09-04 18:01:56 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
nsWeakPtr CameraControlImpl::sCameraThread;
|
|
|
|
|
|
|
|
CameraControlImpl::CameraControlImpl(uint32_t aCameraId)
|
2014-04-25 13:28:15 -07:00
|
|
|
: mListenerLock(PR_NewRWLock(PR_RWLOCK_RANK_NONE, "CameraControlImpl.Listeners.Lock"))
|
|
|
|
, mCameraId(aCameraId)
|
2014-02-14 14:41:07 -08:00
|
|
|
, mPreviewState(CameraControlListener::kPreviewStopped)
|
|
|
|
, mHardwareState(CameraControlListener::kHardwareClosed)
|
2012-10-23 15:30:28 -07:00
|
|
|
{
|
|
|
|
DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
// reuse the same camera thread to conserve resources
|
|
|
|
nsCOMPtr<nsIThread> ct = do_QueryReferent(sCameraThread);
|
|
|
|
if (ct) {
|
|
|
|
mCameraThread = ct.forget();
|
|
|
|
} else {
|
|
|
|
nsresult rv = NS_NewNamedThread("CameraThread", getter_AddRefs(mCameraThread));
|
2014-04-25 13:28:15 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_CRASH("Failed to create new Camera Thread");
|
|
|
|
}
|
2014-02-14 14:41:07 -08:00
|
|
|
|
|
|
|
// keep a weak reference to the new thread
|
|
|
|
sCameraThread = do_GetWeakReference(mCameraThread);
|
|
|
|
}
|
2012-10-23 15:30:28 -07:00
|
|
|
|
2014-04-09 08:52:10 -07:00
|
|
|
// Care must be taken with the mListenerLock read-write lock to prevent
|
|
|
|
// deadlocks. Currently this is handled by ensuring that any attempts to
|
|
|
|
// acquire the lock for writing (as in Add/RemoveListener()) happen in a
|
|
|
|
// runnable dispatched to the Camera Thread--even if the method is being
|
|
|
|
// called from that thread. This ensures that if a registered listener
|
|
|
|
// (which is invoked with a read-lock) tries to call Add/RemoveListener(),
|
|
|
|
// the lock-for-writing attempt won't happen until the listener has
|
|
|
|
// completed.
|
|
|
|
//
|
|
|
|
// Multiple parallel listeners being invoked are not a problem because
|
|
|
|
// the read-write lock allows multiple simultaneous read-locks.
|
2014-04-25 13:28:15 -07:00
|
|
|
if (!mListenerLock) {
|
|
|
|
MOZ_CRASH("Out of memory getting new PRRWLock");
|
|
|
|
}
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::~CameraControlImpl()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-04-25 13:28:15 -07:00
|
|
|
MOZ_ASSERT(mListenerLock, "mListenerLock missing in ~CameraControlImpl()");
|
2014-02-14 14:41:07 -08:00
|
|
|
if (mListenerLock) {
|
|
|
|
PR_DestroyRWLock(mListenerLock);
|
|
|
|
mListenerLock = nullptr;
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
already_AddRefed<RecorderProfileManager>
|
|
|
|
CameraControlImpl::GetRecorderProfileManager()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
return GetRecorderProfileManagerImpl();
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::Shutdown()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnHardwareStateChange(CameraControlListener::HardwareState aNewState)
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it may be called from the camera's
|
|
|
|
// local binder thread, should the mediaserver process die.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2012-09-04 18:01:56 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
if (aNewState == mHardwareState) {
|
|
|
|
DOM_CAMERA_LOGI("OnHardwareStateChange: state did not change from %d\n", mHardwareState);
|
|
|
|
return;
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
const char* state[] = { "open", "closed", "failed" };
|
|
|
|
MOZ_ASSERT(aNewState >= 0);
|
|
|
|
if (static_cast<unsigned int>(aNewState) < sizeof(state) / sizeof(state[0])) {
|
|
|
|
DOM_CAMERA_LOGI("New hardware state is '%s'\n", state[aNewState]);
|
|
|
|
} else {
|
|
|
|
DOM_CAMERA_LOGE("OnHardwareStateChange: got invalid HardwareState value %d\n", aNewState);
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2014-02-14 14:41:07 -08:00
|
|
|
#endif
|
2012-09-04 18:01:56 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
mHardwareState = aNewState;
|
2012-09-04 18:01:56 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnHardwareStateChange(mHardwareState);
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnConfigurationChange()
|
2012-09-30 17:37:47 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mCameraThread);
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2014-02-14 05:27:44 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
DOM_CAMERA_LOGI("OnConfigurationChange : %d listeners\n", mListeners.Length());
|
2012-09-30 17:37:47 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnConfigurationChange(mCurrentConfiguration);
|
2014-02-13 21:28:57 -08:00
|
|
|
}
|
2014-02-14 14:41:07 -08:00
|
|
|
}
|
2012-09-30 17:37:47 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnAutoFocusComplete(bool aAutoFocusSucceeded)
|
|
|
|
{
|
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it is called from the camera
|
|
|
|
// library's auto focus thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2012-09-30 17:37:47 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnAutoFocusComplete(aAutoFocusSucceeded);
|
2014-02-13 21:28:57 -08:00
|
|
|
}
|
2012-09-30 17:37:47 -07:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:12:25 -07:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnAutoFocusMoving(bool aIsMoving)
|
|
|
|
{
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnAutoFocusMoving(aIsMoving);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 08:52:10 -07:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnFacesDetected(const nsTArray<Face>& aFaces)
|
|
|
|
{
|
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it is called from the camera
|
|
|
|
// library's face detection thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnFacesDetected(aFaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnTakePictureComplete(uint8_t* aData, uint32_t aLength, const nsAString& aMimeType)
|
2012-11-02 13:11:50 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it is called from the camera
|
|
|
|
// library's snapshot thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2012-11-02 13:11:50 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnTakePictureComplete(aData, aLength, aMimeType);
|
|
|
|
}
|
2012-11-02 13:11:50 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnShutter()
|
2013-08-05 13:24:43 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it is called from the camera driver's
|
|
|
|
// preview thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2013-08-05 13:24:43 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnShutter();
|
|
|
|
}
|
2013-08-05 13:24:43 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnClosed()
|
2013-10-30 12:41:08 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2013-10-30 12:41:08 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnHardwareStateChange(CameraControlListener::kHardwareClosed);
|
|
|
|
}
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2013-10-30 12:41:08 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnRecorderStateChange(CameraControlListener::RecorderState aState,
|
|
|
|
int32_t aStatus, int32_t aTrackNumber)
|
2014-02-14 05:27:44 -08:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread. On Gonk, it is called from the media encoder
|
|
|
|
// thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2013-12-06 06:47:29 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnRecorderStateChange(aState, aStatus, aTrackNumber);
|
|
|
|
}
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2012-10-23 15:30:28 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnPreviewStateChange(CameraControlListener::PreviewState aNewState)
|
2014-02-14 05:27:44 -08:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This callback runs on the Main Thread and the Camera Thread, and
|
|
|
|
// may run on the local binder thread, should the mediaserver
|
|
|
|
// process die.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
2012-09-30 17:37:47 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
if (aNewState == mPreviewState) {
|
|
|
|
DOM_CAMERA_LOGI("OnPreviewStateChange: state did not change from %d\n", mPreviewState);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
const char* state[] = { "stopped", "paused", "started" };
|
|
|
|
MOZ_ASSERT(aNewState >= 0);
|
|
|
|
if (static_cast<unsigned int>(aNewState) < sizeof(state) / sizeof(state[0])) {
|
|
|
|
DOM_CAMERA_LOGI("New preview state is '%s'\n", state[aNewState]);
|
|
|
|
} else {
|
|
|
|
DOM_CAMERA_LOGE("OnPreviewStateChange: got unknown PreviewState value %d\n", aNewState);
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
mPreviewState = aNewState;
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnPreviewStateChange(mPreviewState);
|
|
|
|
}
|
2012-09-30 17:37:47 -07:00
|
|
|
}
|
|
|
|
|
2014-07-02 16:55:00 -07:00
|
|
|
void
|
|
|
|
CameraControlImpl::OnRateLimitPreview(bool aLimit)
|
|
|
|
{
|
|
|
|
// This function runs on neither the Main Thread nor the Camera Thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("OnRateLimitPreview: %d\n", aLimit);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnRateLimitPreview(aLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
bool
|
|
|
|
CameraControlImpl::OnNewPreviewFrame(layers::Image* aImage, uint32_t aWidth, uint32_t aHeight)
|
2014-02-14 05:27:44 -08:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
// This function runs on neither the Main Thread nor the Camera Thread.
|
|
|
|
// On Gonk, it is called from the camera driver's preview thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
DOM_CAMERA_LOGI("OnNewPreviewFrame: we have %d preview frame listener(s)\n",
|
|
|
|
mListeners.Length());
|
|
|
|
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
consumed = l->OnNewPreviewFrame(aImage, aWidth, aHeight) || consumed;
|
2014-02-13 21:28:57 -08:00
|
|
|
}
|
2014-02-14 14:41:07 -08:00
|
|
|
return consumed;
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 05:27:44 -08:00
|
|
|
void
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlImpl::OnUserError(CameraControlListener::UserContext aContext,
|
|
|
|
nsresult aError)
|
2014-02-14 14:41:07 -08:00
|
|
|
{
|
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2014-02-19 20:18:52 -08:00
|
|
|
const char* context[] = {
|
|
|
|
"StartCamera",
|
|
|
|
"StopCamera",
|
|
|
|
"AutoFocus",
|
2014-04-15 12:46:40 -07:00
|
|
|
"StartFaceDetection",
|
|
|
|
"StopFaceDetection",
|
2014-02-19 20:18:52 -08:00
|
|
|
"TakePicture",
|
|
|
|
"StartRecording",
|
|
|
|
"StopRecording",
|
|
|
|
"SetConfiguration",
|
|
|
|
"StartPreview",
|
|
|
|
"StopPreview",
|
2014-04-25 13:28:15 -07:00
|
|
|
"SetPictureSize",
|
|
|
|
"SetThumbnailSize",
|
2014-04-15 12:46:40 -07:00
|
|
|
"ResumeContinuousFocus",
|
2014-02-19 20:18:52 -08:00
|
|
|
"Unspecified"
|
|
|
|
};
|
2014-04-25 13:28:15 -07:00
|
|
|
if (static_cast<size_t>(aContext) < sizeof(context) / sizeof(context[0])) {
|
|
|
|
DOM_CAMERA_LOGW("CameraControlImpl::OnUserError : aContext='%s' (%d), aError=0x%x\n",
|
|
|
|
context[aContext], aContext, aError);
|
2014-02-14 14:41:07 -08:00
|
|
|
} else {
|
2014-04-25 13:28:15 -07:00
|
|
|
DOM_CAMERA_LOGE("CameraControlImpl::OnUserError : aContext=%d, aError=0x%x\n",
|
2014-02-14 14:41:07 -08:00
|
|
|
aContext, aError);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
2014-04-25 13:28:15 -07:00
|
|
|
l->OnUserError(aContext, aError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraControlImpl::OnSystemError(CameraControlListener::SystemContext aContext,
|
|
|
|
nsresult aError)
|
|
|
|
{
|
|
|
|
// This callback can run on threads other than the Main Thread and
|
|
|
|
// the Camera Thread.
|
|
|
|
RwLockAutoEnterRead lock(mListenerLock);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
const char* context[] = {
|
|
|
|
"Camera Service"
|
|
|
|
};
|
|
|
|
if (static_cast<size_t>(aContext) < sizeof(context) / sizeof(context[0])) {
|
|
|
|
DOM_CAMERA_LOGW("CameraControlImpl::OnSystemError : aContext='%s' (%d), aError=0x%x\n",
|
|
|
|
context[aContext], aContext, aError);
|
|
|
|
} else {
|
|
|
|
DOM_CAMERA_LOGE("CameraControlImpl::OnSystemError : aContext=%d, aError=0x%x\n",
|
|
|
|
aContext, aError);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
CameraControlListener* l = mListeners[i];
|
|
|
|
l->OnSystemError(aContext, aError);
|
2012-09-30 17:37:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
// Camera control asynchronous message; these are dispatched from
|
|
|
|
// the Main Thread to the Camera Thread, where they are consumed.
|
|
|
|
|
|
|
|
class CameraControlImpl::ControlMessage : public nsRunnable
|
2012-09-30 17:37:47 -07:00
|
|
|
{
|
2013-04-17 07:30:44 -07:00
|
|
|
public:
|
2014-02-14 14:41:07 -08:00
|
|
|
ControlMessage(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: mCameraControl(aCameraControl)
|
|
|
|
, mContext(aContext)
|
2014-04-25 13:28:15 -07:00
|
|
|
{ }
|
2013-04-17 07:30:44 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
virtual nsresult RunImpl() = 0;
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCameraControl);
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mCameraControl->mCameraThread);
|
|
|
|
|
|
|
|
nsresult rv = RunImpl();
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-04-25 13:28:15 -07:00
|
|
|
nsPrintfCString msg("Camera control API(%d) failed with 0x%x", mContext, rv);
|
|
|
|
NS_WARNING(msg.get());
|
|
|
|
mCameraControl->OnUserError(mContext, rv);
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2014-02-14 14:41:07 -08:00
|
|
|
|
2013-04-17 07:30:44 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-25 13:28:15 -07:00
|
|
|
virtual ~ControlMessage() { }
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
nsRefPtr<CameraControlImpl> mCameraControl;
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext mContext;
|
2013-04-17 07:30:44 -07:00
|
|
|
};
|
2012-09-30 17:37:47 -07:00
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
nsresult
|
|
|
|
CameraControlImpl::Dispatch(ControlMessage* aMessage)
|
|
|
|
{
|
|
|
|
nsresult rv = mCameraThread->Dispatch(aMessage, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintfCString msg("Failed to dispatch camera control message (0x%x)", rv);
|
|
|
|
NS_WARNING(msg.get());
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:18:52 -08:00
|
|
|
nsresult
|
|
|
|
CameraControlImpl::Start(const Configuration* aConfig)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext,
|
2014-02-19 20:18:52 -08:00
|
|
|
const Configuration* aConfig)
|
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
, mHaveInitialConfig(false)
|
|
|
|
{
|
|
|
|
if (aConfig) {
|
|
|
|
mConfig = *aConfig;
|
|
|
|
mHaveInitialConfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (mHaveInitialConfig) {
|
|
|
|
return mCameraControl->StartImpl(&mConfig);
|
|
|
|
}
|
|
|
|
return mCameraControl->StartImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mHaveInitialConfig;
|
|
|
|
Configuration mConfig;
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStartCamera, aConfig));
|
2014-02-19 20:18:52 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
nsresult
|
|
|
|
CameraControlImpl::SetConfiguration(const Configuration& aConfig)
|
2012-09-30 17:37:47 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext,
|
2014-02-14 14:41:07 -08:00
|
|
|
const Configuration& aConfig)
|
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
, mConfig(aConfig)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->SetConfigurationImpl(mConfig);
|
|
|
|
}
|
2012-11-02 13:11:50 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
protected:
|
|
|
|
Configuration mConfig;
|
|
|
|
};
|
2013-08-05 13:24:43 -07:00
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInSetConfiguration, aConfig));
|
2013-08-05 13:24:43 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
nsresult
|
2014-04-15 12:46:40 -07:00
|
|
|
CameraControlImpl::AutoFocus()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
2014-04-15 12:46:40 -07:00
|
|
|
return mCameraControl->AutoFocusImpl();
|
2014-02-14 14:41:07 -08:00
|
|
|
}
|
|
|
|
};
|
2013-01-08 15:43:50 -08:00
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInAutoFocus));
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-04-09 08:52:10 -07:00
|
|
|
nsresult
|
|
|
|
CameraControlImpl::StartFaceDetection()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-04-09 08:52:10 -07:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StartFaceDetectionImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStartFaceDetection));
|
2014-04-09 08:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CameraControlImpl::StopFaceDetection()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-04-09 08:52:10 -07:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StopFaceDetectionImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStopFaceDetection));
|
2014-04-09 08:52:10 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:01:56 -07:00
|
|
|
nsresult
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::TakePicture()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->TakePictureImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInTakePicture));
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::StartRecording(DeviceStorageFileDescriptor* aFileDescriptor,
|
|
|
|
const StartRecordingOptions* aOptions)
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext,
|
2014-02-14 14:41:07 -08:00
|
|
|
const StartRecordingOptions* aOptions,
|
|
|
|
DeviceStorageFileDescriptor* aFileDescriptor)
|
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
, mOptionsPassed(false)
|
|
|
|
, mFileDescriptor(aFileDescriptor)
|
|
|
|
{
|
|
|
|
if (aOptions) {
|
|
|
|
mOptions = *aOptions;
|
|
|
|
mOptionsPassed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StartRecordingImpl(mFileDescriptor,
|
|
|
|
mOptionsPassed ? &mOptions : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StartRecordingOptions mOptions;
|
|
|
|
bool mOptionsPassed;
|
|
|
|
nsRefPtr<DeviceStorageFileDescriptor> mFileDescriptor;
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
if (!aFileDescriptor) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStartRecording,
|
|
|
|
aOptions, aFileDescriptor));
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::StopRecording()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StopRecordingImpl();
|
|
|
|
}
|
|
|
|
};
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStopRecording));
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::StartPreview()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StartPreviewImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStartPreview));
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:27:44 -08:00
|
|
|
nsresult
|
2014-02-14 14:41:07 -08:00
|
|
|
CameraControlImpl::StopPreview()
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->StopPreviewImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStopPreview));
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2012-09-04 18:01:56 -07:00
|
|
|
|
2014-04-15 12:46:40 -07:00
|
|
|
nsresult
|
|
|
|
CameraControlImpl::ResumeContinuousFocus()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-04-15 12:46:40 -07:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mCameraControl->ResumeContinuousFocusImpl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInResumeContinuousFocus));
|
2014-04-15 12:46:40 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:27:44 -08:00
|
|
|
nsresult
|
2014-02-19 20:18:52 -08:00
|
|
|
CameraControlImpl::Stop()
|
2014-02-14 05:27:44 -08:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
class Message : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
2014-04-25 13:28:15 -07:00
|
|
|
CameraControlListener::UserContext aContext)
|
2014-02-14 14:41:07 -08:00
|
|
|
: ControlMessage(aCameraControl, aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
2014-02-19 20:18:52 -08:00
|
|
|
return mCameraControl->StopImpl();
|
2014-02-14 14:41:07 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
return Dispatch(new Message(this, CameraControlListener::kInStopCamera));
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
class CameraControlImpl::ListenerMessage : public CameraControlImpl::ControlMessage
|
2012-09-28 22:30:52 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
public:
|
|
|
|
ListenerMessage(CameraControlImpl* aCameraControl,
|
|
|
|
CameraControlListener* aListener)
|
|
|
|
: ControlMessage(aCameraControl, CameraControlListener::kInUnspecified)
|
|
|
|
, mListener(aListener)
|
|
|
|
{ }
|
2012-09-28 22:30:52 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
protected:
|
|
|
|
nsRefPtr<CameraControlListener> mListener;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
CameraControlImpl::AddListenerImpl(already_AddRefed<CameraControlListener> aListener)
|
2014-02-14 05:27:44 -08:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
RwLockAutoEnterWrite lock(mListenerLock);
|
|
|
|
|
|
|
|
CameraControlListener* l = *mListeners.AppendElement() = aListener;
|
2014-02-19 20:18:52 -08:00
|
|
|
DOM_CAMERA_LOGI("Added camera control listener %p\n", l);
|
2014-02-14 14:41:07 -08:00
|
|
|
|
|
|
|
// Update the newly-added listener's state
|
|
|
|
l->OnConfigurationChange(mCurrentConfiguration);
|
|
|
|
l->OnHardwareStateChange(mHardwareState);
|
|
|
|
l->OnPreviewStateChange(mPreviewState);
|
2014-02-14 05:27:44 -08:00
|
|
|
}
|
2014-02-13 21:28:57 -08:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::AddListener(CameraControlListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ListenerMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl,
|
|
|
|
CameraControlListener* aListener)
|
|
|
|
: ListenerMessage(aCameraControl, aListener)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mCameraControl->AddListenerImpl(mListener.forget());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
if (aListener) {
|
|
|
|
Dispatch(new Message(this, aListener));
|
|
|
|
}
|
2012-12-23 07:54:54 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::RemoveListenerImpl(CameraControlListener* aListener)
|
2012-09-04 18:01:56 -07:00
|
|
|
{
|
2014-02-14 14:41:07 -08:00
|
|
|
RwLockAutoEnterWrite lock(mListenerLock);
|
2012-09-07 13:23:01 -07:00
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
nsRefPtr<CameraControlListener> l(aListener);
|
|
|
|
mListeners.RemoveElement(l);
|
2014-02-19 20:18:52 -08:00
|
|
|
DOM_CAMERA_LOGI("Removed camera control listener %p\n", l.get());
|
2014-02-14 14:41:07 -08:00
|
|
|
// XXXmikeh - do we want to notify the listener that it has been removed?
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:41:07 -08:00
|
|
|
void
|
|
|
|
CameraControlImpl::RemoveListener(CameraControlListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ListenerMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(CameraControlImpl* aCameraControl, CameraControlListener* aListener)
|
|
|
|
: ListenerMessage(aCameraControl, aListener)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RunImpl() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mCameraControl->RemoveListenerImpl(mListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 13:28:15 -07:00
|
|
|
if (aListener) {
|
|
|
|
Dispatch(new Message(this, aListener));
|
|
|
|
}
|
2012-09-04 18:01:56 -07:00
|
|
|
}
|