Skip to content

Commit b62734f

Browse files
authored
fix: test segments and replays on a clock of their own (#582)
* test: let the last segment of a recording miss its audio Stopped right after a segment began with a key frame, the recorder closes it before any audio went in, which the segment tests that stop at an arbitrary moment did not allow for. * fix: drop key frame requests a replay buffer's key frame answered The buffer asked for a key frame on every round until one came, and the request held back by the interval between requests went out a second later, bringing a key frame the buffer did not need yet: about one more for every two it asked for. * test: drive the segment and replay tests with frames and a clock of their own The tests recorded a live call for a few seconds and checked what came of it, which depended on how fast the machine ran: each slow runner found a new way to fail, and the loose checks this needed missed real bugs. They now feed the recorder encoded frames through a webrtc-java function table of their own, with a clock they set, and wait for the writer after every step. Where each segment and replay begins and ends then follows from the frames alone, and is checked to the frame. One test of each kind still records a real call, checking only what holds at any speed.
1 parent 0991163 commit b62734f

14 files changed

Lines changed: 1745 additions & 360 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_FakeMedia */
19+
20+
#ifndef _Included_dev_onvoid_webrtc_media_recorder_FakeMedia
21+
#define _Included_dev_onvoid_webrtc_media_recorder_FakeMedia
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
/*
26+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
27+
* Method: tableAddress
28+
* Signature: ()J
29+
*/
30+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_tableAddress
31+
(JNIEnv *, jclass);
32+
33+
/*
34+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
35+
* Method: setClock
36+
* Signature: (J)V
37+
*/
38+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_setClock
39+
(JNIEnv *, jclass, jlong);
40+
41+
/*
42+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
43+
* Method: createFrames
44+
* Signature: ()J
45+
*/
46+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_createFrames
47+
(JNIEnv *, jclass);
48+
49+
/*
50+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
51+
* Method: retainFrames
52+
* Signature: (J)V
53+
*/
54+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_retainFrames
55+
(JNIEnv *, jclass, jlong);
56+
57+
/*
58+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
59+
* Method: releaseFrames
60+
* Signature: (J)V
61+
*/
62+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_releaseFrames
63+
(JNIEnv *, jclass, jlong);
64+
65+
/*
66+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
67+
* Method: observers
68+
* Signature: (J)I
69+
*/
70+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_observers
71+
(JNIEnv *, jclass, jlong);
72+
73+
/*
74+
* Class: dev_onvoid_webrtc_media_recorder_FakeMedia
75+
* Method: push
76+
* Signature: (JZLjava/lang/String;IIZIIJ[B)V
77+
*/
78+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_push
79+
(JNIEnv *, jclass, jlong, jboolean, jstring, jint, jint, jboolean, jint, jint, jlong, jbyteArray);
80+
81+
#ifdef __cplusplus
82+
}
83+
#endif
84+
#endif

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ extern "C" {
8686
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_saveReplay
8787
(JNIEnv *, jclass, jlong, jint, jstring);
8888

89+
/*
90+
* Class: dev_onvoid_webrtc_media_recorder_MediaRecorder
91+
* Method: sync
92+
* Signature: (J)V
93+
*/
94+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_MediaRecorder_sync
95+
(JNIEnv *, jclass, jlong);
96+
8997
/*
9098
* Class: dev_onvoid_webrtc_media_recorder_MediaRecorder
9199
* Method: dispose

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ namespace ffmpeg
171171
// observer hears nothing.
172172
bool SaveReplay(int id, std::string path);
173173

174+
// Waits until the writer has handled everything taken before the
175+
// call, frames and requests alike, and has looked at the clock
176+
// after them. Returns at once if there is no writer, or it has
177+
// ended. For tests, which drive the recorder with frames and a
178+
// clock of their own and have to know what it made of them.
179+
void Sync();
180+
174181
private:
175182
enum class Mode
176183
{
@@ -382,6 +389,9 @@ namespace ffmpeg
382389

383390
std::mutex mutex_;
384391
std::condition_variable wake_;
392+
// Counts the rounds of the writer, for Sync().
393+
std::condition_variable looped_;
394+
uint64_t loops_ = 0;
385395
std::vector<std::unique_ptr<Input>> inputs_;
386396
std::deque<RecordedFrame> queue_;
387397
size_t queued_bytes_ = 0;
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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_FakeMedia.h"
18+
#include "webrtc_java_api.h"
19+
20+
#include <algorithm>
21+
#include <atomic>
22+
#include <cstdint>
23+
#include <mutex>
24+
#include <new>
25+
#include <vector>
26+
27+
// The Java class this belongs to exists in the tests only. It stands in for
28+
// the webrtc-java library a recorder takes its frames and its clock from, so
29+
// that a test decides what arrives when, and what time it is: what a
30+
// recorder makes of its frames then follows from them alone, however fast
31+
// the machine is.
32+
33+
namespace
34+
{
35+
std::atomic<int64_t> clock_us{ 0 };
36+
37+
struct Observer;
38+
39+
// What a sender or receiver would be: the frames a test pushes go to the
40+
// observers attached to it.
41+
struct Frames
42+
{
43+
// Held while frames are delivered, so that an observer removed is
44+
// not called any more once the removal returns.
45+
std::mutex mutex;
46+
std::vector<Observer *> observers;
47+
std::atomic<int> references{ 1 };
48+
};
49+
50+
struct Observer
51+
{
52+
Frames * frames;
53+
wj_encoded_frame_fn fn;
54+
void * opaque;
55+
};
56+
57+
Frames * FramesOf(jlong handle)
58+
{
59+
return reinterpret_cast<Frames *>(handle);
60+
}
61+
62+
void Retain(Frames * frames)
63+
{
64+
frames->references.fetch_add(1);
65+
}
66+
67+
void Release(Frames * frames)
68+
{
69+
if (frames->references.fetch_sub(1) == 1) {
70+
delete frames;
71+
}
72+
}
73+
74+
int64_t NowUs()
75+
{
76+
return clock_us.load();
77+
}
78+
79+
int VideoSourcePush(void *, const wj_i420_frame *)
80+
{
81+
return WEBRTC_JAVA_ERR_HANDLE;
82+
}
83+
84+
int AudioSourcePush(void *, const wj_audio_chunk *)
85+
{
86+
return WEBRTC_JAVA_ERR_HANDLE;
87+
}
88+
89+
int VideoSourcePushColor(void *, const wj_i420_frame *, const wj_color_space *)
90+
{
91+
return WEBRTC_JAVA_ERR_HANDLE;
92+
}
93+
94+
void SourceRetain(void *)
95+
{
96+
}
97+
98+
void SourceRelease(void *)
99+
{
100+
}
101+
102+
void * ObserverAdd(void * handle, wj_encoded_frame_fn fn, void * opaque)
103+
{
104+
if (handle == nullptr || fn == nullptr) {
105+
return nullptr;
106+
}
107+
108+
Frames * frames = static_cast<Frames *>(handle);
109+
Observer * observer = new (std::nothrow) Observer{ frames, fn, opaque };
110+
111+
if (observer == nullptr) {
112+
return nullptr;
113+
}
114+
115+
std::lock_guard<std::mutex> lock(frames->mutex);
116+
117+
frames->observers.push_back(observer);
118+
119+
// The observer keeps what it observes alive, as the library's do.
120+
Retain(frames);
121+
122+
return observer;
123+
}
124+
125+
void ObserverRemove(void * handle)
126+
{
127+
Observer * observer = static_cast<Observer *>(handle);
128+
Frames * frames = observer->frames;
129+
130+
{
131+
std::lock_guard<std::mutex> lock(frames->mutex);
132+
133+
auto & observers = frames->observers;
134+
observers.erase(std::remove(observers.begin(), observers.end(), observer), observers.end());
135+
}
136+
137+
delete observer;
138+
139+
Release(frames);
140+
}
141+
142+
void FramesRelease(void * handle)
143+
{
144+
if (handle != nullptr) {
145+
Release(static_cast<Frames *>(handle));
146+
}
147+
}
148+
149+
const webrtc_java_api * Table()
150+
{
151+
static const webrtc_java_api table = [] {
152+
webrtc_java_api api{};
153+
154+
api.version = WEBRTC_JAVA_API_VERSION;
155+
api.size = sizeof(webrtc_java_api);
156+
api.now_us = &NowUs;
157+
api.video_source_push = &VideoSourcePush;
158+
api.audio_source_push = &AudioSourcePush;
159+
api.video_source_retain = &SourceRetain;
160+
api.video_source_release = &SourceRelease;
161+
api.audio_source_retain = &SourceRetain;
162+
api.audio_source_release = &SourceRelease;
163+
api.encoded_observer_add = &ObserverAdd;
164+
api.encoded_observer_remove = &ObserverRemove;
165+
api.encoded_frames_release = &FramesRelease;
166+
api.video_source_push_color = &VideoSourcePushColor;
167+
168+
return api;
169+
}();
170+
171+
return &table;
172+
}
173+
}
174+
175+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_tableAddress
176+
(JNIEnv * env, jclass caller)
177+
{
178+
return reinterpret_cast<jlong>(Table());
179+
}
180+
181+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_setClock
182+
(JNIEnv * env, jclass caller, jlong nowUs)
183+
{
184+
clock_us.store(nowUs);
185+
}
186+
187+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_createFrames
188+
(JNIEnv * env, jclass caller)
189+
{
190+
return reinterpret_cast<jlong>(new (std::nothrow) Frames());
191+
}
192+
193+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_retainFrames
194+
(JNIEnv * env, jclass caller, jlong handle)
195+
{
196+
if (handle != 0) {
197+
Retain(FramesOf(handle));
198+
}
199+
}
200+
201+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_releaseFrames
202+
(JNIEnv * env, jclass caller, jlong handle)
203+
{
204+
if (handle != 0) {
205+
Release(FramesOf(handle));
206+
}
207+
}
208+
209+
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_observers
210+
(JNIEnv * env, jclass caller, jlong handle)
211+
{
212+
if (handle == 0) {
213+
return 0;
214+
}
215+
216+
Frames * frames = FramesOf(handle);
217+
std::lock_guard<std::mutex> lock(frames->mutex);
218+
219+
return static_cast<jint>(frames->observers.size());
220+
}
221+
222+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_recorder_FakeMedia_push
223+
(JNIEnv * env, jclass caller, jlong handle, jboolean video, jstring jMimeType, jint ssrc,
224+
jint rtpTimestamp, jboolean keyFrame, jint width, jint height, jlong timeUs, jbyteArray jData)
225+
{
226+
if (handle == 0 || jMimeType == nullptr || jData == nullptr) {
227+
return;
228+
}
229+
230+
const jsize size = env->GetArrayLength(jData);
231+
std::vector<uint8_t> data(static_cast<size_t>(size));
232+
233+
env->GetByteArrayRegion(jData, 0, size, reinterpret_cast<jbyte *>(data.data()));
234+
235+
if (env->ExceptionCheck()) {
236+
return;
237+
}
238+
239+
const char * mimeType = env->GetStringUTFChars(jMimeType, nullptr);
240+
241+
if (mimeType == nullptr) {
242+
return;
243+
}
244+
245+
wj_encoded_frame frame{};
246+
frame.data = data.data();
247+
frame.size = data.size();
248+
frame.media_type = video ? WEBRTC_JAVA_MEDIA_VIDEO : WEBRTC_JAVA_MEDIA_AUDIO;
249+
frame.mime_type = mimeType;
250+
frame.payload_type = video ? 96 : 111;
251+
frame.ssrc = static_cast<uint32_t>(ssrc);
252+
frame.rtp_timestamp = static_cast<uint32_t>(rtpTimestamp);
253+
frame.key_frame = video && keyFrame ? 1 : 0;
254+
frame.width = width;
255+
frame.height = height;
256+
frame.time_us = timeUs;
257+
258+
{
259+
Frames * frames = FramesOf(handle);
260+
std::lock_guard<std::mutex> lock(frames->mutex);
261+
262+
for (Observer * observer : frames->observers) {
263+
observer->fn(observer->opaque, &frame);
264+
}
265+
}
266+
267+
env->ReleaseStringUTFChars(jMimeType, mimeType);
268+
}

0 commit comments

Comments
 (0)