Skip to content

keep encoders running in stopStream while a recording is starting or paused - #2196

Merged
pedroSG94 merged 1 commit into
pedroSG94:masterfrom
RomanHerbstmann:fix/stopstream-keep-encoders-while-record-starting
Sep 11, 2026
Merged

keep encoders running in stopStream while a recording is starting or paused#2196
pedroSG94 merged 1 commit into
pedroSG94:masterfrom
RomanHerbstmann:fix/stopstream-keep-encoders-while-record-starting

Conversation

@RomanHerbstmann

Copy link
Copy Markdown
Contributor

Keep encoders running in stopStream() while a recording is starting or paused

Problem

startStream() and stopStream() check different record states:

  • startStream() starts the encoders only if !recordController.isRunning(). Otherwise it just requests a keyframe.
  • stopStream() stops encoders, microphone, GL surfaces and camera if !recordController.isRecording().

isRecording() is only true in RECORDING. isRunning() is also true in STARTED, PAUSED and RESUMED. After startRecord() the controller stays in STARTED until the first keyframe arrives, usually 35–150 ms and up to one keyframe interval.

If stopStream() is called in that window (for example because the connection failed right after a new recording segment was started):

  1. isRecording() is false, so the encoders, mic and camera are stopped and the formats are reset.
  2. The record controller stays in STARTED and never receives a frame.
  3. The next startStream() sees isRunning() == true and skips startEncoders(). requestKeyFrame() is a no-op because the encoder is not running.
  4. Result: the stream connects but sends no media. The recording file stays empty, and every later startRecord() while streaming also gets no frames, because startRecord() only starts the encoders when not streaming.

The same applies to PAUSED: stopStream() during a paused recording stops the encoders, although resumeRecord() waits for the next keyframe.

StreamBase.kt already does this consistently: its isRecording property maps to recordController.isRunning(), and stopStream() uses it. Camera2Base.isRecording() also returns isRunning().

Fix

stopStream() now checks !recordController.isRunning(), symmetric to startStream(), in all Java bases:

  • Camera1Base
  • Camera2Base
  • DisplayBase
  • FromFileBase
  • OnlyAudioBase

With this change, stopping the stream while a recording is starting or paused keeps the encoders alive, which is the same behaviour as today while RECORDING.

stopRecord() still cleans up correctly. recordController.stopRecord() sets the status to STOPPED before if (!streaming) stopStream() runs, so the full teardown happens as before.

Test

Android emulator, SrtCamera2 streaming to MediaMTX, with a local recording rotated every 60 s while streaming (stopRecord() + startRecord(newFile)). A debug hook called stopStream() directly after startRecord() of a new segment, while the status was still STARTED.

before after
Encoders after stopStream() stopped 0.2 s later keep running, keyframe requests are served
New recording segment 0 bytes ~93 KB (recorded until the app restarted the session)

Normal reconnects (library reTry and a full stop/start) behave as before.

…g or paused

Co-authored-by: Cursor <cursoragent@cursor.com>
@pedroSG94
pedroSG94 merged commit 4bc2ca3 into pedroSG94:master Sep 11, 2026
1 check passed
@pedroSG94

Copy link
Copy Markdown
Owner

Hello,

Thank you for the fix. Merged

@RomanHerbstmann
RomanHerbstmann deleted the fix/stopstream-keep-encoders-while-record-starting branch September 13, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants