summaryrefslogtreecommitdiff
path: root/examples/demo.c
blob: 34b85deaf58ea8263aefa1b54443cb5bbfd521d4 (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
241
242
243
244
245
246
247
248
249
250
/**
 * Simple callee example (web browser is caller). Sends video to caller upon
 * incoming calls.
 */

#include <errno.h>                      // errno
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>                      // printf
#include <string.h>
#include <unistd.h>

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>

#include "urtc.h"

// STUN servers
const char *stun[] = {
	"stun.liburtc.org",
	NULL
};

static int http_connect(const char *hostname) {
    int err, fd;
	struct addrinfo *res, *res0;

	struct addrinfo hints = {
		.ai_family   = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_protocol = IPPROTO_TCP
	};

    // resolve hostname to IPv4/IPv6 address
	if (err = getaddrinfo(hostname, "http", &hints, &res0), err) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(errno));
        return -1;
	}

	for (res = res0; res; res = res->ai_next) {
		// attempt to open socket
		fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (-1 == fd) {
			continue; // failed. onto the next result.
		}

		// attempt to connect socket
		if (-1 == connect(fd, res->ai_addr, res->ai_addrlen)) {
		    close(fd);
		    fd = -1;
		    continue; // failed. onto the next result.
		}

        break; // success. return.
	}

	// free resolved addresses
	freeaddrinfo(res0);

    return fd;
}

static int http_post(
    const char *hostname,
    const char *path,
    const char *body
) {
    const char *tmpl = 
        "POST %s HTTP/1.0\r\n"
        "Host: %s\r\n"
        "Content-Length: %d\r\n"
        "Content-Type: text/plain\r\n"
        "\r\n";

    int fd, rv = -1;
    char req[128];

    if (fd = http_connect(hostname), -1 == fd) {
        return -1;
    }

    // construct request
    if (-1 == snprintf(req, sizeof(req), tmpl, path, hostname, strlen(body))) {
        fprintf(stderr, "bad request template\n");
        goto _unwind_socket;
    }

    // send request
    if (rv = send(fd, req, strlen(req), 0), -1 == rv) {
        perror("send");
        goto _unwind_socket;
    }

    // send request body
    if (rv = send(fd, body, strlen(body), 0), -1 == rv) {
        perror("send");
        goto _unwind_socket;
    }

_unwind_socket:
    shutdown(fd, SHUT_RDWR);
    close(fd);

_done:
    return rv;
}

/**
 * HTTP GET request
 *
 * Resolves simple signaling server hostname to IPv4/IPv6 address and connects
 * to its HTTP port.
 *
 * \param[out] response Buffer into which response is written.
 * \param[in] size Max size of response.
 * \param hostname Server hostname (e.g. "demo.liburtc.org")
 * \param path HTTP path (e.g. "/offer")
 *
 * \return Socket file descriptor, or -1 on error. Caller must close file
 *         descriptor.
 */
static int get(
    char *response,
    size_t size,
    const char *hostname,
    const char *path
) {
    const char *tmpl = 
        "GET %s HTTP/1.0\r\n"
        "Host: %s\r\n"
        "\r\n";

    int err, fd, rv = -1;
    char req[128];

    if (fd = http_connect(hostname), -1 == fd) {
        return -1;
    }

    // construct request
    if (-1 == snprintf(req, sizeof(req), tmpl, path, hostname)) {
        fprintf(stderr, "bad request template\n");
        goto _unwind_socket;
    }

    // send request
    if (rv = send(fd, req, strlen(req), 0), -1 == rv) {
        perror("send");
        goto _unwind_socket;
    }

    // get response
    if (rv = recv(fd, response, size, 0), -1 == rv) {
        perror("recv");
        goto _unwind_socket;
    }

_unwind_socket:
    shutdown(fd, SHUT_RDWR);
    close(fd);

_done:
    return rv;
}


/**
 * (callback) Handles incoming ICE candidates.
 *
 * \param cand[in] ICE candidate.
 * \param arg[in] User argument.
 */
void handle_ice_candidate(const char *cand, void *arg) {
    urtc_peerconn_t *pc = (urtc_peerconn_t *)arg;

    urtc_add_ice_candidate(pc, cand);
}

static int longpoll(char *response, size_t size, const char *path) {
    int n;

    while (true) {
        n = get(response, size, "demo.liburtc.org", path);
        if (-1 == n || 0 == n) {
            perror("get");
            continue;
        }

        // blindly strip http headers
        char *body = strstr(response, "\r\n\r\n");
        strcpy(response, body + 4);

        // ignore empty responses (i.e. long-poll timeout)
        if (0 == strlen(response)) {
            continue;
        }

        break;
    }

    return n;
}

char offer[4096];
char answer[4096];

int main(int argc, char **argv) {
    int n;

    // create a new peer connection (no actual network communication yet)
    urtc_peerconn_t *pc = urtc_peerconn_create(stun);

    // TODO add tracks here

    // get offer
    n = longpoll(offer, sizeof(offer), "/offer");
    fprintf(stderr, "%s\n", offer);

    if (urtc_set_remote_description(pc, offer)) {
        fprintf(stderr, "error: set remote description\n");
    }
    if (urtc_create_answer(pc, answer, sizeof(answer))) {
        fprintf(stderr, "error: create answer\n");
    }

    http_post("demo.liburtc.org", "/answer", answer);
    fprintf(stderr, "answer:\n%s\n", answer);
    //urtc_set_local_description(pc, answer);


    // block until signal
    {
	    int signal;
	    sigset_t ss;
	    sigemptyset(&ss);
	    sigaddset(&ss, SIGINT);
	    sigaddset(&ss, SIGTERM);
	    sigaddset(&ss, SIGQUIT);
	    sigwait(&ss, &signal);
    }

    // clean up
	urtc_peerconn_destroy(pc);

	return 0;
}

/* vim: set expandtab ts=4 sw=4 tw=0 : */