blob: 65da734ee56f48aec31c4067bdcfec3c484a98b5 [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äkie7592d82018-03-22 13:32:44 +010013import javax.annotation.Nullable;
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015/** Java wrapper for a C++ MediaStreamTrackInterface. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010016@JNINamespace("webrtc::jni")
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017public class MediaStreamTrack {
Seth Hampson9a58cc02018-03-06 17:24:06 -080018 public static final String AUDIO_TRACK_KIND = "audio";
19 public static final String VIDEO_TRACK_KIND = "video";
20
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021 /** Tracks MediaStreamTrackInterface.TrackState */
Magnus Jedvert9060eb12017-12-12 12:52:54 +010022 public enum State {
23 LIVE,
24 ENDED;
25
26 @CalledByNative("State")
27 static State fromNativeIndex(int nativeIndex) {
28 return values()[nativeIndex];
29 }
30 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
Magnus Jedvert4fa5da52017-11-27 13:44:38 +010032 // Must be kept in sync with cricket::MediaType.
zhihuangc4adabf2016-12-07 10:36:40 -080033 public enum MediaType {
Magnus Jedvert4fa5da52017-11-27 13:44:38 +010034 MEDIA_TYPE_AUDIO(0),
35 MEDIA_TYPE_VIDEO(1);
36
37 private final int nativeIndex;
38
39 private MediaType(int nativeIndex) {
40 this.nativeIndex = nativeIndex;
41 }
42
43 @CalledByNative("MediaType")
44 int getNative() {
45 return nativeIndex;
46 }
47
48 @CalledByNative("MediaType")
49 static MediaType fromNativeIndex(int nativeIndex) {
50 for (MediaType type : MediaType.values()) {
51 if (type.getNative() == nativeIndex) {
52 return type;
53 }
54 }
55 throw new IllegalArgumentException("Unknown native media type: " + nativeIndex);
56 }
zhihuangc4adabf2016-12-07 10:36:40 -080057 }
58
Seth Hampson9a58cc02018-03-06 17:24:06 -080059 /** Factory method to create an AudioTrack or VideoTrack subclass. */
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010060 static @Nullable MediaStreamTrack createMediaStreamTrack(long nativeTrack) {
Seth Hampson9a58cc02018-03-06 17:24:06 -080061 if (nativeTrack == 0) {
62 return null;
63 }
64 String trackKind = nativeGetKind(nativeTrack);
65 if (trackKind.equals(AUDIO_TRACK_KIND)) {
66 return new AudioTrack(nativeTrack);
67 } else if (trackKind.equals(VIDEO_TRACK_KIND)) {
68 return new VideoTrack(nativeTrack);
69 } else {
70 return null;
71 }
72 }
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 final long nativeTrack;
75
76 public MediaStreamTrack(long nativeTrack) {
77 this.nativeTrack = nativeTrack;
78 }
79
80 public String id() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010081 return nativeGetId(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 }
83
84 public String kind() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010085 return nativeGetKind(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 }
87
88 public boolean enabled() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010089 return nativeGetEnabled(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 }
91
92 public boolean setEnabled(boolean enable) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010093 return nativeSetEnabled(nativeTrack, enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 }
95
96 public State state() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010097 return nativeGetState(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 }
99
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 public void dispose() {
magjedb1c74532017-08-27 13:47:20 -0700101 JniCommon.nativeReleaseRef(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 }
103
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100104 private static native String nativeGetId(long track);
105 private static native String nativeGetKind(long track);
106 private static native boolean nativeGetEnabled(long track);
107 private static native boolean nativeSetEnabled(long track, boolean enabled);
108 private static native State nativeGetState(long track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109}