MediaBridge
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
| Return | Name | Description |
|---|---|---|
ThreadSignal< void()> | KeyframeRequested | Remote peer requests a keyframe. Connect to encoder to force IDR. |
ThreadSignal< void(unsigned int)> | BitrateEstimate | Remote peer reports estimated bandwidth (bits/sec). |
KeyframeRequested
ThreadSignal< void()> KeyframeRequestedRemote peer requests a keyframe. Connect to encoder to force IDR.
BitrateEstimate
ThreadSignal< void(unsigned int)> BitrateEstimateRemote peer reports estimated bandwidth (bits/sec).
Public Methods
| Return | Name | Description |
|---|---|---|
MediaBridge | Construct a detached bridge with stable sender and receiver adapters. | |
~MediaBridge | Destroy the bridge and release any attached PeerConnection state. | |
MediaBridge | Deleted copy constructor; MediaBridge owns live track and adapter state. | |
void | attach | Create tracks on the PeerConnection and set up handler chains. Only creates tracks for codecs with a non-empty encoder name. |
void | detach | Detach all tracks and adapters. |
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. |
void | requestBitrate | Request that the remote sender reduce to a target bitrate. Sends a TMMBR RTCP message on the video track. |
WebRtcTrackSender & | videoSender | Video send processor. Attach to a PacketStream after a VideoEncoder. Throws if no video track was created. |
WebRtcTrackSender & | audioSender | Audio send processor. Attach to a PacketStream after an AudioEncoder. Throws if no audio track was created. |
WebRtcTrackReceiver & | videoReceiver | Video receive adapter. Attach as a PacketStream source. Only valid after a remote video track arrives. |
WebRtcTrackReceiver & | audioReceiver | Audio receive adapter. Attach as a PacketStream source. Only valid after a remote audio track arrives. |
std::shared_ptr< rtc::Track > | videoTrack const | The underlying libdatachannel video track, or nullptr if none was created. |
std::shared_ptr< rtc::Track > | audioTrack const | The underlying libdatachannel audio track, or nullptr if none was created. |
bool | hasVideo const | True if a video track was created at attach(). |
bool | hasAudio const | True if an audio track was created at attach(). |
bool | attached const | True 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 &) = deleteDeleted 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
bitrateTarget 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() constThe underlying libdatachannel video track, or nullptr if none was created.
audioTrack
const
std::shared_ptr< rtc::Track > audioTrack() constThe underlying libdatachannel audio track, or nullptr if none was created.
hasVideo
const
bool hasVideo() constTrue if a video track was created at attach().
hasAudio
const
bool hasAudio() constTrue if an audio track was created at attach().
attached
const
bool attached() constTrue if attach() has been called and a PeerConnection is held.
Private Attributes
| Return | Name | Description |
|---|---|---|
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