deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | /** Java wrapper for a C++ RtpSenderInterface. */ |
| 14 | public class RtpSender { |
| 15 | final long nativeRtpSender; |
| 16 | |
| 17 | private MediaStreamTrack cachedTrack; |
Taylor Brandstetter | e5ba13b | 2016-01-07 15:11:25 -0800 | [diff] [blame] | 18 | private boolean ownsTrack = true; |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 19 | |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 20 | private final DtmfSender dtmfSender; |
| 21 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 22 | public RtpSender(long nativeRtpSender) { |
| 23 | this.nativeRtpSender = nativeRtpSender; |
| 24 | long track = nativeGetTrack(nativeRtpSender); |
| 25 | // It may be possible for an RtpSender to be created without a track. |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 26 | cachedTrack = (track != 0) ? new MediaStreamTrack(track) : null; |
| 27 | |
| 28 | long nativeDtmfSender = nativeGetDtmfSender(nativeRtpSender); |
| 29 | dtmfSender = (nativeDtmfSender != 0) ? new DtmfSender(nativeDtmfSender) : null; |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 30 | } |
| 31 | |
deadbeef | 7a24688 | 2017-08-09 08:40:10 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Starts sending a new track, without requiring additional SDP negotiation. |
| 34 | * <p> |
| 35 | * Note: This is equivalent to replaceTrack in the official WebRTC API. It |
| 36 | * was just implemented before the standards group settled on a name. |
| 37 | * |
| 38 | * @param takeOwnership If true, the RtpSender takes ownership of the track |
| 39 | * from the caller, and will auto-dispose of it when no |
| 40 | * longer needed. |takeOwnership| should only be used if |
| 41 | * the caller owns the track; it is not appropriate when |
| 42 | * the track is owned by, for example, another RtpSender |
| 43 | * or a MediaStream. |
| 44 | * @return true on success and false on failure. |
| 45 | */ |
Taylor Brandstetter | e5ba13b | 2016-01-07 15:11:25 -0800 | [diff] [blame] | 46 | public boolean setTrack(MediaStreamTrack track, boolean takeOwnership) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 47 | if (!nativeSetTrack(nativeRtpSender, (track == null) ? 0 : track.nativeTrack)) { |
| 48 | return false; |
Taylor Brandstetter | e5ba13b | 2016-01-07 15:11:25 -0800 | [diff] [blame] | 49 | } |
| 50 | if (cachedTrack != null && ownsTrack) { |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 51 | cachedTrack.dispose(); |
| 52 | } |
| 53 | cachedTrack = track; |
Taylor Brandstetter | e5ba13b | 2016-01-07 15:11:25 -0800 | [diff] [blame] | 54 | ownsTrack = takeOwnership; |
| 55 | return true; |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public MediaStreamTrack track() { |
| 59 | return cachedTrack; |
| 60 | } |
| 61 | |
skvlad | 303b3c2 | 2016-03-24 19:36:46 -0700 | [diff] [blame] | 62 | public boolean setParameters(RtpParameters parameters) { |
| 63 | return nativeSetParameters(nativeRtpSender, parameters); |
| 64 | } |
| 65 | |
| 66 | public RtpParameters getParameters() { |
| 67 | return nativeGetParameters(nativeRtpSender); |
| 68 | } |
| 69 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 70 | public String id() { |
| 71 | return nativeId(nativeRtpSender); |
| 72 | } |
| 73 | |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 74 | public DtmfSender dtmf() { |
| 75 | return dtmfSender; |
| 76 | } |
| 77 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 78 | public void dispose() { |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 79 | if (dtmfSender != null) { |
| 80 | dtmfSender.dispose(); |
| 81 | } |
Taylor Brandstetter | e5ba13b | 2016-01-07 15:11:25 -0800 | [diff] [blame] | 82 | if (cachedTrack != null && ownsTrack) { |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 83 | cachedTrack.dispose(); |
| 84 | } |
magjed | b1c7453 | 2017-08-27 13:47:20 -0700 | [diff] [blame] | 85 | JniCommon.nativeReleaseRef(nativeRtpSender); |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 88 | private static native boolean nativeSetTrack(long nativeRtpSender, long nativeTrack); |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 89 | |
| 90 | // This should increment the reference count of the track. |
| 91 | // Will be released in dispose() or setTrack(). |
| 92 | private static native long nativeGetTrack(long nativeRtpSender); |
| 93 | |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 94 | // This should increment the reference count of the DTMF sender. |
| 95 | // Will be released in dispose(). |
| 96 | private static native long nativeGetDtmfSender(long nativeRtpSender); |
| 97 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 98 | private static native boolean nativeSetParameters(long nativeRtpSender, RtpParameters parameters); |
skvlad | 303b3c2 | 2016-03-24 19:36:46 -0700 | [diff] [blame] | 99 | |
| 100 | private static native RtpParameters nativeGetParameters(long nativeRtpSender); |
| 101 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 102 | private static native String nativeId(long nativeRtpSender); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 103 | }; |