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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <avdec_private.h>
#include <http.h>
#include <hls.h>
#define NUM_REDIRECTIONS 5
#define LOG_DOMAIN "in_http"
/* Generic http input module */
typedef struct
{
int64_t total_bytes_read;
int icy_metaint;
int icy_bytes;
bgav_http_t * h;
bgav_charset_converter_t * charset_cnv;
bgav_hls_t * hls;
int64_t bytes_read;
} http_priv;
static void create_header(gavl_dictionary_t * ret, const bgav_options_t * opt)
{
gavl_dictionary_set_string(ret, "User-Agent", PACKAGE"/"VERSION);
gavl_dictionary_set_string(ret, "Accept", "*");
if(opt->http_shoutcast_metadata)
gavl_dictionary_set_string(ret, "Icy-MetaData", "1");
gavl_dictionary_set_string(ret, "GetContentFeatures.DLNA.ORG", "1");
}
static int open_http(bgav_input_context_t * ctx, const char * url, char ** r)
{
const char * var;
http_priv * p;
const gavl_dictionary_t * res;
gavl_dictionary_t extra_header;
gavl_dictionary_init(&extra_header);
p = calloc(1, sizeof(*p));
create_header(&extra_header, ctx->opt);
p->h = bgav_http_open(url, ctx->opt, r, &extra_header);
gavl_dictionary_free(&extra_header);
if(!p->h)
{
free(p);
return 0;
}
ctx->priv = p;
ctx->total_bytes = bgav_http_total_bytes(p->h);
res = bgav_http_get_header(p->h);
bgav_http_set_metadata(p->h, &ctx->m);
// bgav_http_header_dump(header);
var = gavl_dictionary_get_string(res, "icy-metaint");
if(var)
{
p->icy_metaint = atoi(var);
// p->icy_bytes = p->icy_metaint;
/* Then, we'll also need a charset converter */
p->charset_cnv = bgav_charset_converter_create(ctx->opt,
"ISO-8859-1",
BGAV_UTF8);
}
var = gavl_dictionary_get_string(res, "Accept-Ranges");
if(!var || strcasecmp(var, "bytes"))
ctx->flags &= ~BGAV_INPUT_CAN_SEEK_BYTE;
else
ctx->flags |= (BGAV_INPUT_SEEK_SLOW | BGAV_INPUT_CAN_PAUSE);
// ctx->flags |= BGAV_INPUT_DO_BUFFER;
ctx->url = gavl_strdup(url);
return 1;
}
static int64_t seek_byte_http(bgav_input_context_t * ctx,
int64_t pos, int whence)
{
http_priv * p = ctx->priv;
gavl_dictionary_t extra_header;
gavl_dictionary_init(&extra_header);
if(p->h)
{
bgav_http_close(p->h);
p->h = NULL;
}
create_header(&extra_header, ctx->opt);
gavl_dictionary_set_string_nocopy(&extra_header, "Range",
bgav_sprintf("bytes=%"PRId64"-", ctx->position));
p->h = bgav_http_open(ctx->url, ctx->opt, NULL, &extra_header);
gavl_dictionary_free(&extra_header);
p->bytes_read = ctx->position;
return ctx->position;
}
static void pause_http(bgav_input_context_t * ctx)
{
http_priv * p = ctx->priv;
bgav_http_close(p->h);
p->h = NULL;
}
static void resume_http(bgav_input_context_t * ctx)
{
gavl_dictionary_t extra_header;
http_priv * p = ctx->priv;
gavl_dictionary_init(&extra_header);
create_header(&extra_header, ctx->opt);
gavl_dictionary_set_string_nocopy(&extra_header, "Range",
bgav_sprintf("bytes=%"PRId64"-", p->bytes_read));
p->h = bgav_http_open(ctx->url, ctx->opt, NULL, &extra_header);
gavl_dictionary_free(&extra_header);
}
static int read_data(bgav_input_context_t* ctx,
uint8_t * buffer, int len)
{
http_priv * p = ctx->priv;
if(p->hls)
return bgav_hls_read(p->hls, buffer, len);
else
{
int ret = bgav_http_read(p->h, buffer, len);
p->bytes_read += ret;
if((ret < len) && (ctx->total_bytes > 0) && (p->bytes_read < ctx->total_bytes))
{
gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN, "Premature end of stream, reconnecting...");
pause_http(ctx);
resume_http(ctx);
ret = bgav_http_read(p->h, buffer, len);
p->bytes_read += ret;
}
return ret;
}
}
static void * memscan(void * mem_start, int size, void * key, int key_len)
{
void * mem = mem_start;
while(mem - mem_start < size - key_len)
{
if(!memcmp(mem, key, key_len))
return mem;
mem++;
}
return NULL;
}
static int read_shoutcast_metadata(bgav_input_context_t* ctx)
{
char * meta_buffer;
const char * pos, *end_pos;
uint8_t icy_len;
int meta_bytes;
http_priv * priv;
int bitrate;
gavl_time_t timestamp = GAVL_TIME_SCALE;
priv = ctx->priv;
if(!read_data(ctx, &icy_len, 1))
{
return 0;
}
meta_bytes = icy_len * 16;
// fprintf(stderr, "Got ICY metadata %d bytes\n", meta_bytes);
if(meta_bytes)
{
meta_buffer = malloc(meta_bytes);
/* Metadata block is read in blocking mode!! */
if(read_data(ctx, (uint8_t*)meta_buffer, meta_bytes) < meta_bytes)
return 0;
// fprintf(stderr, "Got metadata block\n");
// gavl_hexdump((uint8_t*)meta_buffer, meta_bytes, 16);
if((ctx->tt) && (pos = memscan(meta_buffer, meta_bytes, "StreamTitle='", 13)))
{
pos+=13;
end_pos = strchr(pos, ';');
if(end_pos)
{
end_pos--; // ; -> '
if(bgav_utf8_validate((const uint8_t*)pos, (const uint8_t *)end_pos))
{
gavl_dictionary_set_string_nocopy(ctx->tt->cur->metadata,
GAVL_META_LABEL,
gavl_strndup(pos, end_pos));
}
else
{
gavl_dictionary_set_string_nocopy(ctx->tt->cur->metadata,
GAVL_META_LABEL,
bgav_convert_string(priv->charset_cnv ,
pos, end_pos - pos,
NULL));
}
if(!gavl_dictionary_get_int(&ctx->m, GAVL_META_BITRATE, &bitrate))
timestamp = GAVL_TIME_UNDEFINED;
else
timestamp = (priv->total_bytes_read * 8 * GAVL_TIME_SCALE) / bitrate;
bgav_metadata_changed(ctx->b, ctx->tt->cur->metadata, timestamp);
#if 0
fprintf(stderr, "Got ICY metadata: %s, %f\n",
gavl_dictionary_get_string(ctx->tt->cur->metadata, GAVL_META_LABEL),
gavl_time_to_seconds(timestamp));
#endif
}
}
free(meta_buffer);
}
return 1;
}
static int do_read(bgav_input_context_t* ctx,
uint8_t * buffer, int len)
{
int bytes_to_read;
int bytes_read = 0;
int result;
http_priv * p = ctx->priv;
if(!p->icy_metaint)
return read_data(ctx, buffer, len);
else
{
while(bytes_read < len)
{
/* Read data chunk */
bytes_to_read = len - bytes_read;
if(p->icy_bytes + bytes_to_read > p->icy_metaint)
bytes_to_read = p->icy_metaint - p->icy_bytes;
if(bytes_to_read)
{
result = read_data(ctx, buffer + bytes_read, bytes_to_read);
bytes_read += result;
p->icy_bytes += result;
p->total_bytes_read += result;
if(result < bytes_to_read)
return bytes_read;
}
/* Read metadata */
if(p->icy_bytes == p->icy_metaint)
{
if(!read_shoutcast_metadata(ctx))
return bytes_read;
else
p->icy_bytes = 0;
}
}
}
return bytes_read;
}
static int read_http(bgav_input_context_t* ctx,
uint8_t * buffer, int len)
{
return do_read(ctx, buffer, len);
}
static void close_http(bgav_input_context_t * ctx)
{
http_priv * p = ctx->priv;
if(p->h)
bgav_http_close(p->h);
if(p->charset_cnv)
bgav_charset_converter_destroy(p->charset_cnv);
if(p->hls)
bgav_hls_close(p->hls);
free(p);
}
static int finalize_http(bgav_input_context_t * ctx)
{
http_priv * p = ctx->priv;
if(bgav_hls_detect(ctx))
{
gavl_log(GAVL_LOG_INFO, LOG_DOMAIN,
"Detected http live streaming");
p->hls = bgav_hls_create(ctx);
if(!p->hls)
return 0;
/* hls seeking is not supported */
ctx->flags &= ~BGAV_INPUT_CAN_SEEK_BYTE;
return 1;
}
else
return 1;
}
const bgav_input_t bgav_input_http =
{
.name = "http",
.open = open_http,
.finalize = finalize_http,
.read = read_http,
.seek_byte = seek_byte_http,
.pause = pause_http,
.resume = resume_http,
.close = close_http,
};
|