Curious66
Curious66

Reputation: 11

How to capture incoming audio streams in google meet

I'm trying to build a bot that will capture the incoming video and audio streams from google meetings.

I was using chatgpt to help me with this and I managed to get to the point where I can download the video recordings but no matter what I try, I can't seem to find how/where audio is streamed.

Here is the snipped I used to download incoming video streams:

// Object to store active recorders by stream ID
const recorders = {};

// Override setRemoteDescription method to intercept incoming tracks
const originalSetRemoteDescription = RTCPeerConnection.prototype.setRemoteDescription;
RTCPeerConnection.prototype.setRemoteDescription = async function(desc) {
    const result = await originalSetRemoteDescription.call(this, desc).catch(console.error);
    // Handle incoming tracks
    this.ontrack = (event) => {
        console.log('ontrack event:', event);
        const streamId = event.streams[0].id; // Unique ID for each stream
        if (!recorders[streamId]) {
            // If recorder for this stream does not exist, create a new one
            const recorder = createRecorder(event.streams[0], streamId);
            recorders[streamId] = recorder;
        }
    };
    return result;
};

// Function to create a MediaRecorder and handle recording logic
function createRecorder(stream, id) {
    const recorder = new MediaRecorder(stream);
    const chunks = [];
    recorder.ondataavailable = event => {
        if (event.data.size > 0) {
            chunks.push(event.data);
        }
    };
    recorder.onstop = () => {
        const blob = new Blob(chunks, { type: 'video/webm' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = `${id}__${new Date().toISOString()}.webm`;
        document.body.appendChild(a);
        a.click();
        setTimeout(() => {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 100);
    };
    recorder.start();
    return recorder;
}

// To stop all recordings:
function stopAllRecordings() {
    Object.values(recorders).forEach(recorder => {
        recorder.stop();
        delete recorders[recorder.streamId];
    });
}

Any help in this would be amazing, additionally if anyone could point me in a direction where I could learn more about this stuff would also be nice.

Upvotes: 1

Views: 578

Answers (0)

Related Questions