149 lines
4.6 KiB
Java
149 lines
4.6 KiB
Java
package org.webrtc;
|
|
|
|
import c.d.b.a.a;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
public class MediaStream {
|
|
private static final String TAG = "MediaStream";
|
|
public final List<AudioTrack> audioTracks = new ArrayList();
|
|
private long nativeStream;
|
|
public final List<VideoTrack> preservedVideoTracks = new ArrayList();
|
|
public final List<VideoTrack> videoTracks = new ArrayList();
|
|
|
|
@CalledByNative
|
|
public MediaStream(long j) {
|
|
this.nativeStream = j;
|
|
}
|
|
|
|
private void checkMediaStreamExists() {
|
|
if (this.nativeStream == 0) {
|
|
throw new IllegalStateException("MediaStream has been disposed.");
|
|
}
|
|
}
|
|
|
|
private static native boolean nativeAddAudioTrackToNativeStream(long j, long j2);
|
|
|
|
private static native boolean nativeAddVideoTrackToNativeStream(long j, long j2);
|
|
|
|
private static native String nativeGetId(long j);
|
|
|
|
private static native boolean nativeRemoveAudioTrack(long j, long j2);
|
|
|
|
private static native boolean nativeRemoveVideoTrack(long j, long j2);
|
|
|
|
private static void removeMediaStreamTrack(List<? extends MediaStreamTrack> list, long j) {
|
|
Iterator<? extends MediaStreamTrack> it = list.iterator();
|
|
while (it.hasNext()) {
|
|
MediaStreamTrack mediaStreamTrack = (MediaStreamTrack) it.next();
|
|
if (mediaStreamTrack.getNativeMediaStreamTrack() == j) {
|
|
mediaStreamTrack.dispose();
|
|
it.remove();
|
|
return;
|
|
}
|
|
}
|
|
Logging.e("MediaStream", "Couldn't not find track");
|
|
}
|
|
|
|
@CalledByNative
|
|
public void addNativeAudioTrack(long j) {
|
|
this.audioTracks.add(new AudioTrack(j));
|
|
}
|
|
|
|
@CalledByNative
|
|
public void addNativeVideoTrack(long j) {
|
|
this.videoTracks.add(new VideoTrack(j));
|
|
}
|
|
|
|
public boolean addPreservedTrack(VideoTrack videoTrack) {
|
|
checkMediaStreamExists();
|
|
if (!nativeAddVideoTrackToNativeStream(this.nativeStream, videoTrack.getNativeVideoTrack())) {
|
|
return false;
|
|
}
|
|
this.preservedVideoTracks.add(videoTrack);
|
|
return true;
|
|
}
|
|
|
|
public boolean addTrack(AudioTrack audioTrack) {
|
|
checkMediaStreamExists();
|
|
if (!nativeAddAudioTrackToNativeStream(this.nativeStream, audioTrack.getNativeAudioTrack())) {
|
|
return false;
|
|
}
|
|
this.audioTracks.add(audioTrack);
|
|
return true;
|
|
}
|
|
|
|
public boolean addTrack(VideoTrack videoTrack) {
|
|
checkMediaStreamExists();
|
|
if (!nativeAddVideoTrackToNativeStream(this.nativeStream, videoTrack.getNativeVideoTrack())) {
|
|
return false;
|
|
}
|
|
this.videoTracks.add(videoTrack);
|
|
return true;
|
|
}
|
|
|
|
@CalledByNative
|
|
public void dispose() {
|
|
checkMediaStreamExists();
|
|
while (!this.audioTracks.isEmpty()) {
|
|
AudioTrack audioTrack = this.audioTracks.get(0);
|
|
removeTrack(audioTrack);
|
|
audioTrack.dispose();
|
|
}
|
|
while (!this.videoTracks.isEmpty()) {
|
|
VideoTrack videoTrack = this.videoTracks.get(0);
|
|
removeTrack(videoTrack);
|
|
videoTrack.dispose();
|
|
}
|
|
while (!this.preservedVideoTracks.isEmpty()) {
|
|
removeTrack(this.preservedVideoTracks.get(0));
|
|
}
|
|
JniCommon.nativeReleaseRef(this.nativeStream);
|
|
this.nativeStream = 0;
|
|
}
|
|
|
|
public String getId() {
|
|
checkMediaStreamExists();
|
|
return nativeGetId(this.nativeStream);
|
|
}
|
|
|
|
public long getNativeMediaStream() {
|
|
checkMediaStreamExists();
|
|
return this.nativeStream;
|
|
}
|
|
|
|
@CalledByNative
|
|
public void removeAudioTrack(long j) {
|
|
removeMediaStreamTrack(this.audioTracks, j);
|
|
}
|
|
|
|
public boolean removeTrack(AudioTrack audioTrack) {
|
|
checkMediaStreamExists();
|
|
this.audioTracks.remove(audioTrack);
|
|
return nativeRemoveAudioTrack(this.nativeStream, audioTrack.getNativeAudioTrack());
|
|
}
|
|
|
|
public boolean removeTrack(VideoTrack videoTrack) {
|
|
checkMediaStreamExists();
|
|
this.videoTracks.remove(videoTrack);
|
|
this.preservedVideoTracks.remove(videoTrack);
|
|
return nativeRemoveVideoTrack(this.nativeStream, videoTrack.getNativeVideoTrack());
|
|
}
|
|
|
|
@CalledByNative
|
|
public void removeVideoTrack(long j) {
|
|
removeMediaStreamTrack(this.videoTracks, j);
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder K = a.K("[");
|
|
K.append(getId());
|
|
K.append(":A=");
|
|
K.append(this.audioTracks.size());
|
|
K.append(":V=");
|
|
K.append(this.videoTracks.size());
|
|
K.append("]");
|
|
return K.toString();
|
|
}
|
|
}
|