Skip to content

Commit 7b22ec7

Browse files
committed
refactor: VideoTrackDesktopSource extends AdaptedVideoTrackSource
1 parent 760a0e3 commit 7b22ec7

File tree

2 files changed

+45
-145
lines changed

2 files changed

+45
-145
lines changed

webrtc-jni/src/main/cpp/include/media/video/VideoTrackDesktopSource.h

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,57 @@
1818
#define JNI_WEBRTC_MEDIA_VIDEO_TRACK_DESKTOP_SOURCE_H_
1919

2020
#include "api/video/i420_buffer.h"
21-
#include "api/video/video_frame.h"
22-
#include "api/video/video_sink_interface.h"
23-
#include "media/base/video_adapter.h"
24-
#include "media/base/video_broadcaster.h"
25-
#include "pc/video_track_source.h"
21+
#include "media/base/adapted_video_track_source.h"
2622
#include "modules/desktop_capture/desktop_capturer.h"
2723
#include "rtc_base/thread.h"
2824

2925
namespace jni
3026
{
31-
class VideoTrackDesktopSource : public webrtc::VideoTrackSource, public webrtc::DesktopCapturer::Callback
27+
class VideoTrackDesktopSource : public rtc::AdaptedVideoTrackSource, public webrtc::DesktopCapturer::Callback
3228
{
33-
public:
34-
VideoTrackDesktopSource();
35-
~VideoTrackDesktopSource();
29+
public:
30+
VideoTrackDesktopSource();
31+
~VideoTrackDesktopSource();
3632

37-
void setSourceId(webrtc::DesktopCapturer::SourceId source, bool isWindow);
38-
void setFrameRate(const uint16_t frameRate);
39-
void setMaxFrameSize(webrtc::DesktopSize size);
40-
void setFocusSelectedSource(bool focus);
33+
void setSourceId(webrtc::DesktopCapturer::SourceId source, bool isWindow);
34+
void setFrameRate(const uint16_t frameRate);
35+
void setMaxFrameSize(webrtc::DesktopSize size);
36+
void setFocusSelectedSource(bool focus);
4137

42-
void start();
43-
void stop();
44-
void terminate();
38+
void start();
39+
void stop();
40+
void terminate();
4541

46-
// VideoSourceInterface implementation.
47-
void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame> * sink, const rtc::VideoSinkWants & wants) override;
48-
void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame> * sink) override;
42+
// AdaptedVideoTrackSource implementation.
43+
virtual bool is_screencast() const override;
44+
virtual std::optional<bool> needs_denoising() const override;
45+
SourceState state() const override;
46+
bool remote() const override;
4947

50-
// VideoTrackSource implementation.
51-
rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override;
48+
// DesktopCapturer::Callback implementation.
49+
void OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame) override;
5250

53-
// VideoTrackSourceInterface implementation.
54-
bool GetStats(webrtc::VideoTrackSourceInterface::Stats * stats) override;
55-
void ProcessConstraints(const webrtc::VideoTrackSourceConstraints & constraints) override;
56-
virtual bool is_screencast() const override;
57-
virtual std::optional<bool> needs_denoising() const override;
58-
SourceState state() const override;
59-
bool remote() const override;
51+
private:
52+
void capture();
53+
void process(std::unique_ptr<webrtc::DesktopFrame>& frame);
6054

61-
// DesktopCapturer::Callback implementation.
62-
void OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame) override;
55+
private:
56+
uint16_t frameRate;
57+
bool isCapturing;
58+
bool focusSelectedSource;
6359

64-
protected:
65-
bool AdaptFrame(int width, int height, int64_t time_us, int* out_width, int* out_height, int* crop_width, int* crop_height, int* crop_x, int* crop_y);
66-
void OnFrameDropped();
60+
webrtc::DesktopSize maxFrameSize;
6761

68-
private:
69-
void capture();
70-
void process(std::unique_ptr<webrtc::DesktopFrame> & frame);
71-
void updateVideoAdapter();
62+
webrtc::MediaSourceInterface::SourceState sourceState;
7263

73-
private:
74-
uint16_t frameRate;
75-
bool isCapturing;
76-
bool focusSelectedSource;
64+
webrtc::DesktopCapturer::SourceId sourceId;
65+
bool sourceIsWindow;
7766

78-
webrtc::DesktopSize maxFrameSize;
67+
std::unique_ptr<webrtc::DesktopFrame> lastFrame;
7968

80-
rtc::VideoBroadcaster broadcaster;
81-
cricket::VideoAdapter videoAdapter;
69+
std::unique_ptr<rtc::Thread> captureThread;
8270

83-
webrtc::MediaSourceInterface::SourceState sourceState;
84-
85-
webrtc::DesktopCapturer::SourceId sourceId;
86-
bool sourceIsWindow;
87-
88-
std::unique_ptr<webrtc::DesktopFrame> lastFrame;
89-
90-
std::unique_ptr<rtc::Thread> captureThread;
91-
92-
rtc::scoped_refptr<webrtc::I420Buffer> buffer;
93-
94-
webrtc::Mutex statsMutex;
95-
std::optional<Stats> stats RTC_GUARDED_BY(statsMutex);
71+
rtc::scoped_refptr<webrtc::I420Buffer> buffer;
9672
};
9773
}
9874

webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp

Lines changed: 12 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
namespace jni
3434
{
3535
VideoTrackDesktopSource::VideoTrackDesktopSource() :
36-
VideoTrackSource(/*remote=*/false),
36+
AdaptedVideoTrackSource(),
3737
frameRate(20),
3838
isCapturing(false),
3939
focusSelectedSource(true),
@@ -88,104 +88,29 @@ namespace jni
8888
}
8989
}
9090

