Jorge E. Moreira | 4750e54 | 2020-01-07 14:35:35 -0800 | [diff] [blame] | 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 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 17 | #include <webrtc/ServerState.h> |
| 18 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 19 | #include <webrtc/OpusPacketizer.h> |
| 20 | #include <webrtc/VP8Packetizer.h> |
| 21 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 22 | #include <source/AudioSource.h> |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 23 | #include <source/TouchSink.h> |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 24 | #include <source/FrameBufferSource.h> |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 25 | |
| 26 | #include "host/libs/config/cuttlefish_config.h" |
| 27 | |
| 28 | #include <gflags/gflags.h> |
| 29 | |
| 30 | DECLARE_int32(touch_fd); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 31 | DECLARE_int32(frame_server_fd); |
| 32 | DECLARE_bool(write_virtio_input); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 33 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 34 | ServerState::ServerState( |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 35 | std::shared_ptr<RunLoop> runLoop, VideoFormat videoFormat) |
| 36 | : |
Jorge E. Moreira | f081441 | 2019-11-25 16:17:49 -0800 | [diff] [blame] | 37 | mRunLoop(runLoop), |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 38 | mVideoFormat(videoFormat) { |
| 39 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 40 | auto config = vsoc::CuttlefishConfig::Get(); |
| 41 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 42 | android::FrameBufferSource::Format fbSourceFormat; |
| 43 | switch (videoFormat) { |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 44 | case VideoFormat::VP8: |
| 45 | fbSourceFormat = android::FrameBufferSource::Format::VP8; |
| 46 | break; |
| 47 | default: |
Jorge E. Moreira | 2a6ab08 | 2019-12-11 18:41:58 -0800 | [diff] [blame] | 48 | LOG(FATAL) << "Should not be here."; |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | mFrameBufferSource = |
| 52 | std::make_shared<android::FrameBufferSource>(fbSourceFormat); |
| 53 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 54 | 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. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 60 | static_cast<android::FrameBufferSource *>( |
| 61 | mFrameBufferSource.get())->setScreenParams(screenParams); |
| 62 | |
Jorge E. Moreira | 373640c | 2020-01-07 14:13:20 -0800 | [diff] [blame] | 63 | 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 68 | mAudioSource = std::make_shared<android::AudioSource>( |
| 69 | android::AudioSource::Format::OPUS); |
| 70 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 71 | 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 81 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 82 | static_cast<android::AudioSource *>( |
| 83 | mAudioSource.get())->inject(data, size); |
| 84 | }); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 85 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 86 | mAudioComms->start(); |
| 87 | |
| 88 | CHECK_GE(FLAGS_touch_fd, 0); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 89 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 90 | auto touchSink = |
| 91 | std::make_shared<android::TouchSink>(mRunLoop, FLAGS_touch_fd, FLAGS_write_virtio_input); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 92 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 93 | touchSink->start(); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 94 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 95 | mTouchSink = touchSink; |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Jorge E. Moreira | 373640c | 2020-01-07 14:13:20 -0800 | [diff] [blame] | 98 | void 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 113 | std::shared_ptr<Packetizer> ServerState::getVideoPacketizer() { |
| 114 | auto packetizer = mVideoPacketizer.lock(); |
| 115 | if (!packetizer) { |
| 116 | switch (mVideoFormat) { |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 117 | case VideoFormat::VP8: |
| 118 | { |
| 119 | packetizer = std::make_shared<VP8Packetizer>( |
| 120 | mRunLoop, mFrameBufferSource); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | default: |
Jorge E. Moreira | 2a6ab08 | 2019-12-11 18:41:58 -0800 | [diff] [blame] | 125 | LOG(FATAL) << "Should not be here."; |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | packetizer->run(); |
| 129 | |
| 130 | mVideoPacketizer = packetizer; |
| 131 | } |
| 132 | |
| 133 | return packetizer; |
| 134 | } |
| 135 | |
| 136 | std::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 | |
| 148 | size_t ServerState::acquireHandlerId() { |
| 149 | size_t id = 0; |
| 150 | while (!mAllocatedHandlerIds.insert(id).second) { |
| 151 | ++id; |
| 152 | } |
| 153 | |
| 154 | return id; |
| 155 | } |
| 156 | |
| 157 | void ServerState::releaseHandlerId(size_t id) { |
| 158 | CHECK_EQ(mAllocatedHandlerIds.erase(id), 1); |
| 159 | } |
| 160 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 161 | std::shared_ptr<android::StreamingSink> ServerState::getTouchSink() { |
| 162 | return mTouchSink; |
| 163 | } |