summaryrefslogtreecommitdiffstats
path: root/dom/media/systemservices/video_engine/tab_capturer.cc
blob: 793d9650283be50acf938c7edd2e11ca243080cf (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 *  Copyright (c) 2013 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.
 */

#include "tab_capturer.h"

#include "desktop_device_info.h"
#include "modules/desktop_capture/desktop_capture_options.h"
#include "modules/desktop_capture/desktop_frame.h"
#include "mozilla/Logging.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/ImageBitmap.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/TaskQueue.h"
#include "nsThreadUtils.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"

mozilla::LazyLogModule gTabShareLog("TabShare");
#define LOG_FUNC_IMPL(level) \
  MOZ_LOG(                   \
      gTabShareLog, level,   \
      ("TabCapturerWebrtc %p: %s id=%" PRIu64, this, __func__, mBrowserId))
#define LOG_FUNC() LOG_FUNC_IMPL(LogLevel::Debug)
#define LOG_FUNCV() LOG_FUNC_IMPL(LogLevel::Verbose)

using namespace mozilla::dom;

namespace mozilla {

class CaptureFrameRequest {
  using CapturePromise = TabCapturerWebrtc::CapturePromise;

 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CaptureFrameRequest)

  CaptureFrameRequest() : mCaptureTime(TimeStamp::Now()) {}

  operator MozPromiseRequestHolder<CapturePromise>&() { return mRequest; }

  void Complete() { mRequest.Complete(); }
  void Disconnect() { mRequest.Disconnect(); }
  bool Exists() { return mRequest.Exists(); }

 protected:
  virtual ~CaptureFrameRequest() { MOZ_RELEASE_ASSERT(!Exists()); }

 public:
  const TimeStamp mCaptureTime;

 private:
  MozPromiseRequestHolder<CapturePromise> mRequest;
};

TabCapturerWebrtc::TabCapturerWebrtc(
    SourceId aSourceId, nsCOMPtr<nsISerialEventTarget> aCaptureThread)
    : mBrowserId(aSourceId),
      mMainThreadWorker(
          TaskQueue::Create(do_AddRef(GetMainThreadSerialEventTarget()),
                            "TabCapturerWebrtc::mMainThreadWorker")),
      mCallbackWorker(TaskQueue::Create(aCaptureThread.forget(),
                                        "TabCapturerWebrtc::mCallbackWorker")) {
  RTC_DCHECK_RUN_ON(&mControlChecker);
  MOZ_ASSERT(aSourceId != 0);
  mCallbackChecker.Detach();

  LOG_FUNC();
}

// static
std::unique_ptr<webrtc::DesktopCapturer> TabCapturerWebrtc::Create(
    SourceId aSourceId, nsCOMPtr<nsISerialEventTarget> aCaptureThread) {
  return std::unique_ptr<webrtc::DesktopCapturer>(
      new TabCapturerWebrtc(aSourceId, std::move(aCaptureThread)));
}

TabCapturerWebrtc::~TabCapturerWebrtc() {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  LOG_FUNC();

  // mMainThreadWorker handles frame capture requests async. Since we're in the
  // dtor, no more frame capture requests can be made through CaptureFrame(). It
  // can be shut down now.
  mMainThreadWorker->BeginShutdown();

  // There may still be async frame capture requests in flight, waiting to be
  // reported to mCallback on mCallbackWorker. Disconnect them (must be done on
  // mCallbackWorker) and shut down mCallbackWorker to ensure nothing more can
  // get queued to it.
  MOZ_ALWAYS_SUCCEEDS(
      mCallbackWorker->Dispatch(NS_NewRunnableFunction(__func__, [this] {
        RTC_DCHECK_RUN_ON(&mCallbackChecker);
        for (const auto& req : mRequests) {
          DisconnectRequest(req);
        }
        mCallbackWorker->BeginShutdown();
      })));

  // Block until the workers have run all pending tasks. We must do this for two
  // reasons:
  // - All runnables dispatched to mMainThreadWorker and mCallbackWorker capture
  //   the raw pointer `this` as they rely on `this` outliving the worker
  //   TaskQueues.
  // - mCallback is only guaranteed to outlive `this`. No calls can be made to
  //   it after the dtor is finished.

  // Spin the underlying thread of mCallbackWorker, which we are currently on,
  // until it is empty. We have no other way of waiting for mCallbackWorker to
  // become empty while blocking the current call.
  SpinEventLoopUntil<ProcessFailureBehavior::IgnoreAndContinue>(
      "~TabCapturerWebrtc"_ns, [&] { return mCallbackWorker->IsEmpty(); });

  // No need to await shutdown since it was shut down synchronously above.
  mMainThreadWorker->AwaitIdle();
}

bool TabCapturerWebrtc::GetSourceList(
    webrtc::DesktopCapturer::SourceList* aSources) {
  MOZ_LOG(gTabShareLog, LogLevel::Debug,
          ("TabShare: GetSourceList, result %zu", aSources->size()));
  // XXX UI
  return true;
}

bool TabCapturerWebrtc::SelectSource(webrtc::DesktopCapturer::SourceId) {
  MOZ_ASSERT_UNREACHABLE("Source is passed through ctor for constness");
  return true;
}

bool TabCapturerWebrtc::FocusOnSelectedSource() { return true; }

void TabCapturerWebrtc::Start(webrtc::DesktopCapturer::Callback* aCallback) {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  RTC_DCHECK(!mCallback);
  RTC_DCHECK(aCallback);

  LOG_FUNC();

  mCallback = aCallback;
}

void TabCapturerWebrtc::CaptureFrame() {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  LOG_FUNCV();
  if (mRequests.GetSize() > 2) {
    // Allow two async capture requests in flight
    OnCaptureFrameFailure();
    return;
  }

  auto request = MakeRefPtr<CaptureFrameRequest>();
  InvokeAsync(mMainThreadWorker, __func__, [this] { return CaptureFrameNow(); })
      ->Then(mCallbackWorker, __func__,
             [this, request](CapturePromise::ResolveOrRejectValue&& aValue) {
               if (!CompleteRequest(request)) {
                 // Request was disconnected or overrun. Failure has already
                 // been reported to the callback elsewhere.
                 return;
               }

               if (aValue.IsReject()) {
                 OnCaptureFrameFailure();
                 return;
               }

               OnCaptureFrameSuccess(std::move(aValue.ResolveValue()));
             })
      ->Track(*request);
  mRequests.PushFront(request.forget());
}

void TabCapturerWebrtc::OnCaptureFrameSuccess(
    UniquePtr<dom::ImageBitmapCloneData> aData) {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  MOZ_DIAGNOSTIC_ASSERT(aData);
  LOG_FUNCV();
  webrtc::DesktopSize size(aData->mPictureRect.Width(),
                           aData->mPictureRect.Height());
  webrtc::DesktopRect rect = webrtc::DesktopRect::MakeSize(size);
  std::unique_ptr<webrtc::DesktopFrame> frame(
      new webrtc::BasicDesktopFrame(size));

  gfx::DataSourceSurface::ScopedMap map(aData->mSurface,
                                        gfx::DataSourceSurface::READ);
  if (!map.IsMapped()) {
    OnCaptureFrameFailure();
    return;
  }
  frame->CopyPixelsFrom(map.GetData(), map.GetStride(), rect);

  mCallback->OnCaptureResult(webrtc::DesktopCapturer::Result::SUCCESS,
                             std::move(frame));
}

void TabCapturerWebrtc::OnCaptureFrameFailure() {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  LOG_FUNC();
  mCallback->OnCaptureResult(webrtc::DesktopCapturer::Result::ERROR_TEMPORARY,
                             nullptr);
}

bool TabCapturerWebrtc::IsOccluded(const webrtc::DesktopVector& aPos) {
  return false;
}

class TabCapturedHandler final : public PromiseNativeHandler {
 public:
  NS_DECL_ISUPPORTS

  using CapturePromise = TabCapturerWebrtc::CapturePromise;

  static void Create(Promise* aPromise,
                     MozPromiseHolder<CapturePromise> aHolder) {
    MOZ_ASSERT(aPromise);
    MOZ_ASSERT(NS_IsMainThread());

    RefPtr<TabCapturedHandler> handler =
        new TabCapturedHandler(std::move(aHolder));
    aPromise->AppendNativeHandler(handler);
  }

  void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                        ErrorResult& aRv) override {
    MOZ_ASSERT(NS_IsMainThread());
    if (NS_WARN_IF(!aValue.isObject())) {
      mHolder.Reject(NS_ERROR_UNEXPECTED, __func__);
      return;
    }

    RefPtr<ImageBitmap> bitmap;
    if (NS_WARN_IF(NS_FAILED(
            UNWRAP_OBJECT(ImageBitmap, &aValue.toObject(), bitmap)))) {
      mHolder.Reject(NS_ERROR_UNEXPECTED, __func__);
      return;
    }

    UniquePtr<ImageBitmapCloneData> data = bitmap->ToCloneData();
    if (!data) {
      mHolder.Reject(NS_ERROR_UNEXPECTED, __func__);
      return;
    }

    mHolder.Resolve(std::move(data), __func__);
  }

  void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
                        ErrorResult& aRv) override {
    MOZ_ASSERT(NS_IsMainThread());
    mHolder.Reject(aRv.StealNSResult(), __func__);
  }

 private:
  explicit TabCapturedHandler(MozPromiseHolder<CapturePromise> aHolder)
      : mHolder(std::move(aHolder)) {}

  ~TabCapturedHandler() = default;

  MozPromiseHolder<CapturePromise> mHolder;
};

