static struct list_item * switchto_next_item(struct list_mgt *mgt) { struct list_item *next=NULL; struct list_item *current = NULL; if(!mgt) return NULL; int64_t reload_interval = mgt->item_num> 0&&mgt->current_item!=NULL? mgt->current_item->duration : mgt->target_duration; int isNeedFetch = 1; if(mgt->current_item==NULL || mgt->current_item->next==NULL){ /*new refresh this mgtlist now*/ ByteIOContext *bio; int ret; char* url = NULL; if(mgt->n_variants>0&&NULL!=mgt->playing_variant){ url = mgt->playing_variant->url; av_log(NULL, AV_LOG_INFO,"list open variant url:%s,bandwidth:%d\n",url,mgt->playing_variant->bandwidth); }else{ url = mgt->filename; av_log(NULL, AV_LOG_INFO,"list open url:%s\n",url); } reload_interval *= 500000; if(!mgt->have_list_end&&(av_gettime() - mgt->last_load_time < reload_interval)){ av_log(NULL, AV_LOG_INFO,"drop fetch playlist from server\n"); isNeedFetch = 0; } if(isNeedFetch ==0||(ret=list_open_internet(&bio,mgt,url,mgt->flags| URL_MINI_BUFFER | URL_NO_LP_BUFFER))!=0) { goto switchnext; } url_fclose(bio); if(mgt->current_item && mgt->current_item->file){/*current item,switch to next*/ current=mgt->current_item; next=mgt->current_item->next; for(;next!=NULL;next=next->next){ if(next->file && strcmp(current->file,next->file)==0){ /*found the same item,switch to the next*/ current=next; break; } } #if 0 while(current!=mgt->item_list){ /*del the old item,lest current,and then play current->next*/ list_del_item(mgt,mgt->item_list); } #endif mgt->current_item=current;/*switch to new current;*/ if(!mgt->have_list_end &&mgt->item_num>LIVE_LIST_MAX){ list_shrink_live_list(mgt); } } } switchnext: if(mgt->current_item) next=mgt->current_item->next; else next=mgt->item_list; if(next) av_log(NULL, AV_LOG_INFO, "switch to new file=%s,total=%d,start=%d,duration=%d\n", next->file,mgt->item_num,next->start_time,next->duration); else av_log(NULL, AV_LOG_INFO, "switch to new file=NULL,total=%d\n",mgt->item_num); return next; }
static struct list_item * switchto_next_item(struct list_mgt *mgt) { struct list_item *next = NULL; struct list_item *current = NULL; if (!mgt) { return NULL; } int isNeedFetch = 1; int64_t reload_interval = mgt->item_num > 0 && mgt->current_item != NULL ?\ mgt->current_item->duration:mgt->target_duration; if(reload_interval >0){ //to usec reload_interval *= 1000000; } if (mgt->n_variants > 0&&mgt->codec_buf_level>=0) { //vod,have mulit-bandwidth streams //av_log(NULL, AV_LOG_INFO, "current playing item index: %d,current playing seq:%d\n", mgt->playing_item_index, mgt->playing_item_seq); int is_switch = select_best_variant(mgt); if (is_switch>0) { //will remove this tricks. if (mgt->item_num > 0) { list_delall_item(mgt); } mgt->current_item = mgt->current_item->next = NULL; mgt->start_seq = -1; if(!mgt->have_list_end){ mgt->next_seq = mgt->playing_item_seq+1; }else{ mgt->next_seq = -1; } mgt->next_index = 0; mgt->item_num = 0; mgt->full_time = 0; if(mgt->cur_uio){ url_fclose(mgt->cur_uio); mgt->cur_uio = NULL; } if(mgt->debug_level==1){ hls_base_info_dump(mgt); } } if(mgt->debug_level>1){ hls_base_info_dump(mgt); } //av_log(NULL, AV_LOG_INFO, "select best variant,bandwidth: %d\n", mgt->playing_variant->bandwidth); } reload: if (mgt->current_item == NULL || mgt->current_item->next == NULL) { /*new refresh this mgtlist now*/ ByteIOContext *bio = mgt->cur_uio; int ret; char* url = NULL; if (mgt->n_variants > 0 && NULL != mgt->playing_variant) { url = mgt->playing_variant->url; //av_log(NULL, AV_LOG_INFO, "list open variant url:%s,bandwidth:%d\n", url, mgt->playing_variant->bandwidth); } else { url = mgt->filename; //av_log(NULL, AV_LOG_INFO, "list open url:%s\n", url); } if (!mgt->have_list_end && (av_gettime() - mgt->last_load_time < reload_interval)&&mgt->item_num>0) { //av_log(NULL, AV_LOG_INFO, "drop fetch playlist from server\n"); isNeedFetch = 0; } if (url_interrupt_cb()) { return NULL; } if (isNeedFetch == 0 || (ret = list_open_internet(&bio, mgt, url, mgt->flags | URL_MINI_BUFFER | URL_NO_LP_BUFFER)) != 0) { if(ret!=0&&mgt->n_variants>1){ switch_bw_level(mgt,-1); } goto switchnext; } mgt->cur_uio = bio; if (mgt->current_item && mgt->current_item->file) { /*current item,switch to next*/ current = mgt->current_item; next = mgt->current_item->next; for (; next != NULL; next = next->next) { if (next->file && strcmp(current->file, next->file) == 0) { /*found the same item,switch to the next*/ current = next; break; } } #if 0 while (current != mgt->item_list) { /*del the old item,lest current,and then play current->next*/ list_del_item(mgt, mgt->item_list); } #endif mgt->current_item = current; /*switch to new current;*/ if (!mgt->have_list_end && mgt->item_num > LIVE_LIST_MAX) { list_shrink_live_list(mgt); } } } switchnext: if (mgt->current_item) { next = mgt->current_item->next; } else { if (!mgt->have_list_end) { //live tv next = mgt->item_list; } else { next = list_find_next_item_by_index(mgt, mgt->playing_item_index); } } if (mgt->listclose){ return NULL; } if (next){ if(next->file!=NULL){ if(mgt->debug_level>1){ RLOG("Player switch to new item,url =%s,total item=%d,start=%.4lf,duration=%.4lf,index:%d,seq:%d\n", next->file, mgt->item_num, next->start_time, next->duration,next->index,next->seq); } } }else { if(mgt->debug_level>1){ RLOG("Player can't find new item,total=%d\n", mgt->item_num); } if (!mgt->have_list_end) { if (url_interrupt_cb()) { return NULL; } usleep(100 * 1000); reload_interval = mgt->target_duration * 500000; isNeedFetch = 1; goto reload; } } return next; }