• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <webrtc/ServerState.h>
18 
19 #include <webrtc/OpusPacketizer.h>
20 #include <webrtc/VP8Packetizer.h>
21 
22 #include <source/AudioSource.h>
23 #include <source/TouchSink.h>
24 #include <source/FrameBufferSource.h>
25 
26 #include "host/libs/config/cuttlefish_config.h"
27 
28 #include <gflags/gflags.h>
29 
30 DECLARE_int32(keyboard_fd);
31 DECLARE_int32(touch_fd);
32 DECLARE_int32(frame_server_fd);
33 DECLARE_bool(write_virtio_input);
34 
ServerState(std::shared_ptr<RunLoop> runLoop,VideoFormat videoFormat)35 ServerState::ServerState(
36         std::shared_ptr<RunLoop> runLoop, VideoFormat videoFormat)
37     :
38       mRunLoop(runLoop),
39       mVideoFormat(videoFormat) {
40 
41     auto config = vsoc::CuttlefishConfig::Get();
42 
43     android::FrameBufferSource::Format fbSourceFormat;
44     switch (videoFormat) {
45         case VideoFormat::VP8:
46             fbSourceFormat = android::FrameBufferSource::Format::VP8;
47             break;
48         default:
49             LOG(FATAL) << "Should not be here.";
50     }
51 
52     mFrameBufferSource =
53         std::make_shared<android::FrameBufferSource>(fbSourceFormat);
54 
55     int32_t screenParams[4];
56     screenParams[0] = config->x_res();
57     screenParams[1] = config->y_res();
58     screenParams[2] = config->dpi();
59     screenParams[3] = config->refresh_rate_hz();
60 
61     static_cast<android::FrameBufferSource *>(
62             mFrameBufferSource.get())->setScreenParams(screenParams);
63 
64     mScreenConnector = std::shared_ptr<cvd::ScreenConnector>(
65         cvd::ScreenConnector::Get(FLAGS_frame_server_fd));
66     mScreenConnectorMonitor.reset(
67         new std::thread([this]() { MonitorScreenConnector(); }));
68 
69     mAudioSource = std::make_shared<android::AudioSource>(
70             android::AudioSource::Format::OPUS);
71 
72     CHECK_GE(FLAGS_touch_fd, 0);
73 
74     auto touchSink = std::make_shared<android::TouchSink>(
75         mRunLoop, FLAGS_touch_fd, FLAGS_write_virtio_input);
76 
77     touchSink->start();
78 
79     mTouchSink = touchSink;
80 
81     auto keyboardSink = std::make_shared<android::KeyboardSink>(
82         mRunLoop, FLAGS_keyboard_fd, FLAGS_write_virtio_input);
83 
84     keyboardSink->start();
85 
86     mKeyboardSink = keyboardSink;
87 }
88 
MonitorScreenConnector()89 void ServerState::MonitorScreenConnector() {
90     std::uint32_t last_frame = 0;
91     while (true) {
92       mScreenConnector->OnFrameAfter(last_frame, [this, &last_frame](
93                                                      std::uint32_t frame_num,
94                                                      std::uint8_t *data) {
95         mRunLoop->postAndAwait([this, data]() {
96           static_cast<android::FrameBufferSource *>(mFrameBufferSource.get())
97               ->injectFrame(data, cvd::ScreenConnector::ScreenSizeInBytes());
98         });
99         last_frame = frame_num;
100       });
101     }
102 }
103 
getVideoPacketizer()104 std::shared_ptr<Packetizer> ServerState::getVideoPacketizer() {
105     auto packetizer = mVideoPacketizer.lock();
106     if (!packetizer) {
107         switch (mVideoFormat) {
108             case VideoFormat::VP8:
109             {
110                 packetizer = std::make_shared<VP8Packetizer>(
111                         mRunLoop, mFrameBufferSource);
112                 break;
113             }
114 
115             default:
116                 LOG(FATAL) << "Should not be here.";
117         }
118 
119         packetizer->run();
120 
121         mVideoPacketizer = packetizer;
122     }
123 
124     return packetizer;
125 }
126 
getAudioPacketizer()127 std::shared_ptr<Packetizer> ServerState::getAudioPacketizer() {
128     auto packetizer = mAudioPacketizer.lock();
129     if (!packetizer) {
130         packetizer = std::make_shared<OpusPacketizer>(mRunLoop, mAudioSource);
131         packetizer->run();
132 
133         mAudioPacketizer = packetizer;
134     }
135 
136     return packetizer;
137 }
138 
acquireHandlerId()139 size_t ServerState::acquireHandlerId() {
140     size_t id = 0;
141     while (!mAllocatedHandlerIds.insert(id).second) {
142         ++id;
143     }
144 
145     return id;
146 }
147 
releaseHandlerId(size_t id)148 void ServerState::releaseHandlerId(size_t id) {
149     CHECK_EQ(mAllocatedHandlerIds.erase(id), 1);
150 }
151 
getTouchSink()152 std::shared_ptr<android::TouchSink> ServerState::getTouchSink() {
153     return mTouchSink;
154 }
155 
getKeyboardSink()156 std::shared_ptr<android::KeyboardSink> ServerState::getKeyboardSink() {
157     return mKeyboardSink;
158 }
159