Skip to content

Attaching media and "The play() request was interrupted" error in Chrome #564

Closed

Description

@jakub-zawislak

I have problem with example example code of attaching media. The trackAdded callback is always called twice - in both inbound and outbound calls. In the second call Chrome throws:

The play() request was interrupted by a new load request

My code works anyway, I hear interlocutor and he hears me.

I "fixed" this console error by running play() only in the second call:

var trackAdded = 0

session.on('trackAdded', function() {
  var pc = session.sessionDescriptionHandler.peerConnection

  if (trackAdded == 1) {
    var remoteStream = new MediaStream()
    pc.getReceivers().forEach(function(receiver) {
      remoteStream.addTrack(receiver.track)
    });
    remoteAudio.srcObject = remoteStream
    remoteAudio.play()
  }

  trackAdded++
})

How this callback should look?

It may be related with #349, but it was 0.7.5 while trackAdded was introduced in 0.10.0.

I tested it on 0.10.0 and 0.11.0, I'm migrating from 0.7.3.

EDIT: With my fix I can't hear interlocutor on FF.

Activity

  1. kingkasuo commented on Jun 21, 2018

    @kingkasuo

    @jakub-zawislak Have you sovled this problem? I have same problem

  2. kingkasuo commented on Jun 21, 2018

    @kingkasuo

    Uncaught (in promise) DOMException: The play() request was interrupted by a new load request. https://goo.gl/LdLk22

  3. jakub-zawislak commented on Jun 21, 2018

    @jakub-zawislak
    Author

    Have you sovled this problem?

    I have only added window.chrome to the if condition but I'm still not proud of this 😄

    var trackAdded = 0
    
    session.on('trackAdded', function() {
      var pc = session.sessionDescriptionHandler.peerConnection
    
      if (!window.chrome || trackAdded == 1) {
        var remoteStream = new MediaStream()
        pc.getReceivers().forEach(function(receiver) {
          remoteStream.addTrack(receiver.track)
        });
        remoteAudio.srcObject = remoteStream
        remoteAudio.play()
      }
    
      trackAdded++
    })
  4. james-criscuolo commented on Jun 21, 2018

    @james-criscuolo
    Collaborator

    As the link with the error says, the play call is promise-based, and all play/pause calls should be buffered. While I've seen this error/warning appear as well, I've never actually seen ill effects from it, but the fact that Google wants you to fix it means that behavior can change in the future. I'll leave this open as a reminder that we need to update the example code.

    We've got a function in our code:

     this.bufferPromise = Promise.resolve();
    
     bufferAudioElementCalls(audioElement, audioFunction) {
        this.bufferPromise = this.bufferPromise.then(() => {
          return element[audioFunction]();
        }, () => {
          return element[audioFunction]();
        });
      }
    

    Instead of calling play/pause/any audioElement function directly, you call this function instead.

  5. tm1000 commented on Jul 11, 2018

    @tm1000

    This also works if you just comment out "remoteAudio.play()". Audio is still heard on both ends

  6. seanbright commented on Aug 28, 2018

    @seanbright
    Contributor

    I believe this is caused by SessionDescriptionHandler calling trackAdded() when the promise returned by getUserMedia() promise is resolved.

    At least in Chrome 68.0.3440.106, when that event is first emitted by the Session, both RTCPeerConnection::getReceivers() and RTCPeerConnection::getSenders() are going to return empty arrays, causing MediaStreams with no tracks to be set as the srcObject of the <video> elements and play() being called. The promise returned by that play() is never being resolved (successful or otherwise).

    Later, when the call is setup trackAdded is emitted again, we create a couple new MediaStreams, add the tracks, assign the srcObject and call play(). Because the previous call to play() never resolved, you see the error.

    Assuming that emitting the trackAdded event after getUserMedia() successfully resolves is "correct," adding a check for the count of senders/receivers is all that is required to silence the error:

    var remoteVideo = document.getElementById('remoteVideo');
    var localVideo = document.getElementById('localVideo');
    
    session.on('trackAdded', function() {
      // We need to check the peer connection to determine which track was added
      var pc = session.sessionDescriptionHandler.peerConnection;
    
      // Gets remote tracks
      var receivers = pc.getReceivers();
      if (receivers.length) { // If we don't have receivers, don't do anything
        var remoteStream = new MediaStream();
        receivers.forEach(function(receiver) {
          remoteStream.addTrack(receiver.track);
        });
        remoteVideo.srcObject = remoteStream;
        remoteVideo.play();
      }
    
      // Gets local tracks
      var senders = pc.getSenders();
      if (senders.length) { // If we don't have senders, don't do anything
        var localStream = new MediaStream();
        senders.forEach(function(sender) {
          localStream.addTrack(sender.track);
        });
        localVideo.srcObject = localStream;
        localVideo.play();
      }
    });
    
  7. seanbright commented on Aug 28, 2018

    @seanbright
    Contributor

    Another option which is (at least in my opinion) a little more clear, and results in the localVideo element getting setup much more quickly:

    var remoteVideo = document.getElementById('remoteVideo');
    var localVideo = document.getElementById('localVideo');
    
    session.on('SessionDescriptionHandler-created', function (sdh) {
      sdh.on('userMedia', function (stream) {
        localVideo.srcObject = new MediaStream(stream);
        localVideo.play();
      });
    });
    
    session.on('trackAdded', function() {
      // We need to check the peer connection to determine which track was added
      var pc = session.sessionDescriptionHandler.peerConnection;
    
      // Gets remote tracks
      var receivers = pc.getReceivers();
      if (receivers.length) { // If we don't have receivers, don't do anything
        var remoteStream = new MediaStream();
        receivers.forEach(function (receiver) {
          remoteStream.addTrack(receiver.track);
        });
        remoteVideo.srcObject = remoteStream;
        remoteVideo.play();
      }
    });
    

    I'm not sure that hooking the SessionDescriptionHandler-created event is ideal, but the SessionDescriptionHandlerObserver is a bit limited. In an ideal world, when it emitted trackAdded it would include the MediaStreamTrack as an argument, obviating the need to look at the RTCPeerConnection at all. I suppose that is the price you pay for an abstracted SessionDescriptionHandler.

    FYI - This probably falls down when you start with audio only and try to add video later.

    Reported in the documentation repository.

  8. Minifets commented on May 6, 2019

    @Minifets

    Hello everyone.
    I had a problem when configuring a sip.js with my asterisk server.
    When I make a call from 1 computer to another, then my sound goes only to 1 side, moreover, regardless of the environment. And the image is not transmitted, only 1 stop frame. I am not an expert in asterisk or a sip.js, but I did everything according to the instructions, and I can not understand what could be the problem?

    configs.zip

  9. john-e-riordan commented on May 27, 2020

    @john-e-riordan
    Collaborator

    Hi,

    #815 is a new implementation which I believe/hope addresses this issue.

    In short, the new SDH has a localMediaStream property which always exists and you can add a listener to that MediaStream to watch for track adds and removes.

       * The local media stream initially has no tracks, so the presence of tracks
       * should not be assumed. Furthermore, tracks may be added or removed if the
       * local media changes - for example, on upgrade from audio only to a video session.
       * At any given time there will be at most one audio track and one video track
       * (it's possible that this restriction may not apply to sub-classes).
       * Use `MediaStream.onaddtrack` or add a listener for the `addtrack` event
       * to detect when a new track becomes available:
       * https://developer.mozilla.org/en-US/docs/Web/API/MediaStream/onaddtrack
    
  10. james-criscuolo commented on Jul 6, 2020

    @james-criscuolo
    Collaborator

    As there was no response and we believe this is fixed, we are going to close the issue. Please test with 0.17.0 and re-open if there is still an issue there.

  11. jakub-zawislak commented on Dec 26, 2020

    @jakub-zawislak
    Author

    I have now migrated to the 0.18.1 and I have no errors. I used this code for attaching media

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions