LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-16-2020, 07:38 AM   #1
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Rep: Reputation: Disabled
Trying to understand how Pipe works in this situation


I'm using ffmpeg to pull in an input rtmp stream and then push that out to a destination. In case the main input fails, I would like my ffmpeg process to switch to a backup input.

Unfortunately, ffmpeg CLI does not seem to have this ability built in.

So I came across a solution which uses pipes to manage this.

The input rtmp is pushed to a pipe file first.

Code:
PIPE_FILE= /path/to/pipe/file
mkfifo $PIPE_FILE
exec 7<>$PIPE_FILE
ffmpeg -i rtmp://127.0.0.1/main/stream1 -acodec copy -vcodec copy -vbsf h264_mp4toannexb -f mpegts pipe:1 > $PIPE_FILE
The pipe file is then used as an input to restream to the final destination.
Code:
ffmpeg -re -i $PIPE_FILE -c:v libx264 -preset veryfast -r 25 -g 50 -f flv $RTMP_ENDPOINT
I;m trying to understand if this pipe file is an actual file or just a temporary construct.

Also, if it is an actual file, will it's size keep increasing as the stream continues.

Also, what are mkfifo and the exec 7 commands actually doing?

Thanks
 
Old 07-16-2020, 07:43 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
For starters, see Named pipe in Wikipedia. Here is an introduction to named pipes that may be easier to understand.

The <> is a redirection operator present in Bash, meaning both input and output get redirected to/from $PIPE_FILE on file descriptor 7.

As for exec used for redirections, see this chapter in the Advanced Bash-Scripting Guide.

I should add that using redirections in this way is rarely needed in Bash, because it has process substitution. But when writing a POSIX-conform shell script they often come very handy.

Last edited by shruggy; 07-16-2020 at 08:35 AM.
 
Old 07-16-2020, 08:20 AM   #3
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,567
Blog Entries: 19

Rep: Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447
Quote:
Originally Posted by regstuff View Post
I'm trying to understand if this pipe file is an actual file or just a temporary construct.

Also, if it is an actual file, will it's size keep increasing as the stream continues.

Thanks
No, it's not a file. It has an entry in a directory somewhere and this takes you to a normal inode on disk, so it has an owner, permissions, access dates and so on. But there are no data blocks. Instead, when a process accesses the "file", the kernel directs the data between the newly opened channel in that program and one in the program at the other end of the pipe.

Last edited by hazel; 07-16-2020 at 08:24 AM.
 
Old 07-16-2020, 10:56 AM   #4
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
For starters, see Named pipe in Wikipedia. Here is an introduction to named pipes that may be easier to understand.

The <> is a redirection operator present in Bash, meaning both input and output get redirected to/from $PIPE_FILE on file descriptor 7.

As for exec used for redirections, see this chapter in the Advanced Bash-Scripting Guide.

I should add that using redirections in this way is rarely needed in Bash, because it has process substitution. But when writing a POSIX-conform shell script they often come very handy.
Thank you for the links going to read up now!
 
Old 07-16-2020, 10:59 AM   #5
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
No, it's not a file. It has an entry in a directory somewhere and this takes you to a normal inode on disk, so it has an owner, permissions, access dates and so on. But there are no data blocks. Instead, when a process accesses the "file", the kernel directs the data between the newly opened channel in that program and one in the program at the other end of the pipe.
So technically this would show when I do an ls?
And does it exist once the source starts "inputting" into the pipe, or only after the "sink" starts reading out of the pipe?
 
Old 07-16-2020, 11:29 AM   #6
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,567
Blog Entries: 19

Rep: Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447
Quote:
Originally Posted by regstuff View Post
So technically this would show when I do an ls?
Yes, it behaves just like any other file. For example, if you have a traditional sysvinit startup, there will be a fifo called initctl in the /dev directory and you can list it with ls. init listens on that fifo and programs like shutdown use it to give instructions to init.
Quote:
And does it exist once the source starts "inputting" into the pipe, or only after the "sink" starts reading out of the pipe?
The inode exists from the moment you create the fifo. I don't know at what point the kernel creates the channel. It can certainly exist without the listening process reading from it, because it is possible up to a point to write into a blocked pipe. But as soon as the pipe is full, the write operation halts until the other program reads what was written. My guess is that there is a kernel buffer which actually receives and passes on the text. Maybe a kernel maven can tell us that.
 
Old 07-17-2020, 04:54 AM   #7
regstuff
LQ Newbie
 
Registered: Oct 2018
Distribution: Ubuntu 16.04
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
it is possible up to a point to write into a blocked pipe. But as soon as the pipe is full, the write operation halts until the other program reads what was written.
Thanks for the info. My concern is that in my case, the input stream would be coming into the server and therefore the pipe regardless of whether I am reading out of the pipe. So when I finally do read out of the pipe, I don't want a situation where the pipe feeds "older" chunks of the stream to the next stage.
 
Old 07-17-2020, 05:17 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
You mean "newer"? No, this is not possible. The pipe is not a black hole, no data get lost.
 
Old 07-17-2020, 05:27 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you can imagine that pipe as a "real" pipe (or tube). From one side you put data into it and on the other side you will take that out.
the pipe [may] exists without any data pushed/pulled.
fifo means First In First Out, so the order of the data cannot be changed inside the pipe.
https://www.softprayog.in/programmin...fifos-in-linux
 
Old 07-17-2020, 11:11 AM   #10
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,567
Blog Entries: 19

Rep: Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447Reputation: 4447
I've been trying to picture this and I think I know now how pipes originated. They're just a small wrinkle on normal disk I/O.

The kernel maintains huge buffers whenever it has enough free memory for them. When it has to read from a disk, it reads much more than was requested and stores the rest in a buffer. That way it can satisfy subsequent read commands from the process without having to do any further mechanical disk access. And similarly output from a process goes into a buffer and gets written to disk at a convenient time, when the cpu is less busy. A sync function synchronises the disk with the buffers when required.

When a process wants to do I/O, the kernel assigns a file descriptor (just a number) and links it to the appropriate internal buffer. Now to create a pipe, all the kernel has to do is assign two file descriptors to such a buffer, one in read mode and one in write mode. That way the buffer needn't be linked to the disk surface in any way. Instead one program simply reads out of the buffer what the other program has written into it.
 
  


Reply

Tags
ffmpeg, pipes



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Old situation and New situation bkumar82 Linux - Newbie 2 01-28-2019 01:08 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
pipe using no pipe soathana Programming 2 02-22-2003 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration