|
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/. */ |
|
4 |
|
5 #ifndef DOM_CAMERA_CAMERACONTROLIMPL_H |
|
6 #define DOM_CAMERA_CAMERACONTROLIMPL_H |
|
7 |
|
8 #include "nsTArray.h" |
|
9 #include "nsWeakPtr.h" |
|
10 #include "mozilla/Attributes.h" |
|
11 #include "mozilla/ReentrantMonitor.h" |
|
12 #include "nsIFile.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" |
|
21 |
|
22 namespace mozilla { |
|
23 |
|
24 namespace layers { |
|
25 class Image; |
|
26 } |
|
27 |
|
28 class RecorderProfileManager; |
|
29 |
|
30 class CameraControlImpl : public ICameraControl |
|
31 { |
|
32 public: |
|
33 CameraControlImpl(uint32_t aCameraId); |
|
34 virtual void AddListener(CameraControlListener* aListener) MOZ_OVERRIDE; |
|
35 virtual void RemoveListener(CameraControlListener* aListener) MOZ_OVERRIDE; |
|
36 |
|
37 virtual nsresult Start(const Configuration* aConfig = nullptr) MOZ_OVERRIDE; |
|
38 virtual nsresult Stop() MOZ_OVERRIDE; |
|
39 |
|
40 virtual nsresult SetConfiguration(const Configuration& aConfig) MOZ_OVERRIDE; |
|
41 virtual nsresult StartPreview() MOZ_OVERRIDE; |
|
42 virtual nsresult StopPreview() MOZ_OVERRIDE; |
|
43 virtual nsresult AutoFocus() MOZ_OVERRIDE; |
|
44 virtual nsresult StartFaceDetection() MOZ_OVERRIDE; |
|
45 virtual nsresult StopFaceDetection() MOZ_OVERRIDE; |
|
46 virtual nsresult TakePicture() MOZ_OVERRIDE; |
|
47 virtual nsresult StartRecording(DeviceStorageFileDescriptor* aFileDescriptor, |
|
48 const StartRecordingOptions* aOptions) MOZ_OVERRIDE; |
|
49 virtual nsresult StopRecording() MOZ_OVERRIDE; |
|
50 virtual nsresult ResumeContinuousFocus() MOZ_OVERRIDE; |
|
51 |
|
52 already_AddRefed<RecorderProfileManager> GetRecorderProfileManager(); |
|
53 uint32_t GetCameraId() { return mCameraId; } |
|
54 |
|
55 virtual void Shutdown() MOZ_OVERRIDE; |
|
56 |
|
57 // Event handlers called directly from outside this class. |
|
58 void OnShutter(); |
|
59 void OnClosed(); |
|
60 void OnError(CameraControlListener::CameraErrorContext aContext, |
|
61 CameraControlListener::CameraError aError); |
|
62 void OnAutoFocusMoving(bool aIsMoving); |
|
63 |
|
64 protected: |
|
65 // Event handlers. |
|
66 void OnAutoFocusComplete(bool aAutoFocusSucceeded); |
|
67 void OnFacesDetected(const nsTArray<Face>& aFaces); |
|
68 void OnTakePictureComplete(uint8_t* aData, uint32_t aLength, const nsAString& aMimeType); |
|
69 |
|
70 bool OnNewPreviewFrame(layers::Image* aImage, uint32_t aWidth, uint32_t aHeight); |
|
71 void OnRecorderStateChange(CameraControlListener::RecorderState aState, |
|
72 int32_t aStatus = -1, int32_t aTrackNumber = -1); |
|
73 void OnPreviewStateChange(CameraControlListener::PreviewState aState); |
|
74 void OnHardwareStateChange(CameraControlListener::HardwareState aState); |
|
75 void OnConfigurationChange(); |
|
76 |
|
77 // When we create a new CameraThread, we keep a static reference to it so |
|
78 // that multiple CameraControl instances can find and reuse it; but we |
|
79 // don't want that reference to keep the thread object around unnecessarily, |
|
80 // so we make it a weak reference. The strong dynamic references will keep |
|
81 // the thread object alive as needed. |
|
82 static nsWeakPtr sCameraThread; |
|
83 nsCOMPtr<nsIThread> mCameraThread; |
|
84 |
|
85 virtual ~CameraControlImpl(); |
|
86 |
|
87 virtual void BeginBatchParameterSet() MOZ_OVERRIDE { } |
|
88 virtual void EndBatchParameterSet() MOZ_OVERRIDE { } |
|
89 |
|
90 // Manage camera event listeners. |
|
91 void AddListenerImpl(already_AddRefed<CameraControlListener> aListener); |
|
92 void RemoveListenerImpl(CameraControlListener* aListener); |
|
93 nsTArray<nsRefPtr<CameraControlListener> > mListeners; |
|
94 PRRWLock* mListenerLock; |
|
95 |
|
96 class ControlMessage; |
|
97 class ListenerMessage; |
|
98 |
|
99 virtual nsresult StartImpl(const Configuration* aConfig = nullptr) = 0; |
|
100 virtual nsresult StopImpl() = 0; |
|
101 virtual nsresult SetConfigurationImpl(const Configuration& aConfig) = 0; |
|
102 virtual nsresult StartPreviewImpl() = 0; |
|
103 virtual nsresult StopPreviewImpl() = 0; |
|
104 virtual nsresult AutoFocusImpl() = 0; |
|
105 virtual nsresult StartFaceDetectionImpl() = 0; |
|
106 virtual nsresult StopFaceDetectionImpl() = 0; |
|
107 virtual nsresult TakePictureImpl() = 0; |
|
108 virtual nsresult StartRecordingImpl(DeviceStorageFileDescriptor* aFileDescriptor, |
|
109 const StartRecordingOptions* aOptions) = 0; |
|
110 virtual nsresult StopRecordingImpl() = 0; |
|
111 virtual nsresult ResumeContinuousFocusImpl() = 0; |
|
112 virtual nsresult PushParametersImpl() = 0; |
|
113 virtual nsresult PullParametersImpl() = 0; |
|
114 virtual already_AddRefed<RecorderProfileManager> GetRecorderProfileManagerImpl() = 0; |
|
115 |
|
116 void OnShutterInternal(); |
|
117 void OnClosedInternal(); |
|
118 |
|
119 uint32_t mCameraId; |
|
120 |
|
121 CameraControlListener::CameraListenerConfiguration mCurrentConfiguration; |
|
122 |
|
123 CameraControlListener::PreviewState mPreviewState; |
|
124 CameraControlListener::HardwareState mHardwareState; |
|
125 |
|
126 private: |
|
127 CameraControlImpl(const CameraControlImpl&) MOZ_DELETE; |
|
128 CameraControlImpl& operator=(const CameraControlImpl&) MOZ_DELETE; |
|
129 }; |
|
130 |
|
131 } // namespace mozilla |
|
132 |
|
133 #endif // DOM_CAMERA_CAMERACONTROLIMPL_H |