Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
completer.completeError('Engine disconnected');
}
}

events.once<EngineClosingEvent>((e) => onClosing());

while (!_dcBufferStatus[kind]!) {
Expand Down
128 changes: 19 additions & 109 deletions web/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,103 +8,10 @@ import 'package:web/web.dart' as web;
import 'e2ee.keyhandler.dart';
import 'e2ee.logger.dart';
import 'e2ee.sfi_guard.dart';
import 'e2ee.nalu_utils.dart';

const IV_LENGTH = 12;

const kNaluTypeMask = 0x1f;

/// Coded slice of a non-IDR picture
const SLICE_NON_IDR = 1;

/// Coded slice data partition A
const SLICE_PARTITION_A = 2;

/// Coded slice data partition B
const SLICE_PARTITION_B = 3;

/// Coded slice data partition C
const SLICE_PARTITION_C = 4;

/// Coded slice of an IDR picture
const SLICE_IDR = 5;

/// Supplemental enhancement information
const SEI = 6;

/// Sequence parameter set
const SPS = 7;

/// Picture parameter set
const PPS = 8;

/// Access unit delimiter
const AUD = 9;

/// End of sequence
const END_SEQ = 10;

/// End of stream
const END_STREAM = 11;

/// Filler data
const FILLER_DATA = 12;

/// Sequence parameter set extension
const SPS_EXT = 13;

/// Prefix NAL unit
const PREFIX_NALU = 14;

/// Subset sequence parameter set
const SUBSET_SPS = 15;

/// Depth parameter set
const DPS = 16;

// 17, 18 reserved

/// Coded slice of an auxiliary coded picture without partitioning
const SLICE_AUX = 19;

/// Coded slice extension
const SLICE_EXT = 20;

/// Coded slice extension for a depth view component or a 3D-AVC texture view component
const SLICE_LAYER_EXT = 21;

// 22, 23 reserved

List<int> findNALUIndices(Uint8List stream) {
var result = <int>[];
var start = 0, pos = 0, searchLength = stream.length - 2;
while (pos < searchLength) {
// skip until end of current NALU
while (pos < searchLength &&
!(stream[pos] == 0 && stream[pos + 1] == 0 && stream[pos + 2] == 1)) {
pos++;
}
if (pos >= searchLength) pos = stream.length;
// remove trailing zeros from current NALU
var end = pos;
while (end > start && stream[end - 1] == 0) {
end--;
}
// save current NALU
if (start == 0) {
if (end != start) throw Exception('byte stream contains leading data');
} else {
result.add(start);
}
// begin new NALU
start = pos = pos + 3;
}
return result;
}

int parseNALUType(int startByte) {
return startByte & kNaluTypeMask;
}

enum CryptorError {
kNew,
kOk,
Expand Down Expand Up @@ -289,24 +196,27 @@ class FrameCryptor {
}
}

if (codec != null && codec.toLowerCase() == 'h264') {
var naluIndices = findNALUIndices(data);
for (var index in naluIndices) {
var type = parseNALUType(data[index]);
switch (type) {
case SLICE_IDR:
case SLICE_NON_IDR:
// skipping
logger.finer(
'unEncryptedBytes NALU of type $type, offset ${index + 2}');
return index + 2;
default:
logger.finer('skipping NALU of type $type');
break;
if (['h264', 'h265'].contains(codec?.toLowerCase() ?? '')) {
var result = processNALUsForEncryption(data, codec!);
if (result.detectedCodec == 'unknown') {
if (lastError != CryptorError.kUnsupportedCodec) {
lastError = CryptorError.kUnsupportedCodec;
postMessage({
'type': 'cryptorState',
'msgType': 'event',
'participantId': participantIdentity,
'trackId': trackId,
'kind': kind,
'state': 'unsupportedCodec',
'error':
'Unsupported codec for track $trackId, detected codec ${result.detectedCodec}'
});
}
throw Exception('Unsupported codec for track $trackId');
}
throw Exception('Could not find NALU');
return result.unencryptedBytes;
}

switch (frameType) {
case 'key':
return 10;
Expand Down
Loading
Loading