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 #pragma once 18 19 #include "host/libs/screen_connector/screen_connector.h" 20 21 #include <atomic> 22 #include <cinttypes> 23 #include <condition_variable> 24 #include <mutex> 25 #include <thread> 26 #include <vector> 27 28 namespace cvd { 29 30 class SocketBasedScreenConnector : public ScreenConnector { 31 public: 32 explicit SocketBasedScreenConnector(int frames_fd); 33 34 bool OnFrameAfter(std::uint32_t frame_number, 35 const FrameCallback& frame_callback) override; 36 37 private: 38 static constexpr int NUM_BUFFERS_ = 4; 39 40 int WaitForNewFrameSince(std::uint32_t* seq_num); 41 void* GetBuffer(int buffer_idx); 42 void ServerLoop(int frames_fd); 43 void BroadcastNewFrame(int buffer_idx); 44 45 std::vector<std::uint8_t> buffer_ = 46 std::vector<std::uint8_t>(NUM_BUFFERS_ * ScreenSizeInBytes()); 47 std::uint32_t seq_num_{0}; 48 int newest_buffer_ = 0; 49 std::condition_variable new_frame_cond_var_; 50 std::mutex new_frame_mtx_; 51 std::thread screen_server_thread_; 52 }; 53 54 } // namespace cvd