Skip to content

Commit d79e096

Browse files
committed
Add Virtual Capturer for Audio/Video.
1 parent 4a7b1ac commit d79e096

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

include/rtc_audio_source.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define LIB_WEBRTC_RTC_AUDIO_SOURCE_HXX
33

44
#include "rtc_types.h"
5+
#include "rtc_audio_frame.h"
56

67
namespace libwebrtc {
78

@@ -20,6 +21,15 @@ class RTCAudioSource : public RefCountInterface {
2021
virtual ~RTCAudioSource() {}
2122
};
2223

24+
class VirtualAudioCapturer : public RefCountInterface {
25+
public:
26+
LIB_WEBRTC_API static scoped_refptr<VirtualAudioCapturer> Create();
27+
28+
virtual void OnDataCaptured(scoped_refptr<RTCAudioFrame> data) = 0;
29+
30+
virtual scoped_refptr<RTCAudioSource> source() = 0;
31+
};
32+
2333
} // namespace libwebrtc
2434

2535
#endif // LIB_WEBRTC_RTC_AUDIO_TRACK_HXX

include/rtc_video_source.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
#define LIB_WEBRTC_RTC_VIDEO_SOURCE_HXX
33

44
#include "rtc_types.h"
5+
#include "rtc_video_frame.h"
56

67
namespace libwebrtc {
78

89
class RTCVideoSource : public RefCountInterface {
910
public:
1011
~RTCVideoSource() {}
1112
};
13+
14+
class VirtualVideoCapturer : public RefCountInterface {
15+
public:
16+
LIB_WEBRTC_API static scoped_refptr<VirtualVideoCapturer> Create();
17+
18+
virtual void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) = 0;
19+
20+
virtual scoped_refptr<RTCVideoSource> source() = 0;
21+
};
22+
1223
} // namespace libwebrtc
1324

1425
#endif // LIB_WEBRTC_RTC_VIDEO_SOURCE_HXX

src/rtc_audio_source_impl.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "rtc_audio_source_impl.h"
22

