static int hls_parse_request(HTTPContext *c, char *name, int is_first) { int idx = -1, ret = 0; char *ext = NULL; HLS *s = NULL; ext = strrchr(name, '.'); if(!ext){ http_log("bad name %s\n", name); return -1; } if(ext - name > sizeof(s_hls_name)-1){ http_log("name %s too long\n", name); return -1; } if(!strcmp(ext, ".m3u8")){ idx = S; }else if(!strcmp(ext, ".ts")){ idx = ext[-1] - '0';//todo: S > 10 without get basename. if(!(0 <= idx && idx < S)){ http_log("too large index %d\n", idx); return -1; } } if(-1 == idx){ http_log("unkown name %s\n", name); return -1; } c->hls_idx = idx; s = &s_hls[idx]; if(c->post){/*writer*/ //todo: close http conn with same name //http_log("hls post c %p name %s:%d data %p size %d:%d\n", c, name, idx, s->data, s->msize, s->csize); if(!s->data){ s->data = av_malloc(SEG_INC_SIZE); s->msize = SEG_INC_SIZE; }else{ /*intended not to free*/ } //todo: if someone is reading s->flag = 1; s->csize = 0; c->hls_wpos = 0; c->http_error = 0; c->state = HTTPSTATE_RECEIVE_DATA; ret = 0; }else{/*reader*/ if(is_first && (S == idx) && strncmp(s_hls_name, name, ext-name) ){ hls_close(); strncpy(s_hls_name, name, ext - name); s_hls_name[ext - name] = 0; ret = 1; /*request switch*/ } c->hls_rpos = 0; c->http_error = 0; c->state = HTTPSTATE_SEND_HEADER; } return ret; }
static int hls_open(URLContext *h, const char *uri, int flags) { HLSContext *s = h->priv_data; int ret, i; const char *nested_url; if (flags & AVIO_FLAG_WRITE) return AVERROR(ENOSYS); h->is_streamed = 1; if (av_strstart(uri, "hls+", &nested_url)) { av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl)); } else if (av_strstart(uri, "hls://", &nested_url)) { av_log(h, AV_LOG_ERROR, "No nested protocol specified. Specify e.g. hls+http://%s\n", nested_url); ret = AVERROR(EINVAL); goto fail; } else { av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri); ret = AVERROR(EINVAL); goto fail; } av_log(h, AV_LOG_WARNING, "Using the hls protocol is discouraged, please try using the " "hls demuxer instead. The hls demuxer should be more complete " "and work as well as the protocol implementation. (If not, " "please report it.) To use the demuxer, simply use %s as url.\n", s->playlisturl); if ((ret = parse_playlist(h, s->playlisturl)) < 0) goto fail; if (s->n_segments == 0 && s->n_variants > 0) { int max_bandwidth = 0, maxvar = -1; for (i = 0; i < s->n_variants; i++) { if (s->variants[i]->bandwidth > max_bandwidth || i == 0) { max_bandwidth = s->variants[i]->bandwidth; maxvar = i; } } av_strlcpy(s->playlisturl, s->variants[maxvar]->url, sizeof(s->playlisturl)); if ((ret = parse_playlist(h, s->playlisturl)) < 0) goto fail; } if (s->n_segments == 0) { av_log(h, AV_LOG_WARNING, "Empty playlist\n"); ret = AVERROR(EIO); goto fail; } s->cur_seq_no = s->start_seq_no; if (!s->finished && s->n_segments >= 3) s->cur_seq_no = s->start_seq_no + s->n_segments - 3; return 0; fail: hls_close(h); return ret; }