xref: /aosp_15_r20/external/exoplayer/tree_15dc86382f17a24a3e881e52e31a810c1ea44b49/docs/rtsp.md (revision 30877f796caf59d855b10b687a5d6b3918d765cb)
1*30877f79SAndroid Build Coastguard Worker---
2*30877f79SAndroid Build Coastguard Workertitle: RTSP
3*30877f79SAndroid Build Coastguard Worker---
4*30877f79SAndroid Build Coastguard Worker
5*30877f79SAndroid Build Coastguard Worker{% include_relative _page_fragments/supported-formats-rtsp.md %}
6*30877f79SAndroid Build Coastguard Worker
7*30877f79SAndroid Build Coastguard Worker## Using MediaItem ##
8*30877f79SAndroid Build Coastguard Worker
9*30877f79SAndroid Build Coastguard WorkerTo play an RTSP stream, you need to depend on the RTSP module.
10*30877f79SAndroid Build Coastguard Worker
11*30877f79SAndroid Build Coastguard Worker~~~
12*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:exoplayer-rtsp:2.X.X'
13*30877f79SAndroid Build Coastguard Worker~~~
14*30877f79SAndroid Build Coastguard Worker{: .language-gradle}
15*30877f79SAndroid Build Coastguard Worker
16*30877f79SAndroid Build Coastguard WorkerYou can then create a `MediaItem` for an RTSP URI and pass it to the player.
17*30877f79SAndroid Build Coastguard Worker
18*30877f79SAndroid Build Coastguard Worker~~~
19*30877f79SAndroid Build Coastguard Worker// Create a player instance.
20*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build();
21*30877f79SAndroid Build Coastguard Worker// Set the media item to be played.
22*30877f79SAndroid Build Coastguard Workerplayer.setMediaItem(MediaItem.fromUri(rtspUri));
23*30877f79SAndroid Build Coastguard Worker// Prepare the player.
24*30877f79SAndroid Build Coastguard Workerplayer.prepare();
25*30877f79SAndroid Build Coastguard Worker~~~
26*30877f79SAndroid Build Coastguard Worker{: .language-java}
27*30877f79SAndroid Build Coastguard Worker
28*30877f79SAndroid Build Coastguard Worker### Authentication ###
29*30877f79SAndroid Build Coastguard Worker
30*30877f79SAndroid Build Coastguard WorkerExoPlayer supports playback with RTSP BASIC and DIGEST authentication. To play
31*30877f79SAndroid Build Coastguard Workerprotected RTSP content, the `MediaItem`'s URI must be configured with the
32*30877f79SAndroid Build Coastguard Workerauthentication info. Specifically, the URI should be of the form
33*30877f79SAndroid Build Coastguard Worker`rtsp://<username>:<password>@<host address>`.
34*30877f79SAndroid Build Coastguard Worker
35*30877f79SAndroid Build Coastguard Worker## Using RtspMediaSource ##
36*30877f79SAndroid Build Coastguard Worker
37*30877f79SAndroid Build Coastguard WorkerFor more customization options, you can create an `RtspMediaSource` and pass it
38*30877f79SAndroid Build Coastguard Workerdirectly to the player instead of a `MediaItem`.
39*30877f79SAndroid Build Coastguard Worker
40*30877f79SAndroid Build Coastguard Worker~~~
41*30877f79SAndroid Build Coastguard Worker// Create an RTSP media source pointing to an RTSP uri.
42*30877f79SAndroid Build Coastguard WorkerMediaSource mediaSource =
43*30877f79SAndroid Build Coastguard Worker    new RtspMediaSource.Factory()
44*30877f79SAndroid Build Coastguard Worker        .createMediaSource(MediaItem.fromUri(rtspUri));
45*30877f79SAndroid Build Coastguard Worker// Create a player instance.
46*30877f79SAndroid Build Coastguard WorkerExoPlayer player = new ExoPlayer.Builder(context).build();
47*30877f79SAndroid Build Coastguard Worker// Set the media source to be played.
48*30877f79SAndroid Build Coastguard Workerplayer.setMediaSource(mediaSource);
49*30877f79SAndroid Build Coastguard Worker// Prepare the player.
50*30877f79SAndroid Build Coastguard Workerplayer.prepare();
51*30877f79SAndroid Build Coastguard Worker~~~
52*30877f79SAndroid Build Coastguard Worker{: .language-java}
53*30877f79SAndroid Build Coastguard Worker
54*30877f79SAndroid Build Coastguard Worker## Using RTSP behind a NAT (RTP/TCP support) ##
55*30877f79SAndroid Build Coastguard Worker
56*30877f79SAndroid Build Coastguard WorkerExoPlayer uses UDP as the default protocol for RTP transport.
57*30877f79SAndroid Build Coastguard Worker
58*30877f79SAndroid Build Coastguard WorkerWhen streaming RTSP behind a NAT layer, the NAT might not be able to forward the
59*30877f79SAndroid Build Coastguard Workerincoming RTP/UDP packets to the device. This occurs if the NAT lacks the
60*30877f79SAndroid Build Coastguard Workernecessary UDP port mapping. If ExoPlayer detects there have not been incoming
61*30877f79SAndroid Build Coastguard WorkerRTP packets for a while and the playback has not started yet, ExoPlayer tears
62*30877f79SAndroid Build Coastguard Workerdown the current RTSP playback session, and retries playback using RTP-over-RTSP
63*30877f79SAndroid Build Coastguard Worker(transmitting RTP packets using the TCP connection opened for RTSP).
64*30877f79SAndroid Build Coastguard Worker
65*30877f79SAndroid Build Coastguard WorkerThe timeout for retrying with TCP can be customized by calling the method
66*30877f79SAndroid Build Coastguard Worker`RtspMediaSource.Factory.setTimeoutMs()`. For example, if the timeout is set to
67*30877f79SAndroid Build Coastguard Workerfour seconds, the player will retry with TCP after four seconds of UDP
68*30877f79SAndroid Build Coastguard Workerinactivity.
69*30877f79SAndroid Build Coastguard Worker
70*30877f79SAndroid Build Coastguard WorkerSetting the timeout also affects the end-of-stream detection logic. That is,
71*30877f79SAndroid Build Coastguard WorkerExoPlayer will report the playback has ended if nothing is received for the
72*30877f79SAndroid Build Coastguard Workerduration of the set timeout. Setting this value too small may lead to an early
73*30877f79SAndroid Build Coastguard Workerend-of-stream signal under poor network conditions.
74*30877f79SAndroid Build Coastguard Worker
75*30877f79SAndroid Build Coastguard WorkerRTP/TCP offers better compatibility under some network setups. You can configure
76*30877f79SAndroid Build Coastguard WorkerExoPlayer to use RTP/TCP by default with
77*30877f79SAndroid Build Coastguard Worker`RtspMediaSource.Factory.setForceUseRtpTcp()`.
78*30877f79SAndroid Build Coastguard Worker
79*30877f79SAndroid Build Coastguard Worker### Passing a custom SocketFactory
80*30877f79SAndroid Build Coastguard WorkerCustom `SocketFactory` instances can be useful when particular routing is
81*30877f79SAndroid Build Coastguard Workerrequired (e.g. when RTSP traffic needs to pass a specific interface, or the
82*30877f79SAndroid Build Coastguard Workersocket needs additional connectivity flags).
83*30877f79SAndroid Build Coastguard Worker
84*30877f79SAndroid Build Coastguard WorkerBy default, `RtspMediaSource` will use Java's standard socket factory
85*30877f79SAndroid Build Coastguard Worker(`SocketFactory.getDefault()`) to create connections to the remote endpoints.
86*30877f79SAndroid Build Coastguard WorkerThis behavior can be overridden using
87*30877f79SAndroid Build Coastguard Worker`RtspMediaSource.Factory.setSocketFactory()`.
88*30877f79SAndroid Build Coastguard Worker
89*30877f79SAndroid Build Coastguard Worker~~~
90*30877f79SAndroid Build Coastguard Worker// Create an RTSP media source pointing to an RTSP uri and override the socket
91*30877f79SAndroid Build Coastguard Worker// factory.
92*30877f79SAndroid Build Coastguard WorkerMediaSource mediaSource =
93*30877f79SAndroid Build Coastguard Worker    new RtspMediaSource.Factory()
94*30877f79SAndroid Build Coastguard Worker        .setSocketFactory(...)
95*30877f79SAndroid Build Coastguard Worker        .createMediaSource(MediaItem.fromUri(rtspUri));
96*30877f79SAndroid Build Coastguard Worker~~~
97*30877f79SAndroid Build Coastguard Worker{: .language-java}
98*30877f79SAndroid Build Coastguard Worker
99