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 #include "host/commands/run_cvd/launch/webrtc_recorder.h"
17
18 #include <android-base/logging.h>
19 #include <fruit/fruit.h>
20
21 #include "common/libs/fs/shared_fd.h"
22 #include "common/libs/utils/result.h"
23 #include "common/libs/utils/result.h"
24 #include "host/commands/run_cvd/launch/launch.h"
25
26 namespace cuttlefish {
27
ResultSetup()28 Result<void> WebRtcRecorder::ResultSetup() {
29 LOG(DEBUG) << "Initializing the WebRTC recording sockets.";
30 CF_EXPECT(SharedFD::SocketPair(AF_LOCAL, SOCK_STREAM, 0, &client_socket_,
31 &host_socket_),
32 client_socket_->StrError());
33 struct timeval timeout;
34 timeout.tv_sec = 3;
35 timeout.tv_usec = 0;
36 CHECK(host_socket_->SetSockOpt(SOL_SOCKET, SO_RCVTIMEO, &timeout,
37 sizeof(timeout)) == 0)
38 << "Could not set receive timeout";
39 return {};
40 }
41
GetClientSocket() const42 SharedFD WebRtcRecorder::GetClientSocket() const { return client_socket_;}
43
SendStartRecordingCommand() const44 Result<void> WebRtcRecorder::SendStartRecordingCommand() const {
45 CF_EXPECT(SendCommandAndVerifyResponse("T"));
46 return {};
47 }
48
SendStopRecordingCommand() const49 Result<void> WebRtcRecorder::SendStopRecordingCommand() const {
50 CF_EXPECT(SendCommandAndVerifyResponse("C"));
51 return {};
52 }
53
SendCommandAndVerifyResponse(std::string message) const54 Result<void> WebRtcRecorder::SendCommandAndVerifyResponse(std::string message) const {
55 CF_EXPECTF(WriteAll(host_socket_, message) == message.size(),
56 "Failed to send message: '{}'", message);
57 char response[1];
58 int read_ret = host_socket_->Read(response, sizeof(response));
59 CF_EXPECT_NE(read_ret, 0,
60 "Failed to read response from the recording manager.");
61 CF_EXPECT_EQ(response[0], 'Y',
62 "Did not receive expected success response from the recording "
63 "manager.");
64 return {};
65 }
66
WebRtcRecorderComponent()67 fruit::Component<WebRtcRecorder> WebRtcRecorderComponent() {
68 return fruit::createComponent().addMultibinding<SetupFeature, WebRtcRecorder>();
69 }
70
71 } // namespace cuttlefish