Android-cuttlefish cvd tool
camera_streamer.h
Go to the documentation of this file.
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
25
26#include <atomic>
27#include <mutex>
28#include <thread>
29#include <vector>
30
31namespace cuttlefish {
32namespace webrtc_streaming {
33
34class CameraStreamer : public rtc::VideoSinkInterface<webrtc::VideoFrame>,
35 public CameraController {
36 public:
37 CameraStreamer(unsigned int port, unsigned int cid, bool vhost_user);
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_;
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_;
70 std::thread reader_thread_;
71 std::atomic<bool> camera_session_active_;
72};
73
74} // namespace webrtc_streaming
75} // namespace cuttlefish
Definition: camera_controller.h:24
Definition: vsock_connection.h:72
Definition: camera_streamer.h:35
std::future< bool > pending_connection_
Definition: camera_streamer.h:59
bool ForwardClientMessage(const Json::Value &message)
CameraStreamer(unsigned int port, unsigned int cid, bool vhost_user)
Definition: camera_streamer.cpp:25
std::atomic< bool > camera_session_active_
Definition: camera_streamer.h:71
std::mutex onframe_mutex_
Definition: camera_streamer.h:65
CameraStreamer & operator=(const CameraStreamer &other)=delete
std::thread reader_thread_
Definition: camera_streamer.h:70
unsigned int port_
Definition: camera_streamer.h:68
Resolution GetResolutionFromSettings(const Json::Value &settings)
Definition: camera_streamer.cpp:106
{ int32_t width Resolution
Definition: camera_streamer.h:50
void Disconnect()
Definition: camera_streamer.cpp:170
void OnFrame(const webrtc::VideoFrame &frame) override
Definition: camera_streamer.cpp:35
bool IsConnectionReady()
Definition: camera_streamer.cpp:131
std::mutex settings_mutex_
Definition: camera_streamer.h:62
std::string settings_buffer_
Definition: camera_streamer.h:63
CameraStreamer(const CameraStreamer &other)=delete
rtc::scoped_refptr< webrtc::I420Buffer > scaled_frame_
Definition: camera_streamer.h:66
~CameraStreamer()
Definition: camera_streamer.cpp:32
std::mutex frame_mutex_
Definition: camera_streamer.h:64
VsockClientConnection cvd_connection_
Definition: camera_streamer.h:60
void HandleMessage(const Json::Value &message) override
Definition: camera_streamer.cpp:81
bool vhost_user_
Definition: camera_streamer.h:69
int32_t height
Definition: camera_streamer.h:51
std::atomic< Resolution > resolution_
Definition: camera_streamer.h:61
unsigned int cid_
Definition: camera_streamer.h:67
void StartReadLoop()
Definition: camera_streamer.cpp:146
bool VsockSendYUVFrame(const webrtc::I420BufferInterface *frame)
Definition: camera_streamer.cpp:112
Definition: alloc_utils.cpp:23