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 17 #pragma once 18 #include <api/video/i420_buffer.h> 19 #include <api/video/video_frame.h> 20 #include <api/video/video_sink_interface.h> 21 #include <json/json.h> 22 23 #include "common/libs/utils/vsock_connection.h" 24 #include "host/frontend/webrtc/libdevice/camera_controller.h" 25 26 #include <atomic> 27 #include <mutex> 28 #include <thread> 29 #include <vector> 30 31 namespace cuttlefish { 32 namespace webrtc_streaming { 33 34 class CameraStreamer : public rtc::VideoSinkInterface<webrtc::VideoFrame>, 35 public CameraController { 36 public: 37 CameraStreamer(unsigned int port, unsigned int cid, bool vhost_user); 38 ~CameraStreamer(); 39 40 CameraStreamer(const CameraStreamer& other) = delete; 41 CameraStreamer& operator=(const CameraStreamer& other) = delete; 42 43 void OnFrame(const webrtc::VideoFrame& frame) override; 44 45 void HandleMessage(const Json::Value& message) override; 46 void HandleMessage(const std::vector<char>& message) override; 47 48 private: 49 using Resolution = struct { 50 int32_t width; 51 int32_t height; 52 }; 53 bool ForwardClientMessage(const Json::Value& message); 54 Resolution GetResolutionFromSettings(const Json::Value& settings); 55 bool VsockSendYUVFrame(const webrtc::I420BufferInterface* frame); 56 bool IsConnectionReady(); 57 void StartReadLoop(); 58 void Disconnect(); 59 std::future<bool> pending_connection_; 60 VsockClientConnection cvd_connection_; 61 std::atomic<Resolution> resolution_; 62 std::mutex settings_mutex_; 63 std::string settings_buffer_; 64 std::mutex frame_mutex_; 65 std::mutex onframe_mutex_; 66 rtc::scoped_refptr<webrtc::I420Buffer> scaled_frame_; 67 unsigned int cid_; 68 unsigned int port_; 69 bool vhost_user_; 70 std::thread reader_thread_; 71 std::atomic<bool> camera_session_active_; 72 }; 73 74 } // namespace webrtc_streaming 75 } // namespace cuttlefish 76