Is it possible to play a wav as a local stream input instead of using microphone

510 views
Skip to first unread message

Bharath

unread,
Aug 30, 2021, 1:49:15 PM8/30/21
to SIP.js
Hi All,

I'm using SIPjs version 0.20. I'm trying to use a recorded audio file as an local media stream input instead of a default microphone. Can someone help me how to achieve this ?

Bharath

unread,
Sep 5, 2021, 1:49:01 AM9/5/21
to SIP.js
I tried to achieve stream a .wav/mp3 file audio instead of microphone input using the below code. Although I could hear the .wav file audio in my microphone speaker, it is not heard at the peer/remote user end. I think I'm missing something here. It would really appreciate it if someone can tell me where I'm going wrong. Thanks

// Handle outgoing session state changes.
outgoingSession.stateChange.addListener((newState) => {
console.log(`Session state changed to ${newState}`);
switch (newState) {
case SessionState.Establishing:
// Session is establishing.
break;
case SessionState.Established: {
// Session has been established.

const context = new AudioContext();
const gainNode = context.createGain();
gainNode.connect(context.destination);
// don't play for self
gainNode.gain.value = 0;

let audioFile = fetch("sample_utterance.wav").then(response => response.arrayBuffer()).then(buffer => context.decodeAudioData(buffer)).then(buffer => {
let track = context.createBufferSource();
track.buffer = buffer;
track.start(0, 0 / 1000);
let destination = context.createMediaStreamDestination();
track.connect(destination);
track.connect(context.destination);

outgoingSession.sessionDescriptionHandler.setLocalMediaStream(track).then( response => {
console.log(response);
}).catch(error => {
console.log(error);
});

this.setupRemoteMedia(outgoingSession);
break;
}
case SessionState.Terminated:
this.cleanupMedia();
// Session has terminated.
break;
default:
break;
}
});

Bharath

unread,
Sep 6, 2021, 7:39:02 PM9/6/21
to SIP.js
I manage to achieve it using the below code. This logic would help us play a .wav audio file to peer/remote connection by replacing microphone input


outgoingSession.stateChange.addListener((newState) => {
console.log(`Session state changed to ${newState}`);
switch (newState) {
case SessionState.Establishing:
// Session is establishing.
break;
case SessionState.Established: {
const context = new AudioContext();
const gainNode = context.createGain();
gainNode.connect(context.destination);

// don't play for self
gainNode.gain.value = 0;

let audioFile = fetch("sample.wav").then(response => response.arrayBuffer()).then(buffer => context.decodeAudioData(buffer)).then(buffer => {

let track = context.createBufferSource();
track.buffer = buffer;
track.start(0, 0 / 1000);
track.connect(gainNode);

let destination = context.createMediaStreamDestination();
track.connect(destination);
track.connect(context.destination);
outgoingSession.sessionDescriptionHandler.setLocalMediaStream(destination.stream);


this.setupRemoteMedia(outgoingSession);
break;
}
case SessionState.Terminated:
this.cleanupMedia();
// Session has terminated.
break;
default:
break;
}
})

Reply all
Reply to author
Forward
0 new messages