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
|
// 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.
#ifndef COMPONENTS_MEDIA_EFFECTS_MEDIA_EFFECTS_SERVICE_H_
#define COMPONENTS_MEDIA_EFFECTS_MEDIA_EFFECTS_SERVICE_H_
#include <memory>
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/media_effects/video_effects_manager_impl.h"
#include "content/public/browser/browser_context.h"
#include "media/capture/mojom/video_effects_manager.mojom-forward.h"
#include "services/video_effects/public/cpp/buildflags.h"
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
#include "components/media_effects/media_effects_model_provider.h"
#include "components/viz/host/gpu_client.h"
#include "services/video_effects/public/mojom/video_effects_processor.mojom.h"
#include "services/video_effects/public/mojom/video_effects_service.mojom-forward.h"
#endif
class MediaEffectsService : public KeyedService
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
,
public MediaEffectsModelProvider::Observer
#endif
{
public:
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
// `model_provider` may be null in case the video effects are not enabled.
explicit MediaEffectsService(
PrefService* prefs,
std::unique_ptr<MediaEffectsModelProvider> model_provider);
#endif
explicit MediaEffectsService(PrefService* prefs);
MediaEffectsService(const MediaEffectsService&) = delete;
MediaEffectsService& operator=(const MediaEffectsService&) = delete;
MediaEffectsService(MediaEffectsService&&) = delete;
MediaEffectsService& operator=(MediaEffectsService&&) = delete;
~MediaEffectsService() override;
// Connects a `VideoEffectsManagerImpl` to the provided
// `effects_manager_receiver`. If the keyd profile already has a manager for
// the passed `device_id`, then it will be used. Otherwise, a new manager will
// be created.
//
// The device id must be the raw string from
// `media::mojom::VideoCaptureDeviceDescriptor::device_id`.
//
// Note that this API only allows interacting with the manager via mojo in
// order to support communication with the VideoCaptureService in a different
// process.
void BindReadonlyVideoEffectsManager(
const std::string& device_id,
mojo::PendingReceiver<media::mojom::ReadonlyVideoEffectsManager>
effects_manager_receiver);
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
// Connects a `VideoEffectsManagerImpl` to the provided
// `effects_processor_receiver`. If the keyed profile already has a manager
// for the passed `device_id`, then it will be used. Otherwise, a new manager
// will be created.
//
// The device id must be the raw string from
// `media::mojom::VideoCaptureDeviceDescriptor::device_id`.
//
// The manager remote will be sent to the Video Effects Service, where
// it will be used to subscribe to the effects configuration. The passed in
// pending receiver is going to be used to create a Video Effects Processor
// in the Video Effects Service.
//
// Note that this API does not expose the `VideoEffectsManagerImpl` in any
// way. If you need to interact with the manager, call
// `BindReadonlyVideoEffectsManager()` instead.
//
// Calling this method will launch a new instance of Video Effects Service if
// it's not already running.
void BindVideoEffectsProcessor(
const std::string& device_id,
mojo::PendingReceiver<video_effects::mojom::VideoEffectsProcessor>
effects_processor_receiver);
// MediaEffectsModelProvider::Observer:
void OnBackgroundSegmentationModelUpdated(
base::optional_ref<const base::FilePath> path) override;
#endif
VideoEffectsManagerImpl& GetOrCreateVideoEffectsManager(
const std::string& device_id);
private:
void OnLastReceiverDisconnected(const std::string& device_id);
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
// Invoked when the background segmentation model file has been opened.
void OnBackgroundSegmentationModelOpened(base::File model_file);
#endif
SEQUENCE_CHECKER(sequence_checker_);
raw_ptr<PrefService> prefs_;
#if BUILDFLAG(ENABLE_VIDEO_EFFECTS)
// May be null if the video effects are not enabled:
std::unique_ptr<MediaEffectsModelProvider> model_provider_;
// The model file that was most recently opened. The file may be invalid
// (`.IsValid() == false`) if we have not opened any model file yet.
base::File latest_segmentation_model_file_;
std::unique_ptr<viz::GpuClient, base::OnTaskRunnerDeleter> gpu_client_ = {
nullptr, base::OnTaskRunnerDeleter(nullptr)};
#endif
// Device ID strings mapped to effects manager instances.
base::flat_map<std::string, std::unique_ptr<VideoEffectsManagerImpl>>
video_effects_managers_;
// Must be last:
base::WeakPtrFactory<MediaEffectsService> weak_factory_{this};
};
#endif // COMPONENTS_MEDIA_EFFECTS_MEDIA_EFFECTS_SERVICE_H_
|