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"
20 typedef void (*FrameBuilder
)(mozilla::layers::Image
* aImage
, void* aBuffer
, uint32_t aWidth
, uint32_t aHeight
);
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
31 enum { TRACK_VIDEO
= 1 };
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();
51 virtual ~DOMCameraPreview();
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
65 SetState(uint32_t aNewState
, const char* aFileOrFunc
, int aLine
)
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
);
74 NS_ASSERTION(NS_IsMainThread(), "Preview state set OFF OF main thread!");
80 uint32_t mFramesPerSecond
;
81 CameraPreviewMediaStream
* mInput
;
82 nsRefPtr
<mozilla::layers::ImageContainer
> mImageContainer
;
83 VideoSegment mVideoSegment
;
85 nsRefPtr
<ICameraControl
> mCameraControl
;
87 // Raw pointer; AddListener() keeps the reference for us
88 MediaStreamListener
* mListener
;
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