[22/22] Move capture loop into concrete-agent.cpp

Submitted by Christophe de Dinechin on Feb. 28, 2018, 3:43 p.m.

Details

Message ID 20180228154325.25791-23-christophe@dinechin.org
State New
Headers show
Series "streaming-agent: C++ refactoring" ( rev: 2 1 ) in Spice

Not browsing as part of any series.

Commit Message

Christophe de Dinechin Feb. 28, 2018, 3:43 p.m.
From: Christophe de Dinechin <dinechin@redhat.com>

The capture loop has been isolated, and the required dependencies
have been put in separate files, so we can now easily move the
main event loop where it belongs.

Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
---
 src/concrete-agent.cpp        | 106 +++++++++++++++++++++++++++++++++++++++++-
 src/frame-log.hpp             |   2 +-
 src/spice-streaming-agent.cpp | 102 ----------------------------------------
 3 files changed, 105 insertions(+), 105 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/concrete-agent.cpp b/src/concrete-agent.cpp
index 4cf70e7..93b3653 100644
--- a/src/concrete-agent.cpp
+++ b/src/concrete-agent.cpp
@@ -4,15 +4,22 @@ 
  * Copyright 2017 Red Hat Inc. All rights reserved.
  */
 
+#include "concrete-agent.hpp"
+#include "message.hpp"
+#include "frame-log.hpp"
+
+#include <spice-streaming-agent/frame-capture.hpp>
+#include <spice-streaming-agent/plugin.hpp>
+#include <spice-streaming-agent/errors.hpp>
+
 #include <config.h>
 #include <algorithm>
 #include <syslog.h>
 #include <glob.h>
 #include <dlfcn.h>
+#include <inttypes.h>
 #include <string>
 
-#include "concrete-agent.hpp"
-
 using namespace spice::streaming_agent;
 
 static inline unsigned MajorVersion(unsigned version)
@@ -121,3 +128,98 @@  FrameCapture *ConcreteAgent::GetBestFrameCapture(const std::set<SpiceVideoCodecT
     }
     return nullptr;
 }
