summaryrefslogtreecommitdiffstats
path: root/dom/media/systemservices/video_engine/tab_capturer.h
blob: 92c4fa2ad1b6a781b99ae9d7ccbf97442e57915e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef MODULES_DESKTOP_CAPTURE_TAB_CAPTURER_H_
#define MODULES_DESKTOP_CAPTURE_TAB_CAPTURER_H_

#include "api/sequence_checker.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "mozilla/MozPromise.h"
#include "nsDeque.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace dom {
struct ImageBitmapCloneData;
}  // namespace dom

class CaptureFrameRequest;
class TabCapturedHandler;
class TaskQueue;

class TabCapturerWebrtc : public webrtc::DesktopCapturer {
 protected:
  TabCapturerWebrtc(SourceId aSourceId,
                    nsCOMPtr<nsISerialEventTarget> aCaptureThread);
  ~TabCapturerWebrtc();

 public:
  friend class CaptureFrameRequest;
  friend class TabCapturedHandler;

  static std::unique_ptr<webrtc::DesktopCapturer> Create(
      SourceId aSourceId, nsCOMPtr<nsISerialEventTarget> aCaptureThread);

  TabCapturerWebrtc(const TabCapturerWebrtc&) = delete;
  TabCapturerWebrtc& operator=(const TabCapturerWebrtc&) = delete;

  // DesktopCapturer interface.
  void Start(Callback* aCallback) override;
  void CaptureFrame() override;
  bool GetSourceList(SourceList* aSources) override;
  bool SelectSource(SourceId) override;
  bool FocusOnSelectedSource() override;
  bool IsOccluded(const webrtc::DesktopVector& aPos) override;

 private:
  // Capture code
  using CapturePromise =
      MozPromise<UniquePtr<dom::ImageBitmapCloneData>, nsresult, true>;
  RefPtr<CapturePromise> CaptureFrameNow();

  // Helper that checks for overrun requests. Returns true if aRequest had not
  // been dropped due to disconnection or overrun.
  // Note that if this returns true, the caller takes the responsibility to call
  // mCallback with a capture result for aRequest.
  bool CompleteRequest(CaptureFrameRequest* aRequest);

  // Helper that disconnects the request, and notifies mCallback of a temporary
  // failure.
  void DisconnectRequest(CaptureFrameRequest* aRequest);

  // Handle the result from the async callback from CaptureFrameNow.
  void OnCaptureFrameSuccess(UniquePtr<dom::ImageBitmapCloneData> aData);
  void OnCaptureFrameFailure();

  const uint64_t mBrowserId;
  const RefPtr<TaskQueue> mMainThreadWorker;
  const RefPtr<TaskQueue> mCallbackWorker;
  webrtc::SequenceChecker mControlChecker;
  webrtc::SequenceChecker mCallbackChecker;
  // Set in Start() and guaranteed by the owner of this class to outlive us.
  webrtc::DesktopCapturer::Callback* mCallback
      RTC_GUARDED_BY(mCallbackChecker) = nullptr;

  // mCallbackWorker only
  nsRefPtrDeque<CaptureFrameRequest> mRequests RTC_GUARDED_BY(mCallbackChecker);
};

}  // namespace mozilla

#endif  // MODULES_DESKTOP_CAPTURE_TAB_CAPTURER_H_