forked from nestorInc/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jukebox.c
82 lines (67 loc) · 1.67 KB
/
jukebox.c
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
#include <stdlib.h>
#include <poll.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "encoder.h"
#include "display.h"
#include "channel.h"
#include "event.h"
#include "user.h"
#include "http_tool.h"
#include "song.h"
static char basic_reponse[] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: audio/mpeg\r\n"
"Connection: Close\r\n"
"\r\n";
static int on_stream(http_request_t *hr, void *data, const char *remaining, size_t size)
{
int sck;
user_t *user;
channel_t *channel;
(void) remaining;
(void) size;
(void) data;
user = http_request_get_data(hr);
if(user == NULL)
return -1;
sck = http_request_detach(hr);
send(sck, basic_reponse, sizeof(basic_reponse) - 1, MSG_DONTWAIT);
if(user_add_socket(user, sck) == -1) {
close(sck);
}
channel = channel_add_user(user_get_name(user), user);
user_set_channel(user, channel);
return 0;
}
int auth_session(http_request_t *hr, char *login, char *password)
{
user_t *user;
if(login && password &&
strcmp(login, password) == 0) {
user = user_get(login);
http_request_set_data(hr, user, NULL);
return 0;
}
return -1;
}
http_server_t * jukebox_init(int port)
{
http_server_t *server;
encoder_init("mp3", "encoded", 4);
song_init();
channel_init();
event_init();
server = http_server_new(port);
http_node_new(server, "/stream", on_stream, NULL);
http_map_directory(server, "/", "html");
http_server_set_auth_cb(server, auth_session);
return server;
}
void jukebox_launch(void)
{
print_log("Jukebox started");
event_loop();
}