1/* 2 * libjingle 3 * Copyright 2013, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28package org.webrtc; 29 30import java.util.LinkedList; 31 32/** Java wrapper for a C++ MediaStreamInterface. */ 33public class MediaStream { 34 public final LinkedList<AudioTrack> audioTracks; 35 public final LinkedList<VideoTrack> videoTracks; 36 // Package-protected for PeerConnection. 37 final long nativeStream; 38 39 public MediaStream(long nativeStream) { 40 audioTracks = new LinkedList<AudioTrack>(); 41 videoTracks = new LinkedList<VideoTrack>(); 42 this.nativeStream = nativeStream; 43 } 44 45 public boolean addTrack(AudioTrack track) { 46 if (nativeAddAudioTrack(nativeStream, track.nativeTrack)) { 47 audioTracks.add(track); 48 return true; 49 } 50 return false; 51 } 52 53 public boolean addTrack(VideoTrack track) { 54 if (nativeAddVideoTrack(nativeStream, track.nativeTrack)) { 55 videoTracks.add(track); 56 return true; 57 } 58 return false; 59 } 60 61 public boolean removeTrack(AudioTrack track) { 62 if (nativeRemoveAudioTrack(nativeStream, track.nativeTrack)) { 63 audioTracks.remove(track); 64 return true; 65 } 66 return false; 67 } 68 69 public boolean removeTrack(VideoTrack track) { 70 if (nativeRemoveVideoTrack(nativeStream, track.nativeTrack)) { 71 videoTracks.remove(track); 72 return true; 73 } 74 return false; 75 } 76 77 public void dispose() { 78 while (!audioTracks.isEmpty()) { 79 AudioTrack track = audioTracks.getFirst(); 80 removeTrack(track); 81 track.dispose(); 82 } 83 while (!videoTracks.isEmpty()) { 84 VideoTrack track = videoTracks.getFirst(); 85 removeTrack(track); 86 track.dispose(); 87 } 88 free(nativeStream); 89 } 90 91 public String label() { 92 return nativeLabel(nativeStream); 93 } 94 95 public String toString() { 96 return "[" + label() + ":A=" + audioTracks.size() + 97 ":V=" + videoTracks.size() + "]"; 98 } 99 100 private static native boolean nativeAddAudioTrack( 101 long nativeStream, long nativeAudioTrack); 102 103 private static native boolean nativeAddVideoTrack( 104 long nativeStream, long nativeVideoTrack); 105 106 private static native boolean nativeRemoveAudioTrack( 107 long nativeStream, long nativeAudioTrack); 108 109 private static native boolean nativeRemoveVideoTrack( 110 long nativeStream, long nativeVideoTrack); 111 112 private static native String nativeLabel(long nativeStream); 113 114 private static native void free(long nativeStream); 115} 116