当前位置:   article > 正文

exoplayer播放RTSP和RTP流媒体_exoplayer rtsp

exoplayer rtsp

RTSP

This documentation may be out-of-date. Please refer to the documentation for the latest ExoPlayer release on developer.android.com.

ExoPlayer supports both live and on demand RTSP. Supported sample formats and network types are listed below.

Supported sample formats

  • H264 (the SDP media description must include SPS/PPS data in the fmtp attribute for decoder initialization).
  • AAC (with ADTS bitstream).
  • AC3.

Please comment on this issue to request support for additional sample formats.

Supported network types

  • RTP over UDP unicast (multicast is not supported).
  • Interleaved RTSP, RTP over RTSP using TCP.

Using MediaItem

To play an RTSP stream, you need to depend on the RTSP module.

implementation 'com.google.android.exoplayer:exoplayer-rtsp:2.X.X'

You can then create a MediaItem for an RTSP URI and pass it to the player.

  1. // Create a player instance.
  2. ExoPlayer player = new ExoPlayer.Builder(context).build();
  3. // Set the media item to be played.
  4. player.setMediaItem(MediaItem.fromUri(rtspUri));
  5. // Prepare the player.
  6. player.prepare();

Authentication

ExoPlayer supports playback with RTSP BASIC and DIGEST authentication. To play protected RTSP content, the MediaItem’s URI must be configured with the authentication info. Specifically, the URI should be of the form rtsp://<username>:<password>@<host address>.

Using RtspMediaSource

For more customization options, you can create an RtspMediaSource and pass it directly to the player instead of a MediaItem.

  1. // Create an RTSP media source pointing to an RTSP uri.
  2. MediaSource mediaSource =
  3. new RtspMediaSource.Factory()
  4. .createMediaSource(MediaItem.fromUri(rtspUri));
  5. // Create a player instance.
  6. ExoPlayer player = new ExoPlayer.Builder(context).build();
  7. // Set the media source to be played.
  8. player.setMediaSource(mediaSource);
  9. // Prepare the player.
  10. player.prepare();

Using RTSP behind a NAT (RTP/TCP support)

ExoPlayer uses UDP as the default protocol for RTP transport.

When streaming RTSP behind a NAT layer, the NAT might not be able to forward the incoming RTP/UDP packets to the device. This occurs if the NAT lacks the necessary UDP port mapping. If ExoPlayer detects there have not been incoming RTP packets for a while and the playback has not started yet, ExoPlayer tears down the current RTSP playback session, and retries playback using RTP-over-RTSP (transmitting RTP packets using the TCP connection opened for RTSP).

The timeout for retrying with TCP can be customized by calling the method RtspMediaSource.Factory.setTimeoutMs(). For example, if the timeout is set to four seconds, the player will retry with TCP after four seconds of UDP inactivity.

Setting the timeout also affects the end-of-stream detection logic. That is, ExoPlayer will report the playback has ended if nothing is received for the duration of the set timeout. Setting this value too small may lead to an early end-of-stream signal under poor network conditions.

RTP/TCP offers better compatibility under some network setups. You can configure ExoPlayer to use RTP/TCP by default with RtspMediaSource.Factory.setForceUseRtpTcp().

Passing a custom SocketFactory

Custom SocketFactory instances can be useful when particular routing is required (e.g. when RTSP traffic needs to pass a specific interface, or the socket needs additional connectivity flags).

By default, RtspMediaSource will use Java’s standard socket factory (SocketFactory.getDefault()) to create connections to the remote endpoints. This behavior can be overridden using RtspMediaSource.Factory.setSocketFactory().

  1. // Create an RTSP media source pointing to an RTSP uri and override the socket
  2. // factory.
  3. MediaSource mediaSource =
  4. new RtspMediaSource.Factory()
  5. .setSocketFactory(...)
  6. .createMediaSource(MediaItem.fromUri(rtspUri));

转自:RTSP - ExoPlayer

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/852525
推荐阅读
相关标签
  

闽ICP备14008679号