Reputation: 21
I'm developing a Chrome extension where I want to capture audio & microphone input from a browser tab, send this stream to a transcription service (Azure in my case), and simultaneously play it through the system's speakers. However, I'm facing issues with playing the tab audio through the speakers, and enabling audio playback interferes with the transcription service, possibly creating a feedback loop.
**Transcription is working perfectly fine for both audio and input given from microphone.
tabAudioMediaStream
), there's no sound output.Here's the relevant part of my code:
To get the Media StreamId in background.js
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (tabs.length > 0) {
chrome.tabCapture.getMediaStreamId({
targetTabId: tabs[0].id,
consumerTabId: tabs[0].id
}).then(tabStreamId => {
console.log("StreamId: ", tabStreamId);
chrome.tabs.sendMessage(tabs[0].id, {type: "tabStreamId", tabStreamId});
})
}
});
MediaStreamId is then passed to content script which then capture the microphone stream and then passes to azure transcription service. Here's the relevant code.
const constraints = {
audio: { echoCancellation: true },
};
navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: "tab",
chromeMediaSourceId: tabStreamId,
},
},
video: false
}).then(tabAudioMediaStream => {
// continueToPlayCapturedAudio(tabAudioMediaStream.clone());
if (tabAudioMediaStream.getAudioTracks().length === 0) {
console.error("No audio tracks in tab stream");
return;
}
// Attempt to play audio directly - this did not produce sound
// let audio = new Audio();
// audio.srcObject = tabAudioMediaStream;
// audio.play();
let tabAudioMediaSourceNode = audioContext.createMediaStreamSource(tabAudioMediaStream);
// I expected this connection to play the audio through the speakers, but it's not working instead affecting transcription service response
// tabAudioMediaSourceNode.connect(audioContext.destination);
navigator.mediaDevices.getUserMedia(constraints)
.then(micAudioMediaStream => {
// Handle microphone audio stream
let micAudioMediaSourceNode = audioContext.createMediaStreamSource(micAudioMediaStream);
return micAudioMediaSourceNode
}).then(micAudioMediaSourceNode => {
tabAudioMediaSourceNode.connect(destination);
micAudioMediaSourceNode.connect(destination);
output = new MediaStream();
output.addTrack(destination.stream.getAudioTracks()[0]);
// start the transcription service using the stream
startAzureTranscriptionService(output);
})
})
continueToPlayCapturedAudio()
manifest.js permission. I am using MV3
"permissions": [
"storage",
"identity",
"tabs",
"tabCapture",
"activeTab"
],
I am seeking guidance on the following:
Expecting the audio to be played through the speakers. Tried couple of approaches but none of them worked instead started affecting response of transcription service.
Upvotes: 1
Views: 207