Android-cuttlefish cvd tool
video_track_source_impl.h
Go to the documentation of this file.
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 <cstdint>
20#include <memory>
21#include <mutex>
22#include <optional>
23
24#include "api/media_stream_interface.h"
25#include "api/video/recordable_encoded_frame.h"
26#include "api/video/video_broadcaster.h"
27#include "api/video/video_frame.h"
28#include "api/video/video_sink_interface.h"
29#include "api/video_track_source_constraints.h"
30
32
33namespace cuttlefish {
34namespace webrtc_streaming {
35
36// Video source implementation that bridges Cuttlefish display frames to WebRTC.
37//
38// Receives video frames from the display compositor (via VideoSink interface)
39// and broadcasts them to WebRTC video tracks. Handles both YUV (I420) and
40// native RGBA frame formats.
41//
42// Key features:
43// - Supports native ARGB/ABGR buffers for hardware encoder passthrough
44// - Enforces strictly monotonic timestamps to prevent frame drops
45// - Broadcasts frames to all registered WebRTC sinks
46//
47// Thread safety: OnFrame(), AddOrUpdateSink(), and RemoveSink() may be called
48// from any thread after construction via the thread-safe VideoBroadcaster.
49class VideoTrackSourceImpl : public webrtc::VideoTrackSourceInterface {
50 public:
51 VideoTrackSourceImpl(int width, int height);
52
53 void OnFrame(std::shared_ptr<VideoFrameBuffer> frame, int64_t timestamp_us);
54
55 // MediaSourceInterface implementation
56 SourceState state() const override;
57 bool remote() const override;
58
59 // NotifierInterface implementation
60 void RegisterObserver(webrtc::ObserverInterface* observer) override;
61 void UnregisterObserver(webrtc::ObserverInterface* observer) override;
62
63 // VideoSourceInterface implementation
64 void AddOrUpdateSink(webrtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
65 const webrtc::VideoSinkWants& wants) override;
66 void RemoveSink(
67 webrtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
68
69 // VideoTrackSourceInterface implementation
70 bool is_screencast() const override;
71 std::optional<bool> needs_denoising() const override;
72
73 // Returns false if no stats are available, e.g, for a remote source, or a
74 // source which has not seen its first frame yet.
75 //
76 // Implementation should avoid blocking.
77 bool GetStats(Stats* stats) override;
78
79 bool SupportsEncodedOutput() const override;
80 void GenerateKeyFrame() override {}
82 webrtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink)
83 override {}
85 webrtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink)
86 override {}
88 const webrtc::VideoTrackSourceConstraints& constraints) override;
89
90 private:
91 const int width_;
92 const int height_;
93 std::mutex timestamp_mutex_;
94 int64_t last_timestamp_us_ = 0; // Ensure strictly monotonic timestamps
95 webrtc::VideoBroadcaster broadcaster_;
96};
97
98// Wraps a VideoTrackSourceImpl as an implementation of the VideoSink interface.
99// This is needed as the VideoTrackSourceImpl is a reference counted object that
100// should only be referenced by rtc::scoped_refptr pointers, but the
101// VideoSink interface is not a reference counted object and therefore not
102// compatible with that kind of pointers. This class can be referenced by a
103// shared pointer and it in turn holds a scoped_refptr to the wrapped object.
105 public:
107
109 webrtc::scoped_refptr<VideoTrackSourceImpl> obj)
110 : track_source_impl_(obj) {}
111
112 void OnFrame(std::shared_ptr<VideoFrameBuffer> frame,
113 int64_t timestamp_us) override {
114 track_source_impl_->OnFrame(frame, timestamp_us);
115 }
116
117 private:
118 webrtc::scoped_refptr<VideoTrackSourceImpl> track_source_impl_;
119};
120
121} // namespace webrtc_streaming
122} // namespace cuttlefish
Definition: video_sink.h:26
Definition: video_track_source_impl.h:104
VideoTrackSourceImplSinkWrapper(webrtc::scoped_refptr< VideoTrackSourceImpl > obj)
Definition: video_track_source_impl.h:108
void OnFrame(std::shared_ptr< VideoFrameBuffer > frame, int64_t timestamp_us) override
Definition: video_track_source_impl.h:112
webrtc::scoped_refptr< VideoTrackSourceImpl > track_source_impl_
Definition: video_track_source_impl.h:118
Definition: video_track_source_impl.h:49
void RemoveSink(webrtc::VideoSinkInterface< webrtc::VideoFrame > *sink) override
Definition: video_track_source_impl.cpp:83
void AddEncodedSink(webrtc::VideoSinkInterface< webrtc::RecordableEncodedFrame > *sink) override
Definition: video_track_source_impl.h:81
void GenerateKeyFrame() override
Definition: video_track_source_impl.h:80
void AddOrUpdateSink(webrtc::VideoSinkInterface< webrtc::VideoFrame > *sink, const webrtc::VideoSinkWants &wants) override
Definition: video_track_source_impl.cpp:77
void OnFrame(std::shared_ptr< VideoFrameBuffer > frame, int64_t timestamp_us)
Definition: video_track_source_impl.cpp:41
const int height_
Definition: video_track_source_impl.h:92
const int width_
Definition: video_track_source_impl.h:91
void RegisterObserver(webrtc::ObserverInterface *observer) override
Definition: video_track_source_impl.cpp:71
VideoTrackSourceImpl(int width, int height)
Definition: video_track_source_impl.cpp:38
void UnregisterObserver(webrtc::ObserverInterface *observer) override
Definition: video_track_source_impl.cpp:74
void ProcessConstraints(const webrtc::VideoTrackSourceConstraints &constraints) override
Definition: video_track_source_impl.cpp:102
webrtc::VideoBroadcaster broadcaster_
Definition: video_track_source_impl.h:95
std::optional< bool > needs_denoising() const override
Definition: video_track_source_impl.cpp:90
bool GetStats(Stats *stats) override
Definition: video_track_source_impl.cpp:94
std::mutex timestamp_mutex_
Definition: video_track_source_impl.h:93
bool remote() const override
Definition: video_track_source_impl.cpp:69
void RemoveEncodedSink(webrtc::VideoSinkInterface< webrtc::RecordableEncodedFrame > *sink) override
Definition: video_track_source_impl.h:84
bool is_screencast() const override
Definition: video_track_source_impl.cpp:88
int64_t last_timestamp_us_
Definition: video_track_source_impl.h:94
bool SupportsEncodedOutput() const override
Definition: video_track_source_impl.cpp:100
SourceState state() const override
Definition: video_track_source_impl.cpp:65
Definition: alloc_driver.h:21