Skip to content
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 75 additions & 77 deletions dart/moq_ffi/lib/src/moq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16741,9 +16741,9 @@ class FfiConverterMoqVideoDecodedFrame {

abstract class MoqVideoEncoderInterface {
void cut();
List<MoqVideoPacket> encode({required MoqVideoFrame frame});
List<MoqVideoPacket> finish();
List<MoqVideoPacket> flush();
void encode({required MoqVideoFrame frame});
void flush();
MoqVideoPacket? receive();
void setBitrate({required int bitrate});
}

Expand Down Expand Up @@ -16806,36 +16806,32 @@ class MoqVideoEncoder implements MoqVideoEncoderInterface {
}, moqExceptionErrorHandler);
}

List<MoqVideoPacket> encode({required MoqVideoFrame frame}) {
return rustCallWithLifter(
(status) => uniffi_moq_ffi_fn_method_moqvideoencoder_encode(
void encode({required MoqVideoFrame frame}) {
return rustCall((status) {
uniffi_moq_ffi_fn_method_moqvideoencoder_encode(
uniffiClonePointer(),
FfiConverterMoqVideoFrame.lower(frame),
status,
),
FfiConverterSequenceMoqVideoPacket.lift,
moqExceptionErrorHandler,
);
);
}, moqExceptionErrorHandler);
}

List<MoqVideoPacket> finish() {
return rustCallWithLifter(
(status) => uniffi_moq_ffi_fn_method_moqvideoencoder_finish(
void flush() {
return rustCall((status) {
uniffi_moq_ffi_fn_method_moqvideoencoder_flush(
uniffiClonePointer(),
status,
),
FfiConverterSequenceMoqVideoPacket.lift,
moqExceptionErrorHandler,
);
);
}, moqExceptionErrorHandler);
}

List<MoqVideoPacket> flush() {
MoqVideoPacket? receive() {
return rustCallWithLifter(
(status) => uniffi_moq_ffi_fn_method_moqvideoencoder_flush(
(status) => uniffi_moq_ffi_fn_method_moqvideoencoder_receive(
uniffiClonePointer(),
status,
),
FfiConverterSequenceMoqVideoPacket.lift,
FfiConverterOptionalMoqVideoPacket.lift,
moqExceptionErrorHandler,
);
}
Expand Down Expand Up @@ -19302,6 +19298,53 @@ class FfiConverterOptionalMoqVideoHint {
}
}

class FfiConverterOptionalMoqVideoPacket {
static MoqVideoPacket? lift(RustBuffer buf) {
return FfiConverterOptionalMoqVideoPacket.read(buf.asUint8List()).value;
}

static LiftRetVal<MoqVideoPacket?> read(Uint8List buf) {
if (ByteData.view(buf.buffer, buf.offsetInBytes).getInt8(0) == 0) {
return LiftRetVal(null, 1);
}
final result = FfiConverterMoqVideoPacket.read(
Uint8List.view(buf.buffer, buf.offsetInBytes + 1),
);
return LiftRetVal<MoqVideoPacket?>(result.value, result.bytesRead + 1);
}

static int allocationSize([MoqVideoPacket? value]) {
if (value == null) {
return 1;
}
return FfiConverterMoqVideoPacket.allocationSize(value) + 1;
}

static RustBuffer lower(MoqVideoPacket? value) {
if (value == null) {
return toRustBuffer(Uint8List.fromList([0]));
}
final buf = Uint8List(
FfiConverterOptionalMoqVideoPacket.allocationSize(value),
);
FfiConverterOptionalMoqVideoPacket.write(value, buf);
return toRustBuffer(buf);
}

static int write(MoqVideoPacket? value, Uint8List buf) {
if (value == null) {
buf[0] = 0;
return 1;
}
buf[0] = 1;
return FfiConverterMoqVideoPacket.write(
value,
Uint8List.view(buf.buffer, buf.offsetInBytes + 1),
) +
1;
}
}

class FfiConverterOptionalMoqVideoProducer {
static MoqVideoProducer? lift(RustBuffer buf) {
return FfiConverterOptionalMoqVideoProducer.read(buf.asUint8List()).value;
Expand Down Expand Up @@ -19801,51 +19844,6 @@ class FfiConverterSequenceMoqRange {
}
}

class FfiConverterSequenceMoqVideoPacket {
static List<MoqVideoPacket> lift(RustBuffer buf) {
return FfiConverterSequenceMoqVideoPacket.read(buf.asUint8List()).value;
}

static LiftRetVal<List<MoqVideoPacket>> read(Uint8List buf) {
List<MoqVideoPacket> res = [];
final length = buf.buffer.asByteData(buf.offsetInBytes).getInt32(0);
int offset = buf.offsetInBytes + 4;
for (var i = 0; i < length; i++) {
final ret = FfiConverterMoqVideoPacket.read(
Uint8List.view(buf.buffer, offset),
);
offset += ret.bytesRead;
res.add(ret.value);
}
return LiftRetVal(res, offset - buf.offsetInBytes);
}

static int write(List<MoqVideoPacket> value, Uint8List buf) {
buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, value.length);
int offset = buf.offsetInBytes + 4;
for (var i = 0; i < value.length; i++) {
offset += FfiConverterMoqVideoPacket.write(
value[i],
Uint8List.view(buf.buffer, offset),
);
}
return offset - buf.offsetInBytes;
}

static int allocationSize(List<MoqVideoPacket> value) {
return value
.map((l) => FfiConverterMoqVideoPacket.allocationSize(l))
.fold(0, (a, b) => a + b) +
4;
}

static RustBuffer lower(List<MoqVideoPacket> value) {
final buf = Uint8List(allocationSize(value));
write(value, buf);
return toRustBuffer(buf);
}
}

class FfiConverterSequenceString {
static List<String> lift(RustBuffer buf) {
return FfiConverterSequenceString.read(buf.asUint8List()).value;
Expand Down Expand Up @@ -23229,27 +23227,27 @@ external void uniffi_moq_ffi_fn_method_moqvideoencoder_cut(
Pointer<RustCallStatus> uniffiStatus,
);

@Native<
RustBuffer Function(Pointer<Void>, RustBuffer, Pointer<RustCallStatus>)
>(assetId: _uniffiAssetId)
external RustBuffer uniffi_moq_ffi_fn_method_moqvideoencoder_encode(
@Native<Void Function(Pointer<Void>, RustBuffer, Pointer<RustCallStatus>)>(
assetId: _uniffiAssetId,
)
external void uniffi_moq_ffi_fn_method_moqvideoencoder_encode(
Pointer<Void> ptr,
RustBuffer frame,
Pointer<RustCallStatus> uniffiStatus,
);

@Native<RustBuffer Function(Pointer<Void>, Pointer<RustCallStatus>)>(
@Native<Void Function(Pointer<Void>, Pointer<RustCallStatus>)>(
assetId: _uniffiAssetId,
)
external RustBuffer uniffi_moq_ffi_fn_method_moqvideoencoder_finish(
external void uniffi_moq_ffi_fn_method_moqvideoencoder_flush(
Pointer<Void> ptr,
Pointer<RustCallStatus> uniffiStatus,
);

@Native<RustBuffer Function(Pointer<Void>, Pointer<RustCallStatus>)>(
assetId: _uniffiAssetId,
)
external RustBuffer uniffi_moq_ffi_fn_method_moqvideoencoder_flush(
external RustBuffer uniffi_moq_ffi_fn_method_moqvideoencoder_receive(
Pointer<Void> ptr,
Pointer<RustCallStatus> uniffiStatus,
);
Expand Down Expand Up @@ -24455,10 +24453,10 @@ external int uniffi_moq_ffi_checksum_method_moqvideoencoder_cut();
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_encode();

@Native<Uint16 Function()>(assetId: _uniffiAssetId)
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_finish();
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_flush();

@Native<Uint16 Function()>(assetId: _uniffiAssetId)
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_flush();
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_receive();

@Native<Uint16 Function()>(assetId: _uniffiAssetId)
external int uniffi_moq_ffi_checksum_method_moqvideoencoder_set_bitrate();
Expand Down Expand Up @@ -25363,13 +25361,13 @@ void _checkApiChecksums() {
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_cut() != 28527) {
throw UniffiInternalError.panicked("UniFFI API checksum mismatch");
}
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_encode() != 513) {
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_encode() != 42494) {
throw UniffiInternalError.panicked("UniFFI API checksum mismatch");
}
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_finish() != 5918) {
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_flush() != 24167) {
throw UniffiInternalError.panicked("UniFFI API checksum mismatch");
}
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_flush() != 4585) {
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_receive() != 33059) {
throw UniffiInternalError.panicked("UniFFI API checksum mismatch");
}
if (uniffi_moq_ffi_checksum_method_moqvideoencoder_set_bitrate() != 34315) {
Expand Down
10 changes: 5 additions & 5 deletions demo/carla/bridge/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ impl Encoding {
if planned.keyframe {
encoder.cut().context("request an IDR")?;
}
let packets = encoder.encode(&frame).context("encode")?;
for packet in packets {
encoder.encode(&frame).context("encode")?;
while let Some(packet) = encoder.receive().context("encode")? {
let keyframe = packet.keyframe;
if !self.gate.admit(planned.index, keyframe) {
continue;
Expand Down Expand Up @@ -578,10 +578,10 @@ fn probe(config: &moq_video::encode::Config) -> Result<Bytes> {
let luma = size.width as usize * size.height as usize;
let gray = I420::new(size, vec![0x80; luma + luma / 2])?;
let mut encoder = Encoder::new(config)?;
let mut packets = encoder.encode(&Frame::new(Surface::I420(gray), moq_net::Timestamp::from_micros(0)?))?;
packets.extend(encoder.finish()?);
encoder.encode(&Frame::new(Surface::I420(gray), moq_net::Timestamp::from_micros(0)?))?;
encoder.flush()?;
let mut sets = Vec::new();
for packet in &packets {
while let Some(packet) = encoder.receive()? {
for nal in nals(&packet.payload)? {
if matches!(nal.first().map(|header| header & 0x1f), Some(7 | 8)) {
sets.push(nal);
Expand Down
6 changes: 3 additions & 3 deletions demo/carla/bridge/tests/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ impl Worker {
let timestamp = moq_net::Timestamp::from_micros(frame.timestamp_us).unwrap();
// The decoder may hold a picture back; match pictures to
// packets in order rather than one for one.
for picture in decoder
decoder
.decode(&frame.payload, timestamp, frame.keyframe)
.expect("decode the packet")
{
.expect("decode the packet");
while let Some(picture) = decoder.receive().expect("decode the packet") {
let (number, rgb) = expected.pop_front().expect("more pictures than packets");
let i420 = picture.surface.to_i420().expect("CPU picture");
let size = SIZES[usize::from(frame.rendition)];
Expand Down
17 changes: 9 additions & 8 deletions go/wrapper/video/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,23 @@ func NewEncoder(input EncoderInput, output EncoderOutput) (*Encoder, error) {
return &Encoder{inner}, nil
}

// Encode encodes one picture, returning any access units now available.
func (e *Encoder) Encode(value Frame) ([]Packet, error) {
return e.inner.Encode(value)
}
// Encode submits one picture, whose access units Receive returns.
func (e *Encoder) Encode(value Frame) error { return e.inner.Encode(value) }

// Receive pops the oldest ready access unit, or nil while the codec holds the rest; call it until nil.
func (e *Encoder) Receive() (*Packet, error) { return e.inner.Receive() }

// Cut forces the next encoded picture to start a new group.
func (e *Encoder) Cut() error { return e.inner.Cut() }

// SetBitrate retunes the live encoder in bits per second.
func (e *Encoder) SetBitrate(value uint64) error { return e.inner.SetBitrate(value) }

// Flush drains buffered access units and keeps the encoder open.
func (e *Encoder) Flush() ([]Packet, error) { return e.inner.Flush() }
// Flush marks the end of input, so Receive returns every access unit the codec still holds, without closing the encoder.
func (e *Encoder) Flush() error { return e.inner.Flush() }

// Finish drains and closes the encoder.
func (e *Encoder) Finish() ([]Packet, error) { return e.inner.Finish() }
// Close releases the encoder, dropping any access unit not yet received.
func (e *Encoder) Close() { e.inner.Destroy() }

// Consumer subscribes to a video rendition and yields decoded frames.
type Consumer struct{ inner *ffi.MoqVideoConsumer }
Expand Down
21 changes: 11 additions & 10 deletions py/moq-rs/moq/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,14 @@ class Encoder:
def __init__(self, input: EncoderInput, output: EncoderOutput) -> None:
self._inner = MoqVideoEncoder(input, output)

def encode(self, frame: Frame) -> list[Packet]:
"""Encode one picture, returning zero or more access units."""
return [Packet._from_ffi(value) for value in self._inner.encode(frame._ffi())]
def encode(self, frame: Frame) -> None:
"""Submit one picture, whose access units ``receive`` returns."""
self._inner.encode(frame._ffi())

def receive(self) -> Packet | None:
"""Pop the oldest ready access unit, or ``None`` while the codec holds the rest; call until ``None``."""
value = self._inner.receive()
return None if value is None else Packet._from_ffi(value)

def cut(self) -> None:
"""Start a group at the next encoded picture."""
Expand All @@ -208,13 +213,9 @@ def set_bitrate(self, bitrate: int) -> None:
"""Retune the live encoder in bits per second."""
self._inner.set_bitrate(bitrate)

def flush(self) -> list[Packet]:
"""Drain buffered pictures while keeping the encoder open."""
return [Packet._from_ffi(value) for value in self._inner.flush()]

def finish(self) -> list[Packet]:
"""Drain and close the encoder."""
return [Packet._from_ffi(value) for value in self._inner.finish()]
def flush(self) -> None:
"""Mark the end of input, so ``receive`` returns every access unit the codec still holds."""
self._inner.flush()


__all__ = [
Expand Down
29 changes: 29 additions & 0 deletions py/moq-rs/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,35 @@ def test_audio_encoder_returns_namespaced_records():
assert isinstance(finish, moq.audio.EncoderFinish)


def test_video_encoder_submits_and_receives():
"""The codec-only video encoder takes pictures and hands back access units one at a time."""
encoder = moq.video.Encoder(
moq.video.EncoderInput(
format=moq.video.PixelFormat.I420,
width=64,
height=48,
framerate=moq.video.FrameRate(numerator=30, denominator=1),
),
moq.video.EncoderOutput(
codec=moq.video.Codec.H264,
kind=cast(moq.video.EncoderKind, moq.video.EncoderKind.SOFTWARE()),
),
)
packets: list[moq.video.Packet] = []
picture = bytes([0x80]) * (64 * 48 * 3 // 2)
for i in range(3):
if i == 2:
encoder.cut()
encoder.encode(moq.video.Frame(timestamp=moq.Timestamp(i * 33_333, 1_000_000), data=picture))
while (packet := encoder.receive()) is not None:
packets.append(packet)
encoder.flush()
while (packet := encoder.receive()) is not None:
packets.append(packet)
assert [packet.keyframe for packet in packets] == [True, False, True]
assert all(packet.data for packet in packets)


def test_audio_encoder_frame_init_and_bitrate():
"""A frame duration sets the frame size, the encoder reports its OpusHead, and the bitrate retunes live."""
output = moq.audio.EncoderOutput(
Expand Down
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ quests that require them.
- [Stream credit by priority](/quest/m1/stream-credit/README.md) - every stream opens through one priority entry point, and a higher-priority group takes over or resets a lower one when credit runs out (held in m1 by an open claim)
- [Finalize moq-lite-07](/quest/m2/lite07-finalize.md) - when the maintainer cuts it, lite-07 negotiates as `moq-lite-07` and the next release ships it
- [Unbounded groups](/quest/m2/unbounded-groups/README.md) - groups and frames lose their size caps; a group keeps a 32 MB tail, and readers behind it replay through paced flights or refresh
- [moq-video codecs split submission from receive](/quest/m2/video-codec-receive.md) - video encoders and decoders submit, then `receive` one output, the shape moq-audio took, so no call allocates a `Vec` of outputs
- [Small API fixes before the cut](/quest/m2/api-shape/README.md) - one path and one spelling per item, names by role, and no dead or wrong-role public surface across net, transport, media, native, apps, JS, and bindings
- [Finishing a media producer returns a Finished handle](/quest/m2/finished-handle.md) - `finish(self)` consumes the media crates' producers, as it does moq-net's, so appending after finish doesn't compile
- [Cut readiness](/quest/m2/cut-gate/README.md) - the checks the cut needs that break nothing: main no slower than release, vendored fixes ported, and moq-c at parity
Expand Down
Loading
Loading