Throw IllegalStateException if native objects are used after dispose.

This makes it easier to debug issues related to double dispose /
use after dispose.

Bug: webrtc:7566, webrtc:8297
Change-Id: I07429b2b794deabb62b5f3ea1cf92eea6f66a149
Reviewed-on: https://webrtc-review.googlesource.com/102540
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24894}
diff --git a/sdk/android/api/org/webrtc/RtpTransceiver.java b/sdk/android/api/org/webrtc/RtpTransceiver.java
index 93326a4..a4a5aa0 100644
--- a/sdk/android/api/org/webrtc/RtpTransceiver.java
+++ b/sdk/android/api/org/webrtc/RtpTransceiver.java
@@ -13,7 +13,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import org.webrtc.RtpParameters.Encoding;
 
 /**
  * Java wrapper for a C++ RtpTransceiverInterface.
@@ -96,7 +95,7 @@
     }
   }
 
-  private final long nativeRtpTransceiver;
+  private long nativeRtpTransceiver;
   private RtpSender cachedSender;
   private RtpReceiver cachedReceiver;
 
@@ -112,6 +111,7 @@
    * type as well.
    */
   public MediaStreamTrack.MediaType getMediaType() {
+    checkRtpTransceiverExists();
     return nativeGetMediaType(nativeRtpTransceiver);
   }
 
@@ -122,6 +122,7 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-mid
    */
   public String getMid() {
+    checkRtpTransceiverExists();
     return nativeGetMid(nativeRtpTransceiver);
   }
 
@@ -153,6 +154,7 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stopped
    */
   public boolean isStopped() {
+    checkRtpTransceiverExists();
     return nativeStopped(nativeRtpTransceiver);
   }
 
@@ -162,6 +164,7 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction
    */
   public RtpTransceiverDirection getDirection() {
+    checkRtpTransceiverExists();
     return nativeDirection(nativeRtpTransceiver);
   }
 
@@ -172,6 +175,7 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-currentdirection
    */
   public RtpTransceiverDirection getCurrentDirection() {
+    checkRtpTransceiverExists();
     return nativeCurrentDirection(nativeRtpTransceiver);
   }
 
@@ -183,6 +187,7 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction
    */
   public void setDirection(RtpTransceiverDirection rtpTransceiverDirection) {
+    checkRtpTransceiverExists();
     nativeSetDirection(nativeRtpTransceiver, rtpTransceiverDirection);
   }
 
@@ -192,14 +197,23 @@
    * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stop
    */
   public void stop() {
+    checkRtpTransceiverExists();
     nativeStop(nativeRtpTransceiver);
   }
 
   @CalledByNative
   public void dispose() {
+    checkRtpTransceiverExists();
     cachedSender.dispose();
     cachedReceiver.dispose();
     JniCommon.nativeReleaseRef(nativeRtpTransceiver);
+    nativeRtpTransceiver = 0;
+  }
+
+  private void checkRtpTransceiverExists() {
+    if (nativeRtpTransceiver == 0) {
+      throw new IllegalStateException("RtpTransceiver has been disposed.");
+    }
   }
 
   private static native MediaStreamTrack.MediaType nativeGetMediaType(long rtpTransceiver);