or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

captions.mdcodecs.mdflv.mdindex.mdmp2t.mdmp4.mdpartial.mdtools.md

mp2t.mddocs/

0

# Transport Stream Processing

1

2

MPEG-2 Transport Stream parsing and processing capabilities. The mp2t module handles packet parsing, PES reconstruction, metadata extraction, and caption processing from transport streams used in broadcast and streaming applications.

3

4

## Capabilities

5

6

### Transport Packet Stream

7

8

Stream class that splits incoming binary data into MPEG-2 Transport Stream packets.

9

10

```javascript { .api }

11

/**

12

* Splits binary data into MPEG-2 Transport Stream packets

13

* Handles sync byte detection and packet boundary identification

14

*/

15

class TransportPacketStream {

16

constructor();

17

18

/** Process binary transport stream data */

19

push(data: Uint8Array): void;

20

21

/** Flush any remaining packet data */

22

flush(): void;

23

24

/** End current timeline */

25

endTimeline(): void;

26

27

/** Reset stream state */

28

reset(): void;

29

30

/** Register event listeners */

31

on(event: 'data', callback: (packet: Uint8Array) => void): void;

32

on(event: 'done', callback: () => void): void;

33

}

34

```

35

36

**Usage Examples:**

37

38

```javascript

39

const muxjs = require("mux.js");

40

41

// Create transport packet stream

42

const packetStream = new muxjs.mp2t.TransportPacketStream();

43

44

// Handle parsed packets

45

packetStream.on('data', (packet) => {

46

console.log(`Transport packet: ${packet.length} bytes`);

47

// Each packet is exactly 188 bytes (MP2T_PACKET_LENGTH)

48

49

// Process individual packet

50

processTransportPacket(packet);

51

});

52

53

// Process raw transport stream data

54

packetStream.push(transportStreamData);

55

packetStream.flush();

56

```

57

58

### Transport Parse Stream

59

60

Stream class that parses transport stream packets into structured data with PAT/PMT information.

61

62

```javascript { .api }

63

/**

64

* Parses transport packets into structured data

65

* Extracts PAT (Program Association Table) and PMT (Program Map Table) information

66

*/

67

class TransportParseStream {

68

constructor();

69

70

/** Parse transport packet */

71

push(packet: Uint8Array): void;

72

73

/** Program Map Table PID */

74

pmtPid: number;

75

76

/** Program map table information */

77

programMapTable: {

78

video: number | null;

79

audio: number | null;

80

'timed-metadata': { [pid: number]: number };

81

};

82

83

/** Register event listeners */

84

on(event: 'data', callback: (parsedPacket: ParsedPacket) => void): void;

85

}

86

87

interface ParsedPacket {

88

pid: number;

89

type: 'pat' | 'pmt' | 'pes';

90

payloadUnitStartIndicator: boolean;

91

data?: Uint8Array;

92

streamType?: number;

93

programMapTable?: ProgramMapTable;

94

}

95

96

interface ProgramMapTable {

97

video: number | null;

98

audio: number | null;

99

'timed-metadata': { [pid: number]: number };

100

}

101

```

102

103

**Usage Examples:**

104

105

```javascript

106

const muxjs = require("mux.js");

107

108

// Create parse stream

109

const parseStream = new muxjs.mp2t.TransportParseStream();

110

111

// Handle parsed packets

112

parseStream.on('data', (parsed) => {

113

console.log(`Packet PID: ${parsed.pid}, Type: ${parsed.type}`);

114

115

switch (parsed.type) {

116

case 'pat':

117

console.log('Program Association Table');

118

break;

119

case 'pmt':

120

console.log('Program Map Table:', parsed.programMapTable);

121

break;

122

case 'pes':

123

console.log(`PES packet, stream type: ${parsed.streamType}`);

124

break;

125

}

126

});

127

128

// Connect to packet stream

129

const packetStream = new muxjs.mp2t.TransportPacketStream();

130

packetStream.pipe(parseStream);

131

```

132

133

### Elementary Stream

134

135

Stream class that reconstructs Program Elementary Stream (PES) packets from parsed transport stream packets.

136

137

