summaryrefslogtreecommitdiffstats
path: root/server/tests/test_playback.c
diff options
context:
space:
mode:
authorAlon Levy <alevy@redhat.com>2011-03-08 21:17:56 +0200
committerAlon Levy <alevy@redhat.com>2011-03-22 09:44:52 +0200
commit02c3f54deb780d01f89edd52a2be05cdaa3556a8 (patch)
tree84f99a78602fbfc3d3f51824d37da2d8042b77e6 /server/tests/test_playback.c
parent5fc7fb68aebb884c4a99e85c71ffd88e119c73dc (diff)
downloadspice-02c3f54deb780d01f89edd52a2be05cdaa3556a8.tar.gz
spice-02c3f54deb780d01f89edd52a2be05cdaa3556a8.tar.xz
spice-02c3f54deb780d01f89edd52a2be05cdaa3556a8.zip
server/tests: add test_playback
Diffstat (limited to 'server/tests/test_playback.c')
-rw-r--r--server/tests/test_playback.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/server/tests/test_playback.c b/server/tests/test_playback.c
new file mode 100644
index 00000000..18220eab
--- /dev/null
+++ b/server/tests/test_playback.c
@@ -0,0 +1,99 @@
+#include <stdio.h>
+#include <strings.h>
+#include <sys/select.h>
+#include <math.h>
+
+#include <spice.h>
+#include "test_util.h"
+#include "basic_event_loop.h"
+
+/* test the audio playback interface. Really basic no frils test - create
+ * a single tone sinus sound (easy to hear clicks if it is generated badly
+ * or is transmitted badly).
+ *
+ * TODO: Was going to do white noise to test compression too.
+ *
+ * TODO: gstreamer based test (could be used to play music files so
+ * it has actual merit. Also possibly to simulate network effects?)
+ * */
+
+SpicePlaybackInstance playback_instance;
+
+static const SpicePlaybackInterface playback_sif = {
+ .base.type = SPICE_INTERFACE_PLAYBACK,
+ .base.description = "test playback",
+ .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
+ .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
+};
+
+uint32_t *frame;
+uint32_t num_samples;
+SpiceTimer *playback_timer;
+int playback_timer_ms;
+SpiceCoreInterface *core;
+
+void playback_timer_cb(void *opaque)
+{
+ static int t = 0;
+ static uint64_t last_sent_usec = 0;
+ static uint64_t samples_to_send;
+ int i;
+ struct timeval cur;
+ uint64_t cur_usec;
+ uint32_t batches;
+
+ if (!frame) {
+ spice_server_playback_get_buffer(&playback_instance, &frame, &num_samples);
+ if (frame) {
+ playback_timer_ms = num_samples ? 1000*num_samples/SPICE_INTERFACE_PLAYBACK_FREQ : 100;
+ } else {
+ /* continue waiting until there is a channel */
+ core->timer_start(playback_timer, playback_timer_ms);
+ return;
+ }
+ }
+ /* we have a channel */
+ gettimeofday(&cur, NULL);
+ cur_usec = cur.tv_usec + cur.tv_sec * 1e6;
+ if (last_sent_usec == 0) {
+ samples_to_send = num_samples;
+ } else {
+ samples_to_send += (cur_usec - last_sent_usec) * SPICE_INTERFACE_PLAYBACK_FREQ / 1e6;
+ }
+ last_sent_usec = cur_usec;
+ batches = samples_to_send / num_samples;
+#if 0
+ printf("samples_to_send = %d, batches = %d\n", samples_to_send, batches);
+#endif
+ samples_to_send -= num_samples * batches;
+ for (;batches > 0 ; --batches) {
+ for (i = 0 ; i < num_samples; ++i) {
+ frame[i] = (((uint16_t)((1<<14)*sin((t+i)/10))) << 16) + (((uint16_t)((1<<14)*sin((t+i)/10))));
+ }
+ t += num_samples;
+ spice_server_playback_put_samples(&playback_instance, frame);
+ }
+ core->timer_start(playback_timer, playback_timer_ms);
+}
+
+int main()
+{
+ SpiceServer *server = spice_server_new();
+ core = basic_event_loop_init();
+
+ spice_server_set_port(server, 5701);
+ spice_server_set_noauth(server);
+ spice_server_init(server, core);
+
+ playback_instance.base.sif = &playback_sif.base;
+ spice_server_add_interface(server, &playback_instance.base);
+ spice_server_playback_start(&playback_instance);
+
+ playback_timer_ms = 100;
+ playback_timer = core->timer_add(playback_timer_cb, NULL);
+ core->timer_start(playback_timer, playback_timer_ms);
+
+ basic_event_loop_mainloop();
+
+ return 0;
+}