NS_IMPL_ISUPPORTS0(TabCapturedHandler)

bool TabCapturerWebrtc::CompleteRequest(CaptureFrameRequest* aRequest) {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  if (!aRequest->Exists()) {
    // Request was disconnected or overrun. mCallback has already been notified.
    return false;
  }
  while (CaptureFrameRequest* req = mRequests.Peek()) {
    if (req->mCaptureTime > aRequest->mCaptureTime) {
      break;
    }
    // Pop the request before calling the callback, in case it could mutate
    // mRequests, now or in the future.
    RefPtr<CaptureFrameRequest> dropMe = mRequests.Pop();
    req->Complete();
    if (req->mCaptureTime < aRequest->mCaptureTime) {
      OnCaptureFrameFailure();
    }
  }
  MOZ_DIAGNOSTIC_ASSERT(!aRequest->Exists());
  return true;
}

void TabCapturerWebrtc::DisconnectRequest(CaptureFrameRequest* aRequest) {
  RTC_DCHECK_RUN_ON(&mCallbackChecker);
  LOG_FUNCV();
  aRequest->Disconnect();
  OnCaptureFrameFailure();
}

auto TabCapturerWebrtc::CaptureFrameNow() -> RefPtr<CapturePromise> {
  MOZ_ASSERT(mMainThreadWorker->IsOnCurrentThread());
  LOG_FUNCV();

  WindowGlobalParent* wgp = nullptr;
  RefPtr<BrowsingContext> context =
      BrowsingContext::GetCurrentTopByBrowserId(mBrowserId);
  if (context) {
    wgp = context->Canonical()->GetCurrentWindowGlobal();
  }
  if (!wgp) {
    // If we can't access the window, we just won't capture anything
    return CapturePromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, __func__);
  }

  // XXX This would be more efficient if we used CrossProcessPaint directly and
  // returned a surface.
  RefPtr<Promise> promise =
      wgp->DrawSnapshot(nullptr, 1.0, "white"_ns, false, IgnoreErrors());
  if (!promise) {
    return CapturePromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
  }

  MozPromiseHolder<CapturePromise> holder;
  RefPtr<CapturePromise> p = holder.Ensure(__func__);
  TabCapturedHandler::Create(promise, std::move(holder));
  return p;
}

}  // namespace mozilla