File: media_effects_service_factory.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 (162 lines) | stat: -rw-r--r-- 6,167 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
// 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 "chrome/browser/media_effects/media_effects_service_factory.h"

#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "media/base/media_switches.h"
#include "services/video_effects/public/cpp/buildflags.h"

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
#include "components/media_effects/media_effects_model_provider.h"
#endif

// static
MediaEffectsService* MediaEffectsServiceFactory::GetForBrowserContext(
    content::BrowserContext* browser_context) {
  return static_cast<MediaEffectsService*>(
      GetInstance()->GetServiceForBrowserContext(browser_context, true));
}

// static
MediaEffectsServiceFactory* MediaEffectsServiceFactory::GetInstance() {
  static base::NoDestructor<MediaEffectsServiceFactory> instance;

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
  // Media Effects Service depends on Optimization Guide because it needs to
  // subscribe to the model updates.
  instance->DependsOn(OptimizationGuideKeyedServiceFactory::GetInstance());
#endif

  return instance.get();
}

MediaEffectsServiceFactory::MediaEffectsServiceFactory()
    : BrowserContextKeyedServiceFactory(
          "MediaEffectsServiceFactory",
          BrowserContextDependencyManager::GetInstance()) {}

MediaEffectsServiceFactory::~MediaEffectsServiceFactory() = default;

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
class SegmentationModelObserver
    : public optimization_guide::OptimizationTargetModelObserver,
      public MediaEffectsModelProvider {
 public:
  explicit SegmentationModelObserver(content::BrowserContext* browser_context) {
    // It's safe to store the Optimization Guide service by raw pointer - the
    // `SegmentationModelObserver` will be owned by `MediaEffectsService`, and
    // we add a dependency between `OptimizationGuideKeyedServiceFactory` &
    // `MediaEffectsServiceFactory`, which means that Media Effects Service (and
    // by extension this observer) will be destroyed before Optimization Guide.
    optimization_guide_ = OptimizationGuideKeyedServiceFactory::GetForProfile(
        Profile::FromBrowserContext(browser_context));
    if (!optimization_guide_) {
      // Optimization Guide can be disabled by a feature, so we should tolerate
      // it being null.
      return;
    }

    optimization_guide_->AddObserverForOptimizationTargetModel(
        optimization_guide::proto::OptimizationTarget::
            OPTIMIZATION_TARGET_CAMERA_BACKGROUND_SEGMENTATION,
        std::nullopt, this);
  }

  ~SegmentationModelObserver() override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    if (!optimization_guide_) {
      return;
    }

    optimization_guide_->RemoveObserverForOptimizationTargetModel(
        optimization_guide::proto::OptimizationTarget::
            OPTIMIZATION_TARGET_CAMERA_BACKGROUND_SEGMENTATION,
        this);
  }

  SegmentationModelObserver(const SegmentationModelObserver& other) = delete;
  SegmentationModelObserver& operator=(const SegmentationModelObserver& other) =
      delete;

  SegmentationModelObserver(SegmentationModelObserver&& other) = delete;
  SegmentationModelObserver& operator=(SegmentationModelObserver&& other) =
      delete;

  // optimization_guide::OptimizationTargetModelObserver:
  void OnModelUpdated(
      optimization_guide::proto::OptimizationTarget optimization_target,
      base::optional_ref<const optimization_guide::ModelInfo> model_info)
      override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    // We only subscribe to notifications for
    // `OPTIMIZATION_TARGET_CAMERA_BACKGROUND_SEGMENTATION`, so we know that the
    // updated model info pertains to background segmentation models:
    CHECK_EQ(optimization_target,
             optimization_guide::proto::OptimizationTarget::
                 OPTIMIZATION_TARGET_CAMERA_BACKGROUND_SEGMENTATION);

    background_segmentation_model_path_ =
        model_info.has_value() ? std::optional(model_info->GetModelFilePath())
                               : std::nullopt;

    for (auto& observer : observers_) {
      observer.OnBackgroundSegmentationModelUpdated(
          background_segmentation_model_path_);
    }
  }

  // MediaEffectsModelProvider:
  void AddObserver(MediaEffectsModelProvider::Observer* observer) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    // If we already have a model path, let's inform the new observer about it.
    if (background_segmentation_model_path_) {
      observer->OnBackgroundSegmentationModelUpdated(
          *background_segmentation_model_path_);
    }

    observers_.AddObserver(observer);
  }

  void RemoveObserver(MediaEffectsModelProvider::Observer* observer) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    observers_.RemoveObserver(observer);
  }

 private:
  raw_ptr<OptimizationGuideKeyedService> optimization_guide_;
  base::ObserverList<MediaEffectsModelProvider::Observer> observers_;
  std::optional<base::FilePath> background_segmentation_model_path_;

  SEQUENCE_CHECKER(sequence_checker_);
};
#endif

std::unique_ptr<KeyedService>
MediaEffectsServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* browser_context) const {
  CHECK(browser_context);

#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
  std::unique_ptr<SegmentationModelObserver> model_provider;
  if (base::FeatureList::IsEnabled(media::kCameraMicEffects)) {
    model_provider =
        std::make_unique<SegmentationModelObserver>(browser_context);
  }
  return std::make_unique<MediaEffectsService>(
      user_prefs::UserPrefs::Get(browser_context), std::move(model_provider));
#else
  return std::make_unique<MediaEffectsService>(
      user_prefs::UserPrefs::Get(browser_context));
#endif
}