blob: 5e33e1e5f17ea968cb305001ee345f5944663b0d [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
Magnus Jedvert6062f372017-11-16 16:53:12 +010013import java.util.ArrayList;
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020014import java.util.IdentityHashMap;
Magnus Jedvert6062f372017-11-16 16:53:12 +010015import java.util.List;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
17/** Java version of VideoTrackInterface. */
18public class VideoTrack extends MediaStreamTrack {
sakal0ba43b52017-08-14 05:17:49 -070019 private final IdentityHashMap<VideoSink, Long> sinks = new IdentityHashMap<VideoSink, Long>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21 public VideoTrack(long nativeTrack) {
22 super(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023 }
24
sakal0ba43b52017-08-14 05:17:49 -070025 /**
26 * Adds a VideoSink to the track.
27 *
28 * A track can have any number of VideoSinks. VideoSinks will replace
29 * renderers. However, converting old style texture frames will involve costly
30 * conversion to I420 so it is not recommended to upgrade before all your
31 * sources produce VideoFrames.
32 */
33 public void addSink(VideoSink sink) {
Magnus Jedvertb7700d32018-06-13 17:26:59 +020034 if (sink == null) {
35 throw new IllegalArgumentException("The VideoSink is not allowed to be null");
36 }
37 // We allow calling addSink() with the same sink multiple times. This is similar to the C++
38 // VideoTrack::AddOrUpdateSink().
39 if (!sinks.containsKey(sink)) {
40 final long nativeSink = nativeWrapSink(sink);
41 sinks.put(sink, nativeSink);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020042 nativeAddSink(getNativeMediaStreamTrack(), nativeSink);
Magnus Jedvertb7700d32018-06-13 17:26:59 +020043 }
sakal0ba43b52017-08-14 05:17:49 -070044 }
45
46 /**
47 * Removes a VideoSink from the track.
48 *
49 * If the VideoSink was not attached to the track, this is a no-op.
50 */
51 public void removeSink(VideoSink sink) {
Magnus Jedvertb7700d32018-06-13 17:26:59 +020052 final Long nativeSink = sinks.remove(sink);
53 if (nativeSink != null) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020054 nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070055 nativeFreeSink(nativeSink);
56 }
57 }
58
Sami Kalliomäkibde473e2017-10-30 13:34:41 +010059 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public void dispose() {
sakal0ba43b52017-08-14 05:17:49 -070061 for (long nativeSink : sinks.values()) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020062 nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070063 nativeFreeSink(nativeSink);
64 }
65 sinks.clear();
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000066 super.dispose();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 }
68
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020069 /** Returns a pointer to webrtc::VideoTrackInterface. */
70 long getNativeVideoTrack() {
71 return getNativeMediaStreamTrack();
72 }
73
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010074 private static native void nativeAddSink(long track, long nativeSink);
75 private static native void nativeRemoveSink(long track, long nativeSink);
sakal0ba43b52017-08-14 05:17:49 -070076 private static native long nativeWrapSink(VideoSink sink);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010077 private static native void nativeFreeSink(long sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078}