Skip to content

Commit dba7352

Browse files
committed
feat: add SyncClock for audio and video synchronization support
1 parent c2158f3 commit dba7352

File tree

12 files changed

+446
-0
lines changed

12 files changed

+446
-0
lines changed

webrtc-jni/src/main/cpp/include/JNI_CustomAudioSource.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-jni/src/main/cpp/include/JNI_CustomVideoSource.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-jni/src/main/cpp/include/JNI_SyncClock.h

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-jni/src/main/cpp/src/JNI_CustomAudioSource.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_init
3030
SetHandle(env, caller, source.release());
3131
}
3232

33+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_initializeWithClock
34+
(JNIEnv * env, jobject caller, jobject javaClock)
35+
{
36+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, javaClock);
37+
CHECK_HANDLE(clock);
38+
39+
std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
40+
webrtc::scoped_refptr<jni::CustomAudioSource> source = webrtc::make_ref_counted<jni::CustomAudioSource>(sync_clock);
41+
42+
SetHandle(env, caller, source.release());
43+
}
44+
3345
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_audio_CustomAudioSource_dispose
3446
(JNIEnv * env, jobject caller)
3547
{

webrtc-jni/src/main/cpp/src/JNI_CustomVideoSource.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_init
3131
SetHandle(env, caller, source.release());
3232
}
3333

34+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_initializeWithClock
35+
(JNIEnv * env, jobject caller, jobject javaClock)
36+
{
37+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, javaClock);
38+
CHECK_HANDLE(clock);
39+
40+
std::shared_ptr<jni::SyncClock> sync_clock(clock, [](jni::SyncClock*) {}); // Shared ownership without deleting
41+
webrtc::scoped_refptr<jni::CustomVideoSource> source = webrtc::make_ref_counted<jni::CustomVideoSource>(sync_clock);
42+
43+
SetHandle(env, caller, source.release());
44+
}
45+
3446
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_CustomVideoSource_dispose
3547
(JNIEnv * env, jobject caller)
3648
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2019 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_SyncClock.h"
18+
#include "JavaUtils.h"
19+
#include "media/SyncClock.h"
20+
21+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_initialize
22+
(JNIEnv * env, jobject caller)
23+
{
24+
jni::SyncClock * clock = new jni::SyncClock();
25+
26+
SetHandle(env, caller, clock);
27+
}
28+
29+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampUs
30+
(JNIEnv * env, jobject caller)
31+
{
32+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
33+
CHECK_HANDLEV(clock, 0);
34+
35+
return clock->GetTimestampUs();
36+
}
37+
38+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getTimestampMs
39+
(JNIEnv * env, jobject caller)
40+
{
41+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
42+
CHECK_HANDLEV(clock, 0);
43+
44+
return clock->GetTimestampMs();
45+
}
46+
47+
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_SyncClock_getNtpTime
48+
(JNIEnv * env, jobject caller)
49+
{
50+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
51+
CHECK_HANDLEV(clock, 0);
52+
53+
webrtc::NtpTime ntpTime = clock->GetNtpTime();
54+
return ntpTime.ToMs();
55+
}
56+
57+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_SyncClock_dispose
58+
(JNIEnv * env, jobject caller)
59+
{
60+
jni::SyncClock * clock = GetHandle<jni::SyncClock>(env, caller);
61+
CHECK_HANDLE(clock);
62+
63+
delete clock;
64+
65+
SetHandle<std::nullptr_t>(env, caller, nullptr);
66+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019 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+
package dev.onvoid.webrtc.media;
18+
19+
import dev.onvoid.webrtc.internal.NativeObject;
20+
21+
/**
22+
* Synchronized Clock for A/V timing. Provides timing functionality for
23+
* audio and video synchronization.
24+
*
25+
* @author Alex Andres
26+
*/
27+
public class SyncClock extends NativeObject {
28+
29+
/**
30+
* Constructs a new SyncClock instance.
31+
*/
32+
public SyncClock() {
33+
super();
34+
35+
initialize();
36+
}
37+
38+
/**
39+
* Get the current timestamp in microseconds.
40+
*
41+
* @return The current timestamp in microseconds.
42+
*/
43+
public native long getTimestampUs();
44+
45+
/**
46+
* Get the current timestamp in milliseconds.
47+
*
48+
* @return The current timestamp in milliseconds.
49+
*/
50+
public native long getTimestampMs();
51+
52+
/**
53+
* Get NTP timestamp for RTP synchronization.
54+
*
55+
* @return The NTP timestamp.
56+
*/
57+
public native long getNtpTime();
58+
59+
/**
60+
* Disposes of any native resources held by this clock.
61+
* This method should be called when the clock is no longer needed
62+
* to prevent memory leaks.
63+
*/
64+
public native void dispose();
65+
66+
/**
67+
* Initializes the native resources required by this clock.
68+
*/
69+
private native void initialize();
70+
71+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package dev.onvoid.webrtc.media.audio;
1818

19+
import dev.onvoid.webrtc.media.SyncClock;
20+
1921
/**
2022
* Custom implementation of an audio source for WebRTC that allows pushing audio data
2123
* from external sources directly to the WebRTC audio pipeline.
@@ -33,6 +35,17 @@ public CustomAudioSource() {
3335
initialize();
3436
}
3537

38+
/**
39+
* Constructs a new CustomAudioSource instance with a specified SyncClock.
40+
*
41+
* @param clock The SyncClock to use for timing and synchronization.
42+
*/
43+
public CustomAudioSource(SyncClock clock) {
44+
super();
45+
46+
initializeWithClock(clock);
47+
}
48+
3649
/**
3750
* Pushes audio data to be processed by this audio source.
3851
*
@@ -57,4 +70,11 @@ public native void pushAudio(byte[] audioData, int bits_per_sample,
5770
*/
5871
private native void initialize();
5972

73+
/**
74+
* Initializes the native resources required by this audio source with a specified SyncClock.
75+
*
76+
* @param clock The SyncClock to use for timing and synchronization.
77+
*/
78+
private native void initializeWithClock(SyncClock clock);
79+
6080
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package dev.onvoid.webrtc.media.video;
1818

19+
import dev.onvoid.webrtc.media.SyncClock;
20+
1921
/**
2022
* Custom implementation of a video source for WebRTC that allows pushing video frames
2123
* from external sources directly to the WebRTC video pipeline.
@@ -33,6 +35,17 @@ public CustomVideoSource() {
3335
initialize();
3436
}
3537

38+
/**
39+
* Constructs a new CustomVideoSource instance with a specified SyncClock.
40+
*
41+
* @param clock The SyncClock to use for timing and synchronization.
42+
*/
43+
public CustomVideoSource(SyncClock clock) {
44+
super();
45+
46+
initializeWithClock(clock);
47+
}
48+
3649
/**
3750
* Pushes audio data to be processed by this audio source.
3851
*
@@ -52,4 +65,11 @@ public CustomVideoSource() {
5265
*/
5366
private native void initialize();
5467

68+
/**
69+
* Initializes the native resources required by this video source with a specified SyncClock.
70+
*
71+
* @param clock The SyncClock to use for timing and synchronization.
72+
*/
73+
private native void initializeWithClock(SyncClock clock);
74+
5575
}

0 commit comments

Comments
 (0)