3+
#include "pc/local_audio_source.h"
4+
35
namespace libwebrtc {
46

57
RTCAudioSourceImpl::RTCAudioSourceImpl(
@@ -12,4 +14,55 @@ RTCAudioSourceImpl::~RTCAudioSourceImpl() {
1214
RTC_LOG(LS_INFO) << __FUNCTION__ << ": dtor ";
1315
}
1416

17+
class AdaptedVirtualAudioCapturer : public webrtc::LocalAudioSource {
18+
public:
19+
AdaptedVirtualAudioCapturer() {}
20+
~AdaptedVirtualAudioCapturer() {}
21+
22+
void AddSink(webrtc::AudioTrackSinkInterface* sink) override {
23+
webrtc::MutexLock lock(&mutex_);
24+
sinks_.push_back(sink);
25+
}
26+
27+
void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {
28+
webrtc::MutexLock lock(&mutex_);
29+
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
30+
}
31+
32+
void OnCaptureData(scoped_refptr<RTCAudioFrame> frame) {
33+
webrtc::MutexLock lock(&mutex_);
34+
for (auto sink : sinks_) {
35+
sink->OnData((const void*)frame->data(), 16, frame->sample_rate_hz(),
36+
frame->num_channels(), frame->samples_per_channel());
37+
}
38+
}
39+
40+
private:
41+
mutable webrtc::Mutex mutex_;
42+
std::vector<webrtc::AudioTrackSinkInterface*> sinks_;
43+
};
44+
45+
class VirtualAudioCapturerImpl : public VirtualAudioCapturer {
46+
public:
47+
VirtualAudioCapturerImpl() {}
48+
virtual ~VirtualAudioCapturerImpl() {}
49+
50+
virtual void OnDataCaptured(scoped_refptr<RTCAudioFrame> frame) override {
51+
adapted_source_->OnCaptureData(frame);
52+
}
53+
54+
virtual scoped_refptr<RTCAudioSource> source() override {
55+
return rtc_audio_source_;
56+
}
57+
58+
private:
59+
scoped_refptr<RTCAudioSourceImpl> rtc_audio_source_;
60+
rtc::scoped_refptr<AdaptedVirtualAudioCapturer> adapted_source_;
61+
};
62+
63+
scoped_refptr<VirtualAudioCapturer> VirtualAudioCapturer::Create() {
64+
return scoped_refptr<VirtualAudioCapturer>(
65+
new RefCountedObject<VirtualAudioCapturerImpl>());
66+
}
67+
1568
} // namespace libwebrtc

src/rtc_video_frame_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class VideoFrameBufferImpl : public RTCVideoFrame {
5151

5252
void set_rotation(webrtc::VideoRotation rotation) { rotation_ = rotation; }
5353

54+
webrtc::VideoRotation rtc_rotation() { return rotation_; }
55+
5456
private:
5557
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
5658
int64_t timestamp_us_ = 0;

src/rtc_video_source_impl.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@
66

77
namespace libwebrtc {
88

9+
class AdaptedVirtualVideoCapturer : public rtc::AdaptedVideoTrackSource {
10+
public:
11+
AdaptedVirtualVideoCapturer() {}
12+
~AdaptedVirtualVideoCapturer() override {}
13+
14+
bool is_screencast() const override { return false; }
15+
16+
absl::optional<bool> needs_denoising() const override { return false; }
17+
18+
SourceState state() const override { return kLive; }
19+
20+
bool remote() const override { return false; }
21+
22+
void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) {
23+
VideoFrameBufferImpl* impl =
24+
static_cast<VideoFrameBufferImpl*>(frame.get());
25+
auto newFrame = webrtc::VideoFrame::Builder()
26+
.set_video_frame_buffer(impl->buffer())
27+
.set_rotation(impl->rtc_rotation())
28+
.set_timestamp_us(impl->timestamp_us())
29+
.build();
30+
OnFrame(newFrame);
31+
}
32+
};
33+
34+
class VirtualVideoCapturerImpl : public VirtualVideoCapturer {
35+
public:
36+
VirtualVideoCapturerImpl() {
37+
adapted_source_ = new rtc::RefCountedObject<AdaptedVirtualVideoCapturer>();
38+
rtc_source_ = scoped_refptr<RTCVideoSourceImpl>(
39+
new RefCountedObject<RTCVideoSourceImpl>(adapted_source_));
40+
}
41+
virtual ~VirtualVideoCapturerImpl() {}
42+
43+
virtual scoped_refptr<RTCVideoSource> source() override {
44+
return rtc_source_;
45+
}
46+
47+
virtual void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) override {
48+
adapted_source_->OnFrameCaptured(frame);
49+
}
50+
51+
private:
52+
rtc::scoped_refptr<AdaptedVirtualVideoCapturer> adapted_source_;
53+
scoped_refptr<RTCVideoSourceImpl> rtc_source_;
54+
};
55+
56+
scoped_refptr<VirtualVideoCapturer> VirtualVideoCapturer::Create() {
57+
return scoped_refptr<VirtualVideoCapturer>(
58+
new RefCountedObject<VirtualVideoCapturerImpl>());
59+
}
60+
961
RTCVideoSourceImpl::RTCVideoSourceImpl(
1062
rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> rtc_source_track)
1163
: rtc_source_track_(rtc_source_track) {

src/rtc_video_source_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define LIB_WEBRTC_VIDEO_SOURCE_IMPL_HXX
33

44
#include "api/media_stream_interface.h"
5+
#include "media/base/adapted_video_track_source.h"
56
#include "media/base/video_broadcaster.h"
67
#include "media/base/video_source_base.h"
78
#include "rtc_peerconnection_factory_impl.h"

0 commit comments

Comments
 (0)