arkavo-org / arkavo-org/app

fix: RTMPPublisher SetChunkSize handling during connect phase

Open
#201 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Swift
Stars
0
Forks
0
Avg merge
1h 41m
Merged PRs (30d)
1

Description

## Summary
Fixed a bug where RTMPPublisher was not properly handling the server's SetChunkSize message during the RTMP connect phase, causing chunk parsing to get out of sync.

## Problem
When connecting to an RTMP server, the server typically sends control messages before the connect _result:
1. SetChunkSize (type 1) - Server's chunk size for receiving
2. WindowAcknowledgementSize (type 5)
3. SetPeerBandwidth (type 6)
4. UserControl (type 4)
5. AMF0 _result (type 20)

The RTMPPublisher was using `receiveRTMPChunk()` directly for the connect response, which didn't process the SetChunkSize message. The client continued using the default 128-byte chunk size for receiving, while the server (arkavo-rs/rml_rtmp) was sending with 4096-byte chunks.

## Symptom
When receiving messages larger than 128 bytes (like the 205-byte connect _result), the client would:
1. Read 128 bytes
2. Read 1 byte as "type 3 continuation header" (actually payload data!)
3. Read remaining bytes

This consumed an extra byte, causing all subsequent message parsing to be off by one byte, resulting in garbage message types and lengths.

## Fix
Updated `connectToApp()` to use `receiveRTMPMessage()` in a loop that properly processes control messages:

```swift
// Wait for connect response - may receive control messages first
var receivedConnectResult = false
for _ in 0..<10 {
let (messageType, messageData, messageBytes) = try await receiveRTMPMessage()

switch messageType {
case 1: // Set Chunk Size - CRITICAL: update receive chunk size
let chunkSize = // parse 4-byte big-endian
receiveChunkSize = Int(chunkSize)
case 5: // Window Acknowledgement Size
serverWindowAckSize = // parse
case 20: // AMF0 Command - _result
receivedConnectResult = true
// ...
}
}
```

## Testing
- [x] Verified with `ntdf-test` CLI against arkavo-rs
- [x] Server SetChunkSize (4096) now properly applied
- [x] All NTDF streaming tests pass

## Files Changed
- `Sources/ArkavoStreaming/RTMP/RTMPPublisher.swift`

## Status
✅ **Fixed** - This issue is for documentation/tracking purposes.

Contributor guide

No contributing guide indexed for this repository

Research direction

Review Sources/ArkavoStreaming/RTMP/RTMPPublisher.swift, especially connectToApp() and the receiveRTMPMessage() path. Run the ntdf-test CLI against arkavo-rs and the NTDF streaming tests to verify the documented connect-phase behavior; done means the existing fix remains covered and the issue can be closed as tracking documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
audio-video-rtc
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.