Skip to content

Commit

Permalink
Add Virtual Capturer for Audio/Video.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Aug 15, 2024
1 parent 4a7b1ac commit d79e096
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/rtc_audio_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIB_WEBRTC_RTC_AUDIO_SOURCE_HXX

#include "rtc_types.h"
#include "rtc_audio_frame.h"

namespace libwebrtc {

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

class VirtualAudioCapturer : public RefCountInterface {
public:
LIB_WEBRTC_API static scoped_refptr<VirtualAudioCapturer> Create();

virtual void OnDataCaptured(scoped_refptr<RTCAudioFrame> data) = 0;

virtual scoped_refptr<RTCAudioSource> source() = 0;
};

} // namespace libwebrtc

#endif // LIB_WEBRTC_RTC_AUDIO_TRACK_HXX
11 changes: 11 additions & 0 deletions include/rtc_video_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
#define LIB_WEBRTC_RTC_VIDEO_SOURCE_HXX

#include "rtc_types.h"
#include "rtc_video_frame.h"

namespace libwebrtc {

class RTCVideoSource : public RefCountInterface {
public:
~RTCVideoSource() {}
};

class VirtualVideoCapturer : public RefCountInterface {
public:
LIB_WEBRTC_API static scoped_refptr<VirtualVideoCapturer> Create();

virtual void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) = 0;

virtual scoped_refptr<RTCVideoSource> source() = 0;
};

} // namespace libwebrtc

#endif // LIB_WEBRTC_RTC_VIDEO_SOURCE_HXX
53 changes: 53 additions & 0 deletions src/rtc_audio_source_impl.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "rtc_audio_source_impl.h"

#include "pc/local_audio_source.h"

namespace libwebrtc {

RTCAudioSourceImpl::RTCAudioSourceImpl(
Expand All @@ -12,4 +14,55 @@ RTCAudioSourceImpl::~RTCAudioSourceImpl() {
RTC_LOG(LS_INFO) << __FUNCTION__ << ": dtor ";
}

class AdaptedVirtualAudioCapturer : public webrtc::LocalAudioSource {
public:
AdaptedVirtualAudioCapturer() {}
~AdaptedVirtualAudioCapturer() {}

void AddSink(webrtc::AudioTrackSinkInterface* sink) override {
webrtc::MutexLock lock(&mutex_);
sinks_.push_back(sink);
}

void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {
webrtc::MutexLock lock(&mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
}

void OnCaptureData(scoped_refptr<RTCAudioFrame> frame) {
webrtc::MutexLock lock(&mutex_);
for (auto sink : sinks_) {
sink->OnData((const void*)frame->data(), 16, frame->sample_rate_hz(),
frame->num_channels(), frame->samples_per_channel());
}
}

private:
mutable webrtc::Mutex mutex_;
std::vector<webrtc::AudioTrackSinkInterface*> sinks_;
};

class VirtualAudioCapturerImpl : public VirtualAudioCapturer {
public:
VirtualAudioCapturerImpl() {}
virtual ~VirtualAudioCapturerImpl() {}

virtual void OnDataCaptured(scoped_refptr<RTCAudioFrame> frame) override {
adapted_source_->OnCaptureData(frame);
}

virtual scoped_refptr<RTCAudioSource> source() override {
return rtc_audio_source_;
}

private:
scoped_refptr<RTCAudioSourceImpl> rtc_audio_source_;
rtc::scoped_refptr<AdaptedVirtualAudioCapturer> adapted_source_;
};

scoped_refptr<VirtualAudioCapturer> VirtualAudioCapturer::Create() {
return scoped_refptr<VirtualAudioCapturer>(
new RefCountedObject<VirtualAudioCapturerImpl>());
}

} // namespace libwebrtc
2 changes: 2 additions & 0 deletions src/rtc_video_frame_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class VideoFrameBufferImpl : public RTCVideoFrame {

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

webrtc::VideoRotation rtc_rotation() { return rotation_; }

private:
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
int64_t timestamp_us_ = 0;
Expand Down
52 changes: 52 additions & 0 deletions src/rtc_video_source_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@

namespace libwebrtc {

class AdaptedVirtualVideoCapturer : public rtc::AdaptedVideoTrackSource {
public:
AdaptedVirtualVideoCapturer() {}
~AdaptedVirtualVideoCapturer() override {}

bool is_screencast() const override { return false; }

absl::optional<bool> needs_denoising() const override { return false; }

SourceState state() const override { return kLive; }

bool remote() const override { return false; }

void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) {
VideoFrameBufferImpl* impl =
static_cast<VideoFrameBufferImpl*>(frame.get());
auto newFrame = webrtc::VideoFrame::Builder()
.set_video_frame_buffer(impl->buffer())
.set_rotation(impl->rtc_rotation())
.set_timestamp_us(impl->timestamp_us())
.build();
OnFrame(newFrame);
}
};

class VirtualVideoCapturerImpl : public VirtualVideoCapturer {
public:
VirtualVideoCapturerImpl() {
adapted_source_ = new rtc::RefCountedObject<AdaptedVirtualVideoCapturer>();
rtc_source_ = scoped_refptr<RTCVideoSourceImpl>(
new RefCountedObject<RTCVideoSourceImpl>(adapted_source_));
}
virtual ~VirtualVideoCapturerImpl() {}

virtual scoped_refptr<RTCVideoSource> source() override {
return rtc_source_;
}

virtual void OnFrameCaptured(scoped_refptr<RTCVideoFrame> frame) override {
adapted_source_->OnFrameCaptured(frame);
}

private:
rtc::scoped_refptr<AdaptedVirtualVideoCapturer> adapted_source_;
scoped_refptr<RTCVideoSourceImpl> rtc_source_;
};

scoped_refptr<VirtualVideoCapturer> VirtualVideoCapturer::Create() {
return scoped_refptr<VirtualVideoCapturer>(
new RefCountedObject<VirtualVideoCapturerImpl>());
}

RTCVideoSourceImpl::RTCVideoSourceImpl(
rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> rtc_source_track)
: rtc_source_track_(rtc_source_track) {
Expand Down
1 change: 1 addition & 0 deletions src/rtc_video_source_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIB_WEBRTC_VIDEO_SOURCE_IMPL_HXX

#include "api/media_stream_interface.h"
#include "media/base/adapted_video_track_source.h"
#include "media/base/video_broadcaster.h"
#include "media/base/video_source_base.h"
#include "rtc_peerconnection_factory_impl.h"
Expand Down

0 comments on commit d79e096

Please sign in to comment.