WebRTC module

MediaBridge

Convenience wrapper that creates WebRTC tracks on a PeerConnection and exposes per-track sender/receiver adapters for [PacketStream]({#ref classicy_1_1PacketStream #}) integration.

MediaBridge

#include <icy/webrtc/mediabridge.h>

Convenience wrapper that creates WebRTC tracks on a PeerConnection and exposes per-track sender/receiver adapters for PacketStream integration.

For video-only, leave audioCodec default (disabled). For audio-only, leave videoCodec default (disabled). For data-channel-only, don't create a MediaBridge at all.

For full control over individual tracks, use createVideoTrack(), createAudioTrack(), WebRtcTrackSender, and WebRtcTrackReceiver directly instead of this class.

The sender and receiver adapter objects are stable for the lifetime of the MediaBridge. attach() and detach() rebind or unbind the underlying WebRTC tracks without invalidating references previously obtained from videoSender(), audioSender(), videoReceiver(), or audioReceiver().

Example - send camera to browser:

MediaBridge bridge; bridge.attach(pc, {.videoCodec = {"H264", "libx264", 1280, 720, 30}});

PacketStream stream; stream.attachSource(capture); stream.attach(encoder, 1, true); stream.attach(&bridge.videoSender(), 5, false); stream.start();

Example - receive from browser and record:

bridge.videoReceiver().emitter += packetSlot(&recorder, &Recorder::onEncodedVideo);

The receiver emits owning encoded packets. Feed those into a decoder or recorder callback; see samples/media-recorder for a complete example.

Public Attributes

ReturnNameDescription
ThreadSignal< void()>KeyframeRequestedRemote peer requests a keyframe. Connect to encoder to force IDR.
ThreadSignal< void(unsigned int)>BitrateEstimateRemote peer reports estimated bandwidth (bits/sec).

KeyframeRequested

ThreadSignal< void()> KeyframeRequested

Remote peer requests a keyframe. Connect to encoder to force IDR.


BitrateEstimate

ThreadSignal< void(unsigned int)> BitrateEstimate

Remote peer reports estimated bandwidth (bits/sec).

Public Methods

ReturnNameDescription
MediaBridgeConstruct a detached bridge with stable sender and receiver adapters.
~MediaBridgeDestroy the bridge and release any attached PeerConnection state.
MediaBridgeDeleted copy constructor; MediaBridge owns live track and adapter state.
voidattachCreate tracks on the PeerConnection and set up handler chains. Only creates tracks for codecs with a non-empty encoder name.
voiddetachDetach all tracks and adapters.
voidrequestKeyframeRequest an immediate keyframe (IDR) from the remote sender. Sends a PLI (Picture Loss Indication) RTCP message on the video track. No-op if no video track is attached.
voidrequestBitrateRequest that the remote sender reduce to a target bitrate. Sends a TMMBR RTCP message on the video track.
WebRtcTrackSender &videoSenderVideo send processor. Attach to a PacketStream after a VideoEncoder. Throws if no video track was created.
WebRtcTrackSender &audioSenderAudio send processor. Attach to a PacketStream after an AudioEncoder. Throws if no audio track was created.
WebRtcTrackReceiver &videoReceiverVideo receive adapter. Attach as a PacketStream source. Only valid after a remote video track arrives.
WebRtcTrackReceiver &audioReceiverAudio receive adapter. Attach as a PacketStream source. Only valid after a remote audio track arrives.
std::shared_ptr< rtc::Track >videoTrack constThe underlying libdatachannel video track, or nullptr if none was created.
std::shared_ptr< rtc::Track >audioTrack constThe underlying libdatachannel audio track, or nullptr if none was created.
boolhasVideo constTrue if a video track was created at attach().
boolhasAudio constTrue if an audio track was created at attach().
boolattached constTrue if attach() has been called and a PeerConnection is held.

MediaBridge

MediaBridge()

Construct a detached bridge with stable sender and receiver adapters.


~MediaBridge

~MediaBridge()

Destroy the bridge and release any attached PeerConnection state.


MediaBridge

MediaBridge(const MediaBridge &) = delete

Deleted copy constructor; MediaBridge owns live track and adapter state.


attach

void attach(std::shared_ptr< rtc::PeerConnection > pc, const Options & opts)

Create tracks on the PeerConnection and set up handler chains. Only creates tracks for codecs with a non-empty encoder name.


detach

void detach()

Detach all tracks and adapters.


requestKeyframe

void requestKeyframe()

Request an immediate keyframe (IDR) from the remote sender. Sends a PLI (Picture Loss Indication) RTCP message on the video track. No-op if no video track is attached.


requestBitrate

void requestBitrate(unsigned int bitrate)

Request that the remote sender reduce to a target bitrate. Sends a TMMBR RTCP message on the video track.

Parameters

  • bitrate Target bitrate in bits per second.

videoSender

WebRtcTrackSender & videoSender()

Video send processor. Attach to a PacketStream after a VideoEncoder. Throws if no video track was created.


audioSender

WebRtcTrackSender & audioSender()

Audio send processor. Attach to a PacketStream after an AudioEncoder. Throws if no audio track was created.


videoReceiver

WebRtcTrackReceiver & videoReceiver()

Video receive adapter. Attach as a PacketStream source. Only valid after a remote video track arrives.


audioReceiver

WebRtcTrackReceiver & audioReceiver()

Audio receive adapter. Attach as a PacketStream source. Only valid after a remote audio track arrives.


videoTrack

const

std::shared_ptr< rtc::Track > videoTrack() const

The underlying libdatachannel video track, or nullptr if none was created.


audioTrack

const

std::shared_ptr< rtc::Track > audioTrack() const

The underlying libdatachannel audio track, or nullptr if none was created.


hasVideo

const

bool hasVideo() const

True if a video track was created at attach().


hasAudio

const

bool hasAudio() const

True if an audio track was created at attach().


attached

const

bool attached() const

True if attach() has been called and a PeerConnection is held.

Private Attributes

ReturnNameDescription
std::shared_ptr< rtc::PeerConnection >_pc
WebRtcTrackSender_videoSender
WebRtcTrackSender_audioSender
TrackHandle_videoHandle
TrackHandle_audioHandle
WebRtcTrackReceiver_videoReceiver
WebRtcTrackReceiver_audioReceiver
std::shared_ptr< rtc::Track >_videoReceiveTrack
std::shared_ptr< rtc::Track >_audioReceiveTrack
std::mutex_mutex

_pc

std::shared_ptr< rtc::PeerConnection > _pc

_videoSender

WebRtcTrackSender _videoSender

_audioSender

WebRtcTrackSender _audioSender

_videoHandle

TrackHandle _videoHandle

_audioHandle

TrackHandle _audioHandle

_videoReceiver

WebRtcTrackReceiver _videoReceiver

_audioReceiver

WebRtcTrackReceiver _audioReceiver

_videoReceiveTrack

std::shared_ptr< rtc::Track > _videoReceiveTrack

_audioReceiveTrack

std::shared_ptr< rtc::Track > _audioReceiveTrack

_mutex

std::mutex _mutex