Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to extract any NAL units from video data: #124

Open
Ascol57 opened this issue Oct 28, 2022 · 2 comments
Open

Failed to extract any NAL units from video data: #124

Ascol57 opened this issue Oct 28, 2022 · 2 comments

Comments

@Ascol57
Copy link

Ascol57 commented Oct 28, 2022

Hello.
I don't really good obtain the packet.
Wow who obtain the duration in byte ?

My server.js : `const WebSocket = require('ws');
const fs = require('fs');
const { exec } = require('child_process');
var iconvlite = require('iconv-lite');

const express = require('express')
const app = express()
const port = 3000

app.use(express.static('public'))

app.listen(port, () => {
console.log(Example app listening on port ${port})
})

const PORT = process.env.PORT || 8080;
let minNaluPerChunk = 30,
interval = 0,
current = 0,
start = 0,
end = 0,
wss;
function extractChunks(buffer) {
let i = 0,
length = buffer.byteLength,
naluCount = 0,
value,
unit,
ntype,
state = 0,
lastIndex = 0,
result = [];

while (i < length) {
    value = buffer[i++];
    // finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)
    switch (state) {
        case 0:
            if (value === 0) {
                state = 1;
            }
            break;
        case 1:
            if (value === 0) {
                state = 2;
            } else {
                state = 0;
            }
            break;
        case 2:
        case 3:
            if (value === 0) {
                state = 3;
            } else if (value === 1 && i < length) {
                if (lastIndex) {
                    unit = buffer.slice(lastIndex, i - state -1);
                    ntype = unit[0] & 0x1f;
                    naluCount++;
                }
                if (naluCount >= minNaluPerChunk && ntype !== 1 && ntype !== 5) {
                    result.push(lastIndex - state -1);
                    naluCount = 0;
                }
                state = 0;
                lastIndex = i;
            } else {
                state = 0;
            }
            break;
        default:
            break;
    }
}
if (naluCount > 0) {
    result.push(lastIndex);
}
return result;

}

let buffer = fs.readFileSync('./demo.h264');
let chunks = extractChunks(buffer);
let total = chunks.length;

function openSocket() {
wss = new WebSocket.Server({ port: PORT });
console.log('Server ready on port '+PORT);
wss.on('connection', function connection(ws) {
console.log('Socket connected. sending data...');
if (interval) {
clearInterval(interval);
}
ws.on('error', function error(error) {
console.log('WebSocket error');
});
ws.on('close', function close(msg) {
console.log('WebSocket close');
});

      interval = setInterval(function() {
        sendChunk();
      }, 800);
});

}

function writeChunk() {
for (i = 0; i < chunks.length; i++) {
end = chunks[i];
chunk = buffer.slice(start, end);
start = end;
if (i == 3) {
console.log(chunk)
}
fs.writeFile("./h264/" + i + '.h264', chunk, "binary", function(err) {
if(err) {
console.log(err);
exec('touch h264/' + i + '.h264');
fs.writeFile("h264/" + i + '.h264', chunk, "binary", function(err) {
if(err) {
console.log(err);
}
});
}
});
}
}
writeChunk();

function sendChunk() {
let anybodyThere = false;
if (current >= total) {
current = 0;
start = 0;
}
end = chunks[current];
current++;
wss.clients.forEach(function each(client) {
let chunk;
if (client.readyState === WebSocket.OPEN) {
anybodyThere = true;
chunk = buffer.slice(start, end);
start = end;
try {
client.send(JSON.stringify({data : chunk, type : "v"}));
} catch(e) {
console.log(Sending failed:, e);
}
if (current % 50 == 0) {
console.log(I am serving, no problem!);
}
if (current == 0) {
console.log(Started from first chunk...);
}
}
});

if (!anybodyThere) {
    if (interval) {
        current = start = end = 0;
        clearInterval(interval);
        console.log('nobody is listening. Removing interval for now...');
    }
}

}
openSocket();`

My index.html : `

<title>JMuxer demo</title>

Prerequisite command: `node h264.js`

<script> var jmuxer; window.onload = function() { var socketURL = 'ws://pierrerusche.com:8080'; jmuxer = new JMuxer({ node: 'player', mode: 'video', flushingTime: 800, fps: 30, debug: true, onError: function(data) { if (/Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor)) { jmuxer.reset(); } } });
 var ws = new WebSocket(socketURL);
 ws.binaryType = 'arraybuffer';
 ws.addEventListener('message',function(event) {
    data = JSON.parse(event.data)
    console.log(data)
    if (data.type == "v") {
        jmuxer.feed({
            video: new Uint8Array(data.data)
        });
    }
 });

 ws.addEventListener('error', function(e) {
    console.log('Socket Error');
 });

}

</script> <script type="text/javascript" src="jmuxer.min.js"></script> `
@dtbaker
Copy link

dtbaker commented Feb 9, 2023

did you figure this out @Ascol14 ?

@puchunbing
Copy link

Hello, have you solved this problem? I am also encountering this bug now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants