Browse Source

Embed video: apply DB setting changes, robust logo and time stamp

- validate(): the settings-changed check was dead code after an early
  return; it now runs first and reloads the module when the module_eve
  row changes (e.g. from the Mojo_Web module settings).
- stopService() returned false with EVE_WORK_WITH_MEDIAMTX (m_bDetached
  was only set by the picture server), so a reload stopped the streams
  and never restarted them.
- Logo: computed position and state live in m_bLogoActive / m_nLogoX /
  m_nLogoY instead of overwriting m_sEmbedVideoData (that made the struct
  differ from the DB row and caused endless reloads). Logos without an
  alpha channel no longer crash (whole picture drawn); ROI is checked.
- Time stamp: negative ts_x / ts_y are measured from the right / bottom
  edge like label and logo; background box fits the text instead of the
  fixed (490,60) corner.
- Font face and line type from the DB are sanitised before cv::putText
  (an unsupported line type throws).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/28/head
Claude 4 hours ago
parent
commit
0e8279e22a
Failed to extract signature
  1. 138
      lib/App/common/kernelModuleEmbedVideo.cpp
  2. 5
      lib/App/common/kernelModuleEmbedVideo.h
  3. 82
      lib/App/common/streamHandler.cpp

138
lib/App/common/kernelModuleEmbedVideo.cpp

