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

Dynamic frame delay based on maxClientFrameRate in IPVideoRoute #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions libs/ofxHTTP/src/IPVideoRoute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ void IPVideoRoute::removeConnection(IPVideoConnection* handler)

IPVideoConnection::IPVideoConnection(IPVideoRoute& route):
BaseRouteHandler_<IPVideoRoute>(route),
IPVideoFrameQueue(route.settings().getMaxClientQueueSize())
IPVideoFrameQueue(route.settings().getMaxClientQueueSize()),
_targetFrameDuration(1000 / route.settings().getMaxClientFrameRate())
{
}

Expand Down Expand Up @@ -495,10 +496,13 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt)

while (_isRunning)
{
uint64_t frameSendStart = ofGetElapsedTimeMillis();

if (outputStream.good() && !outputStream.fail() && !outputStream.bad())
{
if (!empty())
{
_nextScheduledFrame = frameSendStart + _targetFrameDuration;
std::shared_ptr<IPVideoFrame> frame = pop();

if (frame != nullptr)
Expand All @@ -514,10 +518,8 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt)
ostr << "\r\n";
ostr << buffer;

uint64_t now = ofGetElapsedTimeMillis();
_lastFrameDuration = now - _lastFrameSent;
_lastFrameSent = now;
_bytesSent += static_cast<uint64_t>(ostr.chars()); // add the counts
_framesSent ++;
ostr.reset(); // reset the counts
ostr.flush();
}
Expand All @@ -529,14 +531,18 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt)
else
{
// ofLogVerbose("IPVideoRouteHandler::handleRequest") << "Queue empty.";
// Delay a little bit to wait for a next frame to come into the queue.
_nextScheduledFrame = frameSendStart + 10;
}
}
else
{
throw Poco::Exception("Response stream failed or went bad -- it was probably interrupted.");
}

Poco::Thread::sleep(30); // TODO: smarter ways of doing for rate / fps limiting
uint64_t now = ofGetElapsedTimeMillis();
if (now < _nextScheduledFrame) {
Poco::Thread::sleep(_nextScheduledFrame - now);
}
}
}
catch (const Poco::Exception& e)
Expand Down