Skip to content

Commit

Permalink
Example reformatting of src/zm_stream.*
Browse files Browse the repository at this point in the history
  • Loading branch information
dougnazar committed Jul 31, 2022
1 parent 01a2d8a commit fc7d5be
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 172 deletions.
143 changes: 63 additions & 80 deletions src/zm_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

#include "zm_stream.h"

#include "zm_box.h"
#include "zm_monitor.h"
#include <cmath>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>

#include <cmath>

#include "zm_box.h"
#include "zm_monitor.h"

constexpr Seconds StreamBase::MAX_STREAM_DELAY;
constexpr Milliseconds StreamBase::MAX_SLEEP;

Expand All @@ -39,7 +41,7 @@ StreamBase::~StreamBase() {
bool StreamBase::loadMonitor(int p_monitor_id) {
monitor_id = p_monitor_id;

if ( !(monitor or (monitor = Monitor::Load(monitor_id, false, Monitor::QUERY))) ) {
if (!(monitor or (monitor = Monitor::Load(monitor_id, false, Monitor::QUERY)))) {
Error("Unable to load monitor id %d for streaming", monitor_id);
return false;
}
Expand Down Expand Up @@ -75,7 +77,7 @@ bool StreamBase::checkInitialised() {
Error("Monitor shm is not connected");
return false;
}
if ((monitor->GetType() == Monitor::FFMPEG) and (monitor->Decoding() == Monitor::DECODING_NONE) ) {
if ((monitor->GetType() == Monitor::FFMPEG) and (monitor->Decoding() == Monitor::DECODING_NONE)) {
Debug(1, "Monitor is not decoding.");
return false;
}
Expand All @@ -84,51 +86,50 @@ bool StreamBase::checkInitialised() {

void StreamBase::updateFrameRate(double fps) {
frame_mod = 1;
if ( (fps < 0) || !fps || std::isinf(fps) ) {
if ((fps < 0) || !fps || std::isinf(fps)) {
Debug(1, "Zero or negative fps %f in updateFrameRate. Setting frame_mod=1 and effective_fps=0.0", fps);
effective_fps = 0.0;
base_fps = 0.0;
return;
}

base_fps = fps;
effective_fps = (base_fps*abs(replay_rate))/ZM_RATE_BASE;
effective_fps = (base_fps * abs(replay_rate)) / ZM_RATE_BASE;
frame_mod = 1;
Debug(3, "FPS:%.2f, MaxFPS:%.2f, BaseFPS:%.2f, EffectiveFPS:%.2f, FrameMod:%d, replay_rate(%d)",
fps, maxfps, base_fps, effective_fps, frame_mod, replay_rate);
Debug(3, "FPS:%.2f, MaxFPS:%.2f, BaseFPS:%.2f, EffectiveFPS:%.2f, FrameMod:%d, replay_rate(%d)", fps, maxfps, base_fps, effective_fps, frame_mod,
replay_rate);
if (maxfps > 0.0) {
// Min frame repeat?
// We want to keep the frame skip easy... problem is ... if effective = 31 and max = 30 then we end up with 15.5 fps.
while ( (int)effective_fps > (int)maxfps ) {
// We want to keep the frame skip easy... problem is ... if effective = 31 and max = 30 then we end up with 15.5 fps.
while ((int)effective_fps > (int)maxfps) {
effective_fps /= 2.0;
frame_mod *= 2;
Debug(3, "Changing fps to be < max %.2f EffectiveFPS:%.2f, FrameMod:%d",
maxfps, effective_fps, frame_mod);
Debug(3, "Changing fps to be < max %.2f EffectiveFPS:%.2f, FrameMod:%d", maxfps, effective_fps, frame_mod);
}
}
} // void StreamBase::updateFrameRate(double fps)
} // void StreamBase::updateFrameRate(double fps)

bool StreamBase::checkCommandQueue() {
if ( sd >= 0 ) {
if (sd >= 0) {
CmdMsg msg;
memset(&msg, 0, sizeof(msg));
int nbytes = recvfrom(sd, &msg, sizeof(msg), MSG_DONTWAIT, 0, 0);
if ( nbytes < 0 ) {
if ( errno != EAGAIN ) {
if (nbytes < 0) {
if (errno != EAGAIN) {
Error("recvfrom(), errno = %d, error = %s", errno, strerror(errno));
return false;
}
}
//else if ( (nbytes != sizeof(msg)) )
// else if ( (nbytes != sizeof(msg)) )
//{
//Error( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes );
// Error( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes );
//}
else {
Debug(2, "Message length is (%d)", nbytes);
processCommand(&msg);
return true;
}
} else if ( connkey ) {
} else if (connkey) {
Warning("No sd in checkCommandQueue, comms not open for connkey %06d?", connkey);
} else {
// Perfectly valid if only getting a snapshot
Expand All @@ -144,11 +145,9 @@ Image *StreamBase::prepareImage(Image *image) {
bool image_copied = false;

if (zoom != 100) {
int base_image_width = image->Width(),
base_image_height = image->Height(),
disp_image_width = image->Width() * scale/ZM_SCALE_BASE,
int base_image_width = image->Width(), base_image_height = image->Height(), disp_image_width = image->Width() * scale / ZM_SCALE_BASE,
disp_image_height = image->Height() * scale / ZM_SCALE_BASE;
/* x and y are scaled by web UI to base dimensions units.
/* x and y are scaled by web UI to base dimensions units.
* When zooming, we blow up the image by the amount 150 for first zoom, right? 150%, then cut out a base sized chunk
* However if we have zoomed before, then we are zooming into the previous cutout
* The box stored in last_crop should be in base_image units, So we need to turn x,y into percentages, then apply to last_crop
Expand All @@ -164,16 +163,14 @@ Image *StreamBase::prepareImage(Image *image) {
int crop_y = last_crop.Lo().y_ + (y_percent * last_crop.Height() / ZM_SCALE_BASE);
Debug(2, "crop click %dx%d => %dx%d out of %dx%d", x, y, crop_x, crop_y, last_crop.Width(), last_crop.Height());

int zoom_image_width = base_image_width * zoom / ZM_SCALE_BASE,
zoom_image_height = base_image_height * zoom / ZM_SCALE_BASE,
click_x = crop_x * zoom / ZM_SCALE_BASE,
click_y = crop_y * zoom / ZM_SCALE_BASE;
int zoom_image_width = base_image_width * zoom / ZM_SCALE_BASE, zoom_image_height = base_image_height * zoom / ZM_SCALE_BASE,
click_x = crop_x * zoom / ZM_SCALE_BASE, click_y = crop_y * zoom / ZM_SCALE_BASE;
Debug(2, "adjusted click %dx%d * %d zoom => %dx%d out of %dx%d", x, y, zoom, click_x, click_y, zoom_image_width, zoom_image_height);

// These can go out of image. Resulting size will be less than base image. That's ok.
// We don't want to center it, we want to keep the relative offset from center where the click is.
int left_dist = base_image_width * x_percent/ZM_SCALE_BASE;
int top_dist = base_image_height * y_percent/ZM_SCALE_BASE;
int left_dist = base_image_width * x_percent / ZM_SCALE_BASE;
int top_dist = base_image_height * y_percent / ZM_SCALE_BASE;
Debug(2, "Dest at %d,%d", left_dist, top_dist);

int lo_x = click_x - left_dist;
Expand All @@ -191,33 +188,33 @@ Image *StreamBase::prepareImage(Image *image) {
amount_to_shrink_y = (hi_x - zoom_image_width) * base_image_height / base_image_width;
hi_x = zoom_image_width - 1;
}
Debug(1, "Shrinking y by %d from %d,%d to %d,%d", amount_to_shrink_y, lo_y, hi_y, lo_y+(amount_to_shrink_y/2), hi_y-(amount_to_shrink_y/2));
Debug(1, "Shrinking y by %d from %d,%d to %d,%d", amount_to_shrink_y, lo_y, hi_y, lo_y + (amount_to_shrink_y / 2), hi_y - (amount_to_shrink_y / 2));
if (amount_to_shrink_y) {
lo_y += amount_to_shrink_y/2;
hi_y -= amount_to_shrink_y/2;
lo_y += amount_to_shrink_y / 2;
hi_y -= amount_to_shrink_y / 2;
}

int amount_to_shrink_x = 0;
if (lo_y < 0) {
amount_to_shrink_x = (-1 * lo_y) * base_image_width / base_image_height;
Debug(1, "%d to %d = %d", lo_y, -1*lo_y, amount_to_shrink_x);
Debug(1, "%d to %d = %d", lo_y, -1 * lo_y, amount_to_shrink_x);
lo_y = 0;
} else if (hi_y >= zoom_image_height) {
amount_to_shrink_x = (hi_y - zoom_image_height) * base_image_width / base_image_height;
hi_y = zoom_image_height - 1;
}
Debug(1, "Shrinking x by %d from %d,%d to %d,%d", amount_to_shrink_x, lo_x, hi_x, lo_x+(amount_to_shrink_x/2), hi_x-(amount_to_shrink_x/2));
Debug(1, "Shrinking x by %d from %d,%d to %d,%d", amount_to_shrink_x, lo_x, hi_x, lo_x + (amount_to_shrink_x / 2), hi_x - (amount_to_shrink_x / 2));
if (amount_to_shrink_x) {
lo_x += amount_to_shrink_x/2;
hi_x -= amount_to_shrink_x/2;
lo_x += amount_to_shrink_x / 2;
hi_x -= amount_to_shrink_x / 2;
}

Debug(3, "Cropping to %d,%d -> %d,%d %dx%din blown up image", lo_x, lo_y, hi_x, hi_y, hi_x-lo_x, hi_y-lo_y);
Debug(3, "Cropping to %d,%d -> %d,%d %dx%din blown up image", lo_x, lo_y, hi_x, hi_y, hi_x - lo_x, hi_y - lo_y);
// Scaled back to base_image dimensions
last_crop = Box({lo_x*ZM_SCALE_BASE/zoom, lo_y*ZM_SCALE_BASE/zoom}, {hi_x*ZM_SCALE_BASE/zoom, hi_y*ZM_SCALE_BASE/zoom});
last_crop = Box({lo_x * ZM_SCALE_BASE / zoom, lo_y * ZM_SCALE_BASE / zoom}, {hi_x * ZM_SCALE_BASE / zoom, hi_y * ZM_SCALE_BASE / zoom});

Debug(3, "Cropping to %d,%d -> %d,%d", last_crop.Lo().x_, last_crop.Lo().y_, last_crop.Hi().x_, last_crop.Hi().y_);
if ( !image_copied ) {
if (!image_copied) {
static Image copy_image;
copy_image.Assign(*image);
image = &copy_image;
Expand Down Expand Up @@ -257,8 +254,7 @@ bool StreamBase::sendTextFrame(const char *frame_text) {
subpixelorder = monitor->SubpixelOrder();
labelsize = monitor->LabelSize();
}
Debug(2, "Sending %dx%dx%dx%d * %d scale text frame '%s'",
width, height, colours, subpixelorder, scale, frame_text);
Debug(2, "Sending %dx%dx%dx%d * %d scale text frame '%s'", width, height, colours, subpixelorder, scale, frame_text);

Image image(width, height, colours, subpixelorder);
image.Clear();
Expand Down Expand Up @@ -303,44 +299,37 @@ bool StreamBase::sendTextFrame(const char *frame_text) {
}

void StreamBase::openComms() {
if ( connkey > 0 ) {

if (connkey > 0) {
// Have to mkdir because systemd is now chrooting and the dir may not exist
if ( mkdir(staticConfig.PATH_SOCKS.c_str(), 0755) ) {
if ( errno != EEXIST ) {
if (mkdir(staticConfig.PATH_SOCKS.c_str(), 0755)) {
if (errno != EEXIST) {
Error("Can't mkdir ZM_PATH_SOCKS %s: %s.", staticConfig.PATH_SOCKS.c_str(), strerror(errno));
}
}

unsigned int length = snprintf(
sock_path_lock,
sizeof(sock_path_lock),
"%s/zms-%06d.lock",
staticConfig.PATH_SOCKS.c_str(),
connkey
);
if ( length >= sizeof(sock_path_lock) ) {
unsigned int length = snprintf(sock_path_lock, sizeof(sock_path_lock), "%s/zms-%06d.lock", staticConfig.PATH_SOCKS.c_str(), connkey);
if (length >= sizeof(sock_path_lock)) {
Warning("Socket lock path was truncated.");
}
Debug(1, "Trying to open the lock on %s", sock_path_lock);

// Under systemd, we get chrooted to something like /tmp/systemd-apache-blh/ so the dir may not exist.
if ( mkdir(staticConfig.PATH_SOCKS.c_str(), 0755) ) {
if ( errno != EEXIST ) {
if (mkdir(staticConfig.PATH_SOCKS.c_str(), 0755)) {
if (errno != EEXIST) {
Error("Can't mkdir %s: %s", staticConfig.PATH_SOCKS.c_str(), strerror(errno));
return;
} else {
Debug(3, "SOCKS dir %s already exists", staticConfig.PATH_SOCKS.c_str() );
Debug(3, "SOCKS dir %s already exists", staticConfig.PATH_SOCKS.c_str());
}
} else {
Debug(3, "Success making SOCKS dir %s", staticConfig.PATH_SOCKS.c_str() );
Debug(3, "Success making SOCKS dir %s", staticConfig.PATH_SOCKS.c_str());
}

lock_fd = open(sock_path_lock, O_CREAT|O_WRONLY, S_IRUSR | S_IWUSR);
if ( lock_fd <= 0 ) {
lock_fd = open(sock_path_lock, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (lock_fd <= 0) {
Error("Unable to open sock lock file %s: %s", sock_path_lock, strerror(errno));
lock_fd = 0;
} else if ( flock(lock_fd, LOCK_EX) != 0 ) {
} else if (flock(lock_fd, LOCK_EX) != 0) {
Error("Unable to lock sock lock file %s: %s", sock_path_lock, strerror(errno));
close(lock_fd);
lock_fd = 0;
Expand All @@ -349,33 +338,27 @@ void StreamBase::openComms() {
}

sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if ( sd < 0 ) {
if (sd < 0) {
Fatal("Can't create socket: %s", strerror(errno));
} else {
Debug(3, "Have socket %d", sd);
}

length = snprintf(
loc_sock_path,
sizeof(loc_sock_path),
"%s/zms-%06ds.sock",
staticConfig.PATH_SOCKS.c_str(),
connkey
);
if ( length >= sizeof(loc_sock_path) ) {
length = snprintf(loc_sock_path, sizeof(loc_sock_path), "%s/zms-%06ds.sock", staticConfig.PATH_SOCKS.c_str(), connkey);
if (length >= sizeof(loc_sock_path)) {
Warning("Socket path was truncated.");
length = sizeof(loc_sock_path)-1;
length = sizeof(loc_sock_path) - 1;
}
// Unlink before bind, in case it already exists
unlink(loc_sock_path);
if ( sizeof(loc_addr.sun_path) < length ) {
if (sizeof(loc_addr.sun_path) < length) {
Error("Not enough space %zu in loc_addr.sun_path for socket file %s", sizeof(loc_addr.sun_path), loc_sock_path);
}

strncpy(loc_addr.sun_path, loc_sock_path, sizeof(loc_addr.sun_path));
loc_addr.sun_family = AF_UNIX;
Debug(3, "Binding to %s", loc_sock_path);
if ( ::bind(sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path)+sizeof(loc_addr.sun_family)+1) < 0 ) {
if (::bind(sd, (struct sockaddr *)&loc_addr, strlen(loc_addr.sun_path) + sizeof(loc_addr.sun_family) + 1) < 0) {
Fatal("Can't bind: %s", strerror(errno));
}

Expand All @@ -385,18 +368,18 @@ void StreamBase::openComms() {

last_comm_update = std::chrono::steady_clock::now();
Debug(3, "comms open at %s", loc_sock_path);
} // end if connKey > 0
} // end void StreamBase::openComms()
} // end if connKey > 0
} // end void StreamBase::openComms()

void StreamBase::closeComms() {
if ( connkey > 0 ) {
if ( sd >= 0 ) {
if (connkey > 0) {
if (sd >= 0) {
close(sd);
sd = -1;
}
// Can't delete any files because another zms might have come along and opened them and is waiting on the lock.
if ( lock_fd > 0 ) {
close(lock_fd); //close it rather than unlock it incase it got deleted.
if (lock_fd > 0) {
close(lock_fd); // close it rather than unlock it incase it got deleted.
}
}
} // end void StreamBase::closeComms
} // end void StreamBase::closeComms
Loading

0 comments on commit fc7d5be

Please sign in to comment.