From 1026f206fbf84e685d0c3ca3cb17fe1c147afa07 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 31 Aug 2017 14:53:48 +0200 Subject: [PATCH 1/2] dynamic frame delay depending on client max frame rate --- libs/ofxHTTP/src/IPVideoRoute.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libs/ofxHTTP/src/IPVideoRoute.cpp b/libs/ofxHTTP/src/IPVideoRoute.cpp index 4c16a0d..5e3969c 100644 --- a/libs/ofxHTTP/src/IPVideoRoute.cpp +++ b/libs/ofxHTTP/src/IPVideoRoute.cpp @@ -358,7 +358,8 @@ void IPVideoRoute::removeConnection(IPVideoConnection* handler) IPVideoConnection::IPVideoConnection(IPVideoRoute& route): BaseRouteHandler_(route), - IPVideoFrameQueue(route.settings().getMaxClientQueueSize()) + IPVideoFrameQueue(route.settings().getMaxClientQueueSize()), + _targetFrameDuration(1000 / route.settings().getMaxClientFrameRate()) { } @@ -399,7 +400,7 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt) Poco::Net::NameValueCollection queryMap = HTTPUtils::getQueryMap(uri); - + if (queryMap.has("vflip")) { std::string vflip = queryMap.get("vflip"); @@ -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 frame = pop(); if (frame != nullptr) @@ -514,10 +518,11 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt) ostr << "\r\n"; ostr << buffer; - uint64_t now = ofGetElapsedTimeMillis(); - _lastFrameDuration = now - _lastFrameSent; - _lastFrameSent = now; +// uint64_t now = ofGetElapsedTimeMillis(); +// _lastFrameDuration = now - _lastFrameSent; +// _lastFrameSent = now; _bytesSent += static_cast(ostr.chars()); // add the counts + _framesSent ++; ostr.reset(); // reset the counts ostr.flush(); } @@ -529,14 +534,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) From e504319d1f05800166f4e899baf8cae112965f28 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 31 Aug 2017 15:00:30 +0200 Subject: [PATCH 2/2] remove unnecessary edits --- libs/ofxHTTP/src/IPVideoRoute.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libs/ofxHTTP/src/IPVideoRoute.cpp b/libs/ofxHTTP/src/IPVideoRoute.cpp index 5e3969c..521e8e8 100644 --- a/libs/ofxHTTP/src/IPVideoRoute.cpp +++ b/libs/ofxHTTP/src/IPVideoRoute.cpp @@ -400,7 +400,7 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt) Poco::Net::NameValueCollection queryMap = HTTPUtils::getQueryMap(uri); - + if (queryMap.has("vflip")) { std::string vflip = queryMap.get("vflip"); @@ -518,9 +518,6 @@ void IPVideoConnection::handleRequest(ServerEventArgs& evt) ostr << "\r\n"; ostr << buffer; -// uint64_t now = ofGetElapsedTimeMillis(); -// _lastFrameDuration = now - _lastFrameSent; -// _lastFrameSent = now; _bytesSent += static_cast(ostr.chars()); // add the counts _framesSent ++; ostr.reset(); // reset the counts