summaryrefslogtreecommitdiff
path: root/src/stream-send.c
blob: fee99619bdfc2f7ab0f23294f4b5aed85fb37caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*  stream-send.c stream-send code

    Copyright 2017 Red Hat, Inc.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <poll.h>
#include <assert.h>
#include <glib.h>

#include <spice/stream-device.h>
#include <spice/enums.h>

/* variables */
static const char *portdev = "/dev/virtio-ports/com.redhat.stream.0";
static int device_fd = -1;

/* main */

static void usage(FILE *fp)
{
    fprintf(fp,
            "Usage: spice-stream-send [OPTIONS] <FILENAME>\n\n"
            "Spice utility to send a stream, version %s.\n\n"
            "Options:\n"
            "  -h             print this text\n"
            "  -s <port>      set virtio serial port  [%s]\n"
            "  -g <WxH>       geometry\n"
            ,VERSION, portdev);
}

static ssize_t
write_all(int fd, const void *buf, size_t len)
{
    size_t written = 0;
    while (written < len) {
        ssize_t l = write(fd, buf + written, len - written);
        if (l < 0 && errno == EINTR) {
            continue;
        }
        if (l < 0) {
            return l;
        }
        written += l;
    }
    return written;
}

static bool got_start = false;
static bool got_stop = false;

static void
on_message(StreamDevType type, const uint8_t *data, size_t size)
{
    assert(type == STREAM_TYPE_STREAM_START);
    assert(size >= sizeof(StreamMsgStart));

    const StreamMsgStart *start = (const StreamMsgStart *) data;
    if (start->num_codecs) {
        printf("got start\n");
        got_start = true;
    } else {
        printf("got stop\n");
        got_stop = true;
    }
}

static void
on_data(void)
{
    static unsigned hdr_pos = 0;
    static StreamDevHeader hdr;
    static unsigned data_pos = 0;
    static uint8_t data[128];

    // read header
    while (hdr_pos < sizeof(hdr)) {
        int n = read(device_fd, ((uint8_t *) &hdr) + hdr_pos, sizeof(hdr) - hdr_pos);
        if (n == -1 && errno == EINTR)
            continue;
        if (n == -1)
            err(1, "read");
        hdr_pos += n;
        if (hdr_pos >= sizeof(hdr)) {
            assert(hdr.protocol_version == STREAM_DEVICE_PROTOCOL);
            hdr.type = GUINT16_FROM_LE(hdr.type);
            hdr.size = GUINT32_FROM_LE(hdr.size);
            data_pos = 0;
        }
    }

    // read data
    assert(hdr.size < sizeof(data));
    while (data_pos < hdr.size) {
        int n = read(device_fd, data + data_pos, hdr.size - data_pos);
        if (n == -1 && errno == EINTR)
            continue;
        if (n == -1)
            err(1, "read");
        data_pos += n;
    }

    hdr_pos = 0;
    on_message((StreamDevType) hdr.type, data, data_pos);
}

static int
wait_read(int fd, int timeout)
{
    for (;;) {
        struct pollfd poll_fd = { fd, POLLIN, 0 };
        switch (poll(&poll_fd, 1, timeout)) {
        case -1:
            if (errno == EINTR)
                continue;
            err(1, "poll");
            break;
        case 0:
            return 0;
        }
        return 1;
    }
}

int main(int argc, char *argv[])
{
    int c;
    int width = 1024, height = 768;

    for (;;) {
        if (-1 == (c = getopt(argc, argv, "hs:g:")))
            break;
        switch (c) {
        case 's':
            portdev = optarg;
            break;
        case 'g':
            if (sscanf(optarg, "%dx%d", &width, &height) != 2 || width <= 0 || height <= 0) {
                errx(1, "Wrong geometry \"%s\"", optarg);
            }
            break;
        case 'h':
            usage(stdout);
            return 0;
        default:
            fputs("\n", stderr);
            usage(stderr);
            return 1;
        }
    }

    if (optind >= argc) {
        usage(stderr);
        return 1;
    }

    device_fd = open(portdev, O_RDWR);
    if (device_fd < 0) {
        err(1, "open %s", portdev);
    }

    const char *fn = argv[optind];
    FILE *f_in = fopen(fn, "r");
    if (!f_in) {
        err(1, "open %s", fn);
    }

    // we must wait for beginning
again:
    while (!got_start) {
        wait_read(device_fd, -1);
        on_data();
    }
    got_start = got_stop = false;
    rewind(f_in);

    StreamDevHeader hdr;
    hdr.protocol_version = STREAM_DEVICE_PROTOCOL;
    hdr.padding = 0;

    // TODO not fixed !!!
    StreamMsgStreamFormat fmt = { width, height, SPICE_VIDEO_CODEC_TYPE_H264, };
    hdr.type = STREAM_TYPE_STREAM_FORMAT;
    hdr.size = sizeof(fmt);
    printf("writing format\n");
    write_all(device_fd, &hdr, sizeof(hdr));
    write_all(device_fd, &fmt, sizeof(fmt));

    for (;;) {
        char buffer[64 * 1024];

        size_t l = fread(buffer, 1, sizeof(buffer), f_in);
        if (!l) {
            break;
        }

        hdr.type = STREAM_TYPE_DATA;
        hdr.size = l;
        printf("writing data %zd bytes\n", l);
        write_all(device_fd, &hdr, sizeof(hdr));
        write_all(device_fd, buffer, l);
        sleep(1);
        // TODO check for request from host
        if (wait_read(device_fd, 0)) {
            on_data();
            if (got_stop) {
                got_start = got_stop = false;
                goto again;
            }
        }
    }

    fclose(f_in);
    close(device_fd);

    return 0;
}