1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_CanvasCaptureMediaStream_h_
7 #define mozilla_dom_CanvasCaptureMediaStream_h_
9 #include "DOMMediaStream.h"
10 #include "mozilla/dom/HTMLCanvasElement.h"
11 #include "PrincipalHandle.h"
17 class SourceMediaTrack
;
24 class CanvasCaptureMediaStream
;
25 class HTMLCanvasElement
;
26 class OutputStreamFrameListener
;
29 * The CanvasCaptureMediaStream is a MediaStream subclass that provides a video
30 * track containing frames from a canvas. See an architectural overview below.
32 * ----------------------------------------------------------------------------
33 * === Main Thread === __________________________
35 * | CanvasCaptureMediaStream |
36 * |__________________________|
40 * ________________________
41 * ________ FrameCaptureRequested? | |
42 * | | ------------------------> | OutputStreamDriver |
43 * | Canvas | SetFrameCapture() | (FrameCaptureListener) |
44 * |________| ------------------------> |________________________|
50 * __________________________
52 * | MTG / SourceMediaTrack |
53 * |__________________________|
54 * ----------------------------------------------------------------------------
58 * Base class for drivers of the output stream.
59 * It is up to each sub class to implement the NewFrame() callback of
60 * FrameCaptureListener.
62 class OutputStreamDriver
: public FrameCaptureListener
{
64 OutputStreamDriver(SourceMediaTrack
* aSourceStream
,
65 const PrincipalHandle
& aPrincipalHandle
);
67 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OutputStreamDriver
);
70 * Called from js' requestFrame() when it wants the next painted frame to be
71 * explicitly captured.
73 virtual void RequestFrameCapture() = 0;
76 * Sub classes can SetImage() to update the image being appended to the
77 * output stream. It will be appended on the next NotifyPull from MTG.
79 void SetImage(RefPtr
<layers::Image
>&& aImage
, const TimeStamp
& aTime
);
82 * Ends the track in mSourceStream when we know there won't be any more images
87 const RefPtr
<SourceMediaTrack
> mSourceStream
;
88 const PrincipalHandle mPrincipalHandle
;
91 virtual ~OutputStreamDriver();
94 class CanvasCaptureMediaStream
: public DOMMediaStream
{
96 CanvasCaptureMediaStream(nsPIDOMWindowInner
* aWindow
,
97 HTMLCanvasElement
* aCanvas
);
99 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CanvasCaptureMediaStream
,
103 nsresult
Init(const dom::Optional
<double>& aFPS
, nsIPrincipal
* aPrincipal
);
105 JSObject
* WrapObject(JSContext
* aCx
,
106 JS::Handle
<JSObject
*> aGivenProto
) override
;
109 HTMLCanvasElement
* Canvas() const { return mCanvas
; }
112 dom::FrameCaptureListener
* FrameCaptureListener();
115 * Stops capturing for this stream at mCanvas.
119 SourceMediaTrack
* GetSourceStream() const;
122 ~CanvasCaptureMediaStream();
125 RefPtr
<HTMLCanvasElement
> mCanvas
;
126 RefPtr
<OutputStreamDriver
> mOutputStreamDriver
;
130 } // namespace mozilla
132 #endif /* mozilla_dom_CanvasCaptureMediaStream_h_ */