[Spice-devel] [PATCH spice-server v2 2/4] record: Allocate recording file globally from reds.c
Frediano Ziglio
fziglio at redhat.com
Thu Nov 10 10:42:01 UTC 2016
Allows to use recording function for multiple purposes.
This will allow to register multiple screen VM or recording
additional stuff like sound.
The mutex code added to red-record-qxl.c is required to avoid
mixing writing from multiple threads.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/red-record-qxl.c | 35 +++++++++++++++++++++++++++++------
server/red-record-qxl.h | 3 ++-
server/red-worker.c | 6 +-----
server/reds-private.h | 2 ++
server/reds.c | 16 ++++++++++++++++
server/reds.h | 5 +++++
6 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
index 14a68a7..5d41bb0 100644
--- a/server/red-record-qxl.c
+++ b/server/red-record-qxl.c
@@ -31,7 +31,9 @@
struct RedRecord {
FILE *fd;
+ pthread_mutex_t lock;
unsigned int counter;
+ gint refs;
};
#if 0
@@ -795,15 +797,17 @@ void red_record_primary_surface_create(RedRecord *record,
{
FILE *fd = record->fd;
+ pthread_mutex_lock(&record->lock);
fprintf(fd, "%d %d %d %d\n", surface->width, surface->height,
surface->stride, surface->format);
fprintf(fd, "%d %d %d %d\n", surface->position, surface->mouse_mode,
surface->flags, surface->type);
write_binary(fd, "data", line_0 ? abs(surface->stride)*surface->height : 0,
line_0);
+ pthread_mutex_unlock(&record->lock);
}
-void red_record_event(RedRecord *record, int what, uint32_t type)
+static void red_record_event_start(RedRecord *record, int what, uint32_t type)
{
red_time_t ts = spice_get_monotonic_time_ns();
// TODO: record the size of the packet in the header. This would make
@@ -814,12 +818,20 @@ void red_record_event(RedRecord *record, int what, uint32_t type)
fprintf(record->fd, "event %u %d %u %"PRIu64"\n", record->counter++, what, type, ts);
}
+void red_record_event(RedRecord *record, int what, uint32_t type)
+{
+ pthread_mutex_lock(&record->lock);
+ red_record_event_start(record, what, type);
+ pthread_mutex_unlock(&record->lock);
+}
+
void red_record_qxl_command(RedRecord *record, RedMemSlotInfo *slots,
QXLCommandExt ext_cmd)
{
FILE *fd = record->fd;
- red_record_event(record, 0, ext_cmd.cmd.type);
+ pthread_mutex_lock(&record->lock);
+ red_record_event_start(record, 0, ext_cmd.cmd.type);
switch (ext_cmd.cmd.type) {
case QXL_CMD_DRAW:
@@ -838,6 +850,7 @@ void red_record_qxl_command(RedRecord *record, RedMemSlotInfo *slots,
red_record_cursor_cmd(fd, slots, ext_cmd.group_id, ext_cmd.cmd.data);
break;
}
+ pthread_mutex_unlock(&record->lock);
}
/**
@@ -900,15 +913,25 @@ RedRecord *red_record_new(const char *filename)
}
record = g_new(RedRecord, 1);
+ record->refs = 1;
record->fd = f;
record->counter = 0;
+ pthread_mutex_init(&record->lock, NULL);
return record;
}
-void red_record_free(RedRecord *record)
+RedRecord *red_record_ref(RedRecord *record)
{
- if (record) {
- fclose(record->fd);
- g_free(record);
+ g_atomic_int_inc(&record->refs);
+ return record;
+}
+
+void red_record_unref(RedRecord *record)
+{
+ if (!record || !g_atomic_int_dec_and_test(&record->refs)) {
+ return;
}
+ fclose(record->fd);
+ pthread_mutex_destroy(&record->lock);
+ g_free(record);
}
diff --git a/server/red-record-qxl.h b/server/red-record-qxl.h
index 0685393..293e24a 100644
--- a/server/red-record-qxl.h
+++ b/server/red-record-qxl.h
@@ -33,7 +33,8 @@ typedef struct RedRecord RedRecord;
*/
RedRecord* red_record_new(const char *filename);
-void red_record_free(RedRecord *record);
+RedRecord *red_record_ref(RedRecord *record);
+void red_record_unref(RedRecord *record);
void red_record_primary_surface_create(RedRecord *record,
QXLDevSurfaceCreate *surface,
diff --git a/server/red-worker.c b/server/red-worker.c
index f8c135f..8795b58 100644
--- a/server/red-worker.c
+++ b/server/red-worker.c
@@ -1305,7 +1305,6 @@ RedWorker* red_worker_new(QXLInstance *qxl,
QXLDevInitInfo init_info;
RedWorker *worker;
Dispatcher *dispatcher;
- const char *record_filename;
RedsState *reds = red_qxl_get_server(qxl->st);
RedChannel *channel;
@@ -1315,10 +1314,7 @@ RedWorker* red_worker_new(QXLInstance *qxl,
worker->core = event_loop_core;
worker->core.main_context = g_main_context_new();
- record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
- if (record_filename) {
- worker->record = red_record_new(record_filename);
- }
+ worker->record = reds_get_record(reds);
dispatcher = red_qxl_get_dispatcher(qxl);
dispatcher_set_opaque(dispatcher, worker);
diff --git a/server/reds-private.h b/server/reds-private.h
index ce78945..85ad279 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -24,6 +24,7 @@
#include "main-dispatcher.h"
#include "main-channel.h"
#include "inputs-channel.h"
+#include "red-record-qxl.h"
#define MIGRATE_TIMEOUT (MSEC_PER_SEC * 10)
#define MM_TIME_DELTA 400 /*ms*/
@@ -143,6 +144,7 @@ struct RedsState {
SpiceCoreInterfaceInternal *core;
GList *qxl_instances;
MainDispatcher *main_dispatcher;
+ RedRecord *record;
};
#define FOREACH_QXL_INSTANCE(_reds, _iter, _qxl) \
diff --git a/server/reds.c b/server/reds.c
index 30b9165..52efce0 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3512,7 +3512,9 @@ static const char default_video_codecs[] = "spice:mjpeg;gstreamer:mjpeg;gstreame
/* new interface */
SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
{
+ const char *record_filename;
RedsState *reds = spice_new0(RedsState, 1);
+
reds->config = spice_new0(RedServerConfig, 1);
reds->config->default_channel_security =
SPICE_CHANNEL_SECURITY_NONE | SPICE_CHANNEL_SECURITY_SSL;
@@ -3538,6 +3540,11 @@ SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
reds->config->agent_copypaste = TRUE;
reds->config->agent_file_xfer = TRUE;
reds->config->exit_on_disconnect = FALSE;
+
+ record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
+ if (record_filename) {
+ reds->record = red_record_new(record_filename);
+ }
return reds;
}
@@ -4517,3 +4524,12 @@ static RedCharDeviceVDIPort *red_char_device_vdi_port_new(RedsState *reds)
"opaque", reds,
NULL);
}
+
+RedRecord *reds_get_record(RedsState *reds)
+{
+ if (reds->record) {
+ return red_record_ref(reds->record);
+ }
+
+ return NULL;
+}
diff --git a/server/reds.h b/server/reds.h
index 28e3444..2748102 100644
--- a/server/reds.h
+++ b/server/reds.h
@@ -101,6 +101,11 @@ SpiceCoreInterfaceInternal* reds_get_core_interface(RedsState *reds);
void reds_update_client_mouse_allowed(RedsState *reds);
MainDispatcher* reds_get_main_dispatcher(RedsState *reds);
+/* Get the recording object stored in RedsState.
+ * You should free with red_record_unref.
+ */
+struct RedRecord *reds_get_record(RedsState *reds);
+
/* fd watches/timers */
SpiceWatch *reds_core_watch_add(RedsState *reds,
int fd, int event_mask,
--
2.7.4
More information about the Spice-devel
mailing list