|
| 1 | +// |
| 2 | +// Copyright (C) 2026 The Android Open Source Project |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#include "cuttlefish/host/commands/run_cvd/launch/vhost_user_media_devices.h" |
| 17 | + |
| 18 | +#include <fcntl.h> |
| 19 | + |
| 20 | +#include <string> |
| 21 | +#include <unordered_set> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include <fruit/component.h> |
| 26 | +#include <fruit/fruit_forward_decls.h> |
| 27 | +#include <fruit/macro.h> |
| 28 | + |
| 29 | +#include "cuttlefish/common/libs/fs/shared_fd.h" |
| 30 | +#include "cuttlefish/common/libs/utils/subprocess.h" |
| 31 | +#include "cuttlefish/host/commands/run_cvd/launch/log_tee_creator.h" |
| 32 | +#include "cuttlefish/host/libs/config/cuttlefish_config.h" |
| 33 | +#include "cuttlefish/host/libs/config/known_paths.h" |
| 34 | +#include "cuttlefish/host/libs/feature/command_source.h" |
| 35 | +#include "cuttlefish/host/libs/feature/feature.h" |
| 36 | +#include "cuttlefish/result/result.h" |
| 37 | + |
| 38 | +namespace cuttlefish { |
| 39 | +namespace { |
| 40 | + |
| 41 | +using Subprocess::StdIOChannel::kStdErr; |
| 42 | + |
| 43 | +Command NewCommand(const std::string& socket_path) { |
| 44 | + Command cmd(VhostUserMediaSimpleDeviceBinary()); |
| 45 | + cmd.AddParameter("--socket-path=", socket_path); |
| 46 | + cmd.AddParameter("--verbosity=", "debug"); |
| 47 | + cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, |
| 48 | + SharedFD::Open("/dev/null", O_WRONLY)); |
| 49 | + return cmd; |
| 50 | +} |
| 51 | + |
| 52 | +class VhostUserMediaDevices : public CommandSource { |
| 53 | + public: |
| 54 | + INJECT(VhostUserMediaDevices(const CuttlefishConfig::InstanceSpecific& instance, LogTeeCreator& log_tee)) : instance_(instance), log_tee_(log_tee) {} |
| 55 | + |
| 56 | + // CommandSource |
| 57 | + Result<std::vector<MonitorCommand>> Commands() override { |
| 58 | + std::vector<MonitorCommand> commands; |
| 59 | + for (int index = 0; index < instance_.media_configs().size(); index++) { |
| 60 | + Command cmd = NewCommand(instance_.media_socket_path(index)); |
| 61 | + Command cmd_log_tee = CF_EXPECT( |
| 62 | + log_tee_.CreateLogTee(cmd, "vhu_media_simple_device", kStdErr), "Failed to create log tee command for media device"); |
| 63 | + commands.emplace_back(std::move(cmd)); |
| 64 | + commands.emplace_back(std::move(cmd_log_tee)); |
| 65 | + } |
| 66 | + return commands; |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + // SetupFeature |
| 71 | + std::string Name() const override { return "VhostUserMediaDevices"; } |
| 72 | + std::unordered_set<SetupFeature*> Dependencies() const override { return {}; } |
| 73 | + Result<void> ResultSetup() override { return {}; } |
| 74 | + |
| 75 | + const CuttlefishConfig::InstanceSpecific& instance_; |
| 76 | + LogTeeCreator& log_tee_; |
| 77 | +}; |
| 78 | + |
| 79 | +} // namespace |
| 80 | + |
| 81 | +fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific, LogTeeCreator>> |
| 82 | +VhostUserMediaDevicesComponent() { |
| 83 | + return fruit::createComponent() |
| 84 | + .addMultibinding<CommandSource, VhostUserMediaDevices>(); |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace cuttlefish |
| 88 | + |
0 commit comments