Skip to content

Feature Request: Add updateMediaStream functionality to dynamically update media source during an active stream #23

@leporel

Description

@leporel

Description:

Could you please add functionality to dynamically update the media source during an active stream? This could be implemented in setMediaStream or like as a new method updateMediaStream.

I’ve created a rough implementation (see example below), but I’m not a JavaScript developer, and I’m concerned about potential issues:

  1. Updating the video source with different constraints (e.g., resolution changes). Do we need to create a new offer or perform additional steps to update the parameters?
  2. Removing and re-adding tracks (e.g., audioSender.replaceTrack(null)). After removing a track, adding a new one fails because the sender is no longer found.

Here’s my example code for video track replacement:

async function replaceVideoTrackOnFly(newVideoTrack) {
    if (!ovenkit || !isStreaming) return;
    
    try {
        // Find the current video sender in the peer connection
        const videoSender = ovenkit.peerConnection.getSenders().find(sender => 
            sender.track && sender.track.kind === 'video'
        );
        
        if (videoSender) {
            // Replace the track
            await videoSender.replaceTrack(newVideoTrack);
            
            // Also update the input stream that OvenKit keeps track of
            const oldTrack = ovenkit.inputStream.getVideoTracks()[0];
            if (oldTrack) {
                ovenkit.inputStream.removeTrack(oldTrack);
            }
            ovenkit.inputStream.addTrack(newVideoTrack);
            
            console.log('Video track replaced successfully');
            return true;
        } else {
            console.error('No video sender found in peer connection');
            return false;
        }
    } catch (err) {
        console.error('Error replacing video track:', err);
        return false;
    }
}

This seems to work, but I’m unsure about the technical nuances. Could you provide guidance or implement a robust solution for this use case? Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions