CritIN
CritIN

Reputation: 1

Why does the audio output become pitched when the laptop is on battery while using chrome.tabCapture.capture?

I am facing this issue with most laptops. When I turn my extension on while on battery power, the tab audio I receive becomes pitched. When I connect the charger back, it returns to normal.


const startCapture = async (socket) => {
    const constraints = {
        audio: true,
        video: false
    };

    chrome.tabCapture.capture(constraints, (capturedStream) => {
        if (chrome.runtime.lastError) {
            console.error("Error capturing tab:", chrome.runtime.lastError.message);
            return;
        }

        if (!capturedStream) {
            console.error("Error: Stream is null.");
            return;
        }

        console.log("Tab capture started successfully at", new Date().toISOString());

        const stream = capturedStream;

        // Play the audio using a native Audio element
        const audioElement = new Audio();
        audioElement.srcObject = stream;
        audioElement.volume = 1.0; // Ensure volume is set to maximum
        audioElement.muted = false; // Ensure audio is not muted

        audioElement.addEventListener('canplay', () => {
            console.log("Audio element can play at", new Date().toISOString());
            audioElement.play().then(() => {
                console.log("Audio is playing at", new Date().toISOString());
            }).catch((error) => {
                console.error("Error playing audio at", new Date().toISOString(), error);
            });
        });

        audioElement.addEventListener('error', (error) => {
            console.error("Audio element error at", new Date().toISOString(), error);
        });

        const recorderOptions = {
            mimeType: 'audio/webm',
            audioBitsPerSecond: 320000 // Ensure high bitrate
        };

        const recorder = new MediaRecorder(stream, recorderOptions);

        recorder.start(1500);

        recorder.addEventListener('dataavailable', evt => {
            if (evt.data.size > 0 && socket.readyState === WebSocket.OPEN) {
                audioData.push(evt.data);
                console.log('Sending Data at', new Date().toISOString(), evt.data);
                socket.send(evt.data);
                console.log('Sent Data at', new Date().toISOString(), evt.data);
            }
        });
    });
};

When I comment out this function, the pitched audio problem does not occur. I have tried changing the browser performance settings and Windows settings, monitored the CPU and memory utilization, optimized this code, and used chrome.tabCapture.getMediaStreamId instead of chrome.tabCapture.capture, but none of these attempts have resolved the issue

Upvotes: 0

Views: 21

Answers (0)

Related Questions