samirkumardas / opus-to-pcm

Decode raw opus packet to PCM without using any external library in your browser.
Other
73 stars 15 forks source link

Opus decoder.on("decode",... never executed. #23

Open el07694 opened 3 years ago

el07694 commented 3 years ago

server.html

<!doctype html>
<html>
    <head>
        <script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
    </head>
    <body>
        <script>
            var peer = new Peer("epalxeis");

            var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
            peer.on('call', function(call) {
              getUserMedia({video: false, audio: true}, function(stream) {
                call.answer(stream); // Answer the call with an A/V stream.
                call.on('stream', function(remoteStream) {
                  // Show stream in some video/canvas element.
                });
              }, function(err) {
                console.log('Failed to get local stream' ,err);
              });
            });

        </script>

    </body>
</html>

client.html

<!doctype html>
<html>
    <head>

        <script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
        <script type="text/javascript" src="dist/opus_to_pcm.js"></script>
    <script type="text/javascript" src="example/player/pcm_player.js"></script>
    </head>
    <body>
        <script>
            var decoder;

            var peer = new Peer();
            var record = false;
            var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
            getUserMedia({video: false, audio: true}, function(stream) {
              var call = peer.call('epalxeis', stream);
              call.on('stream', function(remoteStream) {
                // Show stream in some video/canvas element.
                if(record==false){
                    var audio = document.createElement("audio");
                    audio.srcObject = remoteStream;
                    document.body.appendChild(audio);
                    audio.play();
                    audio.muted = true;
                    var epalxeis_audio = document.getElementById("epalxeis_audio");
                    record=true;
                    var options = {mimeType: "audio/webm;codecs=opus"};
                    var recorder = new MediaRecorder(remoteStream,options);
                    // fires every one second and passes an BlobEvent

                    recorder.ondataavailable = event => {

                        // get the Blob from the event
                        var blob = event.data;

                        decoder = new OpusToPCM({
                          channels: 1,
                          fallback: false,
                        });

                        // Assign function to decode callback
                        decoder.on('decode', function(pcmData) {

                            console.log("123");

                        });

                        // New file 
                        var reader = new FileReader();

                        // Assign callback
                        reader.onload = function(){
                            var audioBuffer = new Int16Array(reader.result);
                            decoder.decode(audioBuffer);
                        }

                        // Read data into file reader (will start the onload function above)
                        reader.readAsArrayBuffer(event.data);

                    };

                    // make data available event fire every one second
                    recorder.start(5000);
                }

              });
            }, function(err) {
              console.log('Failed to get local stream' ,err);
            });

        </script>

    </body>
    <audio id="epalxeis_audio"></audio>

</html>

Problem: decoder.on("decode",... callback function never executed.

samirkumardas commented 3 years ago

It expects a raw opus packet. I don't think MediaRecorder returns the raw opus packet. You can try using decodeAudioData directly