Skip to content

Commit 822258b

Browse files
committed
fix: report a recording that fails to close, and tell how it ended
1 parent 3a9d903 commit 822258b

16 files changed

Lines changed: 648 additions & 33 deletions

File tree

‎docs/guide/media/media-recording.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ try (MediaRecorder recorder = new MediaRecorder(Paths.get("call.mkv"))) {
3030

3131
A sender's frames are recorded as they leave the encoder, a receiver's as they go to the decoder. Senders and receivers of any number of peer connections can go into one file, e.g. both sides of a call.
3232

33-
`stop()` waits until everything received so far is written and the file is finished, and returns whether the file holds a recording. A recorder that never got any media to write deletes its file rather than leave an unplayable one behind. `close()` stops a recording that still runs.
33+
`stop()` waits until everything received so far is written and the file is finished and closed, and returns whether the file holds a recording. A recorder that never got any media to write deletes its file rather than leave an unplayable one behind. `close()` stops a recording that still runs.
34+
35+
How the recording ended, `getResult()` tells once the recorder is stopped, and keeps telling after `close()`:
36+
37+
| Result | File | Meaning |
38+
| --- | --- | --- |
39+
| `COMPLETE` | kept | Everything was written, and the file was finished and closed. |
40+
| `PARTIAL` | kept | Media went into the file, but writing, finishing or closing it failed later. What made it into the file is kept, since it may still play. |
41+
| `EMPTY` | deleted | No media arrived before the stop. |
42+
| `FAILED` | deleted | Writing failed before any media went into the file. |
43+
44+
```java
45+
recorder.stop();
46+
47+
if (recorder.getResult() == MediaRecorderResult.PARTIAL) {
48+
System.out.println("Incomplete recording in " + recorder.getFile());
49+
}
50+
```
3451

3552
::: warning
3653
Keep the senders and receivers undisposed while recording. The recorder asks them for key frames, and a disposed one cannot be asked.
@@ -85,6 +102,8 @@ recorder.setListener(new MediaRecorderListener() {
85102

86103
Calls arrive in order on a thread the recorder keeps for them, so a listener may take its time and may even stop the recorder.
87104

105+
`onError` also hears of a file that cannot be finished or closed when the recorder stops, e.g. because the last data does not fit on the disk. The recording then ends as `PARTIAL`.
106+
88107
## How Tracks Are Recorded
89108

90109
- **Video starts at a key frame.** A decoder can do nothing with the frames before one, so they are skipped. The recorder asks the sender or receiver for a key frame, so recording starts within moments instead of waiting until WebRTC sends one of its own accord, which it rarely does.

‎webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/EncryptedRecordingExample.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import dev.onvoid.webrtc.media.audio.CustomAudioSource;
5858
import dev.onvoid.webrtc.media.recorder.MediaRecorder;
5959
import dev.onvoid.webrtc.media.recorder.MediaRecorderListener;
60+
import dev.onvoid.webrtc.media.recorder.MediaRecorderResult;
6061
import dev.onvoid.webrtc.media.video.CustomVideoSource;
6162
import dev.onvoid.webrtc.media.video.NativeI420Buffer;
6263
import dev.onvoid.webrtc.media.video.VideoFrame;
@@ -170,7 +171,8 @@ public void onError(String message) {
170171
Thread.sleep(TimeUnit.SECONDS.toMillis(RECORD_SECONDS));
171172

172173
if (recorder.stop()) {
173-
LOG.info("Recorded " + RECORD_SECONDS + " seconds into " + recorder.getFile());
174+
LOG.info("Recorded " + RECORD_SECONDS + " seconds into " + recorder.getFile()
175+
+ (recorder.getResult() == MediaRecorderResult.PARTIAL ? ", incomplete" : ""));
174176
}
175177
}
176178
finally {

‎webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/MediaFilePlayerExample.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,18 @@ String stopRecording() {
689689
return "Not recording";
690690
}
691691

692-
boolean recorded = running.stop();
693692
running.close();
694693

695-
return recorded ? "Saved " + recording : "Nothing was recorded";
694+
switch (running.getResult()) {
695+
case COMPLETE:
696+
return "Saved " + recording;
697+
case PARTIAL:
698+
return "Saved " + recording + ", but writing it failed";
699+
case FAILED:
700+
return "Recording failed";
701+
default:
702+
return "Nothing was recorded";
703+
}
696704
}
697705

698706
/**

‎webrtc-media/src/main/cpp/include/JNI_MediaRecorder.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ extern "C" {
4949
/*
5050
* Class: dev_onvoid_webrtc_media_recorder_MediaRecorder
5151
* Method: stop
52-
* Signature: (J)Z
52+
* Signature: (J)I
5353
*/
54-
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_stop
54+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_stop
5555
(JNIEnv *, jclass, jlong);
5656

5757
/*
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <jni.h>
18+
/* Header for class dev_onvoid_webrtc_media_recorder_WriteFault */
19+
20+
#ifndef _Included_dev_onvoid_webrtc_media_recorder_WriteFault
21+
#define _Included_dev_onvoid_webrtc_media_recorder_WriteFault
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
/*
26+
* Class: dev_onvoid_webrtc_media_recorder_WriteFault
27+
* Method: failHeader
28+
* Signature: ()V
29+
*/
30+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failHeader
31+
(JNIEnv *, jclass);
32+
33+
/*
34+
* Class: dev_onvoid_webrtc_media_recorder_WriteFault
35+
* Method: failPacket
36+
* Signature: (I)V
37+
*/
38+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failPacket
39+
(JNIEnv *, jclass, jint);
40+
41+
/*
42+
* Class: dev_onvoid_webrtc_media_recorder_WriteFault
43+
* Method: failTrailer
44+
* Signature: ()V
45+
*/
46+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failTrailer
47+
(JNIEnv *, jclass);
48+
49+
/*
50+
* Class: dev_onvoid_webrtc_media_recorder_WriteFault
51+
* Method: failClose
52+
* Signature: ()V
53+
*/
54+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failClose
55+
(JNIEnv *, jclass);
56+
57+
/*
58+
* Class: dev_onvoid_webrtc_media_recorder_WriteFault
59+
* Method: disarm
60+
* Signature: ()V
61+
*/
62+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_disarm
63+
(JNIEnv *, jclass);
64+
65+
#ifdef __cplusplus
66+
}
67+
#endif
68+
#endif

‎webrtc-media/src/main/cpp/include/media/MediaRecorder.h‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ namespace ffmpeg
5454
class MediaRecorder
5555
{
5656
public:
57+
// How a recording ended, in the order of the Java
58+
// MediaRecorderResult.
59+
enum class Result
60+
{
61+
// Every frame taken was written, and the file was finished
62+
// and closed.
63+
kComplete,
64+
// Media went into the file, but writing, finishing or closing
65+
// it failed later.
66+
kPartial,
67+
// Nothing went wrong, and no media went into the file.
68+
kEmpty,
69+
// Writing failed before any media went into the file.
70+
kFailed
71+
};
72+
5773
MediaRecorder(const webrtc_java_api * api, std::string path);
5874

5975
// Stops the recording if it still runs.
@@ -80,9 +96,10 @@ namespace ffmpeg
8096
void Start();
8197

8298
// Detaches from every track, writes out what is queued, finishes
83-
// the file and waits for the writer thread. Returns whether any
84-
// media was written. Stopping twice does nothing more.
85-
bool Stop();
99+
// the file, waits for the writer thread and closes the file.
100+
// Returns how the recording ended. Stopping twice does nothing
101+
// more, and returns the same.
102+
Result Stop();
86103

87104
private:
88105
// What the observer callback knows about a track. Guarded by
@@ -135,6 +152,10 @@ namespace ffmpeg
135152
bool MaybeWriteHeader(bool finishing);
136153

137154
void Finish();
155+
156+
// Closes the file if it is open, and fails the recording if that
157+
// does.
158+
void Close();
138159
void Fail(const std::string & message, int error);
139160
void Warn(const std::string & message);
140161

@@ -155,6 +176,8 @@ namespace ffmpeg
155176
int64_t first_ready_us_ = 0;
156177
// Also read by Stop(), on the thread that stops.
157178
std::atomic<bool> header_written_{ false };
179+
// Read by Stop() once the writer has ended; the thread that stops
180+
// sets it when it closes a file that had no writer.
158181
bool failed_ = false;
159182

160183
// When recording started, in the clock of the frames.
@@ -175,6 +198,8 @@ namespace ffmpeg
175198
bool stopping_ = false;
176199
bool started_ = false;
177200
bool stopped_ = false;
201+
// Set by the first Stop(), once the file is closed.
202+
Result result_ = Result::kEmpty;
178203
};
179204
}
180205

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WEBRTC_JAVA_MEDIA_WRITE_FAULT_H_
18+
#define WEBRTC_JAVA_MEDIA_WRITE_FAULT_H_
19+
20+
namespace ffmpeg
21+
{
22+
// Makes the writing of a recording fail on purpose, so that the tests can
23+
// see what a recorder does when a disk fills up or a file cannot be
24+
// closed, which no disk does on demand.
25+
//
26+
// Only the tests arm it, through JNI_WriteFault, and a call that is not
27+
// armed pays a single atomic load. The call itself still runs, so the
28+
// file is as it would be; only the result it reports is replaced. A
29+
// fault fires once, for the recorder that runs into it first.
30+
class WriteFault
31+
{
32+
public:
33+
enum class Call
34+
{
35+
kHeader,
36+
kPacket,
37+
kTrailer,
38+
kClose
39+
};
40+
41+
// Fails the call that follows the given number of calls of the
42+
// same kind, once, with the given AVERROR.
43+
static void Fail(Call call, int error, int after_calls);
44+
45+
// Forgets a fault that has not fired.
46+
static void Disarm();
47+
48+
// Returns the result of the given call, or the error it is to
49+
// fail with if a fault is armed for it, which disarms the fault.
50+
static int Check(Call call, int result);
51+
};
52+
}
53+
54+
#endif

‎webrtc-media/src/main/cpp/src/JNI_MediaRecorder.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_start
9999
});
100100
}
101101

