You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ff5dc66
Browse filesBrowse the repository at this point in the historyBrowse files
feat: play RTSP streams, with timeouts and interruptible reads
A source can now be a live stream behind an rtsp:// URL, such as an IP
camera's, as well as a local file. The FFmpeg build enables networking,
the RTSP demuxer and the RTP, UDP and TCP protocols. FFmpeg asks for RTP
over UDP first and falls back to TCP, interleaved on the RTSP
connection, when the server refuses UDP or nothing arrives.
A network source can stall or vanish, so every call that waits on one
now runs under a deadline, enforced through FFmpeg's interrupt
callback: opening, probing, each read, seeking and closing. Running out
of time reads as a timeout. The timeout is 10 seconds unless another is
passed to MediaReader or MediaFileSource. Closing a player interrupts
its reader first, so it never waits on a stalled stream, and the error
that interruption causes is not reported to the listener.
Every source is opened with a protocol whitelist. The RTSP demuxer
brings the HTTP protocol along for tunnelling, and a URL passed on from
a user must not be able to fetch through it.
Testing against a server showed three more things to fix. An error
opening a source quoted its URL, password included, and now leaves the
credentials out. A looping player ignored a failed rewind, so a live
stream that ended while looping reported a connection error; a source
that cannot rewind now simply ends. And network errors read as bare
numbers on Windows, where the C runtime has no words for them and FFmpeg
passes most Winsock codes through; the common ones are now named, in a
single helper that replaces three copies.
The examples take stream URLs as well: the player shows a stream's URL
without its credentials and disables looping for a live source, and
both describe how to run them against a camera. The Media Files guide
covers live streams, their timeouts and their limits.
RtspStreamTest plays against a small RTSP server in the test sources,
which refuses UDP and streams PCM audio over TCP.
Copy file name to clipboardExpand all lines: docs/guide/examples.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,37 +57,38 @@ This example is particularly useful for applications that need to implement scre
57
57
The [`MediaFileExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/MediaFileExample.java) demonstrates how to send a media file over a peer connection, in place of a camera and a microphone. See the [Media Files](/guide/media/media-files) guide for the API it uses.
58
58
59
59
**Key features demonstrated:**
60
-
- Opening a media file with a `MediaFileSource`
60
+
- Opening a media file, or a live RTSP stream, with a `MediaFileSource`
61
61
- Reading what the source contains from its `MediaInfo`
62
62
- Creating audio and video tracks from the media sources it feeds
63
63
- Adding those tracks to a peer connection
64
64
- Following playback through a `MediaPlayerListener`
65
65
66
-
This example is useful for applications that stream pre-recorded media, or that need a dependable stand-in for a camera in testing.
66
+
This example is useful for applications that stream pre-recorded media, relay an IP camera into WebRTC, or need a dependable stand-in for a camera in testing.
67
67
68
68
::: info
69
69
This example needs the `webrtc-java-media` module, which builds FFmpeg from the `third-party/ffmpeg` submodule. Make sure the submodule is checked out before building, as the [Media Files](/guide/media/media-files) guide describes.
The [`MediaFilePlayerExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/MediaFilePlayerExample.java) is a Swing application that sends a media file from one peer connection to another in the same process, then shows the received video and plays the received audio in sync. Start and Stop buttons set up and tear down the whole session.
79
+
The [`MediaFilePlayerExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/MediaFilePlayerExample.java) is a Swing application that sends a media file, or a live RTSP stream, from one peer connection to another in the same process, then shows the received video and plays the received audio in sync. Start and Stop buttons set up and tear down the whole session.
79
80
80
81
**Key features demonstrated:**
81
82
- Connecting two peer connections in one application, without a signaling server
82
83
- Using one factory with a dummy audio layer to send the file, and another with the platform audio layer to play what is received
83
84
- Keeping audio and video in sync by sending both tracks in the same media stream
84
85
- Raising the video sender's `maxBitrate` above WebRTC's default, so a high-resolution file is not held to a fraction of its size
85
86
- Rendering received video frames in a Swing component with `VideoBufferConverter`
86
-
- Showing the file's format, the playback position, and live receive metrics (codec, resolution, frame rate, bitrate, packet loss, jitter, audio level) read from `getStats()`
87
+
- Showing the source's format, the playback position, and live receive metrics (codec, resolution, frame rate, bitrate, packet loss, jitter, audio level) read from `getStats()`
87
88
- Releasing senders, peer connections, tracks and the source in order on Stop
88
89
89
90
::: info
90
-
Like the [Media File](#media-file) example, this one needs the `webrtc-java-media` module. The file argument is optional; a file can also be chosen from the window.
91
+
Like the [Media File](#media-file) example, this one needs the `webrtc-java-media` module. The source argument is optional; a file can also be chosen from the window, or a stream URL typed into it. A stream URL's credentials are never shown, and the Loop box is disabled while a live stream plays, since it has no length to start over from.
Copy file name to clipboardExpand all lines: docs/guide/media/media-files.md
+25-1Lines changed: 25 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@ This guide explains how to send a media file over a peer connection instead of a
7
7
- Reading what a source contains with `MediaReader` and `MediaInfo`
8
8
- Controlling playback and following it with a listener
9
9
- Feeding your own media sources with `MediaPlayer`
10
+
- Playing a live stream from an IP camera or media server over RTSP
10
11
11
12
Sending a file is a common need: a test pattern instead of a webcam, a pre-recorded briefing, a video that has to reach several participants. Without help, an application has to bring its own decoder and push I420 frames into a `CustomVideoSource` itself. The media module removes that work by decoding with [FFmpeg](https://ffmpeg.org) inside the library.
12
13
@@ -203,6 +204,29 @@ The player takes over the reader it is given. That reader must not be used or cl
203
204
It also keeps the native side of the media sources it feeds alive until it is closed, so disposing of a source or its track while the player runs is safe.
204
205
:::
205
206
207
+
## Playing a Live Stream
208
+
209
+
A source can also be a live stream behind an `rtsp://` URL, which is what IP cameras, video recorders and most media servers offer. Everything above works the same way; only the URL differs:
Credentials go into the URL, as `rtsp://user:password@camera.local/stream1`. FFmpeg first asks the server for RTP over UDP, and falls back to RTP interleaved on the RTSP connection over TCP if the server refuses UDP or no UDP packets arrive, which is what gets a stream through most firewalls.
216
+
217
+
A network source can stall or disappear, so every operation that waits on a source has a time limit: opening it, and then each read during playback. It is 10 seconds unless you pass another:
An unreachable server makes the constructor throw an `IOException` once that time is up. A stream that stops sending during playback is reported to the listener's `onError`, and the player is left `PAUSED`. Closing a player never waits for the time limit: it breaks off whatever the player is waiting for at once.
227
+
228
+
A live stream has no end and no length: its `MediaInfo` reports a duration of 0, looping does not apply, and seeking is reported as an error. Pausing stops reading from the server, but the server keeps sending; resuming carries on from where playback paused, which leaves it behind real time by as long as it was paused. To get back to live, close the source and open it again.
229
+
206
230
## Closing
207
231
208
232
Closing a `MediaFileSource` stops playback and releases the player along with both media sources. Release the senders a peer connection handed out, and the tracks, before that:
@@ -227,7 +251,7 @@ The FFmpeg build is deliberately small, and carries only what this module plays:
227
251
228
252
Audio of any rate or layout is resampled to what WebRTC takes, which is 48 kHz 16-bit PCM in mono or stereo. Video that decodes to I420 — almost all 8-bit H.264, VP8, VP9 and MPEG-4 — reaches the encoder without being copied; anything else is converted first.
229
253
230
-
Only local files play today. FFmpeg demuxes network sources just as well, so the same code will cover http, rtsp and rtmp once those protocols are turned on in the build.
254
+
Sources can be local files, or live streams over RTSP (`rtsp://`), with RTP over UDP or TCP. Protocols that need TLS, such as `rtsps://` and `https://`, are not part of the build, and neither are HLS, DASH and RTMP. A source cannot reach any other protocol either, even one FFmpeg uses internally, so a URL passed on from a user cannot make the module fetch something else.
0 commit comments