Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apps] FIX srt-live-transmit stops listening after SRT disconnect #2997... #3108

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

cl-ment
Copy link
Collaborator

@cl-ment cl-ment commented Jan 24, 2025

... during no active input (FIX #2997)

There are two primary reasons for this issue:

  1. The main loop assumes that each time srt_epoll_wait returns, there is data available to read from the source. However, this assumption is incorrect when an SRT socket is in listener mode and on the "target" side. In this mode, srt_epoll_wait can return simply to provide an opportunity to accept a new connection, even if no data is ready to be read. As a result, the process becomes stuck while waiting to read from the source.
  2. The ConsoleSource operates in blocking mode by default. Even worse, its Read() method blocks execution until a full packet (1456 bytes) is received.

@maxsharabayko maxsharabayko added Type: Bug Indicates an unexpected problem or unintended behavior [apps] Area: Test applications related improvements labels Jan 24, 2025
@maxsharabayko maxsharabayko added this to the v1.5.5 milestone Jan 24, 2025
apps/srt-live-transmit.cpp Outdated Show resolved Hide resolved
apps/srt-live-transmit.cpp Outdated Show resolved Hide resolved
apps/srt-live-transmit.cpp Outdated Show resolved Hide resolved
apps/srt-live-transmit.cpp Outdated Show resolved Hide resolved
@@ -712,6 +713,9 @@ class ConsoleSource: public Source
// The default stdin mode on windows is text.
// We have to set it to the binary mode
_setmode(_fileno(stdin), _O_BINARY);
#else
const int fd = fileno(stdin);
may_block = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't make any sense. If this fails (best from the constructor), it should make the application exit. Error from this call is unlikely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, when implementing an event loop for reading, the pattern is to use non-blocking file descriptors and continue reading until errno == EAGAIN is encountered.
If it’s not possible to make the file descriptors non-blocking, an alternative approach is to make a single read() call for each “read” event. While this approach is not optimal due to the increased number of system calls, it still ensures the process functions as expected from the user’s perspective. I prefer this pragmatic approach over halting the process just because it cannot run in an optimal manner.

@@ -800,9 +843,10 @@ int main(int argc, char** argv)

dataqueue.push_back(pkt);
receivedBytes += pkt->payload.size();
if (src->MayBlock())
break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that actually reading should be done this way: either you have the device read-ready, so you read, and then after you read, you don't know, and have to recheck.

Or, you can resolve to reading multiple times, counting on that when particular time reading isn't ready, then the Read call should report an error. Might be, I think, a good idea, to keep the "blocked" state in the fields, which will be written to, in case when particular Read implementation finds out that the call failed due to not being ready. This way it won't need to see if this is SRT and this way we use that function to get the error and maybe check for an SRT-specific readiness failure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, the current implementation already follows this approach. For file descriptors in blocking mode (case 1), it checks if the device is ready before attempting to read, performs the read operation, and rechecks the state afterward since readiness isn’t guaranteed. For file descriptors in non-blocking mode (case 2), it handles multiple read attempts and relies on the error returned by the Read call to detect if the device isn’t ready.
Could you clarify if there’s something specific you’d like me to adjust or add to the current implementation? Perhaps there’s a particular scenario or behavior you’d like to address that isn’t currently handled?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This here is unclear. First, MayBlock() actually represent the state whether setting the nonblocking mode could be done, which is useless. If the architecture requires nonblocking mode, it should be nonblocking always, and all devices should be operated as such. The case if operating the console device in blocking mode should not be even taken into account.

The only thing I'm referring to is the approach to multiple reading calls, which should follow one of two methods:

  • Check if read-ready always before calling Read(), and break the loop if it isn't
  • Check if read-ready once before the loop, then call Read() in loop until Read() informs you that reading is no longer possible

Note that the second approach isn't possible to be used reliably in case of blocking mode, that's why it should not be taken into account.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the benefits of enforcing non-blocking mode for consistency and simplifying the architecture. However, I think it might be preferable to maintain support for both blocking and non-blocking modes. This flexibility ensures compatibility with a broader range of sources and use cases, particularly for systems where blocking mode is either required or more practical.

The current implementation can differentiate the handling of each mode:
• For blocking mode: Always check read-readiness before calling Read().
• For non-blocking mode: Check read-readiness once before the loop and perform multiple Read() calls until it’s no longer possible.

Would you be open to maintaining support for both modes, or do you see specific challenges in doing so?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[apps] Area: Test applications related improvements Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] srt-live-transmit stops listening after SRT disconnect during no active input
3 participants