Ostap Maliuvanchuk
Ostap Maliuvanchuk

Reputation: 1175

How to detect Android WebRTC track ending

MediaStreamTrack from browser version of WebRTC has onended handler which allows to receive notification when the track ends, that is (from MDN docs):

This event occurs when the track will no longer provide data to the stream for any reason, including the end of the media input being reached, the user revoking needed permissions, the source device being removed, or the remote peer ending a connection.

Is there a way to do the same in Android version of WebRTC?

Upvotes: 2

Views: 764

Answers (1)

Jian Zhong
Jian Zhong

Reputation: 501

Recently, i run into this issue. I try to detect the State of a track, I assume, if it is Ended, it was removed. track.State() But the state is always Live, no matter the remote track was removed or not.

Then I check the getCurrentDirection of RtpTransceiver. it works RtpTransceiver.RtpTransceiverDirection direction = transceiver.getCurrentDirection() when the remove track was removed, direction was set to INACTIVE or SEND_ONLY maybe. it means the remote side track was removed and don't send data anymore.

Always check this after renegotiation of webrtc, then process your stuff(after track removed).

List<RtpTransceiver> list = _peerConnection.getTransceivers();
Iterator var1 = list.iterator();
while(var1.hasNext()) {
    RtpTransceiver transceiver = (RtpTransceiver)var1.next();

    RtpTransceiver.RtpTransceiverDirection direction = transceiver.getCurrentDirection();
    if(direction == RtpTransceiver.RtpTransceiverDirection.INACTIVE) {
        // remote track removed, do your stuff
    }
}

Upvotes: 2

Related Questions