1 /* 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. 3 * 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. 9 */ 10 11 package org.webrtc; 12 13 import android.support.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") fromNativeIndex(int nativeIndex)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 MediaType(int nativeIndex)38 private MediaType(int nativeIndex) { 39 this.nativeIndex = nativeIndex; 40 } 41 42 @CalledByNative("MediaType") getNative()43 int getNative() { 44 return nativeIndex; 45 } 46 47 @CalledByNative("MediaType") fromNativeIndex(int nativeIndex)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. */ createMediaStreamTrack(long nativeTrack)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 MediaStreamTrack(long nativeTrack)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 id()82 public String id() { 83 checkMediaStreamTrackExists(); 84 return nativeGetId(nativeTrack); 85 } 86 kind()87 public String kind() { 88 checkMediaStreamTrackExists(); 89 return nativeGetKind(nativeTrack); 90 } 91 enabled()92 public boolean enabled() { 93 checkMediaStreamTrackExists(); 94 return nativeGetEnabled(nativeTrack); 95 } 96 setEnabled(boolean enable)97 public boolean setEnabled(boolean enable) { 98 checkMediaStreamTrackExists(); 99 return nativeSetEnabled(nativeTrack, enable); 100 } 101 state()102 public State state() { 103 checkMediaStreamTrackExists(); 104 return nativeGetState(nativeTrack); 105 } 106 dispose()107 public void dispose() { 108 checkMediaStreamTrackExists(); 109 JniCommon.nativeReleaseRef(nativeTrack); 110 nativeTrack = 0; 111 } 112 getNativeMediaStreamTrack()113 long getNativeMediaStreamTrack() { 114 checkMediaStreamTrackExists(); 115 return nativeTrack; 116 } 117 checkMediaStreamTrackExists()118 private void checkMediaStreamTrackExists() { 119 if (nativeTrack == 0) { 120 throw new IllegalStateException("MediaStreamTrack has been disposed."); 121 } 122 } 123 nativeGetId(long track)124 private static native String nativeGetId(long track); nativeGetKind(long track)125 private static native String nativeGetKind(long track); nativeGetEnabled(long track)126 private static native boolean nativeGetEnabled(long track); nativeSetEnabled(long track, boolean enabled)127 private static native boolean nativeSetEnabled(long track, boolean enabled); nativeGetState(long track)128 private static native State nativeGetState(long track); 129 } 130