-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibshoutcast.cpp
208 lines (176 loc) · 4.68 KB
/
libshoutcast.cpp
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
/**
*
* @package libshoutcast.cpp
*
* @brief Simple cpp libshout functions wrapper
* libshout is a library for streaming audio to icecast or shoutcast-compatible servers.
* To use this functions you must install libshout-dev on your system
*
* to use this functions you must install libshout and libshout-dev on your linux system
*
* @author Salvatore Cerami [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <shout/shout.h>
static char errmessage[512];
/**
* @brief shout_last_error -> retunr the last error string
* @return last errro message
*/
char *shout_last_error()
{
return errmessage;
}
/**
* @brief shout_stop Close opened connection to server, and free resource.
*
* @param shout
* @return 1: closing connection success
* 0: closing connection failure: the connection is already closed or shout handle is invalid
* all resource are anyway freed
*/
int shout_stop(shout_t *shout)
{
bool success = 0;
if (shout)
{
if (shout_close(shout)==SHOUTERR_SUCCESS)
{
success = 1; // close success
}
else
{
success = 0; // close error
}
}
shout_shutdown();
shout = 0;
return success;
}
/**
* @brief shout_start -> Open a connection to stream data to an icecast server
*
* params value example:
*
* host = 127.0.0.1
* protocol = SHOUT_PROTOCOL_HTTP
* port = 8000
* password = hackme (source password for icecast)
* user = source
* mount = stream
* format = SHOUT_FORMAT_MP3
* @param host
* @param port
* @param protocol
* @param password
* @param user
* @param mount
* @param format
* @return connection handle, or null pointer
*/
shout_t *shout_start(char *host, int port, unsigned int protocol, char *password, char *user, char *mount, unsigned int format, unsigned int nonblocking)
{
shout_t *shout;
shout_init();
if (!(shout = shout_new()))
{
sprintf(errmessage,"Could not allocate shout_t\n");
return 0;
}
if (shout_set_host(shout, host) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting hostname: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_protocol(shout, protocol) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting protocol: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_port(shout, port) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting port: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_password(shout, password) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting password: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_mount(shout, mount) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting mount: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_user(shout, user) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting user: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (shout_set_format(shout, format) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting user: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
if (nonblocking==1)
{
if (shout_set_nonblocking(shout, 1) != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Error setting non-blocking mode: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
}
int ret = shout_open(shout); // try to connects to audio streaming server
if (ret == SHOUTERR_SUCCESS)
{
return shout;
}
if (nonblocking)
{
while (ret == SHOUTERR_BUSY)
{
usleep(10000);
shout_get_connected(shout);
}
if (ret == SHOUTERR_CONNECTED)
{
return shout;
}
}
sprintf(errmessage,"Error connecting: %s\n", shout_get_error(shout));
shout_stop(shout);
return 0;
}
/**
* @brief shout_send_buff send a data buffer to icecast server
* @param shout
* @param buff
* @param bytes
* @return 1: on success, 0: on send error
*/
int shout_send_buff(shout_t *shout, const unsigned char *buff, int bytes, unsigned int sync)
{
int ret = shout_send(shout, buff, bytes);
if (ret != SHOUTERR_SUCCESS)
{
sprintf(errmessage,"Send error: %s\n", shout_get_error(shout));
return 0;
}
if (sync==1) // waits for bytes writing (if set, can slow down the caller process)
{
shout_sync(shout);
}
return 1;
}