@ -129,6 +129,9 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
applyLiveSettings(); applyLiveSettings();
n_pNoSignalData = NULL; n_pNoSignalData = NULL;
m_nNoSignalSize = 0; m_nNoSignalSize = 0;
m_bLogoActive = false;
m_nLogoX = 0;
m_nLogoY = 0;
#ifdef HAVE_SMAL_PREVIEW_STREAMS #ifdef HAVE_SMAL_PREVIEW_STREAMS
n_pNoSignalSmallData = NULL; n_pNoSignalSmallData = NULL;
m_nNoSignalSmallSize = 0; m_nNoSignalSmallSize = 0;
@ -169,8 +172,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
sprintf(tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5); sprintf(tPath, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
if (! fileExist(tPath)) if (! fileExist(tPath))
{ {
LOG_ERROR("Logo File Missed!");
m_sEmbedVideoData.show_logo = false;
LOG_ERROR("Logo File Missed!"); // startService() leaves the logo off
} }
#ifdef DEBUG #ifdef DEBUG
else else
@ -1336,86 +1338,68 @@ bool kernelModuleEmbedVideo::startService()
cv::Mat tMat(m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3); cv::Mat tMat(m_sEmbedVideoData.height, m_sEmbedVideoData.width, CV_8UC3);
cv::rectangle(tMat, cv::Point(0, 0), cv::Point(m_sEmbedVideoData.width, m_sEmbedVideoData.height), cv::Scalar(255, 0, 0), cv::FILLED); cv::rectangle(tMat, cv::Point(0, 0), cv::Point(m_sEmbedVideoData.width, m_sEmbedVideoData.height), cv::Scalar(255, 0, 0), cv::FILLED);
cv::putText(tMat, "NO SIGNAL", cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 3); cv::putText(tMat, "NO SIGNAL", cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 3);
if (m_sEmbedVideoData.show_logo)
{
int a_x, a_y;
if (m_sEmbedVideoData.logo_x < 0)
{
a_x = m_sEmbedVideoData.width + m_sEmbedVideoData.logo_x - m_sEmbedVideoData.logo_w;
if (a_x < 0)
{
a_x = 0;
}
}
else
{
a_x = m_sEmbedVideoData.logo_x;
}
if ((a_x + m_sEmbedVideoData.logo_w) > m_sEmbedVideoData.width)
{
a_x = m_sEmbedVideoData.width - m_sEmbedVideoData.logo_w;
}
if (m_sEmbedVideoData.logo_y < 0)
{
a_y = m_sEmbedVideoData.height + m_sEmbedVideoData.logo_y - m_sEmbedVideoData.logo_h;
if (a_y < 0)
{
a_y = 0;
}
}
else
m_bLogoActive = false;
m_mLogoPic.release();
m_mLogoMask.release();
if (m_sEmbedVideoData.show_logo && m_sEmbedVideoData.logo_w > 0 && m_sEmbedVideoData.logo_h > 0 && m_sEmbedVideoData.logo_w <= m_sEmbedVideoData.width && m_sEmbedVideoData.logo_h <= m_sEmbedVideoData.height)
{
// Negative coordinates are measured from the right / bottom edge
int a_x = m_sEmbedVideoData.logo_x;
int a_y = m_sEmbedVideoData.logo_y;
if (a_x < 0)
{ {
a_y = m_sEmbedVideoData.logo_y;
a_x = m_sEmbedVideoData.width + a_x - m_sEmbedVideoData.logo_w;
} }
if ((a_y + m_sEmbedVideoData.logo_h) > m_sEmbedVideoData.height)
if (a_y < 0)
{ {
a_y = m_sEmbedVideoData.height - m_sEmbedVideoData.logo_h;
a_y = m_sEmbedVideoData.height + a_y - m_sEmbedVideoData.logo_h;
} }
if ((a_x > -1) && (a_y > -1))
a_x = std::max(0, std::min(a_x, (int) m_sEmbedVideoData.width - (int) m_sEmbedVideoData.logo_w));
a_y = std::max(0, std::min(a_y, (int) m_sEmbedVideoData.height - (int) m_sEmbedVideoData.logo_h));
char tName[256];
snprintf(tName, sizeof(tName), "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
cv::Mat T = cv::imread(tName, cv::IMREAD_UNCHANGED);
if (T.cols > 0 && T.rows > 0)
{ {
char tName[256];
m_sEmbedVideoData.logo_x = a_x;
m_sEmbedVideoData.logo_y = a_y;
sprintf(tName, "data/assets/images/%s.png", m_sEmbedVideoData.logo_md5);
cv::Mat T = cv::imread(tName, cv::IMREAD_UNCHANGED);
if (T.cols > 0)
cv::Mat rgb, mask;
if (T.channels() == 4)
{ {
cv::Mat mask;
vector<cv::Mat> layers; vector<cv::Mat> layers;
#ifdef DEBUG_EXTRA
LOG("Logo Loaded OK");
#endif
cv::split(T, layers); // seperate channels
cv::Mat rgb[3] = {layers[0], layers[1], layers[2]};
mask = layers[3]; // png's alpha channel used as mask
cv::merge(rgb, 3, T); // put together the RGB channels, now transp insn't transparent
cv::resize(T, m_mLogoPic, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
cv::resize(mask, m_mLogoMask, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), cv::INTER_CUBIC);
cv::Rect roi(cv::Point(m_sEmbedVideoData.logo_x, m_sEmbedVideoData.logo_y), cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h));
cv::Mat destinationROI = tMat(roi);
m_mLogoPic.copyTo(destinationROI, m_mLogoMask);
// m_sEmbedVideoData.show_logo = false;
cv::split(T, layers);
mask = layers[3]; // png's alpha channel used as mask
layers.pop_back();
cv::merge(layers, rgb);
} }
else else
{ {
#ifdef DEBUG_EXTRA
LOG("Logo Disabled - No Picture is Loaded");
#endif
m_sEmbedVideoData.show_logo = false;
};
// No alpha channel: the whole picture is drawn
if (T.channels() == 1)
{
cv::cvtColor(T, rgb, cv::COLOR_GRAY2BGR);
}
else
{
rgb = T;
}
mask = cv::Mat(T.rows, T.cols, CV_8UC1, cv::Scalar(255));
}
if (rgb.depth() != CV_8U)
{
rgb.convertTo(rgb, CV_8U, 1.0 / 257.0);
mask.convertTo(mask, CV_8U, 1.0 / 257.0);
}
cv::resize(rgb, m_mLogoPic, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), 0, 0, cv::INTER_CUBIC);
cv::resize(mask, m_mLogoMask, cv::Size(m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h), 0, 0, cv::INTER_CUBIC);
m_nLogoX = a_x;
m_nLogoY = a_y;
m_bLogoActive = true;
cv::Mat destinationROI = tMat(cv::Rect(m_nLogoX, m_nLogoY, m_sEmbedVideoData.logo_w, m_sEmbedVideoData.logo_h));
m_mLogoPic.copyTo(destinationROI, m_mLogoMask);
} }
else else
{ {
#ifdef DEBUG_EXTRA
LOG("Logo Disabled - Wrong Coords");
#endif
m_sEmbedVideoData.show_logo = false;
LOG_ERROR("Logo Disabled - No Picture is Loaded");
}; };
#ifdef DEBUG_EXTRA
LOG("Logo Draw OK");
#endif
}; };
try try
@ -1573,6 +1557,8 @@ bool kernelModuleEmbedVideo::stopService(bool force)
#ifndef EVE_WORK_WITH_MEDIAMTX #ifndef EVE_WORK_WITH_MEDIAMTX
// Stop Conveyor // Stop Conveyor
m_bDetached = uninit(); m_bDetached = uninit();
#else
m_bDetached = true; // no picture server to detach: stopping always succeeds (reloadSettings() depends on it)
#endif #endif
m_mStreamHandlersProtector.lock(); m_mStreamHandlersProtector.lock();
if (m_mapHANDLERS.size()) if (m_mapHANDLERS.size())
@ -1715,6 +1701,22 @@ bool kernelModuleEmbedVideo::reloadSettings()
bool kernelModuleEmbedVideo::validate() bool kernelModuleEmbedVideo::validate()
{ {
bool RetVal = true; bool RetVal = true;
// Settings changed in the DB (Mojo_Web module settings): restart the service with them.
// m_sEmbedVideoData must stay equal to the DB row, runtime state lives in other members.
if (m_pDBC)
{
DBC_MODULE_EMBED_VIDEO_DATA * tDATA = m_pDBC->getEmbedVideoData(m_nDBAUXID);
if (tDATA)
{
bool tChanged = (memcmp(&m_sEmbedVideoData, tDATA, sizeof(DBC_MODULE_EMBED_VIDEO_DATA)) != 0);
free(tDATA);
if (tChanged)
{
LOG("Module Settings Changed, reloading");
return reloadSettings();
};
};
};
// The WebRTC server could not start (e.g. the port was still used by the MediaMTX service): retry // The WebRTC server could not start (e.g. the port was still used by the MediaMTX service): retry
if (m_bEnabled && m_sEmbedVideoData.live_mode != EVE_LIVE_OFF && ! m_pWebrtc && time(NULL) >= m_tLiveRetry) if (m_bEnabled && m_sEmbedVideoData.live_mode != EVE_LIVE_OFF && ! m_pWebrtc && time(NULL) >= m_tLiveRetry)
{ {

5
lib/App/common/kernelModuleEmbedVideo.h

@ -126,6 +126,11 @@ class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider
#endif #endif
cv::Mat m_mLogoPic; cv::Mat m_mLogoPic;
cv::Mat m_mLogoMask; cv::Mat m_mLogoMask;
// Logo state computed in startService(); m_sEmbedVideoData is kept equal to the DB row
// (validate() compares it with the DB and reloads the module on any difference)
bool m_bLogoActive;
int m_nLogoX;
int m_nLogoY;
char m_pBaseSavePath[PATH_MAX]; char m_pBaseSavePath[PATH_MAX];
bool saveStreamPicture(uint32_t id); bool saveStreamPicture(uint32_t id);
bool saveStreamPicture(DBC_SNAPSHOT_DATA * ptr); bool saveStreamPicture(DBC_SNAPSHOT_DATA * ptr);

82
lib/App/common/streamHandler.cpp

@ -426,6 +426,20 @@ void streamHandler::encodedSourceStopped(void * src)
m_oLiveFeeder.reset(); m_oLiveFeeder.reset();
} }
// OpenCV drawing parameters from the DB: an unsupported value makes cv::putText throw
static inline int eveLineType(int v)
{
switch (v)
{
case 0: case 1: case 4: case 8: case 16: return v; // 0 and 1 are accepted by OpenCV (8 / 4 connected)
default: return cv::LINE_8;
}
}
static inline int eveFontFace(int v)
{
return ((v & 15) <= cv::FONT_HERSHEY_SCRIPT_COMPLEX) ? v : cv::FONT_HERSHEY_SIMPLEX;
}
bool streamHandler::pushFrame(AVFrame * Frame, void * src) bool streamHandler::pushFrame(AVFrame * Frame, void * src)
{ {
if( !Frame ) return false; if( !Frame ) return false;
@ -620,7 +634,7 @@ bool streamHandler::pushFrame(cv::Mat & Frame, void * src)
// sprintf( tBuffer, ">>%d>%d>%s>%s>", tMD->mediaSourceId, tMD->mediaStreamId, tMD->mediaName, tsb ); // sprintf( tBuffer, ">>%d>%d>%s>%s>", tMD->mediaSourceId, tMD->mediaStreamId, tMD->mediaName, tsb );
sprintf(tBuffer, ">>>%s>%s>", tMD->mediaName, tsb); sprintf(tBuffer, ">>>%s>%s>", tMD->mediaName, tsb);
int baseline = 0; int baseline = 0;
cv::Size textSize = cv::getTextSize(tBuffer, m_pMaster->m_sEmbedVideoData.label_font_face, m_pMaster->m_sEmbedVideoData.label_font_scale, m_pMaster->m_sEmbedVideoData.label_font_thickness, &baseline);
cv::Size textSize = cv::getTextSize(tBuffer, eveFontFace(m_pMaster->m_sEmbedVideoData.label_font_face), m_pMaster->m_sEmbedVideoData.label_font_scale, m_pMaster->m_sEmbedVideoData.label_font_thickness, &baseline);
int x = m_pMaster->m_sEmbedVideoData.label_x; int x = m_pMaster->m_sEmbedVideoData.label_x;
int y = m_pMaster->m_sEmbedVideoData.label_y; int y = m_pMaster->m_sEmbedVideoData.label_y;
if (x < 0) if (x < 0)
@ -635,8 +649,8 @@ bool streamHandler::pushFrame(cv::Mat & Frame, void * src)
{ {
cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Rect(x, y - textSize.height - 4, textSize.width, textSize.height + baseline + 4), cv::Scalar(0, 0, 0), cv::FILLED); cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Rect(x, y - textSize.height - 4, textSize.width, textSize.height + baseline + 4), cv::Scalar(0, 0, 0), cv::FILLED);
}; };
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(x, y), (cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.label_font_face, m_pMaster->m_sEmbedVideoData.label_font_scale, cv::Scalar(0, 255, 0),
m_pMaster->m_sEmbedVideoData.label_font_thickness, m_pMaster->m_sEmbedVideoData.label_line_type);
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(x, y), (cv::HersheyFonts) eveFontFace(m_pMaster->m_sEmbedVideoData.label_font_face), m_pMaster->m_sEmbedVideoData.label_font_scale, cv::Scalar(0, 255, 0),
m_pMaster->m_sEmbedVideoData.label_font_thickness, eveLineType(m_pMaster->m_sEmbedVideoData.label_line_type));
}; };
}; };
} }
@ -651,55 +665,43 @@ bool streamHandler::pushFrame(cv::Mat & Frame, void * src)
{ {
if (strftime(tBuffer, sizeof(tBuffer), "%F %T GMT", tmp) != 0) if (strftime(tBuffer, sizeof(tBuffer), "%F %T GMT", tmp) != 0)
{ {
uint8_t shift_x = 0;
uint8_t shift_y = 15;
if (! m_pMaster->m_sEmbedVideoData.ts_transperant)
const DBC_MODULE_EMBED_VIDEO_DATA & E = m_pMaster->m_sEmbedVideoData;
cv::Mat & tFrame = m_pMaster->m_mapMATS[tMD->mediaStreamId];
const int face = eveFontFace(E.ts_font_face);
const int line = eveLineType(E.ts_line_type);
int baseline = 0;
cv::Size textSize = cv::getTextSize(tBuffer, face, E.ts_font_scale, E.ts_font_thickness, &baseline);
// ts_x / ts_y: top left corner of the text box; negative values are measured from the right / bottom edge
int x = (E.ts_x < 0) ? (int) E.width + E.ts_x - textSize.width : E.ts_x;
int y = (E.ts_y < 0) ? (int) E.height + E.ts_y - textSize.height - baseline : E.ts_y;
cv::Point org(x, y + textSize.height);
if (! E.ts_transperant)
{ {
if (m_pMaster->m_sEmbedVideoData.ts_font_face)
{
shift_y = 15;
cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Point(m_pMaster->m_sEmbedVideoData.ts_x, m_pMaster->m_sEmbedVideoData.ts_y), cv::Point(490, 60), cv::Scalar(0, 0, 0), cv::FILLED);
}
else
{
shift_y = 23;
cv::rectangle(m_pMaster->m_mapMATS[tMD->mediaStreamId], cv::Point(m_pMaster->m_sEmbedVideoData.ts_x, m_pMaster->m_sEmbedVideoData.ts_y), cv::Point(490, 60), cv::Scalar(0, 0, 0), cv::FILLED);
};
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x, m_pMaster->m_sEmbedVideoData.ts_y + shift_y),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 255, 255), m_pMaster->m_sEmbedVideoData.ts_font_thickness,
m_pMaster->m_sEmbedVideoData.ts_line_type);
cv::rectangle(tFrame, cv::Rect(x - 2, y - 2, textSize.width + 4, textSize.height + baseline + 4), cv::Scalar(0, 0, 0), cv::FILLED);
} }
else else
{ {
if (m_pMaster->m_sEmbedVideoData.ts_font_face)
// dark outline keeps the text readable on any background
static const cv::Point outline[] = {cv::Point(-1, -1), cv::Point(1, -1), cv::Point(-1, 1), cv::Point(1, 1)};
for (const cv::Point & d : outline)
{ {
shift_y = 15;
cv::putText(tFrame, tBuffer, org + d, face, E.ts_font_scale, cv::Scalar(0, 0, 0), E.ts_font_thickness, line);
} }
else
{
shift_y = 23;
};
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y - 1),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x + 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y - 1),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y + 1),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x - 1, m_pMaster->m_sEmbedVideoData.ts_y + shift_y + 2),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 0, 0), m_pMaster->m_sEmbedVideoData.ts_font_thickness, m_pMaster->m_sEmbedVideoData.ts_line_type);
cv::putText(m_pMaster->m_mapMATS[tMD->mediaStreamId], tBuffer, cv::Point(m_pMaster->m_sEmbedVideoData.ts_x + shift_x, m_pMaster->m_sEmbedVideoData.ts_y + shift_y),
(cv::HersheyFonts) m_pMaster->m_sEmbedVideoData.ts_font_face, m_pMaster->m_sEmbedVideoData.ts_font_scale, cv::Scalar(0, 255, 255), m_pMaster->m_sEmbedVideoData.ts_font_thickness,
m_pMaster->m_sEmbedVideoData.ts_line_type);
} }
cv::putText(tFrame, tBuffer, org, face, E.ts_font_scale, cv::Scalar(0, 255, 255), E.ts_font_thickness, line);
}; };
}; };
}; };
}; };
if (m_pMaster->m_sEmbedVideoData.show_logo)
if (m_pMaster->m_bLogoActive)
{ {
cv::Rect roi(cv::Point(m_pMaster->m_sEmbedVideoData.logo_x, m_pMaster->m_sEmbedVideoData.logo_y), cv::Size(m_pMaster->m_sEmbedVideoData.logo_w, m_pMaster->m_sEmbedVideoData.logo_h));
cv::Mat destinationROI = m_pMaster->m_mapMATS[tMD->mediaStreamId](roi);
m_pMaster->m_mLogoPic.copyTo(destinationROI, m_pMaster->m_mLogoMask);
cv::Mat & tFrame = m_pMaster->m_mapMATS[tMD->mediaStreamId];
cv::Rect roi(m_pMaster->m_nLogoX, m_pMaster->m_nLogoY, m_pMaster->m_mLogoPic.cols, m_pMaster->m_mLogoPic.rows);
if ((roi & cv::Rect(0, 0, tFrame.cols, tFrame.rows)) == roi)
{
cv::Mat destinationROI = tFrame(roi);
m_pMaster->m_mLogoPic.copyTo(destinationROI, m_pMaster->m_mLogoMask);
};
}; };
// Live view of the processed frame (EVE_LIVE_ENCODE): copied for the encoder thread // Live view of the processed frame (EVE_LIVE_ENCODE): copied for the encoder thread
if (m_pLiveEncoder) if (m_pLiveEncoder)

Loading…
Cancel
Save