exoplayer播放RTSP和RTP流媒体

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.

// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the media item to be played.
player.setMediaItem(MediaItem.fromUri(rtspUri));
// Prepare the player.
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.

// Create an RTSP media source pointing to an RTSP uri.
MediaSource mediaSource =
    new RtspMediaSource.Factory()
        .createMediaSource(MediaItem.fromUri(rtspUri));
// Create a player instance.
ExoPlayer player = new ExoPlayer.Builder(context).build();
// Set the media source to be played.
player.setMediaSource(mediaSource);
// Prepare the player.
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().

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

转自:RTSP - ExoPlayer

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是MATLAB调用rtsp的示例代码: ```matlab url = 'rtsp://username:password@ip_address:port/path'; % RTSP URL vidObj = VideoReader(url); % 创建VideoReader对象 while hasFrame(vidObj) img = readFrame(vidObj); % 读取视频帧 imshow(img); % 显示视频帧 end ``` 对于C++ RTSP/RTP流媒体客户端,可以使用开源的Live555库。以下是示例代码: ```cpp #include <liveMedia.hh> #include <BasicUsageEnvironment.hh> #include <stdio.h> int main(int argc, char** argv) { TaskScheduler* scheduler = BasicTaskScheduler::createNew(); UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler); RTSPClient* rtspClient = RTSPClient::createNew(*env, "rtsp://username:password@ip_address:port/path", 0, "username:password"); if (rtspClient == NULL) { fprintf(stderr, "Failed to create RTSP client\n"); exit(1); } RTSPStream* rtspStream = rtspClient->streamingOverTCP() ? (RTSPStream*)new TCPStreamSink(*env) : (RTSPStream*)new UDPStreamSink(*env); if (rtspStream == NULL) { fprintf(stderr, "Failed to create RTSP stream\n"); exit(1); } rtspClient->sendDescribeCommand(continueAfterDESCRIBE, rtspStream); env->taskScheduler().doEventLoop(); // 进入事件循环 Medium::close(rtspStream); Medium::close(rtspClient); env->reclaim(); delete scheduler; return 0; } void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString) { if (resultCode == 0) { rtspClient->sendSetupCommand(continueAfterSETUP, False, False); } else { fprintf(stderr, "Failed to send DESCRIBE command: %s\n", resultString); exit(1); } } void continueAfterSETUP(RTSPClient* rtspClient, int resultCode, char* resultString) { if (resultCode == 0) { rtspClient->sendPlayCommand(continueAfterPLAY); } else { fprintf(stderr, "Failed to send SETUP command: %s\n", resultString); exit(1); } } void continueAfterPLAY(RTSPClient* rtspClient, int resultCode, char* resultString) { if (resultCode != 0) { fprintf(stderr, "Failed to send PLAY command: %s\n", resultString); exit(1); } } ``` 需要注意的是,在使用Live555库时需要将其编译后才能使用。具体编译方法可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值