```javascript { .api }

138

/**

139

* Reconstructs PES packets from transport stream packets

140

* Handles video, audio, and timed metadata streams

141

*/

142

class ElementaryStream {

143

constructor();

144

145

/** Process parsed transport data */

146

push(data: ParsedPacket): void;

147

148

/** Flush buffered streams */

149

flush(): void;

150

151

/** Reset stream state */

152

reset(): void;

153

154

/** Register event listeners */

155

on(event: 'data', callback: (pesPacket: PESPacket) => void): void;

156

}

157

158

interface PESPacket {

159

type: 'video' | 'audio' | 'timed-metadata' | 'metadata';

160

data?: Uint8Array;

161

pts?: number;

162

dts?: number;

163

packetLength?: number;

164

dataAlignmentIndicator?: boolean;

165

trackId?: number;

166

tracks?: Track[];

167

}

168

```

169

170

**Usage Examples:**

171

172

```javascript

173

const muxjs = require("mux.js");

174

175

// Create elementary stream

176

const elementaryStream = new muxjs.mp2t.ElementaryStream();

177

178

// Handle PES packets

179

elementaryStream.on('data', (pesPacket) => {

180

console.log(`PES packet type: ${pesPacket.type}`);

181

182

if (pesPacket.pts !== undefined) {

183

console.log(`PTS: ${pesPacket.pts}`);

184

}

185

if (pesPacket.dts !== undefined) {

186

console.log(`DTS: ${pesPacket.dts}`);

187

}

188

189

// Process based on type

190

switch (pesPacket.type) {

191

case 'video':

192

processVideoPacket(pesPacket);

193

break;

194

case 'audio':

195

processAudioPacket(pesPacket);

196

break;

197

case 'metadata':

198

console.log('Track metadata:', pesPacket.tracks);

199

break;

200

}

201

});

202

203

// Create full pipeline

204

const packetStream = new muxjs.mp2t.TransportPacketStream();

205

const parseStream = new muxjs.mp2t.TransportParseStream();

206

207

packetStream

208

.pipe(parseStream)

209

.pipe(elementaryStream);

210

```

211

212

### Caption Stream Processing

213

214

Comprehensive caption processing for CEA-608 and CEA-708 closed captions.

215

216

```javascript { .api }

217

/**

218

* Main caption stream processor

219

*/

220

class CaptionStream {

221

constructor();

222

push(data: Uint8Array): void;

223

flush(): void;

224

on(event: 'data', callback: (caption: CaptionData) => void): void;

225

}

226

227

/**

228

* CEA-608 closed caption processor

229

*/

230

class Cea608Stream {

231

constructor();

232

push(data: CaptionData): void;

233

flush(): void;

234

on(event: 'data', callback: (caption: Cea608Caption) => void): void;

235

}

236

237

/**

238

* CEA-708 closed caption processor

239

*/

240

class Cea708Stream {

241

constructor();

242

push(data: CaptionData): void;

243

flush(): void;

244

on(event: 'data', callback: (caption: Cea708Caption) => void): void;

245

}

246

247

interface CaptionData {

248

pts: number;

249

dts: number;

250

data: Uint8Array;

251

stream: string;

252

}

253

254

interface Cea608Caption {

255

startTime: number;

256

endTime: number;

257

text: string;

258

line: number;

259

position: number;

260

}

261

262

interface Cea708Caption {

263

startTime: number;

264

endTime: number;

265

text: string;

266

windowId: number;

267

serviceNumber: number;

268

}

269

```

270

271

### Metadata Stream Processing

272

273

Stream processor for extracting timed metadata from transport streams.

274

275

```javascript { .api }

276

/**

277

* Metadata stream processor for ID3 and other timed metadata

278

*/

279

class MetadataStream {

280

constructor();

281

282

/** Process metadata PES packet */

283

push(data: PESPacket): void;

284

285

/** Flush any buffered metadata */

286

flush(): void;

287

288

/** Register event listeners */

289

on(event: 'data', callback: (metadata: MetadataFrame) => void): void;

290

}

291

292

interface MetadataFrame {

293

pts: number;

294

dts: number;

295

data: Uint8Array;

296

frames: ID3Frame[];

297

}

298

299

interface ID3Frame {

300

id: string;

301

data: Uint8Array;

302

description?: string;

303

}

304

```

305

306

### Timestamp Rollover Stream

307

308

Utility stream for handling PTS/DTS timestamp rollover scenarios in transport streams.

309

310

