ExoPlayer supports both live and on demand RTSP. Supported sample formats and network types are listed below.
Supported sample formats
- H264 (توضیحات رسانه SDP باید شامل دادههای SPS/PPS در ویژگی fmtp برای مقداردهی اولیه رمزگشا باشد).
- AAC (با جریان بیتی ADTS).
- AC3.
Supported network types
- RTP روی UDP تکپخشی (چندپخشی پشتیبانی نمیشود).
- Interleaved RTSP, RTP over RTSP using TCP.
Using MediaItem
برای پخش یک جریان RTSP، باید به ماژول RTSP وابسته باشید.
کاتلین
implementation("androidx.media3:media3-exoplayer-rtsp:1.10.0")
گرووی
implementation "androidx.media3:media3-exoplayer-rtsp:1.10.0"
سپس میتوانید یک MediaItem برای یک RTSP URI ایجاد کنید و آن را به پخشکننده منتقل کنید.
کاتلین
// Create a player instance. val player = ExoPlayer.Builder(context).build() // Set the media item to be played. player.setMediaItem(MediaItem.fromUri(rtspUri)) // Prepare the player. player.prepare()
جاوا
// 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();
احراز هویت
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. val mediaSource: MediaSource = RtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(rtspUri)) // Create a player instance. val player = ExoPlayer.Builder(context).build() // Set the media source to be played. player.setMediaSource(mediaSource) // Prepare the player. player.prepare()
جاوا
// 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();
استفاده از RTSP پشت یک NAT (پشتیبانی از RTP/TCP)
ExoPlayer از UDP به عنوان پروتکل پیشفرض برای انتقال RTP استفاده میکند.
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).
زمان تلاش مجدد با TCP را میتوان با فراخوانی متد RtspMediaSource.Factory.setTimeoutMs() تنظیم کرد. برای مثال، اگر زمان انتظار روی چهار ثانیه تنظیم شود، پخشکننده پس از چهار ثانیه عدم فعالیت UDP، دوباره با TCP تلاش خواهد کرد.
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 سازگاری بهتری را تحت برخی تنظیمات شبکه ارائه میدهد. میتوانید ExoPlayer را طوری پیکربندی کنید که به طور پیشفرض از RTP/TCP با RtspMediaSource.Factory.setForceUseRtpTcp() استفاده کند.
Passing a custom SocketFactory
نمونههای سفارشی SocketFactory میتوانند زمانی مفید باشند که مسیریابی خاصی مورد نیاز باشد (برای مثال، زمانی که ترافیک RTSP باید از یک رابط خاص عبور کند، یا سوکت به پرچمهای اتصال اضافی نیاز دارد).
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. val mediaSource: MediaSource = RtspMediaSource.Factory() .setSocketFactory(socketFactory) .createMediaSource(MediaItem.fromUri(rtspUri))
جاوا
// Create an RTSP media source pointing to an RTSP uri and override the socket // factory. MediaSource mediaSource = new RtspMediaSource.Factory() .setSocketFactory(socketFactory) .createMediaSource(MediaItem.fromUri(rtspUri));