Bug 211483 - WebRTC replaceTrack adding latency
Summary: WebRTC replaceTrack adding latency
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebRTC (show other bugs)
Version: Safari 13
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-05-05 19:46 PDT by Marc
Modified: 2020-08-08 15:54 PDT (History)
4 users (show)

See Also:


Attachments
iOS replaceTrack Delay Example (12.66 MB, video/quicktime)
2020-05-07 17:14 PDT, Marc
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Marc 2020-05-05 19:46:17 PDT
I'm using the following code to switch cameras on iOS. It works, however it seems to add around 1s of latency to the video. Each time I switch cameras it appears to grow. I have managed to replicate it in Safari Technology Preview.

navigator.mediaDevices
  .getUserMedia({
    video: {
      deviceId: {
        exact: selectedCamera
      }
    }
  })
  .then(function(stream) {
    let videoTrack = stream.getVideoTracks()[0];
      var sender = pc.getSenders().find(function(s) {
        return s.track.kind == videoTrack.kind;
      });
      sender.replaceTrack(videoTrack);
  })
  .catch(function(err) {
    console.error('Error happens:', err);
  });
Comment 1 Radar WebKit Bug Importer 2020-05-06 03:47:19 PDT
<rdar://problem/62924912>
Comment 2 youenn fablet 2020-05-07 02:54:58 PDT
Hi Marc, just tried your code in Web Inspector on https://webrtc.github.io/samples/src/content/peerconnection/pc1/ and I do not see the added latency. Would you be able to provide a full example?
Comment 3 Marc 2020-05-07 17:13:20 PDT
Sure. I'm testing this on my iPhone XS.

1. Load https://webrtc.github.io/samples/src/content/peerconnection/pc1/
2. Click Start (Allow) and then Call
3. Wave at the camera to show that its almost in sync
4. paste this code into the console:

let cameraIds = [];

navigator.mediaDevices.enumerateDevices().then(function(deviceInfos)
{
	for (let i = 0; i !== deviceInfos.length; ++i)
	{
		const deviceInfo = deviceInfos[i];
		if (deviceInfo.kind === 'videoinput')
		{
			cameraIds.push(deviceInfo.deviceId);
		}
	}
});
let idCount = 1;

function swapCamera()
{
	navigator.mediaDevices
	  .getUserMedia({
	    video: {
	      deviceId: {
	        exact: cameraIds[idCount]
	      }
	    }
	  })
	  .then(function(stream) {
		  console.log('gotstream');
		  localVideo.srcObject = stream;
	    let videoTrack = stream.getVideoTracks()[0];
	      var sender = pc1.getSenders().find(function(s) {
	        return s.track.kind == videoTrack.kind;
	      });
	      sender.replaceTrack(videoTrack);
	  })
	  .catch(function(err) {
	    console.error('Error happens:', err);
	  });
	  
	  idCount++;
	  if(idCount >= cameraIds.length)
	  {
		  idCount = 0;
	  }
 }

5. call swapCamera() function
6. call swapCamera() function again to return to first camera
7. wave at camera to see the delay.
Comment 4 Marc 2020-05-07 17:14:44 PDT
Created attachment 398816 [details]
iOS replaceTrack Delay Example

Example video showing the latency. Notice the latency increases the more I switch cameras.
Comment 5 daginge 2020-07-29 05:10:36 PDT
I'm unable to replicate this now on iPhone X running iOS 13.6. Could you retest Marc on iOS 13.6 and see if you still see the same thing?
Comment 6 Marc 2020-08-08 15:54:17 PDT
(In reply to daginge from comment #5)
> I'm unable to replicate this now on iPhone X running iOS 13.6. Could you
> retest Marc on iOS 13.6 and see if you still see the same thing?

Thanks for looking into this. I think you are right. I can't seem to replicate it now.