Skip to content

Commit bca650c

Browse files
committed
fix: stamp custom media pushed without a timestamp on WebRTC's clock
pushFrame(VideoFrame) and pushAudio() without a timestamp used the time since the SyncClock was created, while the timestamped calls expect webrtc::TimeMicros(). Mixing both made the frame time jump back.
1 parent 764337d commit bca650c

11 files changed

Lines changed: 106 additions & 19 deletions

File tree

‎docs/guide/audio/custom-audio-source.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ audioSource.pushAudio(pcm, 16, sampleRate, channels, frameCount,
104104
baseUs + presentationUs);
105105
```
106106

107-
Use the same base for the audio and the video of one source, and the two stay in sync on the receiving side. See [Custom Video Source](/guide/video/custom-video-source) for the video half. Chunks must still be pushed in real time; the timestamp describes the audio, it does not schedule it.
107+
Use the same base for the audio and the video of one source, and the two stay in sync on the receiving side. See [Custom Video Source](/guide/video/custom-video-source) for the video half. Chunks must still be pushed in real time; the timestamp describes the audio, it does not schedule it. A chunk pushed without a timestamp is stamped with the same clock, so a source may mix the two calls.
108108

109109
## Audio Format Considerations
110110

‎docs/guide/video/custom-video-source.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ Frames must still be pushed in real time. A frame is encoded and sent when it ar
169169
Timestamps must also advance by at least one millisecond from frame to frame. WebRTC drops a frame whose capture time does not move forward.
170170
:::
171171

172+
`pushFrame(VideoFrame)` stamps its frames with the same clock, so a source may mix the two calls.
173+
172174
Push audio with timestamps from the same clock, as described in [Custom Audio Source](/guide/audio/custom-audio-source), and the two stay in sync on the receiving side.
173175

174176
## Integration with Video Tracks

‎webrtc-jni/src/main/cpp/include/media/audio/CustomAudioSource.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ namespace jni
4343
bool remote() const override;
4444

4545
// Push audio data with synchronization. The chunk is captured now,
46-
// so it is stamped with this source's clock less the configured
47-
// capture delay. This is what the Java pushAudio() calls.
46+
// so it is stamped with webrtc::TimeMicros(), the clock of the
47+
// overload below, less the configured capture delay. This is what
48+
// the Java pushAudio() calls.
4849
void PushAudioData(const void * audio_data, int bits_per_sample,
4950
int sample_rate, size_t number_of_channels,
5051
size_t number_of_frames);

‎webrtc-jni/src/main/cpp/include/media/video/CustomVideoSource.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ namespace jni
4040
SourceState state() const override;
4141
bool remote() const override;
4242

43-
// Delivers a frame captured now, stamping it with this source's
44-
// clock. This is what the Java pushFrame() calls, so a caller that
45-
// has no capture time of its own gets the timing of the moment it
46-
// pushes.
43+
// Delivers a frame captured now, stamping it with
44+
// webrtc::TimeMicros(), the clock of the overload below. This is
45+
// what the Java pushFrame() calls, so a caller that has no capture
46+
// time of its own gets the timing of the moment it pushes.
4747
void PushFrame(const webrtc::VideoFrame & frame);
4848

4949
// Delivers a frame captured at the given time, in microseconds on

‎webrtc-jni/src/main/cpp/include/webrtc_java_api.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ struct wj_i420_frame {
117117

118118
/**
119119
* Capture time in the clock of now_us(). Frames must be pushed in
120-
* increasing timestamp order. A value of zero means "stamp it with the
121-
* source's own clock", which is what the Java pushFrame() does.
120+
* increasing timestamp order. A value of zero means "stamp it with
121+
* now_us() at the moment of the push", which is what the Java
122+
* pushFrame() does.
122123
*/
123124
int64_t timestamp_us;
124125

@@ -147,7 +148,8 @@ struct wj_audio_chunk {
147148

148149
/**
149150
* Capture time in the clock of now_us(), or zero to stamp the chunk with
150-
* the source's own clock, which is what the Java pushAudio() does.
151+
* now_us() at the moment of the push, which is what the Java pushAudio()
152+
* does.
151153
*/
152154
int64_t timestamp_us;
153155
};

‎webrtc-jni/src/main/cpp/src/media/audio/CustomAudioSource.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ namespace jni
5454
{
5555
webrtc::MutexLock lock(&mutex_);
5656

57-
// Calculate absolute capture time
58-
int64_t timestamp_us = clock_->GetTimestampUs();
57+
// Captured now, on the clock AudioTrackSinkInterface requires of an
58+
// absolute capture timestamp and the one a caller's capture time is
59+
// on. WebRTC ignores a capture time that is not positive, and turns
60+
// the gap between two of them into an RTP timestamp step when
61+
// sending resumes, so both calls must stay on this clock.
62+
int64_t timestamp_us = webrtc::TimeMicros();
5963

6064
// Apply delay if audio capture has inherent latency
6165
timestamp_us -= audio_capture_delay_us_;

‎webrtc-jni/src/main/cpp/src/media/video/CustomVideoSource.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ namespace jni
4848

4949
void CustomVideoSource::PushFrame(const webrtc::VideoFrame& frame)
5050
{
51-
// No capture time from the caller, so the frame is captured now.
52-
DeliverFrame(frame, clock_->GetTimestampUs(), clock_->GetNtpTime().ToMs());
51+
// No capture time from the caller, so the frame is captured now, on
52+
// the same clock as a caller's capture time. A source that mixes both
53+
// calls would otherwise hand out frame times that jump between two
54+
// clocks, to the frame rate adapter, the encoder's cadence adapter and
55+
// every sink of the track, which all expect them to increase.
56+
PushFrame(frame, webrtc::TimeMicros());
5357
}
5458

5559
void CustomVideoSource::PushFrame(const webrtc::VideoFrame& frame, int64_t timestamp_us)

‎webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public SyncClock() {
5353
* example by {@link dev.onvoid.webrtc.media.video.CustomVideoSource#pushFrame(
5454
* dev.onvoid.webrtc.media.video.VideoFrame, long) pushFrame} and
5555
* {@link dev.onvoid.webrtc.media.audio.CustomAudioSource#pushAudio(byte[],
56-
* int, int, int, int, long) pushAudio}. It is the monotonic clock WebRTC
56+
* int, int, int, int, long) pushAudio}, and the one those sources stamp
57+
* media pushed without a timestamp with. It is the monotonic clock WebRTC
5758
* itself runs on, which is what makes those timestamps comparable with
5859
* WebRTC's own notion of now.
5960
* <p>

‎webrtc/src/main/java/dev/onvoid/webrtc/media/audio/CustomAudioSource.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public CustomAudioSource(SyncClock clock) {
7575
* 10 ms is the size WebRTC works with; at most
7676
* {@value #MAX_SAMPLES_PER_PUSH} samples, counting every channel, fit in
7777
* one chunk.
78+
* <p>
79+
* The chunk is treated as captured at the moment of the call, and stamped
80+
* with {@link dev.onvoid.webrtc.media.SyncClock#currentTimeUs()}, the clock
81+
* of {@link #pushAudio(byte[], int, int, int, int, long)}, so the two may
82+
* be mixed on one source.
7883
*
7984
* @param audioData The raw audio data bytes to process. Must hold at
8085
* least {@code frameCount * channels * 2} bytes.

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/CustomVideoSource.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public CustomVideoSource(SyncClock clock) {
4848

4949
/**
5050
* Pushes a video frame to be processed by this video source. The frame is
51-
* treated as captured at the moment of the call.
51+
* treated as captured at the moment of the call, and stamped with
52+
* {@link dev.onvoid.webrtc.media.SyncClock#currentTimeUs()}, the clock of
53+
* {@link #pushFrame(VideoFrame, long)}, so the two may be mixed on one
54+
* source.
5255
*
5356
* @param frame The video frame to be pushed to the source.
5457
*/

0 commit comments

Comments
 (0)