2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package org.webrtc;
|
|
|
|
|
|
2017-11-16 16:53:12 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
/** Java wrapper for a C++ MediaStreamInterface. */
|
|
|
|
|
public class MediaStream {
|
2017-11-16 16:53:12 +01:00
|
|
|
public final List<AudioTrack> audioTracks = new ArrayList<>();
|
|
|
|
|
public final List<VideoTrack> videoTracks = new ArrayList<>();
|
|
|
|
|
public final List<VideoTrack> preservedVideoTracks = new ArrayList<>();
|
2013-08-12 23:26:21 +00:00
|
|
|
// Package-protected for PeerConnection.
|
2013-07-10 00:45:36 +00:00
|
|
|
final long nativeStream;
|
|
|
|
|
|
|
|
|
|
public MediaStream(long nativeStream) {
|
|
|
|
|
this.nativeStream = nativeStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addTrack(AudioTrack track) {
|
|
|
|
|
if (nativeAddAudioTrack(nativeStream, track.nativeTrack)) {
|
|
|
|
|
audioTracks.add(track);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addTrack(VideoTrack track) {
|
|
|
|
|
if (nativeAddVideoTrack(nativeStream, track.nativeTrack)) {
|
|
|
|
|
videoTracks.add(track);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 11:09:34 -07:00
|
|
|
// Tracks added in addTrack() call will be auto released once MediaStream.dispose()
|
|
|
|
|
// is called. If video track need to be preserved after MediaStream is destroyed it
|
|
|
|
|
// should be added to MediaStream using addPreservedTrack() call.
|
|
|
|
|
public boolean addPreservedTrack(VideoTrack track) {
|
|
|
|
|
if (nativeAddVideoTrack(nativeStream, track.nativeTrack)) {
|
|
|
|
|
preservedVideoTracks.add(track);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
public boolean removeTrack(AudioTrack track) {
|
2016-02-11 09:57:23 -08:00
|
|
|
audioTracks.remove(track);
|
|
|
|
|
return nativeRemoveAudioTrack(nativeStream, track.nativeTrack);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean removeTrack(VideoTrack track) {
|
2016-02-11 09:57:23 -08:00
|
|
|
videoTracks.remove(track);
|
|
|
|
|
preservedVideoTracks.remove(track);
|
|
|
|
|
return nativeRemoveVideoTrack(nativeStream, track.nativeTrack);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void dispose() {
|
2015-09-15 11:09:34 -07:00
|
|
|
// Remove and release previously added audio and video tracks.
|
2013-08-12 23:26:21 +00:00
|
|
|
while (!audioTracks.isEmpty()) {
|
2017-11-16 16:53:12 +01:00
|
|
|
AudioTrack track = audioTracks.get(0 /* index */);
|
2013-08-12 23:26:21 +00:00
|
|
|
removeTrack(track);
|
2013-07-10 00:45:36 +00:00
|
|
|
track.dispose();
|
|
|
|
|
}
|
2013-08-12 23:26:21 +00:00
|
|
|
while (!videoTracks.isEmpty()) {
|
2017-11-16 16:53:12 +01:00
|
|
|
VideoTrack track = videoTracks.get(0 /* index */);
|
2013-08-12 23:26:21 +00:00
|
|
|
removeTrack(track);
|
2013-07-10 00:45:36 +00:00
|
|
|
track.dispose();
|
|
|
|
|
}
|
2015-09-15 11:09:34 -07:00
|
|
|
// Remove, but do not release preserved video tracks.
|
|
|
|
|
while (!preservedVideoTracks.isEmpty()) {
|
2017-11-16 16:53:12 +01:00
|
|
|
removeTrack(preservedVideoTracks.get(0 /* index */));
|
2015-09-15 11:09:34 -07:00
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
free(nativeStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String label() {
|
|
|
|
|
return nativeLabel(nativeStream);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 13:34:41 +01:00
|
|
|
@Override
|
2013-07-10 00:45:36 +00:00
|
|
|
public String toString() {
|
|
|
|
|
return "[" + label() + ":A=" + audioTracks.size() + ":V=" + videoTracks.size() + "]";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static native boolean nativeAddAudioTrack(long nativeStream, long nativeAudioTrack);
|
|
|
|
|
|
|
|
|
|
private static native boolean nativeAddVideoTrack(long nativeStream, long nativeVideoTrack);
|
|
|
|
|
|
|
|
|
|
private static native boolean nativeRemoveAudioTrack(long nativeStream, long nativeAudioTrack);
|
|
|
|
|
|
|
|
|
|
private static native boolean nativeRemoveVideoTrack(long nativeStream, long nativeVideoTrack);
|
|
|
|
|
|
|
|
|
|
private static native String nativeLabel(long nativeStream);
|
|
|
|
|
|
|
|
|
|
private static native void free(long nativeStream);
|
|
|
|
|
}
|