Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / dom / camera / DOMCameraPreview.h
blob96ffad40ae0380485b5dfb6cd73c2051f1c2a4f5
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_DOMCAMERAPREVIEW_H
6 #define DOM_CAMERA_DOMCAMERAPREVIEW_H
8 #include "nsCycleCollectionParticipant.h"
9 #include "MediaStreamGraph.h"
10 #include "StreamBuffer.h"
11 #include "ICameraControl.h"
12 #include "DOMMediaStream.h"
13 #include "CameraPreviewMediaStream.h"
14 #include "CameraCommon.h"
16 class nsGlobalWindow;
18 namespace mozilla {
20 typedef void (*FrameBuilder)(mozilla::layers::Image* aImage, void* aBuffer, uint32_t aWidth, uint32_t aHeight);
22 /**
23 * DOMCameraPreview is only exposed to the DOM as an nsDOMMediaStream,
24 * which is a cycle-collection participant already, and we don't
25 * add any traceable fields here, so we don't need to declare any
26 * more cycle-collection goop.
28 class DOMCameraPreview : public DOMMediaStream
30 protected:
31 enum { TRACK_VIDEO = 1 };
33 public:
34 DOMCameraPreview(nsGlobalWindow* aWindow, ICameraControl* aCameraControl,
35 uint32_t aWidth, uint32_t aHeight, uint32_t aFramesPerSecond = 30);
37 bool ReceiveFrame(void* aBuffer, ImageFormat aFormat, mozilla::FrameBuilder aBuilder);
38 bool HaveEnoughBuffered();
40 void Start(); // called by the MediaStreamListener to start preview
41 void Started(); // called by the CameraControl when preview is started
42 void StopPreview(); // called by the MediaStreamListener to stop preview
43 void Stopped(bool aForced = false);
44 // called by the CameraControl when preview is stopped
45 void Error(); // something went wrong, NS_RELEASE needed
47 void SetStateStarted();
48 void SetStateStopped();
50 protected:
51 virtual ~DOMCameraPreview();
53 enum {
54 STOPPED,
55 STARTING,
56 STARTED,
57 STOPPING
59 uint32_t mState;
61 // Helper function, used in conjunction with the macro below, to make
62 // it easy to track state changes, which must happen only on the main
63 // thread.
64 void
65 SetState(uint32_t aNewState, const char* aFileOrFunc, int aLine)
67 #ifdef PR_LOGGING
68 const char* states[] = { "stopped", "starting", "started", "stopping" };
69 MOZ_ASSERT(mState < sizeof(states) / sizeof(states[0]));
70 MOZ_ASSERT(aNewState < sizeof(states) / sizeof(states[0]));
71 DOM_CAMERA_LOGI("SetState: (this=%p) '%s' --> '%s' : %s:%d\n", this, states[mState], states[aNewState], aFileOrFunc, aLine);
72 #endif
74 NS_ASSERTION(NS_IsMainThread(), "Preview state set OFF OF main thread!");
75 mState = aNewState;
78 uint32_t mWidth;
79 uint32_t mHeight;
80 uint32_t mFramesPerSecond;
81 CameraPreviewMediaStream* mInput;
82 nsRefPtr<mozilla::layers::ImageContainer> mImageContainer;
83 VideoSegment mVideoSegment;
84 uint32_t mFrameCount;
85 nsRefPtr<ICameraControl> mCameraControl;
87 // Raw pointer; AddListener() keeps the reference for us
88 MediaStreamListener* mListener;
90 private:
91 DOMCameraPreview(const DOMCameraPreview&) MOZ_DELETE;
92 DOMCameraPreview& operator=(const DOMCameraPreview&) MOZ_DELETE;
95 } // namespace mozilla
97 #define DOM_CAMERA_SETSTATE(newState) SetState((newState), __func__, __LINE__)
99 #endif // DOM_CAMERA_DOMCAMERAPREVIEW_H