blob: fbb62db853c3cbe9e0f15b125cddbdbf9d9c4165 [file] [log] [blame]
deadbeef4139c0f2015-10-06 12:29:25 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef4139c0f2015-10-06 12:29:25 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
deadbeef4139c0f2015-10-06 12:29:25 -07009 */
10
11package org.webrtc;
12
13/** Java wrapper for a C++ RtpSenderInterface. */
14public class RtpSender {
15 final long nativeRtpSender;
16
17 private MediaStreamTrack cachedTrack;
Taylor Brandstettere5ba13b2016-01-07 15:11:25 -080018 private boolean ownsTrack = true;
deadbeef4139c0f2015-10-06 12:29:25 -070019
deadbeef24af6632017-02-01 21:53:09 -080020 private final DtmfSender dtmfSender;
21
deadbeef4139c0f2015-10-06 12:29:25 -070022 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.
deadbeef24af6632017-02-01 21:53:09 -080026 cachedTrack = (track != 0) ? new MediaStreamTrack(track) : null;
27
28 long nativeDtmfSender = nativeGetDtmfSender(nativeRtpSender);
29 dtmfSender = (nativeDtmfSender != 0) ? new DtmfSender(nativeDtmfSender) : null;
deadbeef4139c0f2015-10-06 12:29:25 -070030 }
31
deadbeef7a246882017-08-09 08:40:10 -070032 /**
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 Brandstettere5ba13b2016-01-07 15:11:25 -080046 public boolean setTrack(MediaStreamTrack track, boolean takeOwnership) {
sakalb6760f92016-09-29 04:12:44 -070047 if (!nativeSetTrack(nativeRtpSender, (track == null) ? 0 : track.nativeTrack)) {
48 return false;
Taylor Brandstettere5ba13b2016-01-07 15:11:25 -080049 }
50 if (cachedTrack != null && ownsTrack) {
deadbeef4139c0f2015-10-06 12:29:25 -070051 cachedTrack.dispose();
52 }
53 cachedTrack = track;
Taylor Brandstettere5ba13b2016-01-07 15:11:25 -080054 ownsTrack = takeOwnership;
55 return true;
deadbeef4139c0f2015-10-06 12:29:25 -070056 }
57
58 public MediaStreamTrack track() {
59 return cachedTrack;
60 }
61
skvlad303b3c22016-03-24 19:36:46 -070062 public boolean setParameters(RtpParameters parameters) {
63 return nativeSetParameters(nativeRtpSender, parameters);
64 }
65
66 public RtpParameters getParameters() {
67 return nativeGetParameters(nativeRtpSender);
68 }
69
deadbeef4139c0f2015-10-06 12:29:25 -070070 public String id() {
71 return nativeId(nativeRtpSender);
72 }
73
deadbeef24af6632017-02-01 21:53:09 -080074 public DtmfSender dtmf() {
75 return dtmfSender;
76 }
77
deadbeef4139c0f2015-10-06 12:29:25 -070078 public void dispose() {
deadbeef24af6632017-02-01 21:53:09 -080079 if (dtmfSender != null) {
80 dtmfSender.dispose();
81 }
Taylor Brandstettere5ba13b2016-01-07 15:11:25 -080082 if (cachedTrack != null && ownsTrack) {
deadbeef4139c0f2015-10-06 12:29:25 -070083 cachedTrack.dispose();
84 }
magjedb1c74532017-08-27 13:47:20 -070085 JniCommon.nativeReleaseRef(nativeRtpSender);
deadbeef4139c0f2015-10-06 12:29:25 -070086 }
87
sakalb6760f92016-09-29 04:12:44 -070088 private static native boolean nativeSetTrack(long nativeRtpSender, long nativeTrack);
deadbeef4139c0f2015-10-06 12:29:25 -070089
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
deadbeef24af6632017-02-01 21:53:09 -080094 // 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
sakalb6760f92016-09-29 04:12:44 -070098 private static native boolean nativeSetParameters(long nativeRtpSender, RtpParameters parameters);
skvlad303b3c22016-03-24 19:36:46 -070099
100 private static native RtpParameters nativeGetParameters(long nativeRtpSender);
101
deadbeef4139c0f2015-10-06 12:29:25 -0700102 private static native String nativeId(long nativeRtpSender);
sakalb6760f92016-09-29 04:12:44 -0700103};