```javascript { .api }

311

/**

312

* Handles timestamp rollover for PTS/DTS values

313

* PTS/DTS are 33-bit values that can rollover

314

*/

315

class TimestampRolloverStream {

316

constructor();

317

318

/** Process data with timestamp correction */

319

push(data: TimestampedData): void;

320

321

/** Flush stream */

322

flush(): void;

323

324

/** Register event listeners */

325

on(event: 'data', callback: (correctedData: TimestampedData) => void): void;

326

}

327

328

interface TimestampedData {

329

pts?: number;

330

dts?: number;

331

data: Uint8Array;

332

type: string;

333

}

334

```

335

336

## Constants

337

338

Transport stream processing constants and identifiers.

339

340

```javascript { .api }

341

/** Program Association Table PID */

342

const PAT_PID: 0x0000;

343

344

/** Standard MPEG-2 Transport Stream packet length */

345

const MP2T_PACKET_LENGTH: 188;

346

347

/** H.264 video stream type identifier */

348

const H264_STREAM_TYPE: 0x1B;

349

350

/** ADTS/AAC audio stream type identifier */

351

const ADTS_STREAM_TYPE: 0x0F;

352

353

/** Metadata stream type identifier */

354

const METADATA_STREAM_TYPE: 0x15;

355

```

356

357

**Usage Examples:**

358

359

```javascript

360

const muxjs = require("mux.js");

361

362

// Use constants for stream type checking

363

elementaryStream.on('data', (pesPacket) => {

364

if (pesPacket.type === 'pes' && pesPacket.streamType) {

365

switch (pesPacket.streamType) {

366

case muxjs.mp2t.H264_STREAM_TYPE:

367

console.log('H.264 video stream');

368

break;

369

case muxjs.mp2t.ADTS_STREAM_TYPE:

370

console.log('ADTS audio stream');

371

break;

372

case muxjs.mp2t.METADATA_STREAM_TYPE:

373

console.log('Metadata stream');

374

break;

375

}

376

}

377

});

378

379

// Check packet length

380

console.log(`Expected packet length: ${muxjs.mp2t.MP2T_PACKET_LENGTH} bytes`);

381

```

382

383

## Complete Processing Pipeline

384

385

Example showing full transport stream processing pipeline:

386

387

```javascript

388

const muxjs = require("mux.js");

389

390

// Create processing pipeline

391

const packetStream = new muxjs.mp2t.TransportPacketStream();

392

const parseStream = new muxjs.mp2t.TransportParseStream();

393

const elementaryStream = new muxjs.mp2t.ElementaryStream();

394

const captionStream = new muxjs.mp2t.CaptionStream();

395

const metadataStream = new muxjs.mp2t.MetadataStream();

396

const timestampRollover = new muxjs.mp2t.TimestampRolloverStream();

397

398

// Connect pipeline

399

packetStream

400

.pipe(parseStream)

401

.pipe(elementaryStream);

402

403

// Handle different data types

404

elementaryStream.on('data', (pesPacket) => {

405

switch (pesPacket.type) {

406

case 'video':

407

case 'audio':

408

// Pass through timestamp rollover correction

409

timestampRollover.push(pesPacket);

410

break;

411

case 'timed-metadata':

412

metadataStream.push(pesPacket);

413

break;

414

}

415

});

416

417

// Handle corrected timestamps

418

timestampRollover.on('data', (correctedData) => {

419

console.log('Corrected timestamps:', correctedData.pts, correctedData.dts);

420

});

421

422

// Handle metadata

423

metadataStream.on('data', (metadata) => {

424

console.log('Metadata frames:', metadata.frames.length);

425

metadata.frames.forEach(frame => {

426

console.log(`ID3 frame: ${frame.id}`);

427

});

428

});

429

430

// Process transport stream

431

packetStream.push(transportStreamData);

432

packetStream.flush();

433

```

434

435

## Types

436

437

```javascript { .api }

438

interface Track {

439

id: number;

440

codec: string;

441

type: 'video' | 'audio';

442

timelineStartInfo: {

443

baseMediaDecodeTime: number;

444

};

445

}

446

447

interface TransportPacket {

448

pid: number;

449

payloadUnitStartIndicator: boolean;

450

data: Uint8Array;

451

}

452

453

interface StreamTypeMap {

454

[pid: number]: number;

455

}

456

```