File: media_effects_service.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 (190 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/media_effects/media_effects_service.h"

#include <optional>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_thread.h"
#include "media/capture/mojom/video_effects_manager.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/video_effects/public/cpp/buildflags.h"

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
#include "components/media_effects/media_effects_model_provider.h"
#include "content/public/browser/gpu_client.h"
#include "services/video_effects/public/cpp/video_effects_service_host.h"
#include "services/video_effects/public/mojom/video_effects_processor.mojom.h"
#include "services/video_effects/public/mojom/video_effects_service.mojom.h"
#endif

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
MediaEffectsService::MediaEffectsService(
    PrefService* prefs,
    std::unique_ptr<MediaEffectsModelProvider> model_provider)
    : prefs_(prefs),
      model_provider_(std::move(model_provider)),
      gpu_client_(nullptr, base::OnTaskRunnerDeleter(nullptr)) {
  if (model_provider_) {
    model_provider_->AddObserver(this);
  }
}
#endif

MediaEffectsService::MediaEffectsService(PrefService* prefs) : prefs_(prefs) {}

MediaEffectsService::~MediaEffectsService() {
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
  if (model_provider_) {
    model_provider_->RemoveObserver(this);
  }

  if (latest_segmentation_model_file_.IsValid()) {
    // Closing a file is considered blocking, schedule it in a context where
    // blocking is allowed:
    base::ThreadPool::PostTask(FROM_HERE, {base::MayBlock()},
                               base::DoNothingWithBoundArgs(
                                   std::move(latest_segmentation_model_file_)));
  }
#endif
}

void MediaEffectsService::BindReadonlyVideoEffectsManager(
    const std::string& device_id,
    mojo::PendingReceiver<media::mojom::ReadonlyVideoEffectsManager>
        effects_manager_receiver) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto& effects_manager = GetOrCreateVideoEffectsManager(device_id);
  effects_manager.Bind(std::move(effects_manager_receiver));
}

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
void MediaEffectsService::BindVideoEffectsProcessor(
    const std::string& device_id,
    mojo::PendingReceiver<video_effects::mojom::VideoEffectsProcessor>
        effects_processor_receiver) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  mojo::PendingRemote<media::mojom::ReadonlyVideoEffectsManager>
      readonly_video_effects_manager;
  BindReadonlyVideoEffectsManager(
      device_id,
      readonly_video_effects_manager.InitWithNewPipeAndPassReceiver());

  auto* video_effects_service = video_effects::GetVideoEffectsService();
  CHECK(video_effects_service);

  // The `video_effects_service` is reset if it is idle for more than 5 seconds.
  // Re-send the model in case that has happened.
  if (latest_segmentation_model_file_.IsValid()) {
    video_effects_service->SetBackgroundSegmentationModel(
        latest_segmentation_model_file_.Duplicate());
  }

  mojo::PendingRemote<viz::mojom::Gpu> gpu_remote;
  mojo::PendingReceiver<viz::mojom::Gpu> gpu_receiver =
      gpu_remote.InitWithNewPipeAndPassReceiver();

  if (!gpu_client_) {
    gpu_client_ = content::CreateGpuClient(std::move(gpu_receiver));
  } else {
    auto task_runner = content::GetUIThreadTaskRunner({});
    task_runner->PostTask(FROM_HERE, base::BindOnce(&viz::GpuClient::Add,
                                                    gpu_client_->GetWeakPtr(),
                                                    std::move(gpu_receiver)));
  }

  video_effects_service->CreateEffectsProcessor(
      device_id, std::move(gpu_remote),
      std::move(readonly_video_effects_manager),
      std::move(effects_processor_receiver));
}

void MediaEffectsService::OnBackgroundSegmentationModelUpdated(
    base::optional_ref<const base::FilePath> path) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!path.has_value()) {
    // We have not received a valid path, so there is nothing to open.
    // Just pass an invalid `base::File`, it will be handled by lower
    // layers.
    OnBackgroundSegmentationModelOpened(base::File());
    return;
  }

  // We have received new path to the model, let's open it and inform the Video
  // Effects Service about it. Opening a file is considered blocking, schedule
  // it in a context where blocking is allowed:
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(
          [](const base::FilePath& model_path) {
            if (!base::PathExists(model_path)) {
              return base::File();
            }

            base::File model(model_path, base::File::Flags::FLAG_OPEN |
                                             base::File::Flags::FLAG_READ);

            return model;
          },
          *path),
      base::BindOnce(&MediaEffectsService::OnBackgroundSegmentationModelOpened,
                     weak_factory_.GetWeakPtr()));
}

void MediaEffectsService::OnBackgroundSegmentationModelOpened(
    base::File model_file) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  video_effects::GetVideoEffectsService()->SetBackgroundSegmentationModel(
      model_file.Duplicate());

  // Swap newly opened file with the old one and then close the old one:
  std::swap(latest_segmentation_model_file_, model_file);
  // Closing a file is considered blocking, schedule it in a context where
  // blocking is allowed:
  if (model_file.IsValid()) {
    base::ThreadPool::PostTask(
        FROM_HERE, {base::MayBlock()},
        base::DoNothingWithBoundArgs(std::move(model_file)));
  }
}
#endif

VideoEffectsManagerImpl& MediaEffectsService::GetOrCreateVideoEffectsManager(
    const std::string& device_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (auto effects_manager = video_effects_managers_.find(device_id);
      effects_manager != video_effects_managers_.end()) {
    return *effects_manager->second;
  }

  // base::Unretained is safe here because `this` owns the
  // `VideoEffectsManagerImpl` that would call this callback.
  auto [effects_manager, inserted] = video_effects_managers_.emplace(
      device_id,
      std::make_unique<VideoEffectsManagerImpl>(
          prefs_,
          base::BindOnce(&MediaEffectsService::OnLastReceiverDisconnected,
                         base::Unretained(this), device_id)));
  CHECK(inserted);
  return *effects_manager->second;
}

void MediaEffectsService::OnLastReceiverDisconnected(
    const std::string& device_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  video_effects_managers_.erase(device_id);
}