1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef DOM_CAMERA_CAMERACONTROLIMPL_H
6 #define DOM_CAMERA_CAMERACONTROLIMPL_H
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ReentrantMonitor.h"
13 #include "nsProxyRelease.h"
14 #include "AutoRwLock.h"
15 #include "nsIDOMDeviceStorage.h"
16 #include "ICameraControl.h"
17 #include "CameraCommon.h"
18 #include "DeviceStorage.h"
19 #include "DeviceStorageFileDescriptor.h"
20 #include "CameraControlListener.h"
28 class CameraControlImpl
: public ICameraControl
31 explicit CameraControlImpl();
32 virtual void AddListener(CameraControlListener
* aListener
) MOZ_OVERRIDE
;
33 virtual void RemoveListener(CameraControlListener
* aListener
) MOZ_OVERRIDE
;
35 // See ICameraControl.h for these methods' return values.
36 virtual nsresult
Start(const Configuration
* aConfig
= nullptr) MOZ_OVERRIDE
;
37 virtual nsresult
Stop() MOZ_OVERRIDE
;
38 virtual nsresult
SetConfiguration(const Configuration
& aConfig
) MOZ_OVERRIDE
;
39 virtual nsresult
StartPreview() MOZ_OVERRIDE
;
40 virtual nsresult
StopPreview() MOZ_OVERRIDE
;
41 virtual nsresult
AutoFocus() MOZ_OVERRIDE
;
42 virtual nsresult
StartFaceDetection() MOZ_OVERRIDE
;
43 virtual nsresult
StopFaceDetection() MOZ_OVERRIDE
;
44 virtual nsresult
TakePicture() MOZ_OVERRIDE
;
45 virtual nsresult
StartRecording(DeviceStorageFileDescriptor
* aFileDescriptor
,
46 const StartRecordingOptions
* aOptions
) MOZ_OVERRIDE
;
47 virtual nsresult
StopRecording() MOZ_OVERRIDE
;
48 virtual nsresult
PauseRecording() MOZ_OVERRIDE
;
49 virtual nsresult
ResumeRecording() MOZ_OVERRIDE
;
50 virtual nsresult
ResumeContinuousFocus() MOZ_OVERRIDE
;
52 // Event handlers called directly from outside this class.
54 void OnUserError(CameraControlListener::UserContext aContext
, nsresult aError
);
55 void OnSystemError(CameraControlListener::SystemContext aContext
, nsresult aError
);
56 void OnAutoFocusMoving(bool aIsMoving
);
60 void OnAutoFocusComplete(bool aAutoFocusSucceeded
);
61 void OnFacesDetected(const nsTArray
<Face
>& aFaces
);
62 void OnTakePictureComplete(uint8_t* aData
, uint32_t aLength
, const nsAString
& aMimeType
);
64 void OnRateLimitPreview(bool aLimit
);
65 bool OnNewPreviewFrame(layers::Image
* aImage
, uint32_t aWidth
, uint32_t aHeight
);
66 void OnRecorderStateChange(CameraControlListener::RecorderState aState
,
67 int32_t aStatus
= -1, int32_t aTrackNumber
= -1);
68 void OnPreviewStateChange(CameraControlListener::PreviewState aState
);
69 void OnHardwareStateChange(CameraControlListener::HardwareState aState
,
71 void OnConfigurationChange();
73 // When we create a new CameraThread, we keep a static reference to it so
74 // that multiple CameraControl instances can find and reuse it; but we
75 // don't want that reference to keep the thread object around unnecessarily,
76 // so we make it a weak reference. The strong dynamic references will keep
77 // the thread object alive as needed.
78 static StaticRefPtr
<nsIThread
> sCameraThread
;
79 nsCOMPtr
<nsIThread
> mCameraThread
;
81 virtual ~CameraControlImpl();
83 virtual void BeginBatchParameterSet() MOZ_OVERRIDE
{ }
84 virtual void EndBatchParameterSet() MOZ_OVERRIDE
{ }
86 // Manage camera event listeners.
87 void AddListenerImpl(already_AddRefed
<CameraControlListener
> aListener
);
88 void RemoveListenerImpl(CameraControlListener
* aListener
);
89 nsTArray
<nsRefPtr
<CameraControlListener
> > mListeners
;
90 PRRWLock
* mListenerLock
;
93 class ListenerMessage
;
95 nsresult
Dispatch(ControlMessage
* aMessage
);
97 // Asynchronous method implementations, invoked on the Camera Thread.
100 // - NS_OK on success;
101 // - NS_ERROR_INVALID_ARG if one or more arguments is invalid;
102 // - NS_ERROR_NOT_INITIALIZED if the underlying hardware is not initialized,
103 // failed to initialize (in the case of StartImpl()), or has been stopped;
104 // for StartRecordingImpl(), this indicates that no recorder has been
105 // configured (either by calling StartImpl() or SetConfigurationImpl());
106 // - NS_ERROR_ALREADY_INITIALIZED if the underlying hardware is already
108 // - NS_ERROR_NOT_IMPLEMENTED if the method is not implemented;
109 // - NS_ERROR_FAILURE on general failures.
110 virtual nsresult
StartImpl(const Configuration
* aConfig
= nullptr) = 0;
111 virtual nsresult
StopImpl() = 0;
112 virtual nsresult
SetConfigurationImpl(const Configuration
& aConfig
) = 0;
113 virtual nsresult
StartPreviewImpl() = 0;
114 virtual nsresult
StopPreviewImpl() = 0;
115 virtual nsresult
AutoFocusImpl() = 0;
116 virtual nsresult
StartFaceDetectionImpl() = 0;
117 virtual nsresult
StopFaceDetectionImpl() = 0;
118 virtual nsresult
TakePictureImpl() = 0;
119 virtual nsresult
StartRecordingImpl(DeviceStorageFileDescriptor
* aFileDescriptor
,
120 const StartRecordingOptions
* aOptions
) = 0;
121 virtual nsresult
StopRecordingImpl() = 0;
122 virtual nsresult
PauseRecordingImpl() = 0;
123 virtual nsresult
ResumeRecordingImpl() = 0;
124 virtual nsresult
ResumeContinuousFocusImpl() = 0;
125 virtual nsresult
PushParametersImpl() = 0;
126 virtual nsresult
PullParametersImpl() = 0;
128 void OnShutterInternal();
129 void OnClosedInternal();
131 CameraControlListener::CameraListenerConfiguration mCurrentConfiguration
;
133 CameraControlListener::PreviewState mPreviewState
;
134 CameraControlListener::HardwareState mHardwareState
;
135 nsresult mHardwareStateChangeReason
;
138 CameraControlImpl(const CameraControlImpl
&) = delete;
139 CameraControlImpl
& operator=(const CameraControlImpl
&) = delete;
142 } // namespace mozilla
144 #endif // DOM_CAMERA_CAMERACONTROLIMPL_H