blob: 0249ffbfcad5a1b778d80b82f1ae524cc0849981 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
11package org.webrtc;
12
Artem Titarenko69540f42018-12-10 11:30:4613import android.support.annotation.Nullable;
Sami Kalliomäkie7592d82018-03-22 12:32:4414
henrike@webrtc.org28e20752013-07-10 00:45:3615/** Java wrapper for a C++ MediaStreamTrackInterface. */
16public class MediaStreamTrack {
Seth Hampson9a58cc02018-03-07 01:24:0617 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:3620 /** Tracks MediaStreamTrackInterface.TrackState */
Magnus Jedvert9060eb12017-12-12 11:52:5421 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:3630
Magnus Jedvert4fa5da52017-11-27 12:44:3831 // Must be kept in sync with cricket::MediaType.
zhihuangc4adabf2016-12-07 18:36:4032 public enum MediaType {
Magnus Jedvert4fa5da52017-11-27 12:44:3833 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 18:36:4056 }
57
Seth Hampson9a58cc02018-03-07 01:24:0658 /** Factory method to create an AudioTrack or VideoTrack subclass. */
Sami Kalliomäkie7592d82018-03-22 12:32:4459 static @Nullable MediaStreamTrack createMediaStreamTrack(long nativeTrack) {
Seth Hampson9a58cc02018-03-07 01:24:0660 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 12:38:2173 private long nativeTrack;
henrike@webrtc.org28e20752013-07-10 00:45:3674
75 public MediaStreamTrack(long nativeTrack) {
Steve Anton1dfac062018-09-27 21:09:4076 if (nativeTrack == 0) {
77 throw new IllegalArgumentException("nativeTrack may not be null");
78 }
henrike@webrtc.org28e20752013-07-10 00:45:3679 this.nativeTrack = nativeTrack;
80 }
81
82 public String id() {
Sami Kalliomäkiee05e902018-09-28 12:38:2183 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 14:12:1084 return nativeGetId(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:3685 }
86
87 public String kind() {
Sami Kalliomäkiee05e902018-09-28 12:38:2188 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 14:12:1089 return nativeGetKind(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:3690 }
91
92 public boolean enabled() {
Sami Kalliomäkiee05e902018-09-28 12:38:2193 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 14:12:1094 return nativeGetEnabled(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:3695 }
96
97 public boolean setEnabled(boolean enable) {
Sami Kalliomäkiee05e902018-09-28 12:38:2198 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 14:12:1099 return nativeSetEnabled(nativeTrack, enable);
henrike@webrtc.org28e20752013-07-10 00:45:36100 }
101
102 public State state() {
Sami Kalliomäkiee05e902018-09-28 12:38:21103 checkMediaStreamTrackExists();
Magnus Jedvert84d8ae52017-12-20 14:12:10104 return nativeGetState(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36105 }
106
henrike@webrtc.org28e20752013-07-10 00:45:36107 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 12:38:21108 checkMediaStreamTrackExists();
magjedb1c74532017-08-27 20:47:20109 JniCommon.nativeReleaseRef(nativeTrack);
Sami Kalliomäkiee05e902018-09-28 12:38:21110 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 }
henrike@webrtc.org28e20752013-07-10 00:45:36122 }
123
Magnus Jedvert84d8ae52017-12-20 14:12:10124 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);
henrike@webrtc.org28e20752013-07-10 00:45:36129}