+
+class FormatMessage : public Message<StreamMsgFormat, FormatMessage, STREAM_TYPE_FORMAT>
+{
+public:
+    FormatMessage(unsigned w, unsigned h, uint8_t c) : Message(w, h, c) {}
+    static size_t size(unsigned width, unsigned height, uint8_t codec)
+    {
+        return sizeof(payload_t);
+    }
+    void write_message_body(Stream &stream, unsigned w, unsigned h, uint8_t c)
+    {
+        StreamMsgFormat msg = { .width = w, .height = h, .codec = c, .padding1 = {} };
+        stream.write_all("format", &msg, sizeof(msg));
+    }
+};
+
+class FrameMessage : public Message<StreamMsgData, FrameMessage, STREAM_TYPE_DATA>
+{
+public:
+    FrameMessage(const void *frame, size_t length) : Message(frame, length) {}
+    static size_t size(const void *frame, size_t length)
+    {
+        return sizeof(payload_t) + length;
+    }
+    void write_message_body(Stream &stream, const void *frame, size_t length)
+    {
+        stream.write_all("frame", frame, length);
+    }
+};
+
+
+void ConcreteAgent::CaptureLoop(Stream &stream, FrameLog &frame_log)
+{
+    unsigned int frame_count = 0;
+    while (!quit_requested) {
+        while (!quit_requested && !stream.streaming_requested()) {
+            if (stream.read_command(true) < 0) {
+                syslog(LOG_ERR, "FAILED to read command\n");
+                return;
+            }
+        }
+
+        if (quit_requested) {
+            return;
+        }
+
+        syslog(LOG_INFO, "streaming starts now\n");
+        uint64_t time_last = 0;
+
+        std::unique_ptr<FrameCapture> capture(GetBestFrameCapture(stream.client_codecs()));
+        if (!capture) {
+            throw Error("cannot find a suitable capture system");
+        }
+
+        while (!quit_requested && stream.streaming_requested()) {
+            if (++frame_count % 100 == 0) {
+                syslog(LOG_DEBUG, "SENT %d frames\n", frame_count);
+            }
+            uint64_t time_before = get_time();
+
+            FrameInfo frame = capture->CaptureFrame();
+
+            uint64_t time_after = get_time();
+            syslog(LOG_DEBUG,
+                   "got a frame -- size is %zu (%" PRIu64 " ms) "
+                   "(%" PRIu64 " ms from last frame)(%" PRIu64 " us)\n",
+                   frame.buffer_size, (time_after - time_before)/1000,
+                   (time_after - time_last)/1000,
+                   (time_before - time_last));
+            time_last = time_after;
+
+            if (frame.stream_start) {
+                unsigned width, height;
+                uint8_t codec;
+
+                width = frame.size.width;
+                height = frame.size.height;
+                codec = capture->VideoCodecType();
+
+                syslog(LOG_DEBUG, "wXh %uX%u  codec=%u\n", width, height, codec);
+
+                stream.send<FormatMessage>(width, height, codec);
+            }
+            if (frame_log) {
+                frame_log.dump(frame.buffer, frame.buffer_size);
+            }
+            stream.send<FrameMessage>(frame.buffer, frame.buffer_size);
+            //usleep(1);
+            if (stream.read_command(false) < 0) {
+                syslog(LOG_ERR, "FAILED to read command\n");
+                return;
+            }
+        }
+    }
+}
diff --git a/src/frame-log.hpp b/src/frame-log.hpp
index 09dbd4a..059819b 100644
--- a/src/frame-log.hpp
+++ b/src/frame-log.hpp
@@ -14,7 +14,7 @@  namespace spice {
 namespace streaming_agent {
 
 /* returns current time in micro-seconds */
-static uint64_t get_time(void)
+static inline uint64_t get_time(void)
 {
     struct timeval now;
 
diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 424db95..4ac4e04 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -40,42 +40,6 @@ 
 
 using namespace spice::streaming_agent;
 
-namespace spice
-{
-namespace streaming_agent
-{
-
-class FormatMessage : public Message<StreamMsgFormat, FormatMessage, STREAM_TYPE_FORMAT>
-{
-public:
-    FormatMessage(unsigned w, unsigned h, uint8_t c) : Message(w, h, c) {}
-    static size_t size(unsigned width, unsigned height, uint8_t codec)
-    {
-        return sizeof(payload_t);
-    }
-    void write_message_body(Stream &stream, unsigned w, unsigned h, uint8_t c)
-    {
-        StreamMsgFormat msg = { .width = w, .height = h, .codec = c, .padding1 = {} };
-        stream.write_all("format", &msg, sizeof(msg));
-    }
-};
-
-class FrameMessage : public Message<StreamMsgData, FrameMessage, STREAM_TYPE_DATA>
-{
-public:
-    FrameMessage(const void *frame, size_t length) : Message(frame, length) {}
-    static size_t size(const void *frame, size_t length)
-    {
-        return sizeof(payload_t) + length;
-    }
-    void write_message_body(Stream &stream, const void *frame, size_t length)
-    {
-        stream.write_all("frame", frame, length);
-    }
-};
-
-}} // namespace spice::streaming_agent
-
 bool quit_requested = false;
 
 static void handle_interrupt(int intr)
@@ -111,72 +75,6 @@  static void usage(const char *progname)
     exit(1);
 }
 
-
-void ConcreteAgent::CaptureLoop(Stream &stream, FrameLog &frame_log)
-{
-    unsigned int frame_count = 0;
-    while (!quit_requested) {
-        while (!quit_requested && !stream.streaming_requested()) {
-            if (stream.read_command(true) < 0) {
-                syslog(LOG_ERR, "FAILED to read command\n");
-                return;
-            }
-        }
-
-        if (quit_requested) {
-            return;
-        }
-
-        syslog(LOG_INFO, "streaming starts now\n");
-        uint64_t time_last = 0;
-
-        std::unique_ptr<FrameCapture> capture(GetBestFrameCapture(stream.client_codecs()));
-        if (!capture) {
-            throw Error("cannot find a suitable capture system");
-        }
-
-        while (!quit_requested && stream.streaming_requested()) {
-            if (++frame_count % 100 == 0) {
-                syslog(LOG_DEBUG, "SENT %d frames\n", frame_count);
-            }
-            uint64_t time_before = get_time();
-
-            FrameInfo frame = capture->CaptureFrame();
-
-            uint64_t time_after = get_time();
-            syslog(LOG_DEBUG,
-                   "got a frame -- size is %zu (%" PRIu64 " ms) "
-                   "(%" PRIu64 " ms from last frame)(%" PRIu64 " us)\n",
-                   frame.buffer_size, (time_after - time_before)/1000,
-                   (time_after - time_last)/1000,
-                   (time_before - time_last));
-            time_last = time_after;
-
-            if (frame.stream_start) {
-                unsigned width, height;
-                uint8_t codec;
-
-                width = frame.size.width;
-                height = frame.size.height;
-                codec = capture->VideoCodecType();
-
-                syslog(LOG_DEBUG, "wXh %uX%u  codec=%u\n", width, height, codec);
-
-                stream.send<FormatMessage>(width, height, codec);
-            }
-            if (frame_log) {
-                frame_log.dump(frame.buffer, frame.buffer_size);
-            }
-            stream.send<FrameMessage>(frame.buffer, frame.buffer_size);
-            //usleep(1);
-            if (stream.read_command(false) < 0) {
-                syslog(LOG_ERR, "FAILED to read command\n");
-                return;
-            }
-        }
-    }
-}
-
 #define arg_error(...) syslog(LOG_ERR, ## __VA_ARGS__);
 
 int main(int argc, char* argv[])