2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package org.webrtc;
|
|
|
|
|
|
2018-09-28 14:38:21 +02:00
|
|
|
import java.util.IdentityHashMap;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
/** Java version of VideoTrackInterface. */
|
|
|
|
|
public class VideoTrack extends MediaStreamTrack {
|
2017-08-14 05:17:49 -07:00
|
|
|
private final IdentityHashMap<VideoSink, Long> sinks = new IdentityHashMap<VideoSink, Long>();
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
public VideoTrack(long nativeTrack) {
|
|
|
|
|
super(nativeTrack);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 05:17:49 -07:00
|
|
|
/**
|
|
|
|
|
* Adds a VideoSink to the track.
|
|
|
|
|
*
|
|
|
|
|
* A track can have any number of VideoSinks. VideoSinks will replace
|
|
|
|
|
* renderers. However, converting old style texture frames will involve costly
|
|
|
|
|
* conversion to I420 so it is not recommended to upgrade before all your
|
|
|
|
|
* sources produce VideoFrames.
|
|
|
|
|
*/
|
|
|
|
|
public void addSink(VideoSink sink) {
|
2018-06-13 17:26:59 +02:00
|
|
|
if (sink == null) {
|
|
|
|
|
throw new IllegalArgumentException("The VideoSink is not allowed to be null");
|
|
|
|
|
}
|
|
|
|
|
// We allow calling addSink() with the same sink multiple times. This is similar to the C++
|
|
|
|
|
// VideoTrack::AddOrUpdateSink().
|
|
|
|
|
if (!sinks.containsKey(sink)) {
|
|
|
|
|
final long nativeSink = nativeWrapSink(sink);
|
|
|
|
|
sinks.put(sink, nativeSink);
|
2018-09-28 14:38:21 +02:00
|
|
|
nativeAddSink(getNativeMediaStreamTrack(), nativeSink);
|
2018-06-13 17:26:59 +02:00
|
|
|
}
|
2017-08-14 05:17:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes a VideoSink from the track.
|
|
|
|
|
*
|
|
|
|
|
* If the VideoSink was not attached to the track, this is a no-op.
|
|
|
|
|
*/
|
|
|
|
|
public void removeSink(VideoSink sink) {
|
2018-06-13 17:26:59 +02:00
|
|
|
final Long nativeSink = sinks.remove(sink);
|
|
|
|
|
if (nativeSink != null) {
|
2018-09-28 14:38:21 +02:00
|
|
|
nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
|
2017-08-14 05:17:49 -07:00
|
|
|
nativeFreeSink(nativeSink);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 13:34:41 +01:00
|
|
|
@Override
|
2013-07-10 00:45:36 +00:00
|
|
|
public void dispose() {
|
2017-08-14 05:17:49 -07:00
|
|
|
for (long nativeSink : sinks.values()) {
|
2018-09-28 14:38:21 +02:00
|
|
|
nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
|
2017-08-14 05:17:49 -07:00
|
|
|
nativeFreeSink(nativeSink);
|
|
|
|
|
}
|
|
|
|
|
sinks.clear();
|
2013-08-12 23:26:21 +00:00
|
|
|
super.dispose();
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 14:38:21 +02:00
|
|
|
/** Returns a pointer to webrtc::VideoTrackInterface. */
|
2023-06-02 07:10:47 +02:00
|
|
|
public long getNativeVideoTrack() {
|
2018-09-28 14:38:21 +02:00
|
|
|
return getNativeMediaStreamTrack();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 15:12:10 +01:00
|
|
|
private static native void nativeAddSink(long track, long nativeSink);
|
|
|
|
|
private static native void nativeRemoveSink(long track, long nativeSink);
|
2017-08-14 05:17:49 -07:00
|
|
|
private static native long nativeWrapSink(VideoSink sink);
|
2017-12-20 15:12:10 +01:00
|
|
|
private static native void nativeFreeSink(long sink);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|