-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshoutcast.cpp
More file actions
188 lines (162 loc) · 4.48 KB
/
shoutcast.cpp
File metadata and controls
188 lines (162 loc) · 4.48 KB
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
/**
* @class SCDShoutcast - https://github.com/sc-develop
*
* @brief interface class to shoutcast streaming library (libshout)
* 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
*
* Require libShoutcast.cpp/hpp files
*
* to use this class you must install libshout and libshout-dev on your linux system
*
* @author Ing. Salvatore Cerami - dev.salvatore.cerami@gmail.com
*/
#include "shoutcast.hpp"
#include "QTextStream"
#include "QFile"
#include "QFileInfo"
/**
* @brief Shoutcast::Shoutcast
* @param host
* @param port
* @param username
* @param password
* @param protocol
* @param format
*/
SCDShoutcast::SCDShoutcast(QString host, int port, QString username, QString password, SCDShoutcast::ShoutProtocol protocol, SCDShoutcast::AudioFormat format)
{
ice_host = host;
ice_port = port;
ice_username = username;
ice_password = password,
ice_protocol = protocol,
ice_format = format;
shout_handle = 0;
}
/**
* @brief Shoutcast::open
* @param mountPoint
* @return
*/
int SCDShoutcast::open(const char *mountPoint, bool nonblocking)
{
return open(mountPoint,nonblocking);
}
/**
* @brief Shoutcast::open try to start new connection to server with provided moutPoint, if already connected (no new mount point mounted) do not nothing and return 1
* @param mountPoint Shoutcast mount point
* @return 1: on success (connection opend, or already opened), 0: on error (connection not opened)
*/
int SCDShoutcast::open(QString mountPoint, bool nonblocking)
{
if (shout_handle)
{
return 1;
}
ice_mount = mountPoint;
shout_handle = shout_start((char *)ice_host.toStdString().c_str(),
ice_port,
ice_protocol,
(char *)ice_password.toStdString().c_str(),
(char *)ice_username.toStdString().c_str(),
(char *)ice_mount.toStdString().c_str(),
ice_format,
nonblocking);
if (shout_handle)
{
lastError = "";
return 1;
}
lastError = shout_last_error();
return 0;
}
/**
* @brief Shoutcast::send
* @param buff
* @param buff_size
* @return 1: on success, 0: on error
*/
int SCDShoutcast::send(const char *buff, int buff_size, unsigned int sync)
{
// Call libShoutcast function
int ret = shout_send_buff(shout_handle, (unsigned char *)buff, buff_size, sync);
if (ret)
{
lastError = "";
return ret;
}
lastError = shout_last_error();
return ret;
}
/**
* @brief Shoutcast::send
* @param buff
* @param buff_size
* @param startFromByte
* @return
*/
int SCDShoutcast::send(const char *buff, int buff_size, int startFromByte, unsigned int sync)
{
return send(buff + startFromByte, buff_size - startFromByte, sync);
}
/**
* @brief Shoutcast::send
* @param buff
* @return 1: on success, 0: on error
*/
int SCDShoutcast::send(QByteArray *buff, unsigned int sync)
{
return send(buff->constData(),buff->size(),sync);
}
/**
* @brief Shoutcast::sendFile Stream the file 'filename' to icecast server. The connection to icecast must be opened.
* The file will stream to current connected mountpoint
* @param filename audio file fullpathname. The format of file must be ogg, mp3 or webm
* @param mountPoint
* @return
*/
int SCDShoutcast::streamFile(QString filename)
{
QFile f(filename);
if (!f.open(QIODevice::ReadOnly))
{
lastError = "Shoutcast: error opening file => " + filename;
return 0;
}
QByteArray buffer;
lastError = "Shoutcast: sending file => " + QFileInfo(filename).fileName();
while (!f.atEnd())
{
buffer = f.read(1024);
if (buffer.size() > 0)
{
if (!shout_send_buff(shout_handle,(unsigned char *)buffer.constData(),buffer.size(),1))
{
f.close();
lastError = "Shoutcast: sending error => " + QString(shout_get_error(shout_handle));
return 0;
}
}
}
f.close();
return 1;
}
/**
* @brief Shoutcast::send
* @param buff
* @param startFromByte
* @return
*/
int SCDShoutcast::send(QByteArray *buff, int startFromByte, unsigned int sync)
{
return send(buff->constData() + startFromByte, buff->size() - startFromByte,sync);
}
/**
* @brief Shoutcast::close
* @return
*/
int SCDShoutcast::close()
{
return shout_stop(shout_handle);
}