+static int open_input(struct variant *var)
+{
+ struct segment *seg = var->segments[var->cur_seq_no - var->start_seq_no];
+ if (seg->key_type == KEY_NONE) {
+ return ffurl_open(&var->input, seg->url, AVIO_FLAG_READ);
+ } else if (seg->key_type == KEY_AES_128) {
+ char iv[33], key[33], url[MAX_URL_SIZE];
+ int ret;
+ if (strcmp(seg->key, var->key_url)) {
+ URLContext *uc;
+ if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ) == 0) {
+ if (ffurl_read_complete(uc, var->key, sizeof(var->key))
+ != sizeof(var->key)) {
+ av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
+ seg->key);
+ }
+ ffurl_close(uc);
+ } else {
+ av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
+ seg->key);
+ }
+ av_strlcpy(var->key_url, seg->key, sizeof(var->key_url));
+ }
+ ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
+ ff_data_to_hex(key, var->key, sizeof(var->key), 0);
+ iv[32] = key[32] = '\0';
+ if (strstr(seg->url, "://"))
+ snprintf(url, sizeof(url), "crypto+%s", seg->url);
+ else
+ snprintf(url, sizeof(url), "crypto:%s", seg->url);
+ if ((ret = ffurl_alloc(&var->input, url, AVIO_FLAG_READ)) < 0)
+ return ret;
+ av_opt_set(var->input->priv_data, "key", key, 0);
+ av_opt_set(var->input->priv_data, "iv", iv, 0);
+ if ((ret = ffurl_connect(var->input)) < 0) {
+ ffurl_close(var->input);
+ var->input = NULL;
+ return ret;
+ }
+ return 0;
+ }
+ return AVERROR(ENOSYS);
+}
+
+static int read_data(void *opaque, uint8_t *buf, int buf_size)
+{
+ struct variant *v = opaque;
+ AppleHTTPContext *c = v->parent->priv_data;
+ int ret, i;
+
+restart:
+ if (!v->input) {
+reload:
+ /* If this is a live stream and target_duration has elapsed since
+ * the last playlist reload, reload the variant playlists now. */
+ if (!v->finished &&
+ av_gettime() - v->last_load_time >= v->target_duration*1000000 &&
+ (ret = parse_playlist(c, v->url, v, NULL)) < 0)
+ return ret;
+ if (v->cur_seq_no < v->start_seq_no) {
+ av_log(NULL, AV_LOG_WARNING,
+ "skipping %d segments ahead, expired from playlists\n",
+ v->start_seq_no - v->cur_seq_no);
+ v->cur_seq_no = v->start_seq_no;
+ }
+ if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
+ if (v->finished)
+ return AVERROR_EOF;
+ while (av_gettime() - v->last_load_time <
+ v->target_duration*1000000) {
+ if (ff_check_interrupt(c->interrupt_callback))
+ return AVERROR_EXIT;
+ usleep(100*1000);
+ }
+ /* Enough time has elapsed since the last reload */
+ goto reload;
+ }
+
+ ret = open_input(v);
+ if (ret < 0)
+ return ret;
+ }
+ ret = ffurl_read(v->input, buf, buf_size);
+ if (ret > 0)
+ return ret;
+ if (ret < 0 && ret != AVERROR_EOF)
+ return ret;
+ ffurl_close(v->input);
+ v->input = NULL;
+ v->cur_seq_no++;
+
+ c->end_of_segment = 1;
+ c->cur_seq_no = v->cur_seq_no;
+
+ if (v->ctx && v->ctx->nb_streams) {
+ v->needed = 0;
+ for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
+ i++) {
+ if (v->parent->streams[i]->discard < AVDISCARD_ALL)
+ v->needed = 1;
+ }
+ }
+ if (!v->needed) {
+ av_log(v->parent, AV_LOG_INFO, "No longer receiving variant %d\n",
+ v->index);
+ return AVERROR_EOF;
+ }
+ goto restart;
+}
+