Skip to content

Commit

Permalink
next-wip
Browse files Browse the repository at this point in the history
  • Loading branch information
steabert committed Dec 12, 2024
1 parent 0e6962d commit b03acc2
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 87 deletions.
2 changes: 1 addition & 1 deletion streams/src/components/aacdepay/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function parse(
}
const packet: ElementaryMessage = {
type: MessageType.ELEMENTARY,
data: Buffer.from(buffer.subarray(headerLength)),
data: new Uint8Array(buffer.subarray(headerLength)),
payloadType: payloadType(rtp.data),
timestamp: timestamp(rtp.data),
ntpTimestamp: rtp.ntpTimestamp,
Expand Down
7 changes: 4 additions & 3 deletions streams/src/components/basicdepay/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { concat } from 'utils/bytes'
import {
marker,
payload,
Expand All @@ -14,7 +15,7 @@ export class BasicDepay extends Tube {
throw new Error('you must supply a payload type to BasicDepayComponent')
}

let buffer = Buffer.alloc(0)
let buffer = new Uint8Array(0)

const incoming = createTransform(function (
msg: Message,
Expand All @@ -26,7 +27,7 @@ export class BasicDepay extends Tube {
payloadType(msg.data) === rtpPayloadType
) {
const rtpPayload = payload(msg.data)
buffer = Buffer.concat([buffer, rtpPayload])
buffer = concat([buffer, rtpPayload])

if (marker(msg.data)) {
if (buffer.length > 0) {
Expand All @@ -38,7 +39,7 @@ export class BasicDepay extends Tube {
type: MessageType.ELEMENTARY,
})
}
buffer = Buffer.alloc(0)
buffer = new Uint8Array(0)
}
callback()
} else {
Expand Down
5 changes: 3 additions & 2 deletions streams/src/components/h264depay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { VideoMedia } from '../../utils/protocols/sdp'
import { Tube } from '../component'
import { Message, MessageType } from '../message'

import { concat } from 'utils/bytes'
import { H264DepayParser, NAL_TYPES } from './parser'

export class H264Depay extends Tube {
constructor() {
let h264PayloadType: number
let idrFound = false
let packets: Buffer[] = []
let packets: Uint8Array[] = []

const h264DepayParser = new H264DepayParser()

Expand Down Expand Up @@ -59,7 +60,7 @@ export class H264Depay extends Tube {
if (endOfFrame) {
this.push({
...h264Message,
data: packets.length === 1 ? packets[0] : Buffer.concat(packets),
data: packets.length === 1 ? packets[0] : concat(packets),
})
packets = []
}
Expand Down
29 changes: 14 additions & 15 deletions streams/src/components/h264depay/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debug from 'debug'

import { concat } from 'utils/bytes'
import { payload, payloadType, timestamp } from '../../utils/protocols/rtp'
import { H264Message, MessageType, RtpMessage } from '../message'

Expand Down Expand Up @@ -30,10 +31,10 @@ First byte in payload (rtp payload header):
const h264Debug = debug('msl:h264depay')

export class H264DepayParser {
private _buffer: Buffer
private _buffer: Uint8Array

constructor() {
this._buffer = Buffer.alloc(0)
this._buffer = new Uint8Array(0)
}

parse(rtp: RtpMessage): H264Message | null {
Expand All @@ -48,17 +49,17 @@ export class H264DepayParser {
const nal = (fuIndicator & 0xe0) | nalType
const stopBit = fuHeader & 64
if (startBit) {
this._buffer = Buffer.concat([
Buffer.from([0, 0, 0, 0, nal]),
this._buffer = concat([
new Uint8Array([0, 0, 0, 0, nal]),
rtpPayload.slice(2),
])
return null
} else if (stopBit) {
/* receieved end bit */ const h264frame = Buffer.concat([
/* receieved end bit */ const h264frame = concat([
this._buffer,
rtpPayload.slice(2),
])
h264frame.writeUInt32BE(h264frame.length - 4, 0)
h264frame.set([h264frame.length - 4], 0)
const msg: H264Message = {
data: h264frame,
type: MessageType.H264,
Expand All @@ -67,21 +68,19 @@ export class H264DepayParser {
payloadType: payloadType(rtp.data),
nalType,
}
this._buffer = Buffer.alloc(0)
this._buffer = new Uint8Array(0)
return msg
}
// Put the received data on the buffer and cut the header bytes
this._buffer = Buffer.concat([this._buffer, rtpPayload.slice(2)])
this._buffer = concat([this._buffer, rtpPayload.slice(2)])
return null
} else if (
(type === NAL_TYPES.NON_IDR_PICTURE || type === NAL_TYPES.IDR_PICTURE) &&
this._buffer.length === 0
) {
/* Single NALU */ const h264frame = Buffer.concat([
Buffer.from([0, 0, 0, 0]),
rtpPayload,
])
h264frame.writeUInt32BE(h264frame.length - 4, 0)
/* Single NALU */
const h264frame = concat([new Uint8Array([0, 0, 0, 0]), rtpPayload])
h264frame.set([h264frame.length - 4], 0)
const msg: H264Message = {
data: h264frame,
type: MessageType.H264,
Expand All @@ -90,13 +89,13 @@ export class H264DepayParser {
payloadType: payloadType(rtp.data),
nalType: type,
}
this._buffer = Buffer.alloc(0)
this._buffer = new Uint8Array(0)
return msg
}
h264Debug(
`H264depayComponent can only extract types 1,5 and 28, got ${type}`
)
this._buffer = Buffer.alloc(0)
this._buffer = new Uint8Array(0)
return null
}
}
6 changes: 3 additions & 3 deletions streams/src/components/jpegdepay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { jpegDepayFactory } from './parser'
export class JPEGDepay extends Tube {
constructor() {
let jpegPayloadType: number
let packets: Buffer[] = []
let jpegDepay: (packets: Buffer[]) => {
let packets: Uint8Array[] = []
let jpegDepay: (packets: Uint8Array[]) => {
size: { width: number; height: number }
data: Buffer
data: Uint8Array
}

const incoming = new Transform({
Expand Down
8 changes: 4 additions & 4 deletions streams/src/components/jpegdepay/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readUInt8, readUInt16BE } from 'utils/bytes'
import { concat, readUInt8, readUInt16BE } from 'utils/bytes'
import { payload } from '../../utils/protocols/rtp'

import {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function jpegDepayFactory(defaultWidth = 0, defaultHeight = 0) {
const HUFFMAN_HEADER = makeHuffmanHeader()
const SCAN_HEADER = makeScanHeader()

return function jpegDepay(packets: Buffer[]) {
return function jpegDepay(packets: Uint8Array[]) {
let metadata
const fragments: Uint8Array[] = []
for (const packet of packets) {
Expand Down Expand Up @@ -118,13 +118,13 @@ export function jpegDepayFactory(defaultWidth = 0, defaultHeight = 0) {
const quantHeader = makeQuantHeader(precision, qTable)

const driHeader =
metadata.DRI === 0 ? Buffer.alloc(0) : makeDRIHeader(metadata.DRI)
metadata.DRI === 0 ? new Uint8Array(0) : makeDRIHeader(metadata.DRI)

const frameHeader = makeFrameHeader(width, height, type)

return {
size: { width, height },
data: Buffer.concat([
data: concat([
IMAGE_HEADER,
quantHeader,
driHeader,
Expand Down
25 changes: 13 additions & 12 deletions streams/src/components/mp4-parser/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import registerDebug from 'debug'

import { concat, readUInt32BE } from 'utils/bytes'
import { BOX_HEADER_BYTES, boxType } from '../../utils/protocols/isom'
import { IsomMessage, MessageType } from '../message'
import { Container } from '../mp4muxer/helpers/isom'
Expand All @@ -20,19 +21,19 @@ interface Mp4BoxInfo {
* Extract type and size information from the box header
* (8-byte section at beginning of the box).
*/
const mp4BoxInfo = (chunks: Buffer[]): Mp4BoxInfo => {
const header = Buffer.alloc(BOX_HEADER_BYTES)
const mp4BoxInfo = (chunks: Uint8Array[]): Mp4BoxInfo => {
const header = new Uint8Array(BOX_HEADER_BYTES)
let i = 0
let bytesRead = 0

while (bytesRead < header.length) {
const chunk = chunks[i++]
const bytesToRead = Math.min(chunk.length, header.length - bytesRead)
chunk.copy(header, bytesRead, 0, bytesToRead)
header.set(chunk.subarray(0, bytesToRead), bytesRead)
bytesRead += bytesToRead
}

const size = header.readUInt32BE(0)
const size = readUInt32BE(header, 0)
const type = boxType(header)

return { type, size }
Expand All @@ -46,10 +47,10 @@ const mp4BoxInfo = (chunks: Buffer[]): Mp4BoxInfo => {
* @type {[type]}
*/
export class Parser {
private _chunks: Buffer[] = []
private _chunks: Uint8Array[] = []
private _length = 0
private _box?: Mp4BoxInfo
private _ftyp?: Buffer
private _ftyp?: Uint8Array

/**
* Create a new Parser object.
Expand All @@ -67,7 +68,7 @@ export class Parser {
this._length = 0
}

_push(chunk: Buffer): void {
_push(chunk: Uint8Array): void {
this._chunks.push(chunk)
this._length += chunk.length
}
Expand All @@ -76,7 +77,7 @@ export class Parser {
* Extract MP4 boxes.
* @return {Array} An array of messages, possibly empty.
*/
_parseBox(): Buffer | null {
_parseBox(): Uint8Array | null {
// Skip as long as we don't have the first 8 bytes
if (this._length < BOX_HEADER_BYTES) {
return null
Expand All @@ -96,7 +97,7 @@ export class Parser {
// The buffer package has a problem that it doesn't optimize concatenation
// of an array with only one buffer, check for that (prevents performance issue)
const buffer =
this._chunks.length === 1 ? this._chunks[0] : Buffer.concat(this._chunks)
this._chunks.length === 1 ? this._chunks[0] : concat(this._chunks)
const box = buffer.slice(0, this._box.size)
const trailing = buffer.slice(this._box.size)

Expand All @@ -109,7 +110,7 @@ export class Parser {
console.warn(
`ignored non-ISO BMFF Byte Stream box type: ${this._box.type} (${this._box.size} bytes)`
)
return Buffer.alloc(0)
return new Uint8Array(0)
}

delete this._box
Expand All @@ -124,7 +125,7 @@ export class Parser {
* @param chunk - The next piece of data.
* @return An array of messages, possibly empty.
*/
parse(chunk: Buffer): IsomMessage[] {
parse(chunk: Uint8Array): IsomMessage[] {
this._push(chunk)

const messages: IsomMessage[] = []
Expand All @@ -142,7 +143,7 @@ export class Parser {
debug('MP4 tracks: ', tracks)
messages.push({
type: MessageType.ISOM,
data: Buffer.concat([this._ftyp ?? Buffer.alloc(0), data]),
data: concat([this._ftyp ?? new Uint8Array(0), data]),
tracks,
})
} else {
Expand Down
2 changes: 1 addition & 1 deletion streams/src/components/mp4muxer/helpers/boxbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class BoxBuilder {
* @param data - Elementary stream data
* @return mdat Box
*/
mdat(data: Buffer) {
mdat(data: Uint8Array) {
const box = new Box('mdat')
box.add('data', data)
return box
Expand Down
2 changes: 1 addition & 1 deletion streams/src/components/mp4muxer/helpers/bufferreader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class BufferReader {
private _bitpos: number
private _byte: number

constructor(buffer: Buffer) {
constructor(buffer: Uint8Array) {
this._buffer = buffer
this._dataView = new DataView(this._buffer)
this._offset = 0
Expand Down
Loading

0 comments on commit b03acc2

Please sign in to comment.