http_transport.c 8.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/********************************************************************
 *                                                                  *
 * 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/                                             *
 *                                                                  *
 ********************************************************************
 
14
 last mod: $Id: http_transport.c,v 1.11 2002/07/17 12:12:18 calc Exp $
15 16 17
 
********************************************************************/

18 19
#ifdef HAVE_CURL

20 21 22 23 24 25 26 27 28
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>

#include <curl/curl.h>
#include <curl/easy.h>
#include <pthread.h>

29
#include "ogg123.h"
30 31 32
#include "transport.h"
#include "buffer.h"
#include "status.h"
33
#include "callbacks.h"
34
#include "i18n.h"
35 36 37

#define INPUT_BUFFER_SIZE 32768

38
extern stat_format_t *stat_format;  /* Bad hack!  Will fix after RC3! */
39
extern signal_request_t sig_request;  /* Need access to global cancel flag */
40

41
typedef struct http_private_t {
42 43
  int cancel_flag;

44 45 46 47 48 49 50 51
  buf_t *buf;
  
  pthread_t curl_thread;

  CURL *curl_handle;
  char error[CURL_ERROR_SIZE];

  data_source_t *data_source;
52 53
  data_source_stats_t stats;
} http_private_t;
54 55 56 57 58 59 60 61


transport_t http_transport;  /* Forward declaration */

/* -------------------------- curl callbacks ----------------------- */

size_t write_callback (void *ptr, size_t size, size_t nmemb, void *arg)
{
62 63 64 65
  http_private_t *myarg = arg;

  if (myarg->cancel_flag || sig_request.cancel)
    return 0;
66

67
  buffer_submit_data(myarg->buf, ptr, size*nmemb);
68

69 70
  if (myarg->cancel_flag || sig_request.cancel)
    return 0;
71

72 73 74
  return size * nmemb;
}

75 76 77
int progress_callback (void *arg, size_t dltotal, size_t dlnow,
		       size_t ultotal, size_t ulnow)
{
78
  http_private_t *myarg = arg;
79
  print_statistics_arg_t *pstats_arg;
80 81 82 83
  data_source_t *source = myarg->data_source;

  if (myarg->cancel_flag || sig_request.cancel)
    return -1;
84 85 86 87 88 89 90

  pstats_arg = new_print_statistics_arg(stat_format,
					source->transport->statistics(source),
					NULL);

  print_statistics_action(NULL, pstats_arg);

91 92 93
  if (myarg->cancel_flag || sig_request.cancel)
    return -1;

94 95 96
  return 0;
}

97 98 99

/* -------------------------- Private functions --------------------- */

100
void set_curl_opts (http_private_t *private)
101
{
102
  CURL *handle = private->curl_handle;
103

104
  curl_easy_setopt(handle, CURLOPT_FILE, private);
105
  curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
106
  curl_easy_setopt(handle, CURLOPT_URL, private->data_source->source_string);
107 108 109 110 111 112 113 114 115
  /*
  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);
116 117
  curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, private->error);
  curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
118
  curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, private);
119
  curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);
120
  curl_easy_setopt(handle, CURLOPT_USERAGENT, "ogg123 "VERSION);
121
  curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
122 123 124 125 126
}


void *curl_thread_func (void *arg)
{
127
  http_private_t *myarg = (http_private_t *) arg;
128 129 130 131 132 133 134 135 136
  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)
137
    status_error(_("Error: Could not set signal mask."));
138 139 140

  ret = curl_easy_perform((CURL *) myarg->curl_handle);

141 142 143 144 145 146
  if (myarg->cancel_flag || sig_request.cancel) {
    buffer_abort_write(myarg->buf);
    ret = 0;  // "error" was on purpose
  } else
    buffer_mark_eos(myarg->buf);

147
  if (ret != 0)
148
    status_error(myarg->error);
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

  curl_easy_cleanup(myarg->curl_handle);
  myarg->curl_handle = 0;

  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;
    
184 185 186
    private->buf = buffer_create (ogg123_opts->input_buffer_size,
				  ogg123_opts->input_buffer_size *
				  ogg123_opts->input_prebuffer / 100.0, 
187 188 189 190
				  NULL, NULL,  /* No write callback, using
						  buffer in pull mode. */
				  0 /* Irrelevant */);
    if (private->buf == NULL) {
191
      status_error(_("Error: Unable to create input buffer.\n"));
192 193 194 195
      exit(1);
    }

    private->curl_handle = NULL;
196
    private->data_source = source;
197 198 199
    private->stats.transfer_rate = 0;
    private->stats.bytes_read = 0;
    private->stats.input_buffer_used = 0;
200
    private->cancel_flag = 0;
201 202

  } else {
203
    fprintf(stderr, _("Error: Out of memory.\n"));
204 205 206 207
    exit(1);
  }

  /* Open URL */
208
  private->curl_handle = curl_easy_init();
209 210 211
  if (private->curl_handle == NULL)
    goto fail;

212
  set_curl_opts(private);
213 214

  /* Start thread */
215 216
  if (pthread_create(&private->curl_thread, NULL, curl_thread_func, 
		     private) != 0)
217 218
    goto fail;

219 220 221 222 223 224

  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  */

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
  return source;


fail:
  if (private->curl_handle != NULL)
    curl_easy_cleanup(private->curl_handle);
  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;

260 261 262
  if (private->cancel_flag || sig_request.cancel)
    return 0;

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
  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;

305
  private->cancel_flag = 1;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  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
};
329 330

#endif /* HAVE_CURL */