henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | /** Java wrapper for a C++ MediaStreamTrackInterface. */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 14 | @JNINamespace("webrtc::jni") |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | public class MediaStreamTrack { |
Seth Hampson | 9a58cc0 | 2018-03-06 17:24:06 -0800 | [diff] [blame^] | 16 | public static final String AUDIO_TRACK_KIND = "audio"; |
| 17 | public static final String VIDEO_TRACK_KIND = "video"; |
| 18 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | /** Tracks MediaStreamTrackInterface.TrackState */ |
Magnus Jedvert | 9060eb1 | 2017-12-12 12:52:54 +0100 | [diff] [blame] | 20 | public enum State { |
| 21 | LIVE, |
| 22 | ENDED; |
| 23 | |
| 24 | @CalledByNative("State") |
| 25 | static State fromNativeIndex(int nativeIndex) { |
| 26 | return values()[nativeIndex]; |
| 27 | } |
| 28 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
Magnus Jedvert | 4fa5da5 | 2017-11-27 13:44:38 +0100 | [diff] [blame] | 30 | // Must be kept in sync with cricket::MediaType. |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 31 | public enum MediaType { |
Magnus Jedvert | 4fa5da5 | 2017-11-27 13:44:38 +0100 | [diff] [blame] | 32 | MEDIA_TYPE_AUDIO(0), |
| 33 | MEDIA_TYPE_VIDEO(1); |
| 34 | |
| 35 | private final int nativeIndex; |
| 36 | |
| 37 | private MediaType(int nativeIndex) { |
| 38 | this.nativeIndex = nativeIndex; |
| 39 | } |
| 40 | |
| 41 | @CalledByNative("MediaType") |
| 42 | int getNative() { |
| 43 | return nativeIndex; |
| 44 | } |
| 45 | |
| 46 | @CalledByNative("MediaType") |
| 47 | static MediaType fromNativeIndex(int nativeIndex) { |
| 48 | for (MediaType type : MediaType.values()) { |
| 49 | if (type.getNative() == nativeIndex) { |
| 50 | return type; |
| 51 | } |
| 52 | } |
| 53 | throw new IllegalArgumentException("Unknown native media type: " + nativeIndex); |
| 54 | } |
zhihuang | c4adabf | 2016-12-07 10:36:40 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Seth Hampson | 9a58cc0 | 2018-03-06 17:24:06 -0800 | [diff] [blame^] | 57 | /** Factory method to create an AudioTrack or VideoTrack subclass. */ |
| 58 | static MediaStreamTrack createMediaStreamTrack(long nativeTrack) { |
| 59 | if (nativeTrack == 0) { |
| 60 | return null; |
| 61 | } |
| 62 | String trackKind = nativeGetKind(nativeTrack); |
| 63 | if (trackKind.equals(AUDIO_TRACK_KIND)) { |
| 64 | return new AudioTrack(nativeTrack); |
| 65 | } else if (trackKind.equals(VIDEO_TRACK_KIND)) { |
| 66 | return new VideoTrack(nativeTrack); |
| 67 | } else { |
| 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | final long nativeTrack; |
| 73 | |
| 74 | public MediaStreamTrack(long nativeTrack) { |
| 75 | this.nativeTrack = nativeTrack; |
| 76 | } |
| 77 | |
| 78 | public String id() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 79 | return nativeGetId(nativeTrack); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public String kind() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 83 | return nativeGetKind(nativeTrack); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public boolean enabled() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 87 | return nativeGetEnabled(nativeTrack); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public boolean setEnabled(boolean enable) { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 91 | return nativeSetEnabled(nativeTrack, enable); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public State state() { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 95 | return nativeGetState(nativeTrack); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | public void dispose() { |
magjed | b1c7453 | 2017-08-27 13:47:20 -0700 | [diff] [blame] | 99 | JniCommon.nativeReleaseRef(nativeTrack); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 102 | private static native String nativeGetId(long track); |
| 103 | private static native String nativeGetKind(long track); |
| 104 | private static native boolean nativeGetEnabled(long track); |
| 105 | private static native boolean nativeSetEnabled(long track, boolean enabled); |
| 106 | private static native State nativeGetState(long track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | } |