Fix fileringbuffer locking up on read()

From time to time, most often seen during LiveTV, FileRingBuffer::safe_read() would lock and not return for a long time (on my system, it would block in excess of 20s).
Upon investigation, the issue occurs while reading a file currently being written to, if a call to read() encountered EOF, returned early and a following call to read() was made, before another write. Under those conditions, read() wouldn't return for a long time, causing the backend to abort the read and dropping the frontend connection.

So instead we now check the size of the current file via a call to POSIX fstat and never try to read beyond the EOF position.
If the file being read isn't a regular, or fstat returned an error, the old behaviour is used: read until we get as much data as we asked.

Surprisingly, this has fixed most of the issues I've been having for years with liveTV:
- Stuttering or temporary free when close to the live
- Freeze at program transition.

Add debugging information while in file mode

Fixes #12045
(cherry picked from commit 20f7cd2858e79385ceec07b2c19c7e4f84714e18)
Have backend advertise which files are currently being written to

It is necessary in some playback scenarios to know if a file being read is currently being written to. This is useful in determining when encountering EOF if we should wait for more data or abort the read.

MythCoreContext will now keep track of files as they are being opened for writing
Connected frontends will receive the information via the event socket in the form of a BACKEND_MESSAGE event.

The myth protocol is extended with two new BACKEND_MESSAGE events:
FILE_WRITTEN: Sent when a file is opened in write mode, and when the file is being written to (at most every 10s)
FILE_CLOSED: Sent when the file opened in write mode is closed.

The myth protocol version doesn’t have to be increased as frontends will make use of it only if available and the changes are backward compatible with exiting myth protocol.
(cherry picked from commit fafc658597b547e2f0173032515e9790170ebcbe)

Conflicts:

	mythtv/libs/libmythbase/threadedfilewriter.cpp
Prevent playback to exit early should backend's writes be delayed

Typical scenario is when watching on-going recordings and being very close to real time TV: should the backend take more than 360ms between two writes, the reader would treat it as an EOF and abort the read.

When encountering EOF, we now check if the file is being written to in parallel and if so wait.

We can also remove all EOF retries in FileRingBuffer::safe_read() as we definitely know now if we did reach EOF (as we check against the file size).
This allows for almost instantaneous program transition in live TV.
(cherry picked from commit 443b624ebcc8ea56026cb49ca833edc7d73832c1)
Fix typo

CR got deleted for some reason
(cherry picked from commit 534394068373c52fd712d653e5fea725c564a673)
Allow seeking within the entire liveTV chain of recordings.

Seeking across liveTV was fundamentally broken. You could only seek within the current recordings or go to the beginning of the next one or 15s to the end of the previous one.
This changes correct this behaviour. It is now possible to seek across the entire live TV session. We ensure however that we do not seek further than 15s from the end of a recordings (3s if reaching end of live program).

Fixes #12076
(cherry picked from commit 11daeaadc52214605e204cdfe6f7c001c9c1cc9b)
Never attempt to read more than in 512kB blocks

Also increase the threshold at which we increase the read block size.
512kB ought to be enough to read a file smoothly (famous last words)

This reduces the amount of unnecessary verbosity. When watching a SD 3Mbit/s DVB stream, it will take over a second to get 1MB of data, leading to a warning entry in the log for every single read.

Refs #10428

(cherry picked from commit 43cba6537fae95cef0ff5ce6ba7352c52abcd947)
Revert "Never attempt to read more than in 512kB blocks"

This reverts commit 43cba6537fae95cef0ff5ce6ba7352c52abcd947.
(cherry picked from commit bcfa0100d94e68df913f3a620da7315a556ccfeb)
Never attempt to read more than in 512kB blocks

Recalculate the threshold and read block size when we've encountered EOF several times in a row.
512kB ought to be enough to read a file smoothly (famous last words)

This reduces the amount of unnecessary verbosity. When watching a SD 3Mbit/s DVB stream, it will take over a second to get 1MB of data, leading to a warning entry in the log for every single read.

Refs #10428
(cherry picked from commit 1cfdfa3f56876eb20170f33e9592b5c7ffd05064)
Merge branch 'fixes/0.27' into devel/027candidates
Small optimisation of FileRingBuffer::GetRealFileSize

If file is already opened, no need to use QFileInfo

(cherry picked from commit ed52e84bb12b0b02bebbf613811623bd5a6c0246)
Fix seek when close to end of program

When seeking repeatedly close to the end of the program in liveTV, it would cause playback to jump immediately to the next program.
The cause was RingBuffer:: IsNearEnd didn’t report if we were close to the end of the file, but if there was little left to read in the ringbuffer. However, when seeking the buffer can be emptied, causing IsNearEnd to always return true
Additionally, with very low bitrate stream (like with the HLS recorder), we could sometimes miss over significant length of video.

A positive side effect is that there’s now no pause whatsoever between two liveTV program,s at worse about 20 frames dropped.

Ref #12076

(cherry picked from commit c24e2e72966f0936f7dbb3f79f421038dbd41579)
Do not abort early if not all data is received within 25ms

This is simply 90f47553371e7778ead7354d33baed95b5b56188 applied to libmythprotoserver/filetransfer.cpp which is 90% identical to the mythbackend version.
Also re-sync the two files…

(cherry picked from commit cad3f0a08b88e1e77ef1e91b9e1b09f6c54d0381)
Fix seek when close to end of program #2

It also fixes rewinding to the previous program on very low bitrate streams where it would jump to the end of the current program instead.

(cherry picked from commit 5d82ba64a6d68057dc1e3199938ab1365ed76947)
Fix calculation of read block size.

RingBuffer::CalcReadAheadThresh was always called at least once with rawbitrate set to 8000; which would initially set readblocksize to 128kB. As any further call to CalcReadAheadThresh would always use the maximum of readblocksize previously calculated, readblocksize would never be set to a lower value; and as such the special case for low bitrate streams would never be triggered.

We now recalculate the read block size only based on the bitrate value. Also change the block size default value so that it would always take less than 200ms to read such block (the reader code expects such duration and displays warnings otherwise)

(cherry picked from commit c64141b0c0b7118ee28be2441374727a1af7c44c)
Only expire LiveTV programs that are less than 30s and more than 5 minutes old

(was 2 minutes programs, 1 minute old)

(cherry picked from commit 9883b4345be9c171c926ff9e4a352a54e8e5237f)
Add QUERY_FILETRANSFER REQUEST_SIZE command

This allows to query the current size of the file. Previously you could only get the size of the file from when it was first opened or had to issue a QUERY_FILE_EXISTS command that is significantly slower (as it requires a new connection to the backend)

(cherry picked from commit 5f47d6922e8e2d740ffd9353bf8d354a189ef80d)

Conflicts:
	mythtv/programs/mythbackend/mainserver.cpp
Add RemoteFile::GetRealFileSize()

Allows to query the current size of the remote file. If the file isn’t opened for write by the backend, the size will be cached and the backend will be queried only once

(cherry picked from commit 6290fd98f55bc11f78366324deb04bf84e40da16)
(cherry picked from commit 5266ceec09a512e9c5a221da85fac0598cadbb99)
Cache the size of the remote file for 500ms

Aim is to speed up common usage…

(cherry picked from commit 8c174135f305efbc7cc1ec8cc0aab4cf26525e4a)
Partial revert of c64141b0c0b7118ee28be2441374727a1af7c44c

We can use the previous readblocksize as it was adjusted during earlier reads. However we set the initial value in such a way that it will be automatically recalculated on the first call to RingBuffer::CalcReadAheadThresh()

(cherry picked from commit dddf14c050297a7bbb74459e3018e457a47d2489)
Set initial FileRingBuffer rawbitrate to 800

In continuation of dddf14c050297a7bbb74459e3018e457a47d2489

(cherry picked from commit 1da2a0ef0d645c0ed19932f478567a67a8048b38)