Package tvi.webrtc

Class PeerConnection

java.lang.Object
tvi.webrtc.PeerConnection

public class PeerConnection extends Object
Java-land version of the PeerConnection APIs; wraps the C++ API http://www.webrtc.org/reference/native-apis, which in turn is inspired by the JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and http://www.w3.org/TR/mediacapture-streams/
  • Constructor Details

    • PeerConnection

      public PeerConnection(NativePeerConnectionFactory factory)
      Wraps a PeerConnection created by the factory. Can be used by clients that want to implement their PeerConnection creation in JNI.
  • Method Details

    • getLocalDescription

      public SessionDescription getLocalDescription()
    • getRemoteDescription

      public SessionDescription getRemoteDescription()
    • getCertificate

      public RtcCertificatePem getCertificate()
    • createDataChannel

      public DataChannel createDataChannel(String label, DataChannel.Init init)
    • createOffer

      public void createOffer(SdpObserver observer, MediaConstraints constraints)
    • createAnswer

      public void createAnswer(SdpObserver observer, MediaConstraints constraints)
    • setLocalDescription

      public void setLocalDescription(SdpObserver observer)
    • setLocalDescription

      public void setLocalDescription(SdpObserver observer, SessionDescription sdp)
    • setRemoteDescription

      public void setRemoteDescription(SdpObserver observer, SessionDescription sdp)
    • restartIce

      public void restartIce()
      Tells the PeerConnection that ICE should be restarted.
    • setAudioPlayout

      public void setAudioPlayout(boolean playout)
      Enables/disables playout of received audio streams. Enabled by default. Note that even if playout is enabled, streams will only be played out if the appropriate SDP is also applied. The main purpose of this API is to be able to control the exact time when audio playout starts.
    • setAudioRecording

      public void setAudioRecording(boolean recording)
      Enables/disables recording of transmitted audio streams. Enabled by default. Note that even if recording is enabled, streams will only be recorded if the appropriate SDP is also applied. The main purpose of this API is to be able to control the exact time when audio recording starts.
    • setConfiguration

      public boolean setConfiguration(PeerConnection.RTCConfiguration config)
    • addIceCandidate

      public boolean addIceCandidate(IceCandidate candidate)
    • addIceCandidate

      public void addIceCandidate(IceCandidate candidate, AddIceObserver observer)
    • removeIceCandidates

      public boolean removeIceCandidates(IceCandidate[] candidates)
    • addStream

      public boolean addStream(MediaStream stream)
      Adds a new MediaStream to be sent on this peer connection. Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use addTrack instead.
    • removeStream

      public void removeStream(MediaStream stream)
      Removes the given media stream from this peer connection. This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use removeTrack instead.
    • createSender

      public RtpSender createSender(String kind, String stream_id)
      Creates an RtpSender without a track.

      This method allows an application to cause the PeerConnection to negotiate sending/receiving a specific media type, but without having a track to send yet.

      When the application does want to begin sending a track, it can call RtpSender.setTrack, which doesn't require any additional SDP negotiation.

      Example use:

       
       audioSender = pc.createSender("audio", "stream1");
       videoSender = pc.createSender("video", "stream1");
       // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
       // media parameters....
       // Later, when the endpoint is ready to actually begin sending:
       audioSender.setTrack(audioTrack, false);
       videoSender.setTrack(videoTrack, false);
       
       

      Note: This corresponds most closely to "addTransceiver" in the official WebRTC API, in that it creates a sender without a track. It was implemented before addTransceiver because it provides useful functionality, and properly implementing transceivers would have required a great deal more work.

      Note: This is only available with SdpSemantics.PLAN_B specified. Please use addTransceiver instead.

      Parameters:
      kind - Corresponds to MediaStreamTrack kinds (must be "audio" or "video").
      stream_id - The ID of the MediaStream that this sender's track will be associated with when SDP is applied to the remote PeerConnection. If createSender is used to create an audio and video sender that should be synchronized, they should use the same stream ID.
      Returns:
      A new RtpSender object if successful, or null otherwise.
    • getSenders

      public List<RtpSender> getSenders()
      Gets all RtpSenders associated with this peer connection. Note that calling getSenders will dispose of the senders previously returned.
    • getReceivers

      public List<RtpReceiver> getReceivers()
      Gets all RtpReceivers associated with this peer connection. Note that calling getReceivers will dispose of the receivers previously returned.
    • getTransceivers

      public List<RtpTransceiver> getTransceivers()
      Gets all RtpTransceivers associated with this peer connection. Note that calling getTransceivers will dispose of the transceivers previously returned. Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
    • addTrack

      public RtpSender addTrack(MediaStreamTrack track)
      Adds a new media stream track to be sent on this peer connection, and returns the newly created RtpSender. If streamIds are specified, the RtpSender will be associated with the streams specified in the streamIds list.
      Throws:
      IllegalStateException - if an error accors in C++ addTrack. An error can occur if: - A sender already exists for the track. - The peer connection is closed.
    • addTrack

      public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds)
    • removeTrack

      public boolean removeTrack(RtpSender sender)
      Stops sending media from sender. The sender will still appear in getSenders. Future calls to createOffer will mark the m section for the corresponding transceiver as receive only or inactive, as defined in JSEP. Returns true on success.
    • addTransceiver

      public RtpTransceiver addTransceiver(MediaStreamTrack track)
      Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a transceiver will cause future calls to CreateOffer to add a media description for the corresponding transceiver.

      The initial value of `mid` in the returned transceiver is null. Setting a new session description may change it to a non-null value.

      https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver

      If a MediaStreamTrack is specified then a transceiver will be added with a sender set to transmit the given track. The kind of the transceiver (and sender/receiver) will be derived from the kind of the track.

      If MediaType is specified then a transceiver will be added based upon that type. This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.

      Optionally, an RtpTransceiverInit structure can be specified to configure the transceiver from construction. If not specified, the transceiver will default to having a direction of kSendRecv and not be part of any streams.

      Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.

      Throws:
      IllegalStateException - if an error accors in C++ addTransceiver
    • addTransceiver

      public RtpTransceiver addTransceiver(MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init)
    • addTransceiver

      public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType)
    • addTransceiver

      public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init)
    • getStats

      @Deprecated public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track)
      Deprecated.
    • getStats

      public void getStats(RTCStatsCollectorCallback callback)
      Gets stats using the new stats collection API, see webrtc/api/stats/. These will replace old stats collection API when the new API has matured enough.
    • getStats

      public void getStats(RtpSender sender, RTCStatsCollectorCallback callback)
      Gets stats using the new stats collection API, see webrtc/api/stats/. These will replace old stats collection API when the new API has matured enough.
    • getStats

      public void getStats(RtpReceiver receiver, RTCStatsCollectorCallback callback)
      Gets stats using the new stats collection API, see webrtc/api/stats/. These will replace old stats collection API when the new API has matured enough.
    • setBitrate

      public boolean setBitrate(Integer min, Integer current, Integer max)
      Limits the bandwidth allocated for all RTP streams sent by this PeerConnection. Pass null to leave a value unchanged.
    • startRtcEventLog

      public boolean startRtcEventLog(int file_descriptor, int max_size_bytes)
      Starts recording an RTC event log. Ownership of the file is transfered to the native code. If an RTC event log is already being recorded, it will be stopped and a new one will start using the provided file. Logging will continue until the stopRtcEventLog function is called. The max_size_bytes argument is ignored, it is added for future use.
    • stopRtcEventLog

      public void stopRtcEventLog()
      Stops recording an RTC event log. If no RTC event log is currently being recorded, this call will have no effect.
    • signalingState

      public PeerConnection.SignalingState signalingState()
    • iceConnectionState

      public PeerConnection.IceConnectionState iceConnectionState()
    • connectionState

      public PeerConnection.PeerConnectionState connectionState()
    • iceGatheringState

      public PeerConnection.IceGatheringState iceGatheringState()
    • close

      public void close()
    • dispose

      public void dispose()
      Free native resources associated with this PeerConnection instance. This method removes a reference count from the C++ PeerConnection object, which should result in it being destroyed. It also calls equivalent "dispose" methods on the Java objects attached to this PeerConnection (streams, senders, receivers), such that their associated C++ objects will also be destroyed.

      Note that this method cannot be safely called from an observer callback (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for example, destroy the PeerConnection after an "ICE failed" callback, you must do this asynchronously (in other words, unwind the stack first). See bug 3721 for more details.

    • getNativePeerConnection

      public long getNativePeerConnection()
      Returns a pointer to the native webrtc::PeerConnectionInterface.
    • createNativePeerConnectionObserver

      public static long createNativePeerConnectionObserver(PeerConnection.Observer observer)