91-
void VideoTrackDesktopSource::AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, const rtc::VideoSinkWants& wants)
92-
{
93-
if (wants.is_active) {
94-
broadcaster.AddOrUpdateSink(sink, wants);
95-
96-
updateVideoAdapter();
97-
}
98-
}
99-
100-
void VideoTrackDesktopSource::RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink)
101-
{
102-
broadcaster.RemoveSink(sink);
103-
104-
updateVideoAdapter();
105-
}
106-
107-
void VideoTrackDesktopSource::updateVideoAdapter()
108-
{
109-
videoAdapter.OnSinkWants(broadcaster.wants());
110-
}
111-
112-
void VideoTrackDesktopSource::OnFrameDropped()
113-
{
114-
broadcaster.OnDiscardedFrame();
115-
}
116-
117-
rtc::VideoSourceInterface<webrtc::VideoFrame>* VideoTrackDesktopSource::source()
118-
{
119-
return this;
120-
}
121-
12291
void VideoTrackDesktopSource::terminate()
12392
{
12493
// Notify the track that we are permanently done.
12594
sourceState = kEnded;
12695
FireOnChanged();
12796
}
12897

129-
bool VideoTrackDesktopSource::GetStats(webrtc::VideoTrackSourceInterface::Stats * stats)
130-
{
131-
webrtc::MutexLock lock(&statsMutex);
132-
133-
if (!stats) {
134-
return false;
135-
}
136-
137-
*stats = *stats;
138-
98+
bool VideoTrackDesktopSource::is_screencast() const {
13999
return true;
140100
}
141101

142-
void VideoTrackDesktopSource::ProcessConstraints(const webrtc::VideoTrackSourceConstraints & constraints)
143-
{
144-
broadcaster.ProcessConstraints(constraints);
145-
}
146-
147-
bool VideoTrackDesktopSource::is_screencast() const
148-
{
149-
return true;
150-
}
151-
152-
std::optional<bool> VideoTrackDesktopSource::needs_denoising() const
153-
{
102+
std::optional<bool> VideoTrackDesktopSource::needs_denoising() const {
154103
return false;
155104
}
156105

157-
webrtc::MediaSourceInterface::SourceState VideoTrackDesktopSource::state() const
158-
{
106+
webrtc::MediaSourceInterface::SourceState VideoTrackDesktopSource::state() const {
159107
return sourceState;
160108
}
161109

162-
bool VideoTrackDesktopSource::remote() const
163-
{
110+
bool VideoTrackDesktopSource::remote() const {
164111
return false;
165112
}
166113

167-
bool VideoTrackDesktopSource::AdaptFrame(int width, int height, int64_t time_us, int* out_width, int* out_height, int* crop_width, int* crop_height, int* crop_x, int* crop_y)
168-
{
169-
{
170-
webrtc::MutexLock lock(&statsMutex);
171-
stats = Stats{ width, height };
172-
}
173-
174-
if (!broadcaster.frame_wanted()) {
175-
return false;
176-
}
177-
178-
if (!videoAdapter.AdaptFrameResolution(width, height, time_us * rtc::kNumNanosecsPerMicrosec, crop_width, crop_height, out_width, out_height)) {
179-
broadcaster.OnDiscardedFrame();
180-
return false;
181-
}
182-
183-
*crop_x = (width - *crop_width) / 2;
184-
*crop_y = (height - *crop_height) / 2;
185-
186-
return true;
187-
}
188-
189114
void VideoTrackDesktopSource::OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame)
190115
{
191116
if (result != webrtc::DesktopCapturer::Result::SUCCESS) {
@@ -195,7 +120,7 @@ namespace jni
195120
terminate();
196121
stop();
197122
}
198-
123+
199124
return;
200125
}
201126

@@ -217,15 +142,15 @@ namespace jni
217142
}
218143
}
219144

220-
void VideoTrackDesktopSource::process(std::unique_ptr<webrtc::DesktopFrame> & frame)
145+
void VideoTrackDesktopSource::process(std::unique_ptr<webrtc::DesktopFrame>& frame)
221146
{
222147
int64_t time = rtc::TimeMicros();
223148

224149
int width = frame->size().width();
225150
int height = frame->size().height();
226-
227-
int adapted_width = width;
228-
int adapted_height = height;
151+
152+
int adapted_width;
153+
int adapted_height;
229154

230155
int crop_x = 0;
231156
int crop_y = 0;
@@ -291,19 +216,18 @@ namespace jni
291216

292217
scaled_buffer->ScaleFrom(*buffer);
293218

294-
broadcaster.OnFrame(webrtc::VideoFrame::Builder()
219+
OnFrame(webrtc::VideoFrame::Builder()
295220
.set_video_frame_buffer(scaled_buffer)
296221
.set_rotation(webrtc::kVideoRotation_0)
297222
.set_timestamp_us(time)
298223
.build());
299224
}
300225
else {
301226
// No adaptations needed, just return the frame as is.
302-
broadcaster.OnFrame(webrtc::VideoFrame::Builder()
227+
OnFrame(webrtc::VideoFrame::Builder()
303228
.set_video_frame_buffer(buffer)
304229
.set_rotation(webrtc::kVideoRotation_0)
305230
.set_timestamp_us(time)
306-
.set_timestamp_rtp(0)
307231
.build());
308232
}
309233
}

0 commit comments

Comments
 (0)