| 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. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import androidx.annotation.Nullable; |
| 14 |
| 15 | /** Java wrapper for a C++ MediaStreamTrackInterface. */ |
| 16 | public class MediaStreamTrack { |
| 17 | public static final String AUDIO_TRACK_KIND = "audio"; |
| 18 | public static final String VIDEO_TRACK_KIND = "video"; |
| 19 |
| 20 | /** Tracks MediaStreamTrackInterface.TrackState */ |
| 21 | public enum State { |
| 22 | LIVE, |
| 23 | ENDED; |
| 24 |
| 25 | @CalledByNative("State") |
| 26 | static State fromNativeIndex(int nativeIndex) { |
| 27 | return values()[nativeIndex]; |
| 28 | } |
| 29 | } |
| 30 |
| 31 | // Must be kept in sync with cricket::MediaType. |
| 32 | public enum MediaType { |
| 33 | MEDIA_TYPE_AUDIO(0), |
| 34 | MEDIA_TYPE_VIDEO(1); |
| 35 |
| 36 | private final int nativeIndex; |
| 37 |
| 38 | private MediaType(int nativeIndex) { |
| 39 | this.nativeIndex = nativeIndex; |
| 40 | } |
| 41 |
| 42 | @CalledByNative("MediaType") |
| 43 | int getNative() { |
| 44 | return nativeIndex; |
| 45 | } |
| 46 |
| 47 | @CalledByNative("MediaType") |
| 48 | static MediaType fromNativeIndex(int nativeIndex) { |
| 49 | for (MediaType type : MediaType.values()) { |
| 50 | if (type.getNative() == nativeIndex) { |
| 51 | return type; |
| 52 | } |
| 53 | } |
| 54 | throw new IllegalArgumentException("Unknown native media type: " + nativeIndex); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | /** Factory method to create an AudioTrack or VideoTrack subclass. */ |
| 59 | static @Nullable MediaStreamTrack createMediaStreamTrack(long nativeTrack) { |
| 60 | if (nativeTrack == 0) { |
| 61 | return null; |
| 62 | } |
| 63 | String trackKind = nativeGetKind(nativeTrack); |
| 64 | if (trackKind.equals(AUDIO_TRACK_KIND)) { |
| 65 | return new AudioTrack(nativeTrack); |
| 66 | } else if (trackKind.equals(VIDEO_TRACK_KIND)) { |
| 67 | return new VideoTrack(nativeTrack); |
| 68 | } else { |
| 69 | return null; |
| 70 | } |
| 71 | } |
| 72 |
| 73 | private long nativeTrack; |
| 74 |
| 75 | public MediaStreamTrack(long nativeTrack) { |
| 76 | if (nativeTrack == 0) { |
| 77 | throw new IllegalArgumentException("nativeTrack may not be null"); |
| 78 | } |
| 79 | this.nativeTrack = nativeTrack; |
| 80 | } |
| 81 |
| 82 | public String id() { |
| 83 | checkMediaStreamTrackExists(); |
| 84 | return nativeGetId(nativeTrack); |
| 85 | } |
| 86 |
| 87 | public String kind() { |
| 88 | checkMediaStreamTrackExists(); |
| 89 | return nativeGetKind(nativeTrack); |
| 90 | } |
| 91 |
| 92 | public boolean enabled() { |
| 93 | checkMediaStreamTrackExists(); |
| 94 | return nativeGetEnabled(nativeTrack); |
| 95 | } |
| 96 |
| 97 | public boolean setEnabled(boolean enable) { |
| 98 | checkMediaStreamTrackExists(); |
| 99 | return nativeSetEnabled(nativeTrack, enable); |
| 100 | } |
| 101 |
| 102 | public State state() { |
| 103 | checkMediaStreamTrackExists(); |
| 104 | return nativeGetState(nativeTrack); |
| 105 | } |
| 106 |
| 107 | public void dispose() { |
| 108 | checkMediaStreamTrackExists(); |
| 109 | JniCommon.nativeReleaseRef(nativeTrack); |
| 110 | nativeTrack = 0; |
| 111 | } |
| 112 |
| 113 | long getNativeMediaStreamTrack() { |
| 114 | checkMediaStreamTrackExists(); |
| 115 | return nativeTrack; |
| 116 | } |
| 117 |
| 118 | private void checkMediaStreamTrackExists() { |
| 119 | if (nativeTrack == 0) { |
| 120 | throw new IllegalStateException("MediaStreamTrack has been disposed."); |
| 121 | } |
| 122 | } |
| 123 |
| 124 | private static native String nativeGetId(long track); |
| 125 | private static native String nativeGetKind(long track); |
| 126 | private static native boolean nativeGetEnabled(long track); |
| 127 | private static native boolean nativeSetEnabled(long track, boolean enabled); |
| 128 | private static native State nativeGetState(long track); |
| 129 | } |
| 130 |