[f7a722]: ogg123 / http_transport.c Maximize Restore History

Download this file

http_transport.c    348 lines (258 with data), 9.0 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
* THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
* PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001 *
* by Kenneth C. Arnold <ogg@arnoldnet.net> AND OTHER CONTRIBUTORS *
* http://www.xiph.org/ *
* *
********************************************************************
last mod: $Id: http_transport.c,v 1.12 2003/08/06 23:14:12 volsung Exp $
********************************************************************/
#ifdef HAVE_CURL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <pthread.h>
#include "ogg123.h"
#include "transport.h"
#include "buffer.h"
#include "status.h"
#include "callbacks.h"
#include "i18n.h"
#define INPUT_BUFFER_SIZE 32768
extern stat_format_t *stat_format; /* Bad hack! Will fix after RC3! */
extern signal_request_t sig_request; /* Need access to global cancel flag */
typedef struct http_private_t {
int cancel_flag;
buf_t *buf;
pthread_t curl_thread;
CURL *curl_handle;
struct curl_slist *header_list;
char error[CURL_ERROR_SIZE];
data_source_t *data_source;
data_source_stats_t stats;
} http_private_t;
transport_t http_transport; /* Forward declaration */
/* -------------------------- curl callbacks ----------------------- */
size_t write_callback (void *ptr, size_t size, size_t nmemb, void *arg)
{
http_private_t *myarg = arg;
if (myarg->cancel_flag || sig_request.cancel)
return 0;
buffer_submit_data(myarg->buf, ptr, size*nmemb);
if (myarg->cancel_flag || sig_request.cancel)
return 0;
return size * nmemb;
}
int progress_callback (void *arg, size_t dltotal, size_t dlnow,
size_t ultotal, size_t ulnow)
{
http_private_t *myarg = arg;
print_statistics_arg_t *pstats_arg;
data_source_t *source = myarg->data_source;
if (myarg->cancel_flag || sig_request.cancel)
return -1;
pstats_arg = new_print_statistics_arg(stat_format,
source->transport->statistics(source),
NULL);
print_statistics_action(NULL, pstats_arg);
if (myarg->cancel_flag || sig_request.cancel)
return -1;
return 0;
}
/* -------------------------- Private functions --------------------- */
void set_curl_opts (http_private_t *private)
{
CURL *handle = private->curl_handle;
curl_easy_setopt(handle, CURLOPT_FILE, private);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(handle, CURLOPT_URL, private->data_source->source_string);
/*
if (inputOpts.ProxyPort)
curl_easy_setopt(handle, CURLOPT_PROXYPORT, inputOpts.ProxyPort);
if (inputOpts.ProxyHost)
curl_easy_setopt(handle, CURLOPT_PROXY, inputOpts.ProxyHost);
if (inputOpts.ProxyTunnel)
curl_easy_setopt (handle, CURLOPT_HTTPPROXYTUNNEL, inputOpts.ProxyTunnel);
*/
curl_easy_setopt(handle, CURLOPT_MUTE, 1);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, private->error);
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, private);
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(handle, CURLOPT_USERAGENT, "ogg123/"VERSION);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, private->header_list);
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
}
void *curl_thread_func (void *arg)
{
http_private_t *myarg = (http_private_t *) arg;
CURLcode ret;
sigset_t set;
/* Block signals to this thread */
sigfillset (&set);
sigaddset (&set, SIGINT);
sigaddset (&set, SIGTSTP);
sigaddset (&set, SIGCONT);
if (pthread_sigmask (SIG_BLOCK, &set, NULL) != 0)
status_error(_("Error: Could not set signal mask."));
ret = curl_easy_perform((CURL *) myarg->curl_handle);
if (myarg->cancel_flag || sig_request.cancel) {
buffer_abort_write(myarg->buf);
ret = 0; // "error" was on purpose
} else
buffer_mark_eos(myarg->buf);
if (ret != 0)
status_error(myarg->error);
curl_easy_cleanup(myarg->curl_handle);
myarg->curl_handle = 0;
curl_slist_free_all(myarg->header_list);
myarg->header_list = NULL;
return (void *) ret;
}
/* -------------------------- Public interface -------------------------- */
int http_can_transport (char *source_string)
{
int tmp;
tmp = strchr(source_string, ':') - source_string;
return tmp < 10 &&
tmp + 2 < strlen(source_string) &&
!strncmp(source_string + tmp, "://", 3);
}
data_source_t* http_open (char *source_string, ogg123_options_t *ogg123_opts)
{
data_source_t *source;
http_private_t *private;
/* Allocate data source structures */
source = malloc(sizeof(data_source_t));
private = malloc(sizeof(http_private_t));
if (source != NULL && private != NULL) {
source->source_string = strdup(source_string);
source->transport = &http_transport;
source->private = private;
private->buf = buffer_create (ogg123_opts->input_buffer_size,
ogg123_opts->input_buffer_size *
ogg123_opts->input_prebuffer / 100.0,
NULL, NULL, /* No write callback, using
buffer in pull mode. */
0 /* Irrelevant */);
if (private->buf == NULL) {
status_error(_("Error: Unable to create input buffer.\n"));
exit(1);
}
private->curl_handle = NULL;
private->header_list = NULL;
private->data_source = source;
private->stats.transfer_rate = 0;
private->stats.bytes_read = 0;
private->stats.input_buffer_used = 0;
private->cancel_flag = 0;
} else {
fprintf(stderr, _("Error: Out of memory.\n"));
exit(1);
}
/* ogg123 only accepts Ogg files, and optionally FLAC as well */
#ifdef HAVE_LIBFLAC
private->header_list = curl_slist_append(NULL, "Accept: application/ogg audio/x-flac;q=0.9");
#else
private->header_list = curl_slist_append(NULL, "Accept: application/ogg");
#endif
if (private->header_list == NULL)
goto fail;
/* Open URL */
private->curl_handle = curl_easy_init();
if (private->curl_handle == NULL)
goto fail;
set_curl_opts(private);
/* Start thread */
if (pthread_create(&private->curl_thread, NULL, curl_thread_func,
private) != 0)
goto fail;
stat_format[2].enabled = 0; /* remaining playback time */
stat_format[3].enabled = 0; /* total playback time */
stat_format[6].enabled = 1; /* Input buffer fill % */
stat_format[7].enabled = 1; /* Input buffer state */
return source;
fail:
if (private->curl_handle != NULL)
curl_easy_cleanup(private->curl_handle);
if (private->header_list != NULL)
curl_slist_free_all(private->header_list);
free(source->source_string);
free(private);
free(source);
return NULL;
}
int http_peek (data_source_t *source, void *ptr, size_t size, size_t nmemb)
{
/*
http_private_t *private = source->private;
int items;
long start;
bytes_read = buffer_get_data(data->buf, ptr, size, nmemb);
private->stats.bytes_read += bytes_read;
*/
return 0;
}
int http_read (data_source_t *source, void *ptr, size_t size, size_t nmemb)
{
http_private_t *private = source->private;
int bytes_read;
if (private->cancel_flag || sig_request.cancel)
return 0;
bytes_read = buffer_get_data(private->buf, ptr, size * nmemb);
private->stats.bytes_read += bytes_read;
return bytes_read;
}
int http_seek (data_source_t *source, long offset, int whence)
{
return -1;
}
data_source_stats_t *http_statistics (data_source_t *source)
{
http_private_t *private = source->private;
data_source_stats_t *data_source_stats;
buffer_stats_t *buffer_stats;
data_source_stats = malloc_data_source_stats(&private->stats);
data_source_stats->input_buffer_used = 1;
data_source_stats->transfer_rate = 0;
buffer_stats = buffer_statistics(private->buf);
data_source_stats->input_buffer = *buffer_stats;
free(buffer_stats);
return data_source_stats;
}
long http_tell (data_source_t *source)
{
return 0;
}
void http_close (data_source_t *source)
{
http_private_t *private = source->private;
private->cancel_flag = 1;
buffer_abort_write(private->buf);
pthread_join(private->curl_thread, NULL);
buffer_destroy(private->buf);
private->buf = NULL;
free(source->source_string);
free(source->private);
free(source);
}
transport_t http_transport = {
"http",
&http_can_transport,
&http_open,
&http_peek,
&http_read,
&http_seek,
&http_statistics,
&http_tell,
&http_close
};
#endif /* HAVE_CURL */