1 /* 2 * Copyright (C) 2020 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 <mutex> 20 #include <memory> 21 22 #include "host/frontend/webrtc/cvd_video_frame_buffer.h" 23 #include "host/frontend/webrtc/lib/video_sink.h" 24 #include "host/libs/screen_connector/screen_connector.h" 25 26 namespace cuttlefish { 27 /** 28 * ScreenConnectorImpl will generate this, and enqueue 29 * 30 * It's basically a (processed) frame, so it: 31 * must be efficiently std::move-able 32 * Also, for the sake of algorithm simplicity: 33 * must be default-constructable & assignable 34 * 35 */ 36 struct WebRtcScProcessedFrame : public ScreenConnectorFrameInfo { 37 // must support move semantic 38 std::unique_ptr<CvdVideoFrameBuffer> buf_; CloneWebRtcScProcessedFrame39 std::unique_ptr<WebRtcScProcessedFrame> Clone() { 40 // copy internal buffer, not move 41 CvdVideoFrameBuffer* new_buffer = new CvdVideoFrameBuffer(*(buf_.get())); 42 auto cloned_frame = std::make_unique<WebRtcScProcessedFrame>(); 43 cloned_frame->buf_ = 44 std::move(std::unique_ptr<CvdVideoFrameBuffer>(new_buffer)); 45 return std::move(cloned_frame); 46 } 47 }; 48 49 class DisplayHandler { 50 public: 51 using ScreenConnector = cuttlefish::ScreenConnector<WebRtcScProcessedFrame>; 52 using GenerateProcessedFrameCallback = ScreenConnector::GenerateProcessedFrameCallback; 53 54 DisplayHandler(std::shared_ptr<webrtc_streaming::VideoSink> display_sink, 55 ScreenConnector& screen_connector); 56 ~DisplayHandler() = default; 57 58 [[noreturn]] void Loop(); 59 void SendLastFrame(); 60 61 void IncClientCount(); 62 void DecClientCount(); 63 64 private: 65 GenerateProcessedFrameCallback GetScreenConnectorCallback(); 66 std::shared_ptr<webrtc_streaming::VideoSink> display_sink_; 67 ScreenConnector& screen_connector_; 68 std::shared_ptr<webrtc_streaming::VideoFrameBuffer> last_buffer_; 69 std::mutex last_buffer_mutex_; 70 std::mutex next_frame_mutex_; 71 int client_count_ = 0; 72 }; 73 } // namespace cuttlefish 74