102-
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_stop
102+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_stop
103103
(JNIEnv * env, jclass caller, jlong handle)
104104
{
105+
const jint failed = static_cast<jint>(ffmpeg::MediaRecorder::Result::kFailed);
106+
105107
if (handle == 0) {
106-
return JNI_FALSE;
108+
return failed;
107109
}
108110

109-
return ffmpeg::CallFromJava<jboolean>(env, JNI_FALSE, [&]() -> jboolean {
110-
return RecorderOf(handle)->Stop() ? JNI_TRUE : JNI_FALSE;
111+
return ffmpeg::CallFromJava<jint>(env, failed, [&]() -> jint {
112+
return static_cast<jint>(RecorderOf(handle)->Stop());
111113
});
112114
}
113115

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "JNI_WriteFault.h"
18+
#include "media/WriteFault.h"
19+
20+
#include <cerrno>
21+
22+
extern "C" {
23+
#include <libavutil/error.h>
24+
}
25+
26+
// The Java class this belongs to exists in the tests only, so that nothing but
27+
// them can make a recording fail. Writing fails as on a full disk, closing as
28+
// on a file system that loses what it was to keep.
29+
30+
using ffmpeg::WriteFault;
31+
32+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failHeader
33+
(JNIEnv * env, jclass caller)
34+
{
35+
WriteFault::Fail(WriteFault::Call::kHeader, AVERROR(ENOSPC), 0);
36+
}
37+
38+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failPacket
39+
(JNIEnv * env, jclass caller, jint afterPackets)
40+
{
41+
WriteFault::Fail(WriteFault::Call::kPacket, AVERROR(ENOSPC), afterPackets);
42+
}
43+
44+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failTrailer
45+
(JNIEnv * env, jclass caller)
46+
{
47+
WriteFault::Fail(WriteFault::Call::kTrailer, AVERROR(ENOSPC), 0);
48+
}
49+
50+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_failClose
51+
(JNIEnv * env, jclass caller)
52+
{
53+
WriteFault::Fail(WriteFault::Call::kClose, AVERROR(EIO), 0);
54+
}
55+
56+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_WriteFault_disarm
57+
(JNIEnv * env, jclass caller)
58+
{
59+
WriteFault::Disarm();
60+
}

0 commit comments

Comments
 (0)