diff --git a/src/EncodingHelper.cpp b/src/EncodingHelper.cpp index 0824f08..8b5d5e8 100644 --- a/src/EncodingHelper.cpp +++ b/src/EncodingHelper.cpp @@ -84,7 +84,7 @@ JpegInformation * convertToJpeg(uint8_t * sourceBuffer, uint32_t width, uint32_t return NULL; } - +extern int32_t curQuality; void EncodingHelper::DoAsyncThreadInternal(CThread *thread) { serverRunning = true; @@ -99,7 +99,7 @@ void EncodingHelper::DoAsyncThreadInternal(CThread *thread) { DEBUG_FUNCTION_LINE("We should stop\n"); break; } - OSSleepTicks(OSMicrosecondsToTicks(5000)); + OSSleepTicks(OSMicrosecondsToTicks(500)); continue; } DCFlushRange(&message,sizeof(OSMessage)); @@ -112,7 +112,7 @@ void EncodingHelper::DoAsyncThreadInternal(CThread *thread) { colorBuffer = (GX2ColorBuffer *) message.args[1]; - JpegInformation * info = convertToJpeg((uint8_t*) colorBuffer->surface.image,colorBuffer->surface.width,colorBuffer->surface.height,colorBuffer->surface.pitch,colorBuffer->surface.format,85); + JpegInformation * info = convertToJpeg((uint8_t*) colorBuffer->surface.image,colorBuffer->surface.width,colorBuffer->surface.height,colorBuffer->surface.pitch,colorBuffer->surface.format, curQuality); if(info != NULL ) { MJPEGStreamServer::getInstance()->streamJPEG(info); diff --git a/src/MJPEGStreamServer.hpp b/src/MJPEGStreamServer.hpp index 0a168fe..03bfd81 100644 --- a/src/MJPEGStreamServer.hpp +++ b/src/MJPEGStreamServer.hpp @@ -28,6 +28,7 @@ extern OSMessageQueue streamSendQueue; extern OSMessage streamSendQueueMessages[STREAM_SEND_QUEUE_MESSAGE_COUNT]; +extern uint32_t frame_counter_skipped; class MJPEGStreamServer: TCPServer { @@ -79,8 +80,9 @@ public: OSMessage message; message.message = (void *) 0x11111; message.args[0] = (uint32_t) info; - if(!OSSendMessage(&streamSendQueue,&message,OS_MESSAGE_FLAGS_BLOCKING)) { - DEBUG_FUNCTION_LINE("Dropping frame\n"); + if(!OSSendMessage(&streamSendQueue,&message,OS_MESSAGE_FLAGS_NONE)) { + frame_counter_skipped++; + //DEBUG_FUNCTION_LINE("Dropping frame\n"); delete info; return false; }; diff --git a/src/stream_utils.cpp b/src/stream_utils.cpp index 0bcf9ab..24670b9 100644 --- a/src/stream_utils.cpp +++ b/src/stream_utils.cpp @@ -91,6 +91,13 @@ bool copyBuffer(GX2ColorBuffer * sourceBuffer, GX2ColorBuffer * targetBuffer, ui uint32_t frame_counter = 0; uint32_t frame_counter_skipped = 0; +int32_t curQuality = 50; +int32_t minQuality = 40; +int32_t maxQuality = 85; +int32_t stepQuality = 1; +int32_t maxFrameDropsQuality = 20; +int32_t minFrameDropsQuality = 95; + bool streamVideo(GX2ColorBuffer *srcBuffer) { if(srcBuffer == NULL) { @@ -163,8 +170,34 @@ bool streamVideo(GX2ColorBuffer *srcBuffer) { result = false; } - if(frame_counter % 120 == 0) { - DEBUG_FUNCTION_LINE("Send %d frames, skipped %d. %.2f\n",frame_counter-frame_counter_skipped,frame_counter_skipped,100.f * (frame_counter_skipped*1.0f/frame_counter)); + if(frame_counter % 60 == 0) { // Print this every second. + + int32_t curRatio = (int32_t)100.f*(frame_counter_skipped*1.0f/frame_counter); + int32_t curQualityOld = curQuality; + if(curRatio > maxFrameDropsQuality) { // Lower the quality if we drop more than [maxFrameDropsQuality]% of the frames. + curQuality -= (curRatio - maxFrameDropsQuality); + } else if(curRatio < minFrameDropsQuality) { // Increase the quality if we drop less than [minFrameDropsQuality]% of the frames. + curQuality += stepQuality; // Increase the quality by [stepQuality]% + } + + // Make sure to set the quality to at least [minQuality]% + if(curQuality < minQuality) { + curQuality = minQuality; + } + + // Make sure to set the quality to at most [maxQuality]% + if(curQuality >= maxQuality) { + curQuality = maxQuality; + } + + DEBUG_FUNCTION_LINE("Streaming at %d fps\n",frame_counter-frame_counter_skipped); + + frame_counter = 0; + frame_counter_skipped = 0; + + if(curQualityOld != curQuality) { + DEBUG_FUNCTION_LINE("Quality is now at %d%%\n",curQuality); + } } return result;