Andrew Mcdonald
Andrew Mcdonald

Reputation: 445

keep pipe open without reading from it?

I'm using vlc media player to connect to a rtsp stream over the internet.

I only need to view the stream periodically but it takes too long to establish a connection each time.

Instead I keep the connection open and vlc writes the output to a named pipe as a transport stream.

Then a second instance of vlc can read from the pipe as and when I need it.

The problem is, as I understand it, a named pipe cannot be written to unless its open for reading.

To get around this problem when the second instance of vlc is not open I:

    cat < $PIPE > /dev/null

Is there a better way to keep a pipe open for writing other than using cat in this way?

Upvotes: 2

Views: 1729

Answers (1)

spbnick
spbnick

Reputation: 5275

You didn't tell what the connecting VLC does, if the pipe is not ready for writing.

If it discards the output when it sees that the pipe is not ready for writing and continues reading from the connection, then you can simply have a process that opens the pipe, but doesn't read it. Something like this:

while sleep 7d; do :; done < $PIPE &

Then, when you start the playing VLC, it will start reading from the pipe and the connecting VLC will be writing to it until the playing VLC stops.

However, if the connecting VLC stalls on writing to the pipe, then with the above setup the RTSP connection will eventually time out and when the playing VLC starts reading from the pipe, the connecting VLC will either terminate or will need to reconnect. The VLC may also simply timeout on writing to the pipe.

In that case you can read from the pipe and discard its contents until the playing VLC arrives, and resume reading when it stops. You can have multiple processes open a pipe, but the data may be read only once from it, i.e. the processes will compete for the data.

You can do it by suspending the discarding process with SIGSTOP before starting the playing VLC and resuming it with SIGCONT after it stops, assuming the gap in reading the pipe isn't big enough to timeout the connection.

Like this:

cat $FIFO > /dev/null &
kill -SIGSTOP %
vlc $FIFO
kill -SIGCONT %

Upvotes: 1

Related Questions