diff --git a/MediaClient/media/video_decoder.cpp b/MediaClient/media/video_decoder.cpp index 33efd46..d84b3e9 100644 --- a/MediaClient/media/video_decoder.cpp +++ b/MediaClient/media/video_decoder.cpp @@ -943,18 +943,20 @@ BOOL CVideoDecoder::decode(AVPacket* pkt) if (!m_bInited) { - log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__); + // Hot-path pre-check; per-packet during early frames if decode() races + // with init. + ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not initialized"); return FALSE; } if (!m_bRunning) { - log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__); + ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not running"); return FALSE; } if (!m_pContext) { - log_print(HT_LOG_ERR, "%s, Context is NULL\r\n", __FUNCTION__); + ANS_DBG("MEDIA_Decode", "decode(pkt) called but m_pContext is NULL"); return FALSE; } @@ -968,7 +970,12 @@ BOOL CVideoDecoder::decode(AVPacket* pkt) { if (!readFrame()) { - log_print(HT_LOG_ERR, "%s, Failed to read frame during retry %d\r\n", __FUNCTION__, retryCount); + // Hot-path error: was log_print() which fflushes per call and can + // spike per-packet during stream stress. ANS_DBG is silent in + // production; the per-frame error count is also visible via + // MEDIA_Pulse drop(err=) so observability is preserved. + ANS_DBG("MEDIA_Decode", + "send_packet retry %d: readFrame failed", retryCount); return FALSE; } Sleep(1); // Reduced sleep time @@ -980,7 +987,8 @@ BOOL CVideoDecoder::decode(AVPacket* pkt) { char error_buf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, error_buf, sizeof(error_buf)); - log_print(HT_LOG_ERR, "%s, avcodec_send_packet failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret); + ANS_DBG("MEDIA_Decode", + "avcodec_send_packet failed: %s (ret=%d)", error_buf, ret); return FALSE; } @@ -993,25 +1001,29 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts) if (!m_bInited) { - log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__); + // Hot-path: per-packet if decoder isn't initialised. Was log_print + // which fflushes per call. + ANS_DBG("MEDIA_Decode", "decode called but decoder not initialized"); return FALSE; } if (!m_bRunning) { - log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__); + // Per-packet during start/stop transitions. + ANS_DBG("MEDIA_Decode", "decode called but decoder not running"); return FALSE; } if (!data || len <= 0) { - log_print(HT_LOG_ERR, "%s, Invalid input data\r\n", __FUNCTION__); + ANS_DBG("MEDIA_Decode", "decode called with invalid input (data=%p len=%d)", + (void*)data, len); return FALSE; } // Allocate packet AVPacket* packet = av_packet_alloc(); if (!packet) { - log_print(HT_LOG_ERR, "%s, Failed to allocate AVPacket\r\n", __FUNCTION__); + ANS_DBG("MEDIA_Decode", "av_packet_alloc failed (OOM?)"); return FALSE; } @@ -1020,7 +1032,8 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts) if (ret < 0) { char error_buf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, error_buf, sizeof(error_buf)); - log_print(HT_LOG_ERR, "%s, Failed to allocate packet data: %s\r\n", __FUNCTION__, error_buf); + ANS_DBG("MEDIA_Decode", + "av_new_packet failed (len=%d): %s", len, error_buf); av_packet_free(&packet); return FALSE; } @@ -1061,13 +1074,21 @@ BOOL CVideoDecoder::readFrame() else if (ret < 0) { char error_buf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, error_buf, sizeof(error_buf)); - log_print(HT_LOG_ERR, "%s, avcodec_receive_frame failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret); + // Hot-path error during stream stress (UDP loss, corrupt I-frame). + // Was log_print(HT_LOG_ERR, ...) which fflushes per call and could + // fire ~25/sec on a degraded stream — observed as sudden GetImage + // slowness. ANS_DBG is silent in production; pulse counter + // MEDIA_Pulse drop(err=) preserves observability. + ANS_DBG("MEDIA_Decode", + "avcodec_receive_frame failed: %s (ret=%d)", error_buf, ret); return FALSE; } // Check if we got a valid frame if (!m_pFrame || m_pFrame->width <= 0 || m_pFrame->height <= 0) { - log_print(HT_LOG_WARN, "%s, Received invalid frame\r\n", __FUNCTION__); + ANS_DBG("MEDIA_Decode", "received invalid frame (w=%d h=%d)", + m_pFrame ? m_pFrame->width : -1, + m_pFrame ? m_pFrame->height : -1); av_frame_unref(m_pFrame); continue; } @@ -1090,8 +1111,10 @@ BOOL CVideoDecoder::readFrame() { char error_buf[AV_ERROR_MAX_STRING_SIZE]; av_strerror(ret, error_buf, sizeof(error_buf)); - log_print(HT_LOG_ERR, "%s, Error transferring hardware frame to system memory: %s (ret=%d)\r\n", - __FUNCTION__, error_buf, ret); + // Hot-path: HW→SW transfer failure. Per-frame on degraded HW + // decoder. Gated to avoid fflush spike during stress. + ANS_DBG("MEDIA_Decode", + "hwframe_transfer_data failed: %s (ret=%d)", error_buf, ret); av_frame_unref(m_pFrame); if (m_pCudaHWFrame) av_frame_free(&m_pCudaHWFrame); continue; @@ -1139,7 +1162,9 @@ int CVideoDecoder::render(AVFrame* frame) m_pCallback(frame, m_pUserdata); } catch (...) { - log_print(HT_LOG_ERR, "%s, Exception in callback function\r\n", __FUNCTION__); + // Hot-path: per-frame if onVideoFrame ever throws. Was log_print + // which fflushes per call. + ANS_DBG("MEDIA_Decode", "exception in onVideoFrame callback"); return 0; } } diff --git a/MediaClient/media/video_player.cpp b/MediaClient/media/video_player.cpp index 7aecd14..8534016 100644 --- a/MediaClient/media/video_player.cpp +++ b/MediaClient/media/video_player.cpp @@ -219,14 +219,14 @@ AVFrame* CVideoPlayer::cropFrame(const AVFrame* srcFrame, cv::Rect bBox, bool cr // Validate the bounding box dimensions if (bBox.width <= 10 || bBox.height <= 10) { - std::cerr << "Invalid bounding box dimensions for cropping." << std::endl; + ANS_DBG("MEDIA_Crop", "invalid bounding box dimensions"); return nullptr; } // Allocate memory for the cropped frame AVFrame* croppedFrame = av_frame_alloc(); if (!croppedFrame) { - std::cerr << "Failed to allocate memory for the cropped frame." << std::endl; + ANS_DBG("MEDIA_Crop", "failed to allocate cropped frame"); return nullptr; } @@ -247,7 +247,7 @@ AVFrame* CVideoPlayer::cropFrame(const AVFrame* srcFrame, cv::Rect bBox, bool cr return croppedFrame; } catch (const std::exception& e) { - std::cerr << "Exception in CVideoPlayer::cropFrame: " << e.what() << std::endl; + ANS_DBG("MEDIA_Crop", "cropFrame exception: %s", e.what()); return nullptr; } } @@ -289,13 +289,14 @@ bool CVideoPlayer::cropFrameData(const AVFrame* srcFrame, AVFrame* croppedFrame, break; default: - std::cerr << "Unsupported pixel format: " << av_get_pix_fmt_name((AVPixelFormat)srcFrame->format) << std::endl; + ANS_DBG("MEDIA_Crop", "unsupported pixel format: %s", + av_get_pix_fmt_name((AVPixelFormat)srcFrame->format)); return false; } return true; } catch (const std::exception& e) { - std::cerr << "Exception in cropFrameData: " << e.what() << std::endl; + ANS_DBG("MEDIA_Crop", "cropFrameData exception: %s", e.what()); return false; } } @@ -310,7 +311,7 @@ void CVideoPlayer::cropPlane(const AVFrame* srcFrame, AVFrame* croppedFrame, int croppedFrame->linesize[planeIndex] = srcFrame->linesize[planeIndex]; } catch (const std::exception& e) { - std::cerr << "Exception in cropPlane: " << e.what() << std::endl; + ANS_DBG("MEDIA_Crop", "cropPlane exception: %s", e.what()); } } @@ -323,7 +324,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) { try { if (!nv12Frame || !nv12Frame->data[0] || nv12Frame->width <= 10 || nv12Frame->height <= 10) { - std::cerr << "Invalid or empty NV12 frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_NV12", "invalid or empty NV12 frame data"); return nullptr; } @@ -333,7 +334,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) { // ✅ Allocate new YUVJ420P frame yuvjFrame = av_frame_alloc(); if (!yuvjFrame) { - std::cerr << "Failed to allocate YUVJ420P frame" << std::endl; + ANS_DBG("MEDIA_NV12", "failed to allocate YUVJ420P frame"); return nullptr; } @@ -343,7 +344,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) { // ✅ Allocate buffer for YUVJ420P frame if (av_frame_get_buffer(yuvjFrame, 32) < 0) { - std::cerr << "Failed to allocate buffer for YUVJ420P" << std::endl; + ANS_DBG("MEDIA_NV12", "failed to allocate YUVJ420P buffer"); av_frame_free(&yuvjFrame); return nullptr; } @@ -376,7 +377,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) { return yuvjFrame; } catch (const std::exception& e) { - std::cerr << "Exception in convertNV12ToYUVJ420P: " << e.what() << std::endl; + ANS_DBG("MEDIA_NV12", "convertNV12ToYUVJ420P exception: %s", e.what()); // ✅ Prevent Memory Leak by Freeing the Allocated Frame if (yuvjFrame) { @@ -393,17 +394,17 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingFFMpeg(const AVFrame* return ""; } if (!pFrame || !pFrame->data[0] || pFrame->width <= 10 || pFrame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return ""; } AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); if (!jpegCodec) { - std::cerr << "Failed to find MJPEG encoder." << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to find MJPEG encoder"); return ""; } AVCodecContext* jpegContext = avcodec_alloc_context3(jpegCodec); if (!jpegContext) { - std::cerr << "Failed to allocate codec context." << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context"); return ""; } int imageSize = std::max(pFrame->width, pFrame->height); @@ -480,7 +481,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingFFMpeg(const AVFrame* return isSuccess ? jpegData : ""; } catch (const std::exception& e) { - std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what()); return ""; // Return empty string on error } } @@ -551,7 +552,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingTurboJPEG(const AVFram return std::string(reinterpret_cast(jpegDataPtr), jpegSize); } catch (const std::exception& e) { - std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what()); return ""; // Return empty string on error } } @@ -559,21 +560,21 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) { std::lock_guard lock(_mutex); try { if (!frame || frame->format != AV_PIX_FMT_YUVJ420P) { - std::cerr << "Invalid frame format (must be YUVJ420P)" << std::endl; + ANS_DBG("MEDIA_Convert", "invalid frame format (must be YUVJ420P)"); return ""; } // Find MJPEG encoder AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); if (!codec) { - std::cerr << "JPEG encoder not found" << std::endl; + ANS_DBG("MEDIA_Jpeg", "JPEG encoder not found"); return ""; } // Allocate codec context AVCodecContext* codecCtx = avcodec_alloc_context3(codec); if (!codecCtx) { - std::cerr << "Failed to allocate codec context" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context (yuvj420p)"); return ""; } @@ -595,7 +596,7 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) { // Open codec if (avcodec_open2(codecCtx, codec, &opts) < 0) { - std::cerr << "Failed to open JPEG encoder" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to open JPEG encoder"); avcodec_free_context(&codecCtx); return ""; } @@ -607,14 +608,14 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) { // Send frame to encoder if (avcodec_send_frame(codecCtx, frame) < 0) { - std::cerr << "Failed to send frame for encoding" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to send frame for encoding"); avcodec_free_context(&codecCtx); return ""; } // Receive encoded packet if (avcodec_receive_packet(codecCtx, &pkt) < 0) { - std::cerr << "Failed to receive encoded packet" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to receive encoded packet"); avcodec_free_context(&codecCtx); return ""; } @@ -628,7 +629,7 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) { return jpegString; } catch (const std::exception& e) { - std::cerr << "Exception in encodeYUVJ420PToJPEG: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "encodeYUVJ420PToJPEG exception: %s", e.what()); return ""; // Return empty string on error } } @@ -649,7 +650,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegString(const AVFrame* spFrame) { } if (!spFrame || !spFrame->data[0] || spFrame->width <= 10 || spFrame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return ""; } @@ -735,7 +736,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegString(const AVFrame* spFrame) { return isSuccess ? jpegData : ""; } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUVJ420PToJpegString: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "avframeYUVJ420PToJpegString exception: %s", e.what()); // ✅ Ensure all allocated frames are freed in case of an exception if (croppedFrame && croppedFrame != pFrame && croppedFrame != convertedFrame) { @@ -762,14 +763,14 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int } // Ensure TurboJPEG instance is valid if (!_tjInstance) { - std::cerr << "TurboJPEG instance is not initialized." << std::endl; + ANS_DBG("MEDIA_Jpeg", "TurboJPEG instance not initialized"); return ""; } // Ensure the frame format is NV12 if (pFrame->format != AV_PIX_FMT_NV12) { - std::cerr << "Unsupported format! Expected NV12, got: " - << av_get_pix_fmt_name((AVPixelFormat)pFrame->format) << std::endl; + ANS_DBG("MEDIA_Jpeg", "TurboJPEG: unsupported format (expected NV12, got %s)", + av_get_pix_fmt_name((AVPixelFormat)pFrame->format)); return ""; } @@ -860,7 +861,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int ); if (ret < 0) { - std::cerr << "TurboJPEG compression failed: " << tjGetErrorStr() << std::endl; + ANS_DBG("MEDIA_Jpeg", "TurboJPEG compression failed: %s", tjGetErrorStr()); return ""; } @@ -875,7 +876,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int return std::string(reinterpret_cast(jpegDataPtr), jpegSize); } catch (const std::exception& e) { - std::cerr << "Exception in avframeNV12ToJpegStringUsingTurboJPEG: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "avframeNV12ToJpegStringUsingTurboJPEG exception: %s", e.what()); return ""; // Return empty string on error } } @@ -890,7 +891,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int } if (nv12Frame->format != AV_PIX_FMT_NV12) { - std::cerr << "Invalid frame format! Expected NV12." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid frame format (expected NV12)"); return ""; } @@ -900,13 +901,13 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int // ✅ Find and allocate MJPEG encoder AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); if (!jpegCodec) { - std::cerr << "MJPEG encoder not found!" << std::endl; + ANS_DBG("MEDIA_Jpeg", "MJPEG encoder not found"); return ""; } codecCtx = avcodec_alloc_context3(jpegCodec); if (!codecCtx) { - std::cerr << "Failed to allocate codec context!" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context (NV12)"); return ""; } @@ -922,7 +923,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int codecCtx->global_quality = quality * FF_QP2LAMBDA; if (avcodec_open2(codecCtx, jpegCodec, nullptr) < 0) { - std::cerr << "Failed to open MJPEG encoder!" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to open MJPEG encoder"); avcodec_free_context(&codecCtx); return ""; } @@ -930,7 +931,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int // ✅ Allocate YUVJ420P frame yuvjFrame = av_frame_alloc(); if (!yuvjFrame) { - std::cerr << "Failed to allocate YUVJ420P frame!" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to allocate YUVJ420P frame"); avcodec_free_context(&codecCtx); return ""; } @@ -940,7 +941,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int yuvjFrame->height = height; if (av_frame_get_buffer(yuvjFrame, 32) < 0) { - std::cerr << "Failed to allocate buffer for YUVJ420P frame!" << std::endl; + ANS_DBG("MEDIA_Jpeg", "failed to allocate buffer for YUVJ420P frame"); av_frame_free(&yuvjFrame); avcodec_free_context(&codecCtx); return ""; @@ -994,7 +995,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int return isSuccess ? jpegData : ""; } catch (const std::exception& e) { - std::cerr << "Exception in encodeNV12ToJPEG_FFmpeg: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "encodeNV12ToJPEG_FFmpeg exception: %s", e.what()); } // ✅ Ensure memory cleanup in case of exceptions @@ -1019,7 +1020,7 @@ std::string CVideoPlayer::avframeToJpegString(const AVFrame* spFrame) { } if (!spFrame || !spFrame->data[0] || spFrame->width <= 10 || spFrame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return ""; } @@ -1091,7 +1092,7 @@ std::string CVideoPlayer::avframeToJpegString(const AVFrame* spFrame) { return isSuccess ? jpegData : ""; } catch (const std::exception& e) { - std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl; + ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what()); // ✅ Cleanup memory in case of exceptions if (croppedFrame && croppedFrame != pFrame && croppedFrame != convertedFrame) { @@ -1159,7 +1160,7 @@ bool CVideoPlayer::areFramesIdentical(AVFrame* frame1, AVFrame* frame2) { return true; // If all planes match } catch (const std::exception& e) { - std::cerr << "Exception in areFramesIdentical: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "areFramesIdentical exception: %s", e.what()); return false; } } @@ -1169,9 +1170,9 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A try { // Validate input dimensions and pixel format if (width <= 0 || height <= 0 || pixFmt == AV_PIX_FMT_NONE) { - std::cerr << "Invalid parameters: width=" << width - << ", height=" << height - << ", pixFmt=" << pixFmt << std::endl; + ANS_DBG("MEDIA_Convert", + "initSwsContext invalid params: w=%d h=%d pixFmt=%d", + width, height, (int)pixFmt); return; } @@ -1199,10 +1200,9 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A // Check for errors in SwsContext creation if (!swsCtx) { - std::cerr << "Failed to create SwsContext: width=" << width - << ", height=" << height - << ", inputPixFmt=" << pixFmt - << ", outputPixFmt=" << outputPixFmt << std::endl; + ANS_DBG("MEDIA_Convert", + "sws_getContext failed: w=%d h=%d in=%d out=%d", + width, height, (int)pixFmt, (int)outputPixFmt); return; } @@ -1213,17 +1213,17 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A lastOutPixFmt = outputPixFmt; } catch (const std::exception& e) { - std::cerr << "Exception in initSwsContext: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "initSwsContext exception: %s", e.what()); } catch (...) { - std::cerr << "Unknown exception in initSwsContext." << std::endl; + ANS_DBG("MEDIA_Convert", "initSwsContext unknown exception"); } } cv::Mat CVideoPlayer::avframeAnyToCvmat(const AVFrame* frame) { std::lock_guard lock(_mutex); // Protect against concurrent access try { if (!frame || !frame->data[0] || frame->width <= 10 || frame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return cv::Mat(); // Return an empty matrix if the frame is invalid } initSwsContext(frame->width, frame->height, static_cast(frame->format), AV_PIX_FMT_BGR24); @@ -1237,14 +1237,14 @@ cv::Mat CVideoPlayer::avframeAnyToCvmat(const AVFrame* frame) { // Perform the conversion using sws_scale int result = sws_scale(swsCtx, frame->data, frame->linesize, 0, frame->height, dst, dstStride); if (result < 0) { - std::cerr << "Failed to scale the frame." << std::endl; + ANS_DBG("MEDIA_Convert", "sws_scale failed"); return cv::Mat(); // Return an empty matrix if scaling fails } return image; // Return the successfully converted OpenCV Mat } catch (const std::exception& e) { - std::cerr << "Exception in avframeToCvmat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeToCvmat exception: %s", e.what()); return cv::Mat(); // Return an empty matrix on error } } @@ -1254,7 +1254,7 @@ cv::Mat CVideoPlayer::avframeYUVJ420PToCvmat(const AVFrame* frame) { std::lock_guard lock(_mutex); try { if (!frame || !frame->data[0] || frame->width <= 10 || frame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return cv::Mat(); } @@ -1304,7 +1304,7 @@ cv::Mat CVideoPlayer::avframeYUVJ420PToCvmat(const AVFrame* frame) { return image; // Return the converted OpenCV Mat (BGR) } catch (const std::exception& e) { - std::cerr << "Exception in avframeToCvmatYUVJ420P: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeToCvmatYUVJ420P exception: %s", e.what()); return cv::Mat(); // Return an empty matrix on error } } @@ -1359,7 +1359,7 @@ void CVideoPlayer::initNV12SwsContext(const AVFrame* frame) { nullptr, nullptr, nullptr); if (!m_nv12SwsCtx) { - std::cerr << "Failed to create NV12 SwsContext" << std::endl; + ANS_DBG("MEDIA_Convert", "failed to create NV12 SwsContext"); return; } @@ -1383,7 +1383,7 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame) { try { if (!frame || frame->width <= 0 || frame->height <= 0) { - std::cerr << "Invalid frame! Either null, incorrect format, or zero dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid frame: null/wrong-format/zero-dims"); return cv::Mat(); } // Software decode handler @@ -1417,7 +1417,7 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame) return bgrImage; } catch (const std::exception& e) { - std::cerr << "Exception in avframeNV12ToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeNV12ToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1496,7 +1496,7 @@ cv::Mat CVideoPlayer::avframeYUV420PToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (ret != 0) { - std::cerr << "libyuv::I420/J420ToRGB24 failed with ret=" << ret << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I420/J420ToRGB24 failed ret=%d %dx%d", ret, width, height); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1589,7 +1589,7 @@ cv::Mat CVideoPlayer::avframeYUV420PToCvMat(const AVFrame* frame) { #endif // ANSCORE_HAS_LIBYUV } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV420PToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV420PToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1618,7 +1618,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) { frame->data[2], frame->linesize[2], s_argbBuf.data(), argbStride, width, height); if (r1 != 0) { - std::cerr << "libyuv::J422ToARGB failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv J422ToARGB failed ret=%d", r1); return cv::Mat(); } ret = libyuv::ARGBToRGB24( @@ -1634,7 +1634,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) { width, height); } if (ret != 0) { - std::cerr << "libyuv::I422/J422ToRGB24 failed ret=" << ret << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I422/J422ToRGB24 failed ret=%d", ret); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1646,7 +1646,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV422PToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV422PToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1672,7 +1672,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) { frame->data[2], frame->linesize[2], s_argbBuf.data(), argbStride, width, height); if (r1 != 0) { - std::cerr << "libyuv::J444ToARGB failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv J444ToARGB failed ret=%d", r1); return cv::Mat(); } ret = libyuv::ARGBToRGB24( @@ -1688,7 +1688,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) { width, height); } if (ret != 0) { - std::cerr << "libyuv::I444/J444ToRGB24 failed ret=" << ret << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I444/J444ToRGB24 failed ret=%d", ret); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1700,7 +1700,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV444PToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV444PToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1728,7 +1728,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) { reinterpret_cast(frame->data[2]), frame->linesize[2] / 2, s_argbBuf.data(), argbStride, width, height); if (r1 != 0) { - std::cerr << "libyuv::I010ToARGB failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I010ToARGB failed ret=%d", r1); return cv::Mat(); } @@ -1738,7 +1738,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1750,7 +1750,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV420P10LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV420P10LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1774,7 +1774,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) { reinterpret_cast(frame->data[2]), frame->linesize[2] / 2, s_argbBuf.data(), argbStride, width, height); if (r1 != 0) { - std::cerr << "libyuv::I210ToARGB failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I210ToARGB failed ret=%d", r1); return cv::Mat(); } @@ -1784,7 +1784,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1796,7 +1796,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV422P10LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV422P10LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1824,7 +1824,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) { s_argbBuf.data(), argbStride, &libyuv::kYuvI601Constants, width, height); if (r1 != 0) { - std::cerr << "libyuv::I410ToARGBMatrix failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I410ToARGBMatrix failed ret=%d", r1); return cv::Mat(); } @@ -1834,7 +1834,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1846,7 +1846,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV444P10LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV444P10LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1873,7 +1873,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) { s_argbBuf.data(), argbStride, &libyuv::kYuvI601Constants, width, height); if (r1 != 0) { - std::cerr << "libyuv::I012ToARGBMatrix failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I012ToARGBMatrix failed ret=%d", r1); return cv::Mat(); } @@ -1883,7 +1883,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1895,7 +1895,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV420P12LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV420P12LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1930,7 +1930,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) { v8, uvW, width, height); if (r1 != 0) { - std::cerr << "libyuv::I212ToI422 failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I212ToI422 failed ret=%d", r1); return cv::Mat(); } @@ -1942,7 +1942,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::I422ToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I422ToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -1954,7 +1954,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV422P12LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV422P12LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -1987,7 +1987,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) { v8, width, width, height); if (r1 != 0) { - std::cerr << "libyuv::I412ToI444 failed ret=" << r1 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I412ToI444 failed ret=%d", r1); return cv::Mat(); } @@ -1999,7 +1999,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) { bgrImage.data, static_cast(bgrImage.step), width, height); if (r2 != 0) { - std::cerr << "libyuv::I444ToRGB24 failed ret=" << r2 << std::endl; + ANS_DBG("MEDIA_Convert", "libyuv I444ToRGB24 failed ret=%d", r2); return cv::Mat(); } if (m_nImageQuality == 1) { @@ -2011,7 +2011,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) { #endif } catch (const std::exception& e) { - std::cerr << "Exception in avframeYUV444P12LEToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeYUV444P12LEToCvMat exception: %s", e.what()); return cv::Mat(); } } @@ -2024,7 +2024,7 @@ cv::Mat CVideoPlayer::avframeToCVMat(const AVFrame* pFrame) { try { // 1. Validate input frame if (!pFrame || !pFrame->data[0] || pFrame->width <= 10 || pFrame->height <= 10) { - std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl; + ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions"); return cv::Mat(); } @@ -2142,7 +2142,7 @@ cv::Mat CVideoPlayer::avframeToCVMat(const AVFrame* pFrame) { } } catch (const std::exception& e) { - std::cerr << "Exception in avframeToCvMat: " << e.what() << std::endl; + ANS_DBG("MEDIA_Convert", "avframeToCvMat dispatcher exception: %s", e.what()); return cv::Mat(); // Return an empty matrix on error } } diff --git a/MediaClient/media/video_player.h b/MediaClient/media/video_player.h index 67c820f..b9d465b 100644 --- a/MediaClient/media/video_player.h +++ b/MediaClient/media/video_player.h @@ -18,6 +18,7 @@ #include #include #include +#include "ANSLicense.h" // ANS_DBG (used by inline FrameQueue::pushFrame below) // Leak diagnostics — net counters surfaced in MEDIA_Leak heartbeat. // Defined in video_player.cpp; also incremented from FrameQueue here. @@ -71,7 +72,9 @@ public: // Clone the frame to ensure proper ownership transfer AVFrame* frameCopy = av_frame_clone(newFrame); if (!frameCopy) { - std::cerr << "Failed to clone AVFrame!" << std::endl; + // av_frame_clone failure (basically OOM). Gated to avoid spamming + // stderr during memory pressure. + ANS_DBG("MEDIA_Queue", "av_frame_clone failed (OOM?)"); return; } g_queueClones.fetch_add(1, std::memory_order_relaxed); diff --git a/modules/ANSCV/ANSRTSP.cpp b/modules/ANSCV/ANSRTSP.cpp index b07bce2..da5946a 100644 --- a/modules/ANSCV/ANSRTSP.cpp +++ b/modules/ANSCV/ANSRTSP.cpp @@ -451,28 +451,31 @@ namespace ANSCENTER { } } + // areImagesIdentical: honest image-equality check. Returns true iff the two + // Mats refer to the same pixels. Used by callers who want to know "did I + // just get the same frame back?" — typical pattern after GetImage to skip + // expensive downstream work on rate-limited / buffered duplicates. + // + // For camera-aliveness ("did the decoder go silent?") use + // GetRTSPLastFrameAgeMs() instead — that's a separate concern and was the + // wrong thing to put inside this function before. With the new pts + // discipline (same image -> same pts), most callers can simply compare + // pts instead of calling this function at all. bool ANSRTSPClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) { - double ageMs = _playerClient->getLastFrameAgeMs(); - - if (ageMs > 5000.0) { - ANS_DBG("RTSP_Stale", "FROZEN DETECTED: ageMs=%.1f url=%s playing=%d — camera truly stale", - ageMs, _url.c_str(), (int)_isPlaying); - return true; // Truly stale — no decoder output for 5+ seconds - } - if (ageMs > 0.0) { - return false; // Decoder is receiving frames — camera is alive - } - - // ageMs == 0 means no frame has been decoded yet (startup). - // Fall back to pixel comparison for backward compatibility. if (img1.empty() && img2.empty()) return true; if (img1.empty() || img2.empty()) return false; if (img1.size() != img2.size() || img1.type() != img2.type()) return false; - // Same data pointer = same cv::Mat (shallow copy) + // Fast path — same buffer pointer. With the new GetImage logic, the + // duplicate path returns _pLastFrame, so the caller gets back the + // same buffer it received last time → this branch hits and we're done + // in O(1). The vast majority of "is this a duplicate?" queries land + // here. if (img1.data == img2.data) return true; - // Quick 5-point sampling + // Distinct buffers — fall back to a real pixel comparison. Quick + // 4-point sample first to early-out on obvious mismatches, then a + // full memcmp for confirmation. if (img1.isContinuous() && img2.isContinuous()) { const size_t totalBytes = img1.total() * img1.elemSize(); const size_t quarter = totalBytes / 4; @@ -545,31 +548,34 @@ namespace ANSCENTER { } // Duplicate-frame detection. We track the inner player's currentPts - // (in _lastInnerPts) separately from our own _pts. This is important: - // _pts gets synthetically advanced on every duplicate so LabVIEW's - // staleness check sees a changing timestamp. If we compared currentPts - // against _pts, the synthetic advance would make the comparison fail - // on the next call and route the same cached frame through the - // "new frame" path — pointless resize + storage on identical pixels. + // (in _lastInnerPts) separately from our own _pts so that: + // 1. duplicate detection stays correct even when _pts has been + // bumped past currentPts in earlier "new frame" calls + // 2. LabVIEW's invariant "same pts == same image" holds — required + // so a caller comparing pts (or calling areImagesIdentical()) + // correctly recognises rate-limited / buffered duplicates. + // + // Camera-alive detection MUST use GetRTSPLastFrameAgeMs() — pts is no + // longer abused as a heartbeat. ageMs is updated for every decoded + // frame inside CVideoPlayer::onVideoFrame, independent of pacing. if (currentPts == _lastInnerPts && !_pLastFrame.empty()) { width = _imageWidth; height = _imageHeight; + // Optional: log the stale case so DebugView still surfaces it. double ageMs = _playerClient->getLastFrameAgeMs(); - if (ageMs > 0.0 && ageMs < 5000.0) { - _pts++; // synthetic advance — LabVIEW staleness signal - } else if (ageMs >= 5000.0) { - ANS_DBG("RTSP_GetImage", "STALE PTS: ageMs=%.1f pts=%lld url=%s — not advancing PTS", + if (ageMs >= 5000.0) { + ANS_DBG("RTSP_GetImage", "STALE PTS: ageMs=%.1f pts=%lld url=%s", ageMs, (long long)_pts, _url.c_str()); } - pts = _pts; + pts = _pts; // unchanged — same image, same pts g_rtspPulseDupPts.fetch_add(1, std::memory_order_relaxed); return _pLastFrame; } // Inner advanced — real new frame. Sync our duplicate-detection cursor - // and bump _pts monotonically (never assign to currentPts, which would - // make _pts go backwards if it had been advanced past currentPts on - // previous duplicates). + // and bump _pts monotonically. Each ++ corresponds to a unique frame + // delivered to the consumer, so a caller can use pts as a stable + // "did I get a new frame?" signal. _lastInnerPts = currentPts; _pts++; pts = _pts; @@ -1202,11 +1208,11 @@ extern "C" __declspec(dllexport) int GetRTSPStrImage(ANSCENTER::ANSRTSPClient * else return 0; } catch (const std::exception& e) { - std::cerr << "Error getting RTSP image: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting RTSP image: Unknown exception." << std::endl; + ANS_DBG("RTSP_GetImage", "unknown exception"); return 0; } } @@ -1219,7 +1225,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage( { // Validate input parameters if (!Handle || !*Handle || !image) { - std::cerr << "Error: Invalid input parameters in GetRTSPCVImage" << std::endl; + ANS_DBG("RTSP_GetImage", "invalid input parameters"); return -1; } @@ -1327,15 +1333,15 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage( return 1; // Success } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetRTSPCVImage: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetRTSPCVImage" << std::endl; + ANS_DBG("RTSP_GetImage", "unknown exception in GetRTSPCVImage"); return -2; } } @@ -1379,11 +1385,11 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage( // return 1; // Success // } // catch (const cv::Exception& e) { -// std::cerr << "OpenCV exception in GetRTSPCVImage: " << e.what() << std::endl; +// ANS_DBG("RTSP_GetImage", "OpenCV exception: %s", e.what()); // return -2; // Error code for OpenCV exceptions // } // catch (const std::exception& e) { -// std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl; +// ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what()); // return -2; // Error code for standard exceptions // } // catch (...) { @@ -1418,7 +1424,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage( // return 1; // Success // } // catch (const std::exception& e) { -// std::cerr << "Exception in GetRTSPCVImage: " << e.what() << std::endl; +// ANS_DBG("RTSP_GetImage", "exception in GetRTSPCVImage: %s", e.what()); // return -2; // Error code for exceptions // } // catch (...) { @@ -1447,22 +1453,22 @@ extern "C" __declspec(dllexport) int GetRTSPImage(ANSCENTER::ANSRTSPClient** Han } else { // Return failure if there was an error in resizing the handle - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("RTSP_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { // If the JPEG image string is empty, return failure - std::cerr << "No image data retrieved from FLV client." << std::endl; + ANS_DBG("RTSP_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting RTSP image: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting RTSP image: Unknown exception." << std::endl; + ANS_DBG("RTSP_GetImage", "unknown exception"); return 0; } } @@ -1475,11 +1481,11 @@ extern "C" __declspec(dllexport) int StartRTSP(ANSCENTER::ANSRTSPClient **Handle else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "StartRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "StartRTSP unknown exception"); return 0; } } @@ -1491,11 +1497,11 @@ extern "C" __declspec(dllexport) int ReconnectRTSP(ANSCENTER::ANSRTSPClient * *H else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP unknown exception"); return 0; } } @@ -1508,11 +1514,11 @@ extern "C" __declspec(dllexport) int StopRTSP(ANSCENTER::ANSRTSPClient * *Handle else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "StopRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "StopRTSP unknown exception"); return 0; } @@ -1525,11 +1531,11 @@ extern "C" __declspec(dllexport) int PauseRTSP(ANSCENTER::ANSRTSPClient** Handle else return 0; } catch (const std::exception& e) { - std::cerr << "Error pausing RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "PauseRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error pausing RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "PauseRTSP unknown exception"); return 0; } } @@ -1541,11 +1547,11 @@ extern "C" __declspec(dllexport) int IsRTSPPaused(ANSCENTER::ANSRTSPClient** Han else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is paused: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is paused: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused unknown exception"); return 0; } } @@ -1557,11 +1563,11 @@ extern "C" __declspec(dllexport) int IsRTSPRunning(ANSCENTER::ANSRTSPClient **Ha else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is running: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is running: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning unknown exception"); return 0; } } @@ -1573,11 +1579,11 @@ extern "C" __declspec(dllexport) int IsRTSPRecording(ANSCENTER::ANSRTSPClient ** else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is recording: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is recording: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording unknown exception"); return 0; } } @@ -1609,11 +1615,11 @@ extern "C" __declspec(dllexport) int SetBBoxRTSP(ANSCENTER::ANSRTSPClient** Hand return 1; } catch (const std::exception& e) { - std::cerr << "Error setting RTSP bounding box: " << e.what() << std::endl; + ANS_DBG("RTSP_Cfg", "SetBBoxRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error setting RTSP bounding box: Unknown exception." << std::endl; + ANS_DBG("RTSP_Cfg", "SetBBoxRTSP unknown exception"); return 0; } } @@ -1698,11 +1704,11 @@ extern "C" __declspec(dllexport) int SetCropFlagRTSP(ANSCENTER::ANSRTSPClient** return 1; } catch (const std::exception& e) { - std::cerr << "Error setting RTSP crop flag: " << e.what() << std::endl; + ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error setting RTSP crop flag: Unknown exception." << std::endl; + ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP unknown exception"); return 0; } } @@ -1727,21 +1733,21 @@ extern "C" __declspec(dllexport) int GetRTSPImage_V2(uint64_t handleVal, int& wi return 1; } else { - std::cerr << "Error resizing jpegImage handle: " << error << std::endl; + ANS_DBG("RTSP_GetImage", "DSSetHandleSize failed: error=%d", (int)error); return 0; } } else { - std::cerr << "No image data retrieved from FLV client." << std::endl; + ANS_DBG("RTSP_GetImage", "no image data retrieved (jpegString empty)"); return 0; } } catch (const std::exception& e) { - std::cerr << "Error getting RTSP image: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error getting RTSP image: Unknown exception." << std::endl; + ANS_DBG("RTSP_GetImage", "unknown exception"); return 0; } } @@ -1755,7 +1761,7 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage_V2( { auto* h = reinterpret_cast(handleVal); if (!h) return -1; if (!image) { - std::cerr << "Error: Invalid input parameters in GetRTSPCVImage_V2" << std::endl; + ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 invalid input parameters"); return -1; } try { @@ -1767,15 +1773,15 @@ extern "C" __declspec(dllexport) int GetRTSPCVImage_V2( return 1; } catch (const cv::Exception& e) { - std::cerr << "OpenCV exception in GetRTSPCVImage_V2: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 OpenCV exception: %s", e.what()); return -2; } catch (const std::exception& e) { - std::cerr << "Exception in GetRTSPCVImage_V2: " << e.what() << std::endl; + ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 exception: %s", e.what()); return -2; } catch (...) { - std::cerr << "Unknown exception in GetRTSPCVImage_V2" << std::endl; + ANS_DBG("RTSP_GetImage", "GetRTSPCVImage_V2 unknown exception"); return -2; } } @@ -1788,11 +1794,11 @@ extern "C" __declspec(dllexport) int StartRTSP_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error starting RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "StartRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error starting RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "StartRTSP unknown exception"); return 0; } } @@ -1805,11 +1811,11 @@ extern "C" __declspec(dllexport) int ReconnectRTSP_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error reconnecting RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error reconnecting RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "ReconnectRTSP unknown exception"); return 0; } } @@ -1822,11 +1828,11 @@ extern "C" __declspec(dllexport) int StopRTSP_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error stopping RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "StopRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error stopping RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "StopRTSP unknown exception"); return 0; } } @@ -1839,11 +1845,11 @@ extern "C" __declspec(dllexport) int PauseRTSP_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error pausing RTSP client: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "PauseRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error pausing RTSP client: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "PauseRTSP unknown exception"); return 0; } } @@ -1856,11 +1862,11 @@ extern "C" __declspec(dllexport) int IsRTSPPaused_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is paused: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is paused: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPPaused unknown exception"); return 0; } } @@ -1873,11 +1879,11 @@ extern "C" __declspec(dllexport) int IsRTSPRunning_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is running: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is running: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRunning unknown exception"); return 0; } } @@ -1890,11 +1896,11 @@ extern "C" __declspec(dllexport) int IsRTSPRecording_V2(uint64_t handleVal) { else return 0; } catch (const std::exception& e) { - std::cerr << "Error checking if RTSP client is recording: " << e.what() << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error checking if RTSP client is recording: Unknown exception." << std::endl; + ANS_DBG("RTSP_Lifecycle", "IsRTSPRecording unknown exception"); return 0; } } @@ -1933,11 +1939,11 @@ extern "C" __declspec(dllexport) int SetBBoxRTSP_V2(uint64_t handleVal, int x, i return 1; } catch (const std::exception& e) { - std::cerr << "Error setting RTSP bounding box: " << e.what() << std::endl; + ANS_DBG("RTSP_Cfg", "SetBBoxRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error setting RTSP bounding box: Unknown exception." << std::endl; + ANS_DBG("RTSP_Cfg", "SetBBoxRTSP unknown exception"); return 0; } } @@ -1951,11 +1957,11 @@ extern "C" __declspec(dllexport) int SetCropFlagRTSP_V2(uint64_t handleVal, int return 1; } catch (const std::exception& e) { - std::cerr << "Error setting RTSP crop flag: " << e.what() << std::endl; + ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP exception: %s", e.what()); return 0; } catch (...) { - std::cerr << "Error setting RTSP crop flag: Unknown exception." << std::endl; + ANS_DBG("RTSP_Cfg", "SetCropFlagRTSP unknown exception"); return 0; } }