blob: 198da1e3e8c0d4b5caffbe24e907c5b1f2a47d41 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 final long nativeTrack;
74
75 public MediaStreamTrack(long nativeTrack) {
76 this.nativeTrack = nativeTrack;
77 }
78
79 public String id() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010080 return nativeGetId(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 }
82
83 public String kind() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010084 return nativeGetKind(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 }
86
87 public boolean enabled() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010088 return nativeGetEnabled(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 }
90
91 public boolean setEnabled(boolean enable) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010092 return nativeSetEnabled(nativeTrack, enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 }
94
95 public State state() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010096 return nativeGetState(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 }
98
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 public void dispose() {
magjedb1c74532017-08-27 13:47:20 -0700100 JniCommon.nativeReleaseRef(nativeTrack);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 }
102
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100103 private static native String nativeGetId(long track);
104 private static native String nativeGetKind(long track);
105 private static native boolean nativeGetEnabled(long track);
106 private static native boolean nativeSetEnabled(long track, boolean enabled);
107 private static native State nativeGetState(long track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108}