Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / dom / camera / CameraControlImpl.h
blobe3e1618a8a15d33790c193e22f40419261cc895e
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
8 #include "mozilla/Attributes.h"
9 #include "nsDOMFile.h"
10 #include "nsProxyRelease.h"
11 #include "DictionaryHelpers.h"
12 #include "nsIDOMDeviceStorage.h"
13 #include "DOMCameraManager.h"
14 #include "DOMCameraPreview.h"
15 #include "ICameraControl.h"
16 #include "CameraCommon.h"
18 namespace mozilla {
20 class GetPreviewStreamTask;
21 class StartPreviewTask;
22 class StopPreviewTask;
23 class AutoFocusTask;
24 class TakePictureTask;
25 class StartRecordingTask;
26 class StopRecordingTask;
27 class SetParameterTask;
28 class GetParameterTask;
29 class GetPreviewStreamVideoModeTask;
30 class ReleaseHardwareTask;
32 class DOMCameraPreview;
33 class RecorderProfileManager;
35 class CameraControlImpl : public ICameraControl
37 friend class GetPreviewStreamTask;
38 friend class StartPreviewTask;
39 friend class StopPreviewTask;
40 friend class AutoFocusTask;
41 friend class TakePictureTask;
42 friend class StartRecordingTask;
43 friend class StopRecordingTask;
44 friend class SetParameterTask;
45 friend class GetParameterTask;
46 friend class GetPreviewStreamVideoModeTask;
47 friend class ReleaseHardwareTask;
49 public:
50 CameraControlImpl(uint32_t aCameraId, nsIThread* aCameraThread, uint64_t aWindowId);
52 nsresult GetPreviewStream(idl::CameraSize aSize, nsICameraPreviewStreamCallback* onSuccess, nsICameraErrorCallback* onError);
53 nsresult StartPreview(DOMCameraPreview* aDOMPreview);
54 void StopPreview();
55 nsresult AutoFocus(nsICameraAutoFocusCallback* onSuccess, nsICameraErrorCallback* onError);
56 nsresult TakePicture(const idl::CameraSize& aSize, int32_t aRotation, const nsAString& aFileFormat, idl::CameraPosition aPosition, uint64_t aDateTime, nsICameraTakePictureCallback* onSuccess, nsICameraErrorCallback* onError);
57 nsresult StartRecording(idl::CameraStartRecordingOptions* aOptions, nsIFile* aFolder, const nsAString& aFilename, nsICameraStartRecordingCallback* onSuccess, nsICameraErrorCallback* onError);
58 nsresult StopRecording();
59 nsresult GetPreviewStreamVideoMode(idl::CameraRecorderOptions* aOptions, nsICameraPreviewStreamCallback* onSuccess, nsICameraErrorCallback* onError);
60 nsresult ReleaseHardware(nsICameraReleaseCallback* onSuccess, nsICameraErrorCallback* onError);
62 nsresult Set(uint32_t aKey, const nsAString& aValue);
63 nsresult Get(uint32_t aKey, nsAString& aValue);
64 nsresult Set(uint32_t aKey, double aValue);
65 nsresult Get(uint32_t aKey, double* aValue);
66 nsresult Set(JSContext* aCx, uint32_t aKey, const JS::Value& aValue, uint32_t aLimit);
67 nsresult Get(JSContext* aCx, uint32_t aKey, JS::Value* aValue);
68 nsresult Set(nsICameraShutterCallback* aOnShutter);
69 nsresult Get(nsICameraShutterCallback** aOnShutter);
70 nsresult Set(nsICameraClosedCallback* aOnClosed);
71 nsresult Get(nsICameraClosedCallback** aOnClosed);
72 nsresult Set(nsICameraRecorderStateChange* aOnRecorderStateChange);
73 nsresult Get(nsICameraRecorderStateChange** aOnRecorderStateChange);
74 nsresult Set(nsICameraPreviewStateChange* aOnPreviewStateChange);
75 nsresult Get(nsICameraPreviewStateChange** aOnPreviewStateChange);
76 nsresult Set(uint32_t aKey, const idl::CameraSize& aSize);
77 nsresult Get(uint32_t aKey, idl::CameraSize& aSize);
78 nsresult Get(uint32_t aKey, int32_t* aValue);
80 nsresult SetFocusAreas(JSContext* aCx, const JS::Value& aValue)
82 return Set(aCx, CAMERA_PARAM_FOCUSAREAS, aValue, mMaxFocusAreas);
85 nsresult SetMeteringAreas(JSContext* aCx, const JS::Value& aValue)
87 return Set(aCx, CAMERA_PARAM_METERINGAREAS, aValue, mMaxMeteringAreas);
90 already_AddRefed<RecorderProfileManager> GetRecorderProfileManager();
91 uint32_t GetCameraId() { return mCameraId; }
93 virtual const char* GetParameter(const char* aKey) = 0;
94 virtual const char* GetParameterConstChar(uint32_t aKey) = 0;
95 virtual double GetParameterDouble(uint32_t aKey) = 0;
96 virtual int32_t GetParameterInt32(uint32_t aKey) = 0;
97 virtual void GetParameter(uint32_t aKey, nsTArray<idl::CameraRegion>& aRegions) = 0;
98 virtual void GetParameter(uint32_t aKey, idl::CameraSize& aSize) = 0;
99 virtual void SetParameter(const char* aKey, const char* aValue) = 0;
100 virtual void SetParameter(uint32_t aKey, const char* aValue) = 0;
101 virtual void SetParameter(uint32_t aKey, double aValue) = 0;
102 virtual void SetParameter(uint32_t aKey, const nsTArray<idl::CameraRegion>& aRegions) = 0;
103 virtual void SetParameter(uint32_t aKey, const idl::CameraSize& aSize) = 0;
104 virtual nsresult GetVideoSizes(nsTArray<idl::CameraSize>& aVideoSizes) = 0;
105 virtual nsresult PushParameters() = 0;
106 virtual void Shutdown();
108 bool ReceiveFrame(void* aBuffer, ImageFormat aFormat, FrameBuilder aBuilder);
109 void OnShutter();
110 void OnClosed();
111 void OnRecorderStateChange(const nsString& aStateMsg, int32_t aStatus, int32_t aTrackNumber);
113 enum PreviewState {
114 PREVIEW_STOPPED,
115 PREVIEW_STARTED
117 void OnPreviewStateChange(PreviewState aNewState);
119 uint64_t GetWindowId()
121 return mWindowId;
124 protected:
125 virtual ~CameraControlImpl();
127 virtual nsresult GetPreviewStreamImpl(GetPreviewStreamTask* aGetPreviewStream) = 0;
128 virtual nsresult StartPreviewImpl(StartPreviewTask* aStartPreview) = 0;
129 virtual nsresult StopPreviewImpl(StopPreviewTask* aStopPreview) = 0;
130 virtual nsresult AutoFocusImpl(AutoFocusTask* aAutoFocus) = 0;
131 virtual nsresult TakePictureImpl(TakePictureTask* aTakePicture) = 0;
132 virtual nsresult StartRecordingImpl(StartRecordingTask* aStartRecording) = 0;
133 virtual nsresult StopRecordingImpl(StopRecordingTask* aStopRecording) = 0;
134 virtual nsresult PushParametersImpl() = 0;
135 virtual nsresult PullParametersImpl() = 0;
136 virtual nsresult GetPreviewStreamVideoModeImpl(GetPreviewStreamVideoModeTask* aGetPreviewStreamVideoMode) = 0;
137 virtual nsresult ReleaseHardwareImpl(ReleaseHardwareTask* aReleaseHardware) = 0;
138 virtual already_AddRefed<RecorderProfileManager> GetRecorderProfileManagerImpl() = 0;
140 void OnShutterInternal();
141 void OnClosedInternal();
143 uint32_t mCameraId;
144 nsCOMPtr<nsIThread> mCameraThread;
145 uint64_t mWindowId;
146 nsString mFileFormat;
147 uint32_t mMaxMeteringAreas;
148 uint32_t mMaxFocusAreas;
149 PreviewState mPreviewState;
152 * 'mDOMPreview' is a raw pointer to the object that will receive incoming
153 * preview frames. This is guaranteed to be valid, or null.
155 * It is set by a call to StartPreview(), and set to null on StopPreview().
156 * It is up to the caller to ensure that the object will not disappear
157 * out from under this pointer--usually by calling NS_ADDREF().
159 DOMCameraPreview* mDOMPreview;
161 nsMainThreadPtrHandle<nsICameraAutoFocusCallback> mAutoFocusOnSuccessCb;
162 nsMainThreadPtrHandle<nsICameraErrorCallback> mAutoFocusOnErrorCb;
163 nsMainThreadPtrHandle<nsICameraTakePictureCallback> mTakePictureOnSuccessCb;
164 nsMainThreadPtrHandle<nsICameraErrorCallback> mTakePictureOnErrorCb;
165 nsMainThreadPtrHandle<nsICameraShutterCallback> mOnShutterCb;
166 nsMainThreadPtrHandle<nsICameraClosedCallback> mOnClosedCb;
167 nsMainThreadPtrHandle<nsICameraRecorderStateChange> mOnRecorderStateChangeCb;
168 nsMainThreadPtrHandle<nsICameraPreviewStateChange> mOnPreviewStateChangeCb;
170 private:
171 CameraControlImpl(const CameraControlImpl&) MOZ_DELETE;
172 CameraControlImpl& operator=(const CameraControlImpl&) MOZ_DELETE;
175 // Error result runnable
176 class CameraErrorResult : public nsRunnable
178 public:
179 CameraErrorResult(nsMainThreadPtrHandle<nsICameraErrorCallback> onError, const nsString& aErrorMsg, uint64_t aWindowId)
180 : mOnErrorCb(onError)
181 , mErrorMsg(aErrorMsg)
182 , mWindowId(aWindowId)
185 NS_IMETHOD Run() MOZ_OVERRIDE
187 MOZ_ASSERT(NS_IsMainThread());
189 if (mOnErrorCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
190 mOnErrorCb->HandleEvent(mErrorMsg);
192 return NS_OK;
195 protected:
196 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
197 const nsString mErrorMsg;
198 uint64_t mWindowId;
201 // Return the resulting preview stream to JS. Runs on the main thread.
202 class GetPreviewStreamResult : public nsRunnable
204 public:
205 GetPreviewStreamResult(CameraControlImpl* aCameraControl, uint32_t aWidth, uint32_t aHeight, uint32_t aFramesPerSecond, nsMainThreadPtrHandle<nsICameraPreviewStreamCallback>& onSuccess, uint64_t aWindowId)
206 : mCameraControl(aCameraControl)
207 , mWidth(aWidth)
208 , mHeight(aHeight)
209 , mFramesPerSecond(aFramesPerSecond)
210 , mOnSuccessCb(onSuccess)
211 , mWindowId(aWindowId)
213 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
216 virtual ~GetPreviewStreamResult()
218 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
221 // Run() method is implementation specific.
222 NS_IMETHOD Run() MOZ_OVERRIDE;
224 protected:
225 nsRefPtr<CameraControlImpl> mCameraControl;
226 uint32_t mWidth;
227 uint32_t mHeight;
228 uint32_t mFramesPerSecond;
229 nsMainThreadPtrHandle<nsICameraPreviewStreamCallback> mOnSuccessCb;
230 uint64_t mWindowId;
233 // Get the desired preview stream.
234 class GetPreviewStreamTask : public nsRunnable
236 public:
237 GetPreviewStreamTask(CameraControlImpl* aCameraControl, idl::CameraSize aSize, nsICameraPreviewStreamCallback* onSuccess, nsICameraErrorCallback* onError)
238 : mSize(aSize)
239 , mCameraControl(aCameraControl)
240 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraPreviewStreamCallback>(onSuccess))
241 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
243 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
246 virtual ~GetPreviewStreamTask()
248 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
251 NS_IMETHOD Run() MOZ_OVERRIDE
253 nsresult rv = mCameraControl->GetPreviewStreamImpl(this);
255 if (NS_FAILED(rv)) {
256 nsCOMPtr<nsIRunnable> cameraErrorResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mCameraControl->GetWindowId());
257 rv = NS_DispatchToMainThread(cameraErrorResult);
258 NS_ENSURE_SUCCESS(rv, rv);
260 return rv;
263 idl::CameraSize mSize;
264 nsRefPtr<CameraControlImpl> mCameraControl;
265 nsMainThreadPtrHandle<nsICameraPreviewStreamCallback> mOnSuccessCb;
266 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
269 // Return the autofocus status to JS. Runs on the main thread.
270 class AutoFocusResult : public nsRunnable
272 public:
273 AutoFocusResult(bool aSuccess, nsMainThreadPtrHandle<nsICameraAutoFocusCallback> onSuccess, uint64_t aWindowId)
274 : mSuccess(aSuccess)
275 , mOnSuccessCb(onSuccess)
276 , mWindowId(aWindowId)
279 virtual ~AutoFocusResult() { }
281 NS_IMETHOD Run() MOZ_OVERRIDE
283 MOZ_ASSERT(NS_IsMainThread());
285 if (mOnSuccessCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
286 mOnSuccessCb->HandleEvent(mSuccess);
288 return NS_OK;
291 protected:
292 bool mSuccess;
293 nsMainThreadPtrHandle<nsICameraAutoFocusCallback> mOnSuccessCb;
294 uint64_t mWindowId;
297 // Autofocus the camera.
298 class AutoFocusTask : public nsRunnable
300 public:
301 AutoFocusTask(CameraControlImpl* aCameraControl, bool aCancel, nsICameraAutoFocusCallback* onSuccess, nsICameraErrorCallback* onError)
302 : mCameraControl(aCameraControl)
303 , mCancel(aCancel)
304 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraAutoFocusCallback>(onSuccess))
305 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
307 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
310 virtual ~AutoFocusTask()
312 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
315 NS_IMETHOD Run() MOZ_OVERRIDE
317 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
318 nsresult rv = mCameraControl->AutoFocusImpl(this);
319 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
321 if (NS_FAILED(rv)) {
322 nsCOMPtr<nsIRunnable> cameraErrorResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mCameraControl->GetWindowId());
323 rv = NS_DispatchToMainThread(cameraErrorResult);
324 NS_ENSURE_SUCCESS(rv, rv);
326 return rv;
329 nsRefPtr<CameraControlImpl> mCameraControl;
330 bool mCancel;
331 nsMainThreadPtrHandle<nsICameraAutoFocusCallback> mOnSuccessCb;
332 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
335 // Return the captured picture to JS. Runs on the main thread.
336 class TakePictureResult : public nsRunnable
338 public:
339 TakePictureResult(uint8_t* aData, uint64_t aLength, const nsAString& aMimeType, nsMainThreadPtrHandle<nsICameraTakePictureCallback> onSuccess, uint64_t aWindowId)
340 : mData(aData)
341 , mLength(aLength)
342 , mMimeType(aMimeType)
343 , mOnSuccessCb(onSuccess)
344 , mWindowId(aWindowId)
346 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
349 virtual ~TakePictureResult()
351 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
354 NS_IMETHOD Run() MOZ_OVERRIDE
356 MOZ_ASSERT(NS_IsMainThread());
358 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
359 if (mOnSuccessCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
360 nsCOMPtr<nsIDOMBlob> image = new nsDOMMemoryFile(static_cast<void*>(mData), static_cast<uint64_t>(mLength), mMimeType);
361 mOnSuccessCb->HandleEvent(image);
362 } else {
363 delete[] mData;
365 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
366 return NS_OK;
369 protected:
370 uint8_t* mData;
371 uint64_t mLength;
372 nsString mMimeType;
373 nsMainThreadPtrHandle<nsICameraTakePictureCallback> mOnSuccessCb;
374 uint64_t mWindowId;
377 // Capture a still image with the camera.
378 class TakePictureTask : public nsRunnable
380 public:
381 TakePictureTask(CameraControlImpl* aCameraControl, bool aCancel, const idl::CameraSize& aSize, int32_t aRotation, const nsAString& aFileFormat, idl::CameraPosition aPosition, uint64_t aDateTime, nsICameraTakePictureCallback* onSuccess, nsICameraErrorCallback* onError)
382 : mCameraControl(aCameraControl)
383 , mCancel(aCancel)
384 , mSize(aSize)
385 , mRotation(aRotation)
386 , mFileFormat(aFileFormat)
387 , mPosition(aPosition)
388 , mDateTime(aDateTime)
389 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraTakePictureCallback>(onSuccess))
390 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
392 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
395 virtual ~TakePictureTask()
397 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
400 NS_IMETHOD Run() MOZ_OVERRIDE
402 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
403 nsresult rv = mCameraControl->TakePictureImpl(this);
404 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
406 if (NS_FAILED(rv)) {
407 nsCOMPtr<nsIRunnable> cameraErrorResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mCameraControl->GetWindowId());
408 rv = NS_DispatchToMainThread(cameraErrorResult);
409 NS_ENSURE_SUCCESS(rv, rv);
411 return rv;
414 nsRefPtr<CameraControlImpl> mCameraControl;
415 bool mCancel;
416 idl::CameraSize mSize;
417 int32_t mRotation;
418 nsString mFileFormat;
419 idl::CameraPosition mPosition;
420 uint64_t mDateTime;
421 nsMainThreadPtrHandle<nsICameraTakePictureCallback> mOnSuccessCb;
422 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
425 // Return the result of starting recording. Runs on the main thread.
426 class StartRecordingResult : public nsRunnable
428 public:
429 StartRecordingResult(nsMainThreadPtrHandle<nsICameraStartRecordingCallback> onSuccess, uint64_t aWindowId)
430 : mOnSuccessCb(onSuccess)
431 , mWindowId(aWindowId)
434 virtual ~StartRecordingResult() { }
436 NS_IMETHOD Run() MOZ_OVERRIDE
438 MOZ_ASSERT(NS_IsMainThread());
440 if (mOnSuccessCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
441 mOnSuccessCb->HandleEvent();
443 return NS_OK;
446 protected:
447 nsMainThreadPtrHandle<nsICameraStartRecordingCallback> mOnSuccessCb;
448 uint64_t mWindowId;
451 // Start video recording.
452 class StartRecordingTask : public nsRunnable
454 public:
455 StartRecordingTask(CameraControlImpl* aCameraControl, idl::CameraStartRecordingOptions aOptions, nsIFile* aFolder, const nsAString& aFilename, nsICameraStartRecordingCallback* onSuccess, nsICameraErrorCallback* onError, uint64_t aWindowId)
456 : mCameraControl(aCameraControl)
457 , mOptions(aOptions)
458 , mFolder(aFolder)
459 , mFilename(aFilename)
460 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraStartRecordingCallback>(onSuccess))
461 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
462 , mWindowId(aWindowId)
464 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
467 virtual ~StartRecordingTask()
469 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
472 NS_IMETHOD Run() MOZ_OVERRIDE
474 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
475 nsresult rv = mCameraControl->StartRecordingImpl(this);
476 DOM_CAMERA_LOGT("%s:%d : result %d\n", __func__, __LINE__, rv);
478 // dispatch the callback
479 nsCOMPtr<nsIRunnable> startRecordingResult;
480 if (NS_SUCCEEDED(rv)) {
481 startRecordingResult = new StartRecordingResult(mOnSuccessCb, mWindowId);
482 } else {
483 startRecordingResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mWindowId);
485 rv = NS_DispatchToMainThread(startRecordingResult);
486 if (NS_FAILED(rv)) {
487 DOM_CAMERA_LOGE("Failed to dispatch start recording result to main thread (%d)!", rv);
489 return rv;
492 nsRefPtr<CameraControlImpl> mCameraControl;
493 idl::CameraStartRecordingOptions mOptions;
494 nsCOMPtr<nsIFile> mFolder;
495 nsString mFilename;
496 nsMainThreadPtrHandle<nsICameraStartRecordingCallback> mOnSuccessCb;
497 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
498 uint64_t mWindowId;
501 // Stop video recording.
502 class StopRecordingTask : public nsRunnable
504 public:
505 StopRecordingTask(CameraControlImpl* aCameraControl)
506 : mCameraControl(aCameraControl)
508 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
511 virtual ~StopRecordingTask()
513 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
516 NS_IMETHOD Run() MOZ_OVERRIDE
518 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
519 nsresult rv = mCameraControl->StopRecordingImpl(this);
520 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
522 NS_ENSURE_SUCCESS(rv, rv);
523 return NS_OK;
526 nsRefPtr<CameraControlImpl> mCameraControl;
529 // Start the preview.
530 class StartPreviewTask : public nsRunnable
532 public:
533 StartPreviewTask(CameraControlImpl* aCameraControl, DOMCameraPreview* aDOMPreview)
534 : mCameraControl(aCameraControl)
535 , mDOMPreview(aDOMPreview)
537 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
540 virtual ~StartPreviewTask()
542 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
545 NS_IMETHOD Run() MOZ_OVERRIDE
547 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
548 nsresult rv = mCameraControl->StartPreviewImpl(this);
549 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
551 NS_ENSURE_SUCCESS(rv, rv);
552 return NS_OK;
555 nsRefPtr<CameraControlImpl> mCameraControl;
556 DOMCameraPreview* mDOMPreview; // DOMCameraPreview NS_ADDREFs itself for us
559 // Stop the preview.
560 class StopPreviewTask : public nsRunnable
562 public:
563 StopPreviewTask(CameraControlImpl* aCameraControl)
564 : mCameraControl(aCameraControl)
566 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
569 virtual ~StopPreviewTask()
571 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
574 NS_IMETHOD Run() MOZ_OVERRIDE
576 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
577 mCameraControl->StopPreviewImpl(this);
578 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
580 return NS_OK;
583 nsRefPtr<CameraControlImpl> mCameraControl;
586 // Get the video mode preview stream.
587 class GetPreviewStreamVideoModeTask : public nsRunnable
589 public:
590 GetPreviewStreamVideoModeTask(CameraControlImpl* aCameraControl, idl::CameraRecorderOptions aOptions, nsICameraPreviewStreamCallback* onSuccess, nsICameraErrorCallback* onError)
591 : mCameraControl(aCameraControl)
592 , mOptions(aOptions)
593 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraPreviewStreamCallback>(onSuccess))
594 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
597 NS_IMETHOD Run() MOZ_OVERRIDE
599 DOM_CAMERA_LOGI("%s:%d -- BEFORE IMPL\n", __func__, __LINE__);
600 nsresult rv = mCameraControl->GetPreviewStreamVideoModeImpl(this);
601 DOM_CAMERA_LOGI("%s:%d -- AFTER IMPL : rv = %d\n", __func__, __LINE__, rv);
603 if (NS_FAILED(rv)) {
604 nsCOMPtr<nsIRunnable> cameraErrorResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mCameraControl->GetWindowId());
605 rv = NS_DispatchToMainThread(cameraErrorResult);
606 NS_ENSURE_SUCCESS(rv, rv);
608 return NS_OK;
611 nsRefPtr<CameraControlImpl> mCameraControl;
612 idl::CameraRecorderOptions mOptions;
613 nsMainThreadPtrHandle<nsICameraPreviewStreamCallback> mOnSuccessCb;
614 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
617 // Return the result of releasing the camera hardware. Runs on the main thread.
618 class ReleaseHardwareResult : public nsRunnable
620 public:
621 ReleaseHardwareResult(nsMainThreadPtrHandle<nsICameraReleaseCallback> onSuccess, uint64_t aWindowId)
622 : mOnSuccessCb(onSuccess)
623 , mWindowId(aWindowId)
625 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
628 virtual ~ReleaseHardwareResult()
630 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
633 NS_IMETHOD Run() MOZ_OVERRIDE
635 MOZ_ASSERT(NS_IsMainThread());
637 if (mOnSuccessCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
638 mOnSuccessCb->HandleEvent();
640 return NS_OK;
643 protected:
644 nsMainThreadPtrHandle<nsICameraReleaseCallback> mOnSuccessCb;
645 uint64_t mWindowId;
648 // Release the camera hardware.
649 class ReleaseHardwareTask : public nsRunnable
651 public:
652 ReleaseHardwareTask(CameraControlImpl* aCameraControl, nsICameraReleaseCallback* onSuccess, nsICameraErrorCallback* onError)
653 : mCameraControl(aCameraControl)
654 , mOnSuccessCb(new nsMainThreadPtrHolder<nsICameraReleaseCallback>(onSuccess))
655 , mOnErrorCb(new nsMainThreadPtrHolder<nsICameraErrorCallback>(onError))
657 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
660 virtual ~ReleaseHardwareTask()
662 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
665 NS_IMETHOD Run() MOZ_OVERRIDE
667 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
668 nsresult rv = mCameraControl->ReleaseHardwareImpl(this);
669 DOM_CAMERA_LOGT("%s:%d\n", __func__, __LINE__);
671 if (NS_FAILED(rv)) {
672 nsCOMPtr<nsIRunnable> cameraErrorResult = new CameraErrorResult(mOnErrorCb, NS_LITERAL_STRING("FAILURE"), mCameraControl->GetWindowId());
673 rv = NS_DispatchToMainThread(cameraErrorResult);
674 NS_ENSURE_SUCCESS(rv, rv);
676 return rv;
679 nsRefPtr<CameraControlImpl> mCameraControl;
680 nsMainThreadPtrHandle<nsICameraReleaseCallback> mOnSuccessCb;
681 nsMainThreadPtrHandle<nsICameraErrorCallback> mOnErrorCb;
684 // Report that the video recorder state has changed.
685 class CameraRecorderStateChange : public nsRunnable
687 public:
688 CameraRecorderStateChange(nsMainThreadPtrHandle<nsICameraRecorderStateChange> onStateChange, const nsString& aStateMsg, int32_t aStatus, int32_t aTrackNumber, uint64_t aWindowId)
689 : mOnStateChangeCb(onStateChange)
690 , mStateMsg(aStateMsg)
691 , mStatus(aStatus)
692 , mTrackNumber(aTrackNumber)
693 , mWindowId(aWindowId)
696 NS_IMETHOD Run() MOZ_OVERRIDE
698 MOZ_ASSERT(NS_IsMainThread());
700 if (mOnStateChangeCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
701 // For now, just pass the state message and swallow mStatus and mTrackNumber
702 mOnStateChangeCb->HandleStateChange(mStateMsg);
704 return NS_OK;
707 protected:
708 nsMainThreadPtrHandle<nsICameraRecorderStateChange> mOnStateChangeCb;
709 const nsString mStateMsg;
710 int32_t mStatus;
711 int32_t mTrackNumber;
712 uint64_t mWindowId;
715 // Report that the preview stream state has changed.
716 class CameraPreviewStateChange : public nsRunnable
718 public:
719 CameraPreviewStateChange(nsMainThreadPtrHandle<nsICameraPreviewStateChange> onStateChange, const nsString& aStateMsg, uint64_t aWindowId)
720 : mOnStateChangeCb(onStateChange)
721 , mStateMsg(aStateMsg)
722 , mWindowId(aWindowId)
725 NS_IMETHOD Run()
727 MOZ_ASSERT(NS_IsMainThread());
729 if (mOnStateChangeCb.get() && nsDOMCameraManager::IsWindowStillActive(mWindowId)) {
730 mOnStateChangeCb->HandleStateChange(mStateMsg);
732 return NS_OK;
735 protected:
736 nsMainThreadPtrHandle<nsICameraPreviewStateChange> mOnStateChangeCb;
737 const nsString mStateMsg;
738 uint64_t mWindowId;
741 } // namespace mozilla
743 #endif // DOM_CAMERA_CAMERACONTROLIMPL_H