// Create a WebRTC PeerConnection
const peerConnection = new RTCPeerConnection();

// Function to handle received video tracks
function handleVideoTrack(event) {
  const receivedVideoTrack = event.track;

  // Attach the received video track to a video element for rendering
  const videoElement = document.getElementById('videoElement');
  videoElement.srcObject = new MediaStream([receivedVideoTrack]);
}

// Handle the SDP negotiation and add received tracks to the PeerConnection
peerConnection.ontrack = handleVideoTrack;

// Function to handle manual layer selection
function selectVideoLayer(layerId) {
  // Get the sender's video transceiver
  const videoTransceiver = peerConnection.getTransceivers()
    .find(transceiver => transceiver.sender.track.kind === 'video');

  // Get the list of available encodings for the sender's video track
  const sendEncodings = videoTransceiver.sender.getParameters().encodings;

  // Find the selected encoding layer by ID
  const selectedLayer = sendEncodings.find(encoding => encoding.rid === layerId);

  if (selectedLayer) {
    // Set the selected encoding layer as the active one
    selectedLayer.active = true;

    // Update the sender's encoding parameters with the selected layer
    videoTransceiver.sender.setParameters({ encodings: sendEncodings });
  }
}

// Example usage: Manually selecting a video layer with ID 'low'
selectVideoLayer('low');