From 0f65c895e851af37222300703e6988f166a875df Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 21 Feb 2021 10:55:52 -0800 Subject: [PATCH] [cscore] HttpCamera: Auto-detect mode from stream if not set --- cscore/src/main/native/cpp/HttpCameraImpl.cpp | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.cpp b/cscore/src/main/native/cpp/HttpCameraImpl.cpp index 16ee2cc9f89..924e2dcf99d 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.cpp +++ b/cscore/src/main/native/cpp/HttpCameraImpl.cpp @@ -294,12 +294,27 @@ bool HttpCameraImpl::DeviceStreamFrame(wpi::raw_istream& is, return false; } - unsigned int contentLength = 0; + int width, height; if (auto v = wpi::parse_integer(contentLengthBuf, 10)) { - contentLength = v.value(); + // We know how big it is! Just get a frame of the right size and read + // the data directly into it. + unsigned int contentLength = v.value(); + auto image = + AllocImage(VideoMode::PixelFormat::kMJPEG, 0, 0, contentLength); + is.read(image->data(), contentLength); + if (!m_active || is.has_error()) { + return false; + } + if (!GetJpegSize(image->str(), &width, &height)) { + SWARNING("did not receive a JPEG image"); + PutError("did not receive a JPEG image", wpi::Now()); + return false; + } + image->width = width; + image->height = height; + PutFrame(std::move(image), wpi::Now()); } else { // Ugh, no Content-Length? Read the blocks of the JPEG file. - int width, height; if (!ReadJpeg(is, imageBuf, &width, &height)) { SWARNING("did not receive a JPEG image"); PutError("did not receive a JPEG image", wpi::Now()); @@ -307,27 +322,18 @@ bool HttpCameraImpl::DeviceStreamFrame(wpi::raw_istream& is, } PutFrame(VideoMode::PixelFormat::kMJPEG, width, height, imageBuf, wpi::Now()); - ++m_frameCount; - return true; } - // We know how big it is! Just get a frame of the right size and read - // the data directly into it. - auto image = AllocImage(VideoMode::PixelFormat::kMJPEG, 0, 0, contentLength); - is.read(image->data(), contentLength); - if (!m_active || is.has_error()) { - return false; - } - int width, height; - if (!GetJpegSize(image->str(), &width, &height)) { - SWARNING("did not receive a JPEG image"); - PutError("did not receive a JPEG image", wpi::Now()); - return false; - } - image->width = width; - image->height = height; - PutFrame(std::move(image), wpi::Now()); ++m_frameCount; + + // update video mode if not set + std::scoped_lock lock(m_mutex); + if (m_mode.pixelFormat != VideoMode::PixelFormat::kMJPEG || + m_mode.width == 0 || m_mode.height == 0) { + m_mode.pixelFormat = VideoMode::PixelFormat::kMJPEG; + m_mode.width = width; + m_mode.height = height; + } return true; }