From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Frediano Ziglio <fziglio@redhat.com>
Date: Sat, 21 Jan 2017 11:24:58 +0000
Subject: [spice-server] stream-device: Add device to handle streaming

Add a stub device in guest.
The aim of this device is to make it possible for the guest to send a
stream through a DisplayChannel (in the sense of protocol channel).
This stub allows the guest to send some data and you can see some debug
lines of data arrived on host logs.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
---
 server/Makefile.am     |   1 +
 server/char-device.h   |   1 +
 server/reds.c          |   2 +
 server/stream-device.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 134 insertions(+)
 create mode 100644 server/stream-device.c

diff --git a/server/Makefile.am b/server/Makefile.am
index 5d5590af9..f08ddf883 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -166,6 +166,7 @@ libserver_la_SOURCES =				\
 	stat.h					\
 	stream.c				\
 	stream.h				\
+	stream-device.c				\
 	sw-canvas.c				\
 	tree.c					\
 	tree.h					\
diff --git a/server/char-device.h b/server/char-device.h
index dccd576da..54a1ef939 100644
--- a/server/char-device.h
+++ b/server/char-device.h
@@ -236,6 +236,7 @@ RedCharDevice *spicevmc_device_connect(RedsState *reds,
                                        uint8_t channel_type);
 void spicevmc_device_disconnect(RedsState *reds,
                                 SpiceCharDeviceInstance *char_device);
+RedCharDevice *stream_device_connect(RedsState *reds, SpiceCharDeviceInstance *sin);
 
 SpiceCharDeviceInterface *spice_char_device_get_interface(SpiceCharDeviceInstance *instance);
 
diff --git a/server/reds.c b/server/reds.c
index 99b1fd76b..b24f61ab2 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3223,6 +3223,8 @@ static int spice_server_char_device_add_interface(SpiceServer *reds,
     else if (strcmp(char_device->subtype, SUBTYPE_PORT) == 0) {
         if (strcmp(char_device->portname, "org.spice-space.webdav.0") == 0) {
             dev_state = spicevmc_device_connect(reds, char_device, SPICE_CHANNEL_WEBDAV);
+        } else if (strcmp(char_device->portname, "com.redhat.stream.0") == 0) {
+            dev_state = stream_device_connect(reds, char_device);
         } else {
             dev_state = spicevmc_device_connect(reds, char_device, SPICE_CHANNEL_PORT);
         }
diff --git a/server/stream-device.c b/server/stream-device.c
new file mode 100644
index 000000000..f3a147b80
--- /dev/null
+++ b/server/stream-device.c
@@ -0,0 +1,130 @@
+/* spice-server character device to handle a video stream
+
+   Copyright (C) 2017 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "char-device.h"
+
+#define TYPE_STREAM_DEVICE stream_device_get_type()
+
+#define STREAM_DEVICE(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_STREAM_DEVICE, StreamDevice))
+#define STREAM_DEVICE_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_STREAM_DEVICE, StreamDeviceClass))
+#define STREAM_DEVICE_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_STREAM_DEVICE, StreamDeviceClass))
+
+typedef struct StreamDevice StreamDevice;
+typedef struct StreamDeviceClass StreamDeviceClass;
+
+struct StreamDevice {
+    RedCharDevice parent;
+};
+
+struct StreamDeviceClass {
+    RedCharDeviceClass parent_class;
+};
+
+static GType stream_device_get_type(void) G_GNUC_CONST;
+static StreamDevice *stream_device_new(SpiceCharDeviceInstance *sin, RedsState *reds);
+
+G_DEFINE_TYPE(StreamDevice, stream_device, RED_TYPE_CHAR_DEVICE)
+
+static RedPipeItem *
+stream_device_read_msg_from_dev(RedCharDevice *self, SpiceCharDeviceInstance *sin)
+{
+    SpiceCharDeviceInterface *sif;
+    int n;
+
+    sif = spice_char_device_get_interface(sin);
+
+    do {
+        uint8_t buf[256];
+        n = sif->read(sin, buf, sizeof(buf));
+        spice_debug("read %d bytes from device", n);
+    } while (n > 0);
+
+    return NULL;
+}
+
+static void
+stream_device_send_msg_to_client(RedCharDevice *self, RedPipeItem *msg, RedClient *client)
+{
+}
+
+static void
+stream_device_send_tokens_to_client(RedCharDevice *self, RedClient *client, uint32_t tokens)
+{
+    spice_printerr("Not implemented!");
+}
+
+static void
+stream_device_remove_client(RedCharDevice *self, RedClient *client)
+{
+}
+
+RedCharDevice *
+stream_device_connect(RedsState *reds, SpiceCharDeviceInstance *sin)
+{
+    SpiceCharDeviceInterface *sif;
+
+    StreamDevice *dev = stream_device_new(sin, reds);
+
+    sif = spice_char_device_get_interface(sin);
+    if (sif->state) {
+        sif->state(sin, 1);
+    }
+
+    return RED_CHAR_DEVICE(dev);
+}
+
+static void
+stream_device_dispose(GObject *object)
+{
+}
+
+static void
+stream_device_class_init(StreamDeviceClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+    RedCharDeviceClass *char_dev_class = RED_CHAR_DEVICE_CLASS(klass);
+
+    object_class->dispose = stream_device_dispose;
+
+    char_dev_class->read_one_msg_from_device = stream_device_read_msg_from_dev;
+    char_dev_class->send_msg_to_client = stream_device_send_msg_to_client;
+    char_dev_class->send_tokens_to_client = stream_device_send_tokens_to_client;
+    char_dev_class->remove_client = stream_device_remove_client;
+}
+
+static void
+stream_device_init(StreamDevice *self)
+{
+}
+
+static StreamDevice *
+stream_device_new(SpiceCharDeviceInstance *sin, RedsState *reds)
+{
+    return g_object_new(TYPE_STREAM_DEVICE,
+                        "sin", sin,
+                        "spice-server", reds,
+                        "client-tokens-interval", 0ULL,
+                        "self-tokens", ~0ULL,
+                        NULL);
+}
