From 0bcafedebf813ad45469a3fbde9a2704533375af Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 12 Nov 2016 21:57:25 -0800 Subject: [PATCH] MJPEGServerImpl: Refactor NeedsDHT. --- src/MJPEGServerImpl.cpp | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/MJPEGServerImpl.cpp b/src/MJPEGServerImpl.cpp index ca3d9a8b40..c75f0d5b4b 100644 --- a/src/MJPEGServerImpl.cpp +++ b/src/MJPEGServerImpl.cpp @@ -432,6 +432,23 @@ void MJPEGServerImpl::Stop() { source->Wakeup(); } +static bool NeedsDHT(const char* data, std::size_t* size, std::size_t* locSOF) { + // Search first 2048 bytes (or until SOS) for DHT tag + for (llvm::StringRef::size_type i = 0; i < 2048 && i < *size; ++i) { + if (static_cast(data[i]) != 0xff) continue; // not a tag + unsigned char tag = static_cast(data[i + 1]); + if (tag == 0xda) break; // SOS + if (tag == 0xc4) return false; // DHT + if (tag == 0xc0) *locSOF = i; // SOF + } + // Only add DHT if we also found SOF (insertion point) + if (*locSOF != *size) { + *size += sizeof(dhtData); + return true; + } + return false; +} + // Send HTTP response and a stream of JPG-frames void MJPEGServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) { os.SetUnbuffered(); @@ -470,21 +487,7 @@ void MJPEGServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) { case VideoMode::kMJPEG: // Determine if we need to add DHT to it, and allocate enough space // for adding it if required. - // Search first 2048 bytes (or until SOS) for DHT tag - for (llvm::StringRef::size_type i = 0; i < 2048 && i < size; ++i) { - if (static_cast(data[i]) != 0xff) - continue; // not a tag - unsigned char tag = static_cast(data[i + 1]); - if (tag == 0xda) break; // SOS - if (tag == 0xc4) goto done; // DHT - if (tag == 0xc0) locSOF = i; // SOF - } - // Only add DHT if we also found SOF (insertion point) - if (locSOF != size) { - addDHT = true; - size += sizeof(dhtData); - } - done: + addDHT = NeedsDHT(data, &size, &locSOF); break; case VideoMode::kYUYV: case VideoMode::kRGB565: