• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
10 
11 package org.webrtc;
12 
13 import android.support.annotation.Nullable;
14 import java.util.List;
15 
16 /** Java wrapper for a C++ RtpSenderInterface. */
17 public class RtpSender {
18   private long nativeRtpSender;
19 
20   @Nullable private MediaStreamTrack cachedTrack;
21   private boolean ownsTrack = true;
22   private final @Nullable DtmfSender dtmfSender;
23 
24   @CalledByNative
RtpSender(long nativeRtpSender)25   public RtpSender(long nativeRtpSender) {
26     this.nativeRtpSender = nativeRtpSender;
27     long nativeTrack = nativeGetTrack(nativeRtpSender);
28     cachedTrack = MediaStreamTrack.createMediaStreamTrack(nativeTrack);
29 
30     long nativeDtmfSender = nativeGetDtmfSender(nativeRtpSender);
31     dtmfSender = (nativeDtmfSender != 0) ? new DtmfSender(nativeDtmfSender) : null;
32   }
33 
34   /**
35    * Starts sending a new track, without requiring additional SDP negotiation.
36    * <p>
37    * Note: This is equivalent to replaceTrack in the official WebRTC API. It
38    * was just implemented before the standards group settled on a name.
39    *
40    * @param takeOwnership If true, the RtpSender takes ownership of the track
41    *                      from the caller, and will auto-dispose of it when no
42    *                      longer needed. |takeOwnership| should only be used if
43    *                      the caller owns the track; it is not appropriate when
44    *                      the track is owned by, for example, another RtpSender
45    *                      or a MediaStream.
46    * @return              true on success and false on failure.
47    */
setTrack(@ullable MediaStreamTrack track, boolean takeOwnership)48   public boolean setTrack(@Nullable MediaStreamTrack track, boolean takeOwnership) {
49     checkRtpSenderExists();
50     if (!nativeSetTrack(nativeRtpSender, (track == null) ? 0 : track.getNativeMediaStreamTrack())) {
51       return false;
52     }
53     if (cachedTrack != null && ownsTrack) {
54       cachedTrack.dispose();
55     }
56     cachedTrack = track;
57     ownsTrack = takeOwnership;
58     return true;
59   }
60 
61   @Nullable
track()62   public MediaStreamTrack track() {
63     return cachedTrack;
64   }
65 
setStreams(List<String> streamIds)66   public void setStreams(List<String> streamIds) {
67     checkRtpSenderExists();
68     nativeSetStreams(nativeRtpSender, streamIds);
69   }
70 
getStreams()71   public List<String> getStreams() {
72     checkRtpSenderExists();
73     return nativeGetStreams(nativeRtpSender);
74   }
75 
setParameters(RtpParameters parameters)76   public boolean setParameters(RtpParameters parameters) {
77     checkRtpSenderExists();
78     return nativeSetParameters(nativeRtpSender, parameters);
79   }
80 
getParameters()81   public RtpParameters getParameters() {
82     checkRtpSenderExists();
83     return nativeGetParameters(nativeRtpSender);
84   }
85 
id()86   public String id() {
87     checkRtpSenderExists();
88     return nativeGetId(nativeRtpSender);
89   }
90 
91   @Nullable
dtmf()92   public DtmfSender dtmf() {
93     return dtmfSender;
94   }
95 
setFrameEncryptor(FrameEncryptor frameEncryptor)96   public void setFrameEncryptor(FrameEncryptor frameEncryptor) {
97     checkRtpSenderExists();
98     nativeSetFrameEncryptor(nativeRtpSender, frameEncryptor.getNativeFrameEncryptor());
99   }
100 
dispose()101   public void dispose() {
102     checkRtpSenderExists();
103     if (dtmfSender != null) {
104       dtmfSender.dispose();
105     }
106     if (cachedTrack != null && ownsTrack) {
107       cachedTrack.dispose();
108     }
109     JniCommon.nativeReleaseRef(nativeRtpSender);
110     nativeRtpSender = 0;
111   }
112 
113   /** Returns a pointer to webrtc::RtpSenderInterface. */
getNativeRtpSender()114   long getNativeRtpSender() {
115     checkRtpSenderExists();
116     return nativeRtpSender;
117   }
118 
checkRtpSenderExists()119   private void checkRtpSenderExists() {
120     if (nativeRtpSender == 0) {
121       throw new IllegalStateException("RtpSender has been disposed.");
122     }
123   }
124 
nativeSetTrack(long rtpSender, long nativeTrack)125   private static native boolean nativeSetTrack(long rtpSender, long nativeTrack);
126 
127   // This should increment the reference count of the track.
128   // Will be released in dispose() or setTrack().
nativeGetTrack(long rtpSender)129   private static native long nativeGetTrack(long rtpSender);
130 
nativeSetStreams(long rtpSender, List<String> streamIds)131   private static native void nativeSetStreams(long rtpSender, List<String> streamIds);
132 
nativeGetStreams(long rtpSender)133   private static native List<String> nativeGetStreams(long rtpSender);
134 
135   // This should increment the reference count of the DTMF sender.
136   // Will be released in dispose().
nativeGetDtmfSender(long rtpSender)137   private static native long nativeGetDtmfSender(long rtpSender);
138 
nativeSetParameters(long rtpSender, RtpParameters parameters)139   private static native boolean nativeSetParameters(long rtpSender, RtpParameters parameters);
140 
nativeGetParameters(long rtpSender)141   private static native RtpParameters nativeGetParameters(long rtpSender);
142 
nativeGetId(long rtpSender)143   private static native String nativeGetId(long rtpSender);
144 
nativeSetFrameEncryptor(long rtpSender, long nativeFrameEncryptor)145   private static native void nativeSetFrameEncryptor(long rtpSender, long nativeFrameEncryptor);
146 };
147