1 /* 2 * Copyright (C) 2021 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 #pragma once 17 #include <android/hardware/graphics/mapper/2.0/IMapper.h> 18 #include <atomic> 19 #include <mutex> 20 #include <thread> 21 #include <vector> 22 #include "utils/Timers.h" 23 #include "vsock_connection.h" 24 25 namespace cuttlefish { 26 27 using ::android::hardware::graphics::mapper::V2_0::YCbCrLayout; 28 29 // VsockFrameProvider reads data from vsock 30 // Users can get the data by using copyYUVFrame/copyJpegData methods 31 class VsockFrameProvider { 32 public: 33 VsockFrameProvider() = default; 34 ~VsockFrameProvider(); 35 36 VsockFrameProvider(const VsockFrameProvider&) = delete; 37 VsockFrameProvider& operator=(const VsockFrameProvider&) = delete; 38 39 void start(std::shared_ptr<cuttlefish::VsockConnection> connection, 40 uint32_t expected_width, uint32_t expected_height); 41 void stop(); 42 void requestJpeg(); 43 void cancelJpegRequest(); jpegPending()44 bool jpegPending() const { return jpeg_pending_.load(); } isRunning()45 bool isRunning() const { return running_.load(); } 46 bool waitYUVFrame(unsigned int max_wait_ms); 47 bool copyYUVFrame(uint32_t width, uint32_t height, YCbCrLayout dst); 48 bool copyJpegData(uint32_t size, void* dst); 49 50 private: 51 bool isBlob(const std::vector<char>& blob); 52 bool framesizeMatches(uint32_t width, uint32_t height, 53 const std::vector<char>& data); 54 void VsockReadLoop(uint32_t expected_width, uint32_t expected_height); 55 std::thread reader_thread_; 56 std::mutex frame_mutex_; 57 std::mutex jpeg_mutex_; 58 std::atomic<nsecs_t> timestamp_; 59 std::atomic<bool> running_; 60 std::atomic<bool> jpeg_pending_; 61 std::vector<char> frame_; 62 std::vector<char> next_frame_; 63 std::vector<char> cached_jpeg_; 64 std::condition_variable yuv_frame_updated_; 65 std::shared_ptr<cuttlefish::VsockConnection> connection_; 66 }; 67 68 } // namespace cuttlefish 69