1919
2020#include " zm_stream.h"
2121
22- #include " zm_box.h"
23- #include " zm_monitor.h"
24- #include < cmath>
2522#include < sys/file.h>
2623#include < sys/socket.h>
2724#include < sys/stat.h>
2825#include < unistd.h>
2926
27+ #include < cmath>
28+
29+ #include " zm_box.h"
30+ #include " zm_monitor.h"
31+
3032constexpr Seconds StreamBase::MAX_STREAM_DELAY;
3133constexpr Milliseconds StreamBase::MAX_SLEEP;
3234
@@ -39,7 +41,7 @@ StreamBase::~StreamBase() {
3941bool StreamBase::loadMonitor (int p_monitor_id) {
4042 monitor_id = p_monitor_id;
4143
42- if ( !(monitor or (monitor = Monitor::Load (monitor_id, false , Monitor::QUERY))) ) {
44+ if (!(monitor or (monitor = Monitor::Load (monitor_id, false , Monitor::QUERY)))) {
4345 Error (" Unable to load monitor id %d for streaming" , monitor_id);
4446 return false ;
4547 }
@@ -75,7 +77,7 @@ bool StreamBase::checkInitialised() {
7577 Error (" Monitor shm is not connected" );
7678 return false ;
7779 }
78- if ((monitor->GetType () == Monitor::FFMPEG) and (monitor->Decoding () == Monitor::DECODING_NONE) ) {
80+ if ((monitor->GetType () == Monitor::FFMPEG) and (monitor->Decoding () == Monitor::DECODING_NONE)) {
7981 Debug (1 , " Monitor is not decoding." );
8082 return false ;
8183 }
@@ -84,51 +86,50 @@ bool StreamBase::checkInitialised() {
8486
8587void StreamBase::updateFrameRate (double fps) {
8688 frame_mod = 1 ;
87- if ( (fps < 0 ) || !fps || std::isinf (fps) ) {
89+ if ((fps < 0 ) || !fps || std::isinf (fps)) {
8890 Debug (1 , " Zero or negative fps %f in updateFrameRate. Setting frame_mod=1 and effective_fps=0.0" , fps);
8991 effective_fps = 0.0 ;
9092 base_fps = 0.0 ;
9193 return ;
9294 }
9395
9496 base_fps = fps;
95- effective_fps = (base_fps* abs (replay_rate))/ ZM_RATE_BASE;
97+ effective_fps = (base_fps * abs (replay_rate)) / ZM_RATE_BASE;
9698 frame_mod = 1 ;
97- Debug (3 , " FPS:%.2f, MaxFPS:%.2f, BaseFPS:%.2f, EffectiveFPS:%.2f, FrameMod:%d, replay_rate(%d)" ,
98- fps, maxfps, base_fps, effective_fps, frame_mod, replay_rate);
99+ Debug (3 , " FPS:%.2f, MaxFPS:%.2f, BaseFPS:%.2f, EffectiveFPS:%.2f, FrameMod:%d, replay_rate(%d)" , fps, maxfps, base_fps, effective_fps, frame_mod,
100+ replay_rate);
99101 if (maxfps > 0.0 ) {
100102 // Min frame repeat?
101- // We want to keep the frame skip easy... problem is ... if effective = 31 and max = 30 then we end up with 15.5 fps.
102- while ( (int )effective_fps > (int )maxfps ) {
103+ // We want to keep the frame skip easy... problem is ... if effective = 31 and max = 30 then we end up with 15.5 fps.
104+ while ((int )effective_fps > (int )maxfps) {
103105 effective_fps /= 2.0 ;
104106 frame_mod *= 2 ;
105- Debug (3 , " Changing fps to be < max %.2f EffectiveFPS:%.2f, FrameMod:%d" ,
106- maxfps, effective_fps, frame_mod);
107+ Debug (3 , " Changing fps to be < max %.2f EffectiveFPS:%.2f, FrameMod:%d" , maxfps, effective_fps, frame_mod);
107108 }
108109 }
109- } // void StreamBase::updateFrameRate(double fps)
110+ } // void StreamBase::updateFrameRate(double fps)
110111
111112bool StreamBase::checkCommandQueue () {
112- if ( sd >= 0 ) {
113+ if (sd >= 0 ) {
113114 CmdMsg msg;
114115 memset (&msg, 0 , sizeof (msg));
115116 int nbytes = recvfrom (sd, &msg, sizeof (msg), MSG_DONTWAIT, 0 , 0 );
116- if ( nbytes < 0 ) {
117- if ( errno != EAGAIN ) {
117+ if (nbytes < 0 ) {
118+ if (errno != EAGAIN) {
118119 Error (" recvfrom(), errno = %d, error = %s" , errno, strerror (errno));
119120 return false ;
120121 }
121122 }
122- // else if ( (nbytes != sizeof(msg)) )
123+ // else if ( (nbytes != sizeof(msg)) )
123124 // {
124- // Error( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes );
125+ // Error( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes );
125126 // }
126127 else {
127128 Debug (2 , " Message length is (%d)" , nbytes);
128129 processCommand (&msg);
129130 return true ;
130131 }
131- } else if ( connkey ) {
132+ } else if (connkey) {
132133 Warning (" No sd in checkCommandQueue, comms not open for connkey %06d?" , connkey);
133134 } else {
134135 // Perfectly valid if only getting a snapshot
@@ -144,11 +145,9 @@ Image *StreamBase::prepareImage(Image *image) {
144145 bool image_copied = false ;
145146
146147 if (zoom != 100 ) {
147- int base_image_width = image->Width (),
148- base_image_height = image->Height (),
149- disp_image_width = image->Width () * scale/ZM_SCALE_BASE,
148+ int base_image_width = image->Width (), base_image_height = image->Height (), disp_image_width = image->Width () * scale / ZM_SCALE_BASE,
150149 disp_image_height = image->Height () * scale / ZM_SCALE_BASE;
151- /* x and y are scaled by web UI to base dimensions units.
150+ /* x and y are scaled by web UI to base dimensions units.
152151 * When zooming, we blow up the image by the amount 150 for first zoom, right? 150%, then cut out a base sized chunk
153152 * However if we have zoomed before, then we are zooming into the previous cutout
154153 * 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
@@ -164,16 +163,14 @@ Image *StreamBase::prepareImage(Image *image) {
164163 int crop_y = last_crop.Lo ().y_ + (y_percent * last_crop.Height () / ZM_SCALE_BASE);
165164 Debug (2 , " crop click %dx%d => %dx%d out of %dx%d" , x, y, crop_x, crop_y, last_crop.Width (), last_crop.Height ());
166165
167- int zoom_image_width = base_image_width * zoom / ZM_SCALE_BASE,
168- zoom_image_height = base_image_height * zoom / ZM_SCALE_BASE,
169- click_x = crop_x * zoom / ZM_SCALE_BASE,
170- click_y = crop_y * zoom / ZM_SCALE_BASE;
166+ int zoom_image_width = base_image_width * zoom / ZM_SCALE_BASE, zoom_image_height = base_image_height * zoom / ZM_SCALE_BASE,
167+ click_x = crop_x * zoom / ZM_SCALE_BASE, click_y = crop_y * zoom / ZM_SCALE_BASE;
171168 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);
172169
173170 // These can go out of image. Resulting size will be less than base image. That's ok.
174171 // We don't want to center it, we want to keep the relative offset from center where the click is.
175- int left_dist = base_image_width * x_percent/ ZM_SCALE_BASE;
176- int top_dist = base_image_height * y_percent/ ZM_SCALE_BASE;
172+ int left_dist = base_image_width * x_percent / ZM_SCALE_BASE;
173+ int top_dist = base_image_height * y_percent / ZM_SCALE_BASE;
177174 Debug (2 , " Dest at %d,%d" , left_dist, top_dist);
178175
179176 int lo_x = click_x - left_dist;
@@ -191,33 +188,33 @@ Image *StreamBase::prepareImage(Image *image) {
191188 amount_to_shrink_y = (hi_x - zoom_image_width) * base_image_height / base_image_width;
192189 hi_x = zoom_image_width - 1 ;
193190 }
194- 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 ));
191+ 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 ));
195192 if (amount_to_shrink_y) {
196- lo_y += amount_to_shrink_y/ 2 ;
197- hi_y -= amount_to_shrink_y/ 2 ;
193+ lo_y += amount_to_shrink_y / 2 ;
194+ hi_y -= amount_to_shrink_y / 2 ;
198195 }
199196
200197 int amount_to_shrink_x = 0 ;
201198 if (lo_y < 0 ) {
202199 amount_to_shrink_x = (-1 * lo_y) * base_image_width / base_image_height;
203- Debug (1 , " %d to %d = %d" , lo_y, -1 * lo_y, amount_to_shrink_x);
200+ Debug (1 , " %d to %d = %d" , lo_y, -1 * lo_y, amount_to_shrink_x);
204201 lo_y = 0 ;
205202 } else if (hi_y >= zoom_image_height) {
206203 amount_to_shrink_x = (hi_y - zoom_image_height) * base_image_width / base_image_height;
207204 hi_y = zoom_image_height - 1 ;
208205 }
209- 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 ));
206+ 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 ));
210207 if (amount_to_shrink_x) {
211- lo_x += amount_to_shrink_x/ 2 ;
212- hi_x -= amount_to_shrink_x/ 2 ;
208+ lo_x += amount_to_shrink_x / 2 ;
209+ hi_x -= amount_to_shrink_x / 2 ;
213210 }
214211
215- 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);
212+ 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);
216213 // Scaled back to base_image dimensions
217- 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});
214+ 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});
218215
219216 Debug (3 , " Cropping to %d,%d -> %d,%d" , last_crop.Lo ().x_ , last_crop.Lo ().y_ , last_crop.Hi ().x_ , last_crop.Hi ().y_ );
220- if ( !image_copied ) {
217+ if (!image_copied) {
221218 static Image copy_image;
222219 copy_image.Assign (*image);
223220 image = ©_image;
@@ -257,8 +254,7 @@ bool StreamBase::sendTextFrame(const char *frame_text) {
257254 subpixelorder = monitor->SubpixelOrder ();
258255 labelsize = monitor->LabelSize ();
259256 }
260- Debug (2 , " Sending %dx%dx%dx%d * %d scale text frame '%s'" ,
261- width, height, colours, subpixelorder, scale, frame_text);
257+ Debug (2 , " Sending %dx%dx%dx%d * %d scale text frame '%s'" , width, height, colours, subpixelorder, scale, frame_text);
262258
263259 Image image (width, height, colours, subpixelorder);
264260 image.Clear ();
@@ -303,44 +299,37 @@ bool StreamBase::sendTextFrame(const char *frame_text) {
303299}
304300
305301void StreamBase::openComms () {
306- if ( connkey > 0 ) {
307-
302+ if (connkey > 0 ) {
308303 // Have to mkdir because systemd is now chrooting and the dir may not exist
309- if ( mkdir (staticConfig.PATH_SOCKS .c_str (), 0755 ) ) {
310- if ( errno != EEXIST ) {
304+ if (mkdir (staticConfig.PATH_SOCKS .c_str (), 0755 )) {
305+ if (errno != EEXIST) {
311306 Error (" Can't mkdir ZM_PATH_SOCKS %s: %s." , staticConfig.PATH_SOCKS .c_str (), strerror (errno));
312307 }
313308 }
314309
315- unsigned int length = snprintf (
316- sock_path_lock,
317- sizeof (sock_path_lock),
318- " %s/zms-%06d.lock" ,
319- staticConfig.PATH_SOCKS .c_str (),
320- connkey
321- );
322- if ( length >= sizeof (sock_path_lock) ) {
310+ unsigned int length = snprintf (sock_path_lock, sizeof (sock_path_lock), " %s/zms-%06d.lock" , staticConfig.PATH_SOCKS .c_str (), connkey);
311+ if (length >= sizeof (sock_path_lock)) {
323312 Warning (" Socket lock path was truncated." );
324313 }
325314 Debug (1 , " Trying to open the lock on %s" , sock_path_lock);
326315
327316 // Under systemd, we get chrooted to something like /tmp/systemd-apache-blh/ so the dir may not exist.
328- if ( mkdir (staticConfig.PATH_SOCKS .c_str (), 0755 ) ) {
329- if ( errno != EEXIST ) {
317+ if (mkdir (staticConfig.PATH_SOCKS .c_str (), 0755 )) {
318+ if (errno != EEXIST) {
330319 Error (" Can't mkdir %s: %s" , staticConfig.PATH_SOCKS .c_str (), strerror (errno));
331320 return ;
332321 } else {
333- Debug (3 , " SOCKS dir %s already exists" , staticConfig.PATH_SOCKS .c_str () );
322+ Debug (3 , " SOCKS dir %s already exists" , staticConfig.PATH_SOCKS .c_str ());
334323 }
335324 } else {
336- Debug (3 , " Success making SOCKS dir %s" , staticConfig.PATH_SOCKS .c_str () );
325+ Debug (3 , " Success making SOCKS dir %s" , staticConfig.PATH_SOCKS .c_str ());
337326 }
338327
339- lock_fd = open (sock_path_lock, O_CREAT| O_WRONLY, S_IRUSR | S_IWUSR);
340- if ( lock_fd <= 0 ) {
328+ lock_fd = open (sock_path_lock, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
329+ if (lock_fd <= 0 ) {
341330 Error (" Unable to open sock lock file %s: %s" , sock_path_lock, strerror (errno));
342331 lock_fd = 0 ;
343- } else if ( flock (lock_fd, LOCK_EX) != 0 ) {
332+ } else if (flock (lock_fd, LOCK_EX) != 0 ) {
344333 Error (" Unable to lock sock lock file %s: %s" , sock_path_lock, strerror (errno));
345334 close (lock_fd);
346335 lock_fd = 0 ;
@@ -349,33 +338,27 @@ void StreamBase::openComms() {
349338 }
350339
351340 sd = socket (AF_UNIX, SOCK_DGRAM, 0 );
352- if ( sd < 0 ) {
341+ if (sd < 0 ) {
353342 Fatal (" Can't create socket: %s" , strerror (errno));
354343 } else {
355344 Debug (3 , " Have socket %d" , sd);
356345 }
357346
358- length = snprintf (
359- loc_sock_path,
360- sizeof (loc_sock_path),
361- " %s/zms-%06ds.sock" ,
362- staticConfig.PATH_SOCKS .c_str (),
363- connkey
364- );
365- if ( length >= sizeof (loc_sock_path) ) {
347+ length = snprintf (loc_sock_path, sizeof (loc_sock_path), " %s/zms-%06ds.sock" , staticConfig.PATH_SOCKS .c_str (), connkey);
348+ if (length >= sizeof (loc_sock_path)) {
366349 Warning (" Socket path was truncated." );
367- length = sizeof (loc_sock_path)- 1 ;
350+ length = sizeof (loc_sock_path) - 1 ;
368351 }
369352 // Unlink before bind, in case it already exists
370353 unlink (loc_sock_path);
371- if ( sizeof (loc_addr.sun_path ) < length ) {
354+ if (sizeof (loc_addr.sun_path ) < length) {
372355 Error (" Not enough space %zu in loc_addr.sun_path for socket file %s" , sizeof (loc_addr.sun_path ), loc_sock_path);
373356 }
374357
375358 strncpy (loc_addr.sun_path , loc_sock_path, sizeof (loc_addr.sun_path ));
376359 loc_addr.sun_family = AF_UNIX;
377360 Debug (3 , " Binding to %s" , loc_sock_path);
378- if ( ::bind (sd, (struct sockaddr *)&loc_addr, strlen (loc_addr.sun_path )+ sizeof (loc_addr.sun_family )+ 1 ) < 0 ) {
361+ if (::bind (sd, (struct sockaddr *)&loc_addr, strlen (loc_addr.sun_path ) + sizeof (loc_addr.sun_family ) + 1 ) < 0 ) {
379362 Fatal (" Can't bind: %s" , strerror (errno));
380363 }
381364
@@ -385,18 +368,18 @@ void StreamBase::openComms() {
385368
386369 last_comm_update = std::chrono::steady_clock::now ();
387370 Debug (3 , " comms open at %s" , loc_sock_path);
388- } // end if connKey > 0
389- } // end void StreamBase::openComms()
371+ } // end if connKey > 0
372+ } // end void StreamBase::openComms()
390373
391374void StreamBase::closeComms () {
392- if ( connkey > 0 ) {
393- if ( sd >= 0 ) {
375+ if (connkey > 0 ) {
376+ if (sd >= 0 ) {
394377 close (sd);
395378 sd = -1 ;
396379 }
397380 // Can't delete any files because another zms might have come along and opened them and is waiting on the lock.
398- if ( lock_fd > 0 ) {
399- close (lock_fd); // close it rather than unlock it incase it got deleted.
381+ if (lock_fd > 0 ) {
382+ close (lock_fd); // close it rather than unlock it incase it got deleted.
400383 }
401384 }
402- } // end void StreamBase::closeComms
385+ } // end void StreamBase::closeComms
0 commit comments