blob: 944c7ef1b269ba0b82a249dc864a91df1e6febbf [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. */
16public class MediaStreamTrack {
Seth Hampson9a58cc02018-03-06 17:24:06 -080017 public static final String AUDIO_TRACK_KIND = "audio";
18 public static final String VIDEO_TRACK_KIND = "video";
19
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020 /** Tracks MediaStreamTrackInterface.TrackState */
Magnus Jedvert9060eb12017-12-12 12:52:54 +010021 public enum State {
22 LIVE,
23 ENDED;
24
25 @CalledByNative("State")
26 static State fromNativeIndex(int nativeIndex) {
27 return values()[nativeIndex];
28 }
29 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
Magnus Jedvert4fa5da52017-11-27 13:44:38 +010031 // Must be kept in sync with cricket::MediaType.
zhihuangc4adabf2016-12-07 10:36:40 -080032 public enum MediaType {
Magnus Jedvert4fa5da52017-11-27 13:44:38 +010033 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 }
zhihuangc4adabf2016-12-07 10:36:40 -080056 }
57
Seth Hampson9a58cc02018-03-06 17:24:06 -080058 /** Factory method to create an AudioTrack or VideoTrack subclass. */
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010059 static @Nullable MediaStreamTrack createMediaStreamTrack(long nativeTrack) {
Seth Hampson9a58cc02018-03-06 17:24:06 -080060 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
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020073 private long nativeTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75 public MediaStreamTrack(long nativeTrack) {
76 this.nativeTrack = nativeTrack;
77 }
78
79 public String id() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020080 checkMediaStreamTrackExists();
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() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020085 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010086 return nativeGetKind(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 }
88
89 public boolean enabled() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020090 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010091 return nativeGetEnabled(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 }
93
94 public boolean setEnabled(boolean enable) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020095 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010096 return nativeSetEnabled(nativeTrack, enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 }
98
99 public State state() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200100 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100101 return nativeGetState(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 }
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200105 checkMediaStreamTrackExists();
magjedb1c74532017-08-27 13:47:20 -0700106 JniCommon.nativeReleaseRef(nativeTrack);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200107 nativeTrack = 0;
108 }
109
110 long getNativeMediaStreamTrack() {
111 checkMediaStreamTrackExists();
112 return nativeTrack;
113 }
114
115 private void checkMediaStreamTrackExists() {
116 if (nativeTrack == 0) {
117 throw new IllegalStateException("MediaStreamTrack has been disposed.");
118 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 }
120
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100121 private static native String nativeGetId(long track);
122 private static native String nativeGetKind(long track);
123 private static native boolean nativeGetEnabled(long track);
124 private static native boolean nativeSetEnabled(long track, boolean enabled);
125 private static native State nativeGetState(long track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126}