javascript - WebRTC - How to change the audio track for a existing stream - Stack Overflow

I have a webRTC connection established with audio and video.On the caller side, I'd like to change

I have a webRTC connection established with audio and video.

On the caller side, I'd like to change the audio input.

e.g. the User changes the audioinput from a dropdown list.

What's the workflow to substitute the audio track of an existing stream?

Can I add another audio track and make one active over the other? how?

Looks like I may need to call getUserMedia again passing constraints (?), which to my understanding es to create a New mediaStream instances and not modify the existing.

I have a webRTC connection established with audio and video.

On the caller side, I'd like to change the audio input.

e.g. the User changes the audioinput from a dropdown list.

What's the workflow to substitute the audio track of an existing stream?

Can I add another audio track and make one active over the other? how?

Looks like I may need to call getUserMedia again passing constraints (?), which to my understanding es to create a New mediaStream instances and not modify the existing.

Share Improve this question edited Nov 9, 2016 at 13:36 zabumba asked Oct 3, 2016 at 12:07 zabumbazabumba 12.4k18 gold badges75 silver badges134 bronze badges 3
  • 1 Modify the MediaStream in an active peerconnection, the peerconnection object will fire an onnegotiationneeded event. Handle that event and re-exchange SDPs. – Sourav Ghosh Commented Oct 8, 2016 at 8:20
  • thx. Could you detail this in as an Answer below (sequence of functions to call). I think this will create a new MediaStream with a different ID right? So that wouldn't be changing the audio track of an existing MediaStream (keeping the same ID) but rather renegociating the connection with a new MediaStream that will contain the same video + a different audio track. – zabumba Commented Oct 9, 2016 at 23:26
  • stackoverflow./questions/56944864/… – bain Commented Feb 9, 2021 at 16:58
Add a ment  | 

2 Answers 2

Reset to default 3

There is now a much simpler API for this operation: RTCRtpSender.replaceTrack().

It could look something like this:

const currentSenders = peerConnection.getSenders();

const currentAudioSender = currentSenders.find((s) => s.track.kind === 'audio');

currentAudioSender.replaceTrack(newAudioTrack);

For us it looks something like this:

const replaceTrack = async (peerConnection, oldSender, track, stream) => {
  peerConnection.removeTrack(oldSender);

  const newSender = peerConnection.addTrack(track, stream);

  const localSdp = await peerConnection.createOffer({ offerToReceiveAudio: 1 });
  await peerConnection.setLocalDescription(reply);

  const response = await sendOffer(peerConnection.localDescription);

  const description = new RTCSessionDescription(response);
  peerConnection.setRemoteDescription(description);

  return newSender;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745533207a4631788.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信