File: mojo_video_capturer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,639 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/mojo_video_capturer.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/task/bind_post_task.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "remoting/host/video_memory_utils.h"
#include "remoting/protocol/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/shared_memory.h"

namespace remoting {

MojoVideoCapturer::MojoVideoCapturer(
    std::unique_ptr<DesktopCapturer> capturer,
    scoped_refptr<AutoThreadTaskRunner> caller_task_runner)
    : video_capturer_(std::move(capturer)) {
  weak_ptr_ = weak_factory_.GetWeakPtr();

  video_capturer_->SetSharedMemoryFactory(
      std::make_unique<SharedVideoMemoryFactory>(
          base::BindPostTask(
              caller_task_runner,
              base::BindRepeating(
                  &MojoVideoCapturer::OnSharedMemoryRegionCreated, weak_ptr_)),
          base::BindPostTask(
              caller_task_runner,
              base::BindRepeating(
                  &MojoVideoCapturer::OnSharedMemoryRegionReleased,
                  weak_ptr_))));
}

MojoVideoCapturer::~MojoVideoCapturer() = default;

void MojoVideoCapturer::SetDisconnectHandler(base::OnceClosure handler) {
  capturer_control_.set_disconnect_handler(std::move(handler));
}

mojom::CreateVideoCapturerResultPtr MojoVideoCapturer::Start() {
  video_capturer_->Start(this);
  return mojom::CreateVideoCapturerResult::New(
      capturer_control_.BindNewPipeAndPassRemote(),
      event_handler_.BindNewPipeAndPassReceiver());
}

void MojoVideoCapturer::CaptureFrame() {
  video_capturer_->CaptureFrame();
}

void MojoVideoCapturer::SetComposeEnabled(bool enabled) {
  video_capturer_->SetComposeEnabled(enabled);
}

void MojoVideoCapturer::SetMouseCursor(
    std::unique_ptr<webrtc::MouseCursor> mouse_cursor) {
  video_capturer_->SetMouseCursor(std::move(mouse_cursor));
}

void MojoVideoCapturer::SetMouseCursorPosition(
    const webrtc::DesktopVector& position) {
  video_capturer_->SetMouseCursorPosition(position);
}

void MojoVideoCapturer::OnCaptureResult(
    webrtc::DesktopCapturer::Result result,
    std::unique_ptr<webrtc::DesktopFrame> frame) {
  mojom::CaptureResultPtr capture_result;
  if (frame) {
    DCHECK_EQ(result, webrtc::DesktopCapturer::Result::SUCCESS);
    std::vector<webrtc::DesktopRect> dirty_region;
    for (webrtc::DesktopRegion::Iterator i(frame->updated_region());
         !i.IsAtEnd(); i.Advance()) {
      dirty_region.push_back(i.rect());
    }
    capture_result =
        mojom::CaptureResult::NewDesktopFrame(mojom::DesktopFrame::New(
            frame->shared_memory()->id(), frame->stride(), frame->size(),
            std::move(dirty_region), frame->capture_time_ms(), frame->dpi(),
            frame->capturer_id()));
  } else {
    DCHECK_NE(result, webrtc::DesktopCapturer::Result::SUCCESS);
    capture_result = mojom::CaptureResult::NewCaptureError(result);
  }

  last_frame_ = std::move(frame);

  if (event_handler_) {
    event_handler_->OnCaptureResult(std::move(capture_result));
  }
}

void MojoVideoCapturer::OnSharedMemoryRegionCreated(
    int id,
    base::ReadOnlySharedMemoryRegion region,
    uint32_t size) {
  if (event_handler_) {
    event_handler_->OnSharedMemoryRegionCreated(id, std::move(region), size);
  }
}

void MojoVideoCapturer::OnSharedMemoryRegionReleased(int id) {
  if (event_handler_) {
    event_handler_->OnSharedMemoryRegionReleased(id);
  }
}

}  // namespace remoting