example/server-h264.js
// offsets to nal units other than type 1 AND 5
offsets to nal units other than type 1 AND 5
offsets to nal units other than type 1 AND 5
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++];
        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;
}
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 ++ ] ; 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 ; }
offsets to nal units other than type 1 AND 5
offsets to nal units other than type 1 AND 5
// finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)