<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>174327</bug_id>
          
          <creation_ts>2017-07-10 16:22:32 -0700</creation_ts>
          <short_desc>Adding, removing, and then adding back same MediaStreamTrack throws InvalidAccessError</short_desc>
          <delta_ts>2019-05-01 12:18:23 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebRTC</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>CONFIGURATION CHANGED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Mark Roberts">mroberts</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ben.browitt</cc>
    
    <cc>eric.carlson</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>youennf</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1327243</commentid>
    <comment_count>0</comment_count>
    <who name="Mark Roberts">mroberts</who>
    <bug_when>2017-07-10 16:22:32 -0700</bug_when>
    <thetext>It should be possible to add a MediaStreamTrack, remove it, and then add it back to an RTCPeerConnection. See the following code:

    (async () =&gt; {
      const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
      const [track] = stream.getTracks()

      const pc = new RTCPeerConnection()

      console.log(&apos;1. addTrack(track, stream)&apos;)
      const sender = pc.addTrack(track, stream)

      console.log(&apos;2. removeTrack(sender)&apos;)
      pc.removeTrack(sender)
      if (sender.track !== null) {
        console.error(&apos;The RTCRtpSender\&apos;s track should be set to null&apos;)
      }

      console.log(&apos;3. addTrack(track, stream)&apos;)
      pc.addTrack(track, stream)

      console.log(&apos;Success!&apos;)
    })()

Expected output:

    1. addTrack(track, stream) 
    2. removeTrack(sender) 
    3. addTrack(track, stream) 
    Success!

Actual output (Safari):

    1. addTrack(track, stream) 
    2. removeTrack(sender) 
    The RTCRtpSender&apos;s track should be set to null
    3. addTrack(track, stream) 
    Unhandled Promise Rejection: InvalidAccessError (DOM Exception 15): The object does not support the operation or argument.

(Note: In Firefox, I mostly get the expected output, except I also get the &quot;The RTCRtpSender&apos;s track should be set to null&quot; error.)

FWIW, I tried to workaround this by calling `sender.replaceTrack` in Step 3 instead of `sender.addTrack`. Although the code prints &quot;Success!&quot; in that case, I later get &quot;Unhandled Promise Rejection: InvalidStateError (DOM Exception 11): The object is in an invalid state.&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1328118</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2017-07-12 10:34:51 -0700</bug_when>
    <thetext>&lt;rdar://problem/33267829&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1415225</commentid>
    <comment_count>2</comment_count>
    <who name="Mark Roberts">mroberts</who>
    <bug_when>2018-04-17 11:36:32 -0700</bug_when>
    <thetext>Here is a potential workaround: instead of just calling `removeTrack`, call `replaceTrack(null)` on the RTCRtpSender first. For example:

    sender.replaceTrack(null)
    pc.removeTrack(sender)

If you do this and then call at least `createOffer` and `setLocalDescription`, then you can remove and add back the same MediaStreamTrack.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1531886</commentid>
    <comment_count>3</comment_count>
    <who name="Ben">ben.browitt</who>
    <bug_when>2019-05-01 02:36:01 -0700</bug_when>
    <thetext>This is still an issue in Safari 12.1

The workaround doesn&apos;t work for me. Mark, setting sender.replaceTrack(null) works for you in Safari 12.1? Any other workaround to add/remove/add same track?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1531945</commentid>
    <comment_count>4</comment_count>
    <who name="Ben">ben.browitt</who>
    <bug_when>2019-05-01 09:11:31 -0700</bug_when>
    <thetext>If you add a track with let sender = pc.addTrack(track, stream) and remove it with pc.removeTrack(sender) you can&apos;t add a track of the same kind again. The sender has the new track and the transceiver direction is sendrecv but the sdp doesn&apos;t have the track (no ssrc lines).

To reproduce:
const stream = await navigator.mediaDevices.getUserMedia({ video: true })
const [track] = stream.getTracks()
let sender = pc.addTrack(track, stream);
// negotiate
pc.removeTrack(sender);
// you can also try sender.replaceTrack(null) and track.stop() but it doesn&apos;t help.
// negotiate
const stream2 = await navigator.mediaDevices.getUserMedia({ video: true })
const [track2] = stream2.getTracks()
let sender2 = pc.addTrack(track2, stream2);
let offer = await this.pc.createOffer();
console.log(offer.sdp);
// direction of the video section in the sdp is inactive and there are no ssrc lines.

Youenn, is there a workaround?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1531951</commentid>
    <comment_count>5</comment_count>
    <who name="youenn fablet">youennf</who>
    <bug_when>2019-05-01 09:34:36 -0700</bug_when>
    <thetext>I think the workaround regressed in Safari 12.1 unfortunately.
This should be fixed in Safari Tech Preview and should also be ok on latest MacOS/iOS betas.
I am closing this issue, please reopen if you still see that issue in STP or in latest MacOS/iOS betas.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1532003</commentid>
    <comment_count>6</comment_count>
    <who name="Ben">ben.browitt</who>
    <bug_when>2019-05-01 12:18:23 -0700</bug_when>
    <thetext>Duplicates?
https://bugs.webkit.org/show_bug.cgi?id=195878
https://bugs.webkit.org/show_bug.cgi?id=195489

How come major bugs like this not detected with regression tests?
How bugs that are reported during beta reach stable?</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>