CanvasCaptureMediaStream.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/. */
  5. #ifndef mozilla_dom_CanvasCaptureMediaStream_h_
  6. #define mozilla_dom_CanvasCaptureMediaStream_h_
  7. #include "DOMMediaStream.h"
  8. #include "mozilla/dom/HTMLCanvasElement.h"
  9. #include "StreamTracks.h"
  10. class nsIPrincipal;
  11. namespace mozilla {
  12. class DOMMediaStream;
  13. class MediaStreamListener;
  14. class SourceMediaStream;
  15. namespace layers {
  16. class Image;
  17. } // namespace layers
  18. namespace dom {
  19. class CanvasCaptureMediaStream;
  20. class HTMLCanvasElement;
  21. class OutputStreamFrameListener;
  22. /*
  23. * The CanvasCaptureMediaStream is a MediaStream subclass that provides a video
  24. * track containing frames from a canvas. See an architectural overview below.
  25. *
  26. * ----------------------------------------------------------------------------
  27. * === Main Thread === __________________________
  28. * | |
  29. * | CanvasCaptureMediaStream |
  30. * |__________________________|
  31. * |
  32. * | RequestFrame()
  33. * v
  34. * ________________________
  35. * ________ FrameCaptureRequested? | |
  36. * | | ------------------------> | OutputStreamDriver |
  37. * | Canvas | SetFrameCapture() | (FrameCaptureListener) |
  38. * |________| ------------------------> |________________________|
  39. * |
  40. * | SetImage()
  41. * v
  42. * ___________________
  43. * | StreamListener |
  44. * ---------------------------------------| (All image access |----------------
  45. * === MediaStreamGraph Thread === | Mutex Guarded) |
  46. * |___________________|
  47. * ^ |
  48. * NotifyPull() | | AppendToTrack()
  49. * | v
  50. * ___________________________
  51. * | |
  52. * | MSG / SourceMediaStream |
  53. * |___________________________|
  54. * ----------------------------------------------------------------------------
  55. */
  56. /*
  57. * Base class for drivers of the output stream.
  58. * It is up to each sub class to implement the NewFrame() callback of
  59. * FrameCaptureListener.
  60. */
  61. class OutputStreamDriver : public FrameCaptureListener
  62. {
  63. public:
  64. OutputStreamDriver(SourceMediaStream* aSourceStream,
  65. const TrackID& aTrackId,
  66. const PrincipalHandle& aPrincipalHandle);
  67. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OutputStreamDriver);
  68. /*
  69. * Sub classes can SetImage() to update the image being appended to the
  70. * output stream. It will be appended on the next NotifyPull from MSG.
  71. */
  72. void SetImage(const RefPtr<layers::Image>& aImage);
  73. /*
  74. * Makes sure any internal resources this driver is holding that may create
  75. * reference cycles are released.
  76. */
  77. virtual void Forget() {}
  78. protected:
  79. virtual ~OutputStreamDriver();
  80. class StreamListener;
  81. private:
  82. RefPtr<SourceMediaStream> mSourceStream;
  83. RefPtr<StreamListener> mStreamListener;
  84. };
  85. class CanvasCaptureMediaStream : public DOMMediaStream
  86. {
  87. public:
  88. CanvasCaptureMediaStream(nsPIDOMWindowInner* aWindow, HTMLCanvasElement* aCanvas);
  89. NS_DECL_ISUPPORTS_INHERITED
  90. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CanvasCaptureMediaStream, DOMMediaStream)
  91. nsresult Init(const dom::Optional<double>& aFPS, const TrackID& aTrackId,
  92. nsIPrincipal* aPrincipal);
  93. JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  94. // WebIDL
  95. HTMLCanvasElement* Canvas() const { return mCanvas; }
  96. void RequestFrame();
  97. dom::FrameCaptureListener* FrameCaptureListener();
  98. /**
  99. * Stops capturing for this stream at mCanvas.
  100. */
  101. void StopCapture();
  102. /**
  103. * Create a CanvasCaptureMediaStream whose underlying stream is a SourceMediaStream.
  104. */
  105. static already_AddRefed<CanvasCaptureMediaStream>
  106. CreateSourceStream(nsPIDOMWindowInner* aWindow,
  107. HTMLCanvasElement* aCanvas);
  108. protected:
  109. ~CanvasCaptureMediaStream();
  110. private:
  111. RefPtr<HTMLCanvasElement> mCanvas;
  112. RefPtr<OutputStreamDriver> mOutputStreamDriver;
  113. };
  114. } // namespace dom
  115. } // namespace mozilla
  116. #endif /* mozilla_dom_CanvasCaptureMediaStream_h_ */