blob: 8f5fbbb0ae7455c510e4966727ad186b0520f383 [file] [log] [blame]
Jorge E. Moreira4750e542020-01-07 14:35:35 -08001/*
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
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080017#include <webrtc/ServerState.h>
18
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080019#include <webrtc/OpusPacketizer.h>
20#include <webrtc/VP8Packetizer.h>
21
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080022#include <source/AudioSource.h>
Jorge E. Moreirae049b792019-12-18 18:17:48 -080023#include <source/TouchSink.h>
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080024#include <source/FrameBufferSource.h>
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080025
26#include "host/libs/config/cuttlefish_config.h"
27
28#include <gflags/gflags.h>
29
30DECLARE_int32(touch_fd);
Jorge E. Moreirae049b792019-12-18 18:17:48 -080031DECLARE_int32(frame_server_fd);
32DECLARE_bool(write_virtio_input);
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080033
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080034ServerState::ServerState(
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080035 std::shared_ptr<RunLoop> runLoop, VideoFormat videoFormat)
36 :
Jorge E. Moreiraf0814412019-11-25 16:17:49 -080037 mRunLoop(runLoop),
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080038 mVideoFormat(videoFormat) {
39
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080040 auto config = vsoc::CuttlefishConfig::Get();
41
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080042 android::FrameBufferSource::Format fbSourceFormat;
43 switch (videoFormat) {
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080044 case VideoFormat::VP8:
45 fbSourceFormat = android::FrameBufferSource::Format::VP8;
46 break;
47 default:
Jorge E. Moreira2a6ab082019-12-11 18:41:58 -080048 LOG(FATAL) << "Should not be here.";
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080049 }
50
51 mFrameBufferSource =
52 std::make_shared<android::FrameBufferSource>(fbSourceFormat);
53
Jorge E. Moreirae049b792019-12-18 18:17:48 -080054 int32_t screenParams[4];
55 screenParams[0] = config->x_res();
56 screenParams[1] = config->y_res();
57 screenParams[2] = config->dpi();
58 screenParams[3] = config->refresh_rate_hz();
59
Jorge E. Moreirae049b792019-12-18 18:17:48 -080060 static_cast<android::FrameBufferSource *>(
61 mFrameBufferSource.get())->setScreenParams(screenParams);
62
Jorge E. Moreira373640c2020-01-07 14:13:20 -080063 mScreenConnector = std::shared_ptr<cvd::ScreenConnector>(
64 cvd::ScreenConnector::Get(FLAGS_frame_server_fd));
65 mScreenConnectorMonitor.reset(
66 new std::thread([this]() { MonitorScreenConnector(); }));
67
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080068 mAudioSource = std::make_shared<android::AudioSource>(
69 android::AudioSource::Format::OPUS);
70
Jorge E. Moreirae049b792019-12-18 18:17:48 -080071 mAudioComms = std::make_shared<HostToGuestComms>(
72 mRunLoop,
73 true /* isServer */,
74 VMADDR_CID_HOST,
75 HostToGuestComms::kPortAudio,
76 [this](const void *data, size_t size) {
77 LOG(VERBOSE)
78 << "Received packet of "
79 << size
80 << " bytes of data from audio HAL.";
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080081
Jorge E. Moreirae049b792019-12-18 18:17:48 -080082 static_cast<android::AudioSource *>(
83 mAudioSource.get())->inject(data, size);
84 });
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080085
Jorge E. Moreirae049b792019-12-18 18:17:48 -080086 mAudioComms->start();
87
88 CHECK_GE(FLAGS_touch_fd, 0);
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080089
Jorge E. Moreirae049b792019-12-18 18:17:48 -080090 auto touchSink =
91 std::make_shared<android::TouchSink>(mRunLoop, FLAGS_touch_fd, FLAGS_write_virtio_input);
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080092
Jorge E. Moreirae049b792019-12-18 18:17:48 -080093 touchSink->start();
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080094
Jorge E. Moreirae049b792019-12-18 18:17:48 -080095 mTouchSink = touchSink;
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080096}
97
Jorge E. Moreira373640c2020-01-07 14:13:20 -080098void ServerState::MonitorScreenConnector() {
99 std::uint32_t last_frame = 0;
100 while (true) {
101 mScreenConnector->OnFrameAfter(last_frame, [this, &last_frame](
102 std::uint32_t frame_num,
103 std::uint8_t *data) {
104 mRunLoop->postAndAwait([this, data]() {
105 static_cast<android::FrameBufferSource *>(mFrameBufferSource.get())
106 ->injectFrame(data, cvd::ScreenConnector::ScreenSizeInBytes());
107 });
108 last_frame = frame_num;
109 });
110 }
111}
112
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -0800113std::shared_ptr<Packetizer> ServerState::getVideoPacketizer() {
114 auto packetizer = mVideoPacketizer.lock();
115 if (!packetizer) {
116 switch (mVideoFormat) {
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -0800117 case VideoFormat::VP8:
118 {
119 packetizer = std::make_shared<VP8Packetizer>(
120 mRunLoop, mFrameBufferSource);
121 break;
122 }
123
124 default:
Jorge E. Moreira2a6ab082019-12-11 18:41:58 -0800125 LOG(FATAL) << "Should not be here.";
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -0800126 }
127
128 packetizer->run();
129
130 mVideoPacketizer = packetizer;
131 }
132
133 return packetizer;
134}
135
136std::shared_ptr<Packetizer> ServerState::getAudioPacketizer() {
137 auto packetizer = mAudioPacketizer.lock();
138 if (!packetizer) {
139 packetizer = std::make_shared<OpusPacketizer>(mRunLoop, mAudioSource);
140 packetizer->run();
141
142 mAudioPacketizer = packetizer;
143 }
144
145 return packetizer;
146}
147
148size_t ServerState::acquireHandlerId() {
149 size_t id = 0;
150 while (!mAllocatedHandlerIds.insert(id).second) {
151 ++id;
152 }
153
154 return id;
155}
156
157void ServerState::releaseHandlerId(size_t id) {
158 CHECK_EQ(mAllocatedHandlerIds.erase(id), 1);
159}
160
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -0800161std::shared_ptr<android::StreamingSink> ServerState::getTouchSink() {
162 return mTouchSink;
163}