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