blob: 5593d424f315fa2de81479e724b08e5b17ec65fd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11package org.webrtc;
12
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020013import java.util.IdentityHashMap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15/** Java version of VideoTrackInterface. */
16public class VideoTrack extends MediaStreamTrack {
sakal0ba43b52017-08-14 05:17:49 -070017 private final IdentityHashMap<VideoSink, Long> sinks = new IdentityHashMap<VideoSink, Long>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19 public VideoTrack(long nativeTrack) {
20 super(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021 }
22
sakal0ba43b52017-08-14 05:17:49 -070023 /**
24 * Adds a VideoSink to the track.
25 *
26 * A track can have any number of VideoSinks. VideoSinks will replace
27 * renderers. However, converting old style texture frames will involve costly
28 * conversion to I420 so it is not recommended to upgrade before all your
29 * sources produce VideoFrames.
30 */
31 public void addSink(VideoSink sink) {
Magnus Jedvertb7700d32018-06-13 17:26:59 +020032 if (sink == null) {
33 throw new IllegalArgumentException("The VideoSink is not allowed to be null");
34 }
35 // We allow calling addSink() with the same sink multiple times. This is similar to the C++
36 // VideoTrack::AddOrUpdateSink().
37 if (!sinks.containsKey(sink)) {
38 final long nativeSink = nativeWrapSink(sink);
39 sinks.put(sink, nativeSink);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020040 nativeAddSink(getNativeMediaStreamTrack(), nativeSink);
Magnus Jedvertb7700d32018-06-13 17:26:59 +020041 }
sakal0ba43b52017-08-14 05:17:49 -070042 }
43
44 /**
45 * Removes a VideoSink from the track.
46 *
47 * If the VideoSink was not attached to the track, this is a no-op.
48 */
49 public void removeSink(VideoSink sink) {
Magnus Jedvertb7700d32018-06-13 17:26:59 +020050 final Long nativeSink = sinks.remove(sink);
51 if (nativeSink != null) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020052 nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070053 nativeFreeSink(nativeSink);
54 }
55 }
56
Sami Kalliomäkibde473e2017-10-30 13:34:41 +010057 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 public void dispose() {
sakal0ba43b52017-08-14 05:17:49 -070059 for (long nativeSink : sinks.values()) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020060 nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070061 nativeFreeSink(nativeSink);
62 }
63 sinks.clear();
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000064 super.dispose();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 }
66
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020067 /** Returns a pointer to webrtc::VideoTrackInterface. */
68 long getNativeVideoTrack() {
69 return getNativeMediaStreamTrack();
70 }
71
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010072 private static native void nativeAddSink(long track, long nativeSink);
73 private static native void nativeRemoveSink(long track, long nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070074 private static native long nativeWrapSink(VideoSink sink);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010075 private static native void nativeFreeSink(long sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076}