1 /*
2 * Copyright (C) 2023 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
17 #include <cstdint>
18 #include <cstdlib>
19
20 #include <android-base/logging.h>
21 #include <gflags/gflags.h>
22
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/utils/result.h"
25 #include "host/libs/command_util/runner/defs.h"
26 #include "host/libs/command_util/util.h"
27 #include "host/libs/config/cuttlefish_config.h"
28 #include "run_cvd.pb.h"
29
30 DEFINE_int32(instance_num, cuttlefish::GetInstance(),
31 "Which instance to screen record.");
32
33 DEFINE_int32(
34 wait_for_launcher, 30,
35 "How many seconds to wait for the launcher to respond to the status "
36 "command. A value of zero means wait indefinitely.");
37
38 namespace cuttlefish {
39 namespace {
40
RecordCvdMain(int argc,char * argv[])41 Result<void> RecordCvdMain(int argc, char* argv[]) {
42 CF_EXPECT_EQ(argc, 2, "Expected exactly one argument with record_cvd.");
43
44 std::string command = argv[1];
45 CF_EXPECT(command == "start" || command == "stop",
46 "Expected the argument to be either start or start.");
47
48 const CuttlefishConfig* config =
49 CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
50 SharedFD monitor_socket = CF_EXPECT(
51 GetLauncherMonitor(*config, FLAGS_instance_num, FLAGS_wait_for_launcher));
52
53 bool is_start = command == "start";
54 run_cvd::ExtendedLauncherAction extended_action;
55 if (is_start) {
56 extended_action.mutable_start_screen_recording();
57 } else {
58 extended_action.mutable_stop_screen_recording();
59 }
60 CF_EXPECT(RunLauncherAction(monitor_socket, extended_action, std::nullopt));
61 LOG(INFO) << "record_cvd " << command << " was successful.";
62 return {};
63 }
64
65 } // namespace
66 } // namespace cuttlefish
67
main(int argc,char * argv[])68 int main(int argc, char* argv[]) {
69 ::android::base::InitLogging(argv, android::base::StderrLogger);
70 google::ParseCommandLineFlags(&argc, &argv, true);
71
72 cuttlefish::Result<void> result = cuttlefish::RecordCvdMain(argc, argv);
73 if (!result.ok()) {
74 LOG(DEBUG) << result.error().FormatForEnv();
75 return EXIT_FAILURE;
76 }
77 return EXIT_SUCCESS;
78 }
79