Is there any tips for refreshing MediaStreamingTrack?

I want to make Publishing and Finish buttons for streaming.

I was First used “track.enable = false” but this cannot refer close cuz the camera icon is still on
image

const finishStream = async () =>{
  if(isStreaming == false){
    console.log("already Finished")
    return
  }
  if(producer)
  {
    console.log("Producer exited roomdd")
    await socket.emit('exitRoom',{
      rtpCapabilities : device.rtpCapabilities,
      remoteProducerId : producer.id,
      serverside_ConsumerTransportId : producerTransport.id,
      producer,
    }, async()=>{
      await deleteVideo(true)
      producer.enabled = false
      producerTransport.enabled = false
      Streaming.getVideoTracks()[0].enabled = false
    })
  }
}

So I want to use track.close() function but this makes error cuz readyState did not refresh

const finishStream = async () =>{
  if(isStreaming == false){
    console.log("already Finished")
    return
  }
  if(producer)
  {
    console.log("Producer exited roomdd")
    await socket.emit('exitRoom',{
      rtpCapabilities : device.rtpCapabilities,
      remoteProducerId : producer.id,
      serverside_ConsumerTransportId : producerTransport.id,
      producer,
    }, async()=>{
      await deleteVideo(true)
      producer.close()
      producerTransport.close()
      Streaming.getVideoTracks()[0].stop()
    })
  }
}

Is there any tips for using close()?

There is no such thing like track.close(). Track is only stopped and that is it, it is released from the device. I guess by refreshing you mean that the stream is recaptured from the device and is produced again automatically. If that is right then you will have to do this like this there is no automatic way:

1- When user will click publish then you will get stream from camera/microphone and then you will produce the tracks.
2- When user will click finish then you will stop all the tracks and then close the producer and transport as well.
3- When user click publish again then do step 1). And that continues.

1 Like

THX!!! really glad to find what’s the problem is,
but the readyState is not update properly… is this the problem of producer & producerTransport is not finish??

What do you mean by ‘readystate not updated properly’?

it was about WebRTC - MeidaStreamTrack 's readyState
I found at MediaStream’s - track object.

this state is read-only, so I don’t have no choice to refresh,
and If, readyState is ended. we can’t transfer media data

I’ve solved this problem today… make ‘Params = undefined’ to get new track
but, I don’t know that this is good choice to do…

When readyState of stream is ended after you stop tracks then you will have to get fresh track from camera/mic, there is no other choice.

1 Like