【ijkplayer】read_thread 流程梳理(二)

37 篇文章 9 订阅
37 篇文章 2 订阅

ijkplayer

【ijkplayer】整体结构总结(一)

【ijkplayer】read_thread 流程梳理(二)

【ijkplayer】解码流程梳理(三)

【ijkplayer】渲染流程梳理(四)


【ijkplayer】read_thread 流程梳理

prepare方法中,主要分为两个流程

read_thread 负责打开流创建音视频解码线程读取packet等流程
video_refresh_thread,主要负责音视频同步,以及音视频渲染显示流程

这里把read_thread拿出来单独分析下

read_thread

位于ff_ffplay.c之中,先总结一下大概做的工作

  • 打开文件,检测Stream信息

  • 循环等待start接口调用,进入播放的流程

  • 打开音频播放器,创建音频解码线程audio_thread;

  • 创建视频解码器,创建视频解码线程video_thread;

  • 创建字幕解码线程subtitle_thread;

  • 循环读取Packet,解封装,并存入PacketQueue

删减代码,大概如下

static int read_thread(void *arg)
{
    // Open an input stream and read the header. The codecs are not opened.
    // The stream must be closed with avformat_close_input().
    // 打开输入流,并读取文件头部,解码器还未打开。主要作用是探测流的协议,如http还是rtmp等。
    err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);
  
    // Read packets of a media file to get stream information. This
    // is useful for file formats with no headers such as MPEG. This
    // function also computes the real framerate in case of MPEG-2 repeat
    // frame mode.
    // The logical file position is not changed by this function;
    // examined packets may buffered for later processing. 
    // 探测文件封装格式,音视频编码参数等信息。
    err = avformat_find_stream_info(ic, opts);
  
    // Find the "best" stream in the file.
    // The best stream is determined according to various heuristics as the most
    // likely to be what the user expects.
    // If the decoder parameter is non-NULL, av_find_best_stream will find the
    // default decoder for the stream's codec; streams for which no decoder can
    // be found are ignored.
    // 根据 AVFormatContext,找到最佳的流。
    av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                        st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
  
    // 内部分别开启audio,video,subtitle的解码器的线程,开始各自的解码的工作。解码线程中分析这里的内容
    stream_component_open(ffp, st_index[AVMEDIA_TYPE_AUDIO]);
    stream_component_open(ffp, st_index[AVMEDIA_TYPE_VIDEO]);
    stream_component_open(ffp, st_index[AVMEDIA_TYPE_SUBTITLE]);// 开始无限循环,调用ffmpeg的av_read_frame()读取AVPacket,并入队。
      for (;;) {
      //AVPacket pkt;
      
      ret = av_read_frame(ic, pkt);
      
      // 把网络读取到并解封装到的pkt包入队列。(稍后在解码线程会拿到这些pkt包去解码。)
      
      // 如果是音频流的包
      packet_queue_put(&is->audioq, pkt);
      // 如果是视频流的包
      packet_queue_put(&is->videoq, pkt);
      // 如果是字幕流的包
      packet_queue_put(&is->subtitleq, pkt);
    }  
}

分析

static int read_thread(void *arg) {
    AVFormatContext *ic = NULL;
    ic = avformat_alloc_context();

     /*
      *  1. 打开url
      */
    if (ffp->iformat_name)
        is->iformat = av_find_input_format(ffp->iformat_name);
    err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);

     /*
      *  2. 查找流
      */
    int i;
    int orig_nb_streams = ic->nb_streams;
    if (ffp->find_stream_info) {
        do {
            if (av_stristart(is->filename, "data:", NULL) && orig_nb_streams > 0) {
                for (i = 0; i < orig_nb_streams; i++) {
                    if (!ic->streams[i] || !ic->streams[i]->codecpar ||
                        ic->streams[i]->codecpar->profile == FF_PROFILE_UNKNOWN) {
                        break;
                    }
                }

                if (i == orig_nb_streams) {
                    break;
                }
            }
            err = avformat_find_stream_info(ic, opts);
        } while (0);
    }
    
    /*
     *  3.  遍历nb_streams,区分各个流,类似MediaExtractor中的trackIndex
     */
    int st_index[AVMEDIA_TYPE_NB];
    int video_stream_count = 0;
    int h264_stream_count = 0;
    int first_h264_stream = -1;

    for (i = 0; i < ic->nb_streams; i++) {
        AVStream *st = ic->streams[i];
        enum AVMediaType type = st->codecpar->codec_type;
        if (type >= 0 && ffp->wanted_stream_spec[type] && st_index[type] == -1)
            if (avformat_match_stream_specifier(ic, st, ffp->wanted_stream_spec[type]) > 0)
                st_index[type] = i;

        // choose first h264
        if (type == AVMEDIA_TYPE_VIDEO) {
            enum AVCodecID codec_id = st->codecpar->codec_id;
            video_stream_count++;
            if (codec_id == AV_CODEC_ID_H264) {
                h264_stream_count++;
                if (first_h264_stream < 0)
                    first_h264_stream = i;
            }
        }
    }

     /*
      *  4. 调用av_find_best_stream确定trackIndex,找到最佳流
      */
    if (video_stream_count > 1 && st_index[AVMEDIA_TYPE_VIDEO] < 0) {
        st_index[AVMEDIA_TYPE_VIDEO] = first_h264_stream;
    }
    if (!ffp->video_disable)
        st_index[AVMEDIA_TYPE_VIDEO] =
                av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                                    st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
    if (!ffp->audio_disable)
        st_index[AVMEDIA_TYPE_AUDIO] =
                av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                                    st_index[AVMEDIA_TYPE_AUDIO],
                                    st_index[AVMEDIA_TYPE_VIDEO],
                                    NULL, 0);
    if (!ffp->video_disable && !ffp->subtitle_disable)
        st_index[AVMEDIA_TYPE_SUBTITLE] =
                av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
                                    st_index[AVMEDIA_TYPE_SUBTITLE],
                                    (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
                                     st_index[AVMEDIA_TYPE_AUDIO] :
                                     st_index[AVMEDIA_TYPE_VIDEO]),
                                    NULL, 0);
    
     /*
      *  5. open the streams,读取各个数据流
      */
    if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
        stream_component_open(ffp, st_index[AVMEDIA_TYPE_AUDIO]);
    }
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        ret = stream_component_open(ffp, st_index[AVMEDIA_TYPE_VIDEO]);
    }
    if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
        stream_component_open(ffp, st_index[AVMEDIA_TYPE_SUBTITLE]);
    }

    if (!ffp->render_wait_start && !ffp->start_on_prepared)
        toggle_pause(ffp, 1);

	/*
     * 6. while循环等待start()调用,修改pause_req为0,进入读数据流程
     * start()必须在prepared后调用
	 */
	ffp->prepared = true;
    ffp_notify_msg1(ffp, FFP_MSG_PREPARED);
    if (!ffp->render_wait_start && !ffp->start_on_prepared) {
        while (is->pause_req && !is->abort_request) {
            SDL_Delay(20);
        }
    }

    /*
     *  7. 读数据,存入packet_queue
     */
    for (;;) {
        if (is->abort_request)
            break;

        if (is->seek_req) {
            //...
        }

        // 如果不是无限制大小的话,并且overSize || 队列都慢了,则睡10ms,等一下;控制缓冲区大小
        // #define MAX_QUEUE_SIZE (15 * 1024 * 1024) 15M
        overSize = is->audioq.size + is->videoq.size + is->subtitleq.size > ffp->dcc.max_buffer_size;
        if (ffp->infinite_buffer < 1 && !is->seek_req
            && (overSize ||
                (stream_has_enough_packets(is->audio_st, is->audio_stream, &is->audioq, MIN_FRAMES)                 
                 && stream_has_enough_packets(is->video_st, is->video_stream, &is->videoq, MIN_FRAMES)
                 && stream_has_enough_packets(is->subtitle_st, is->subtitle_stream, &is->subtitleq, MIN_FRAMES)))) {

            /* wait 10 ms */
            SDL_LockMutex(wait_mutex);
            SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
            SDL_UnlockMutex(wait_mutex);
            continue;
        }

		// 判断是否播放结束
		if ((!is->paused || completed) &&
            (!is->audio_st || (is->auddec.finished == is->audioq.serial && frame_queue_nb_remaining(&is->sampq) == 0)) &&
            (!is->video_st || (is->viddec.finished == is->videoq.serial && frame_queue_nb_remaining(&is->pictq) == 0))) {
            

            if (ffp->loop != 1 && (!ffp->loop || --ffp->loop)) {
                // 循环播放几次,从1开始计算
                stream_seek(is, ffp->start_time != AV_NOPTS_VALUE ? ffp->start_time : 0, 0, 0);
            } else if (ffp->autoexit) {
            	// 自动结束,走销毁逻辑
                ret = AVERROR_EOF;
                goto fail;
            } else {
                ffp_statistic_l(ffp);
                if (completed) {
                    av_log(ffp,
                           AV_LOG_INFO, "ffp_toggle_buffering: eof\n");
                    SDL_LockMutex(wait_mutex);

					// 结束了,一直等
                    while (!is->abort_request && !is->seek_req)
                        SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 100);
                    SDL_UnlockMutex(wait_mutex);
                    if (!is->abort_request)
                        continue;
                } else {
                	// 第一次进来是0,再次循环走上面if逻辑
                    completed = 1;
                    ffp->auto_resume = 0;

                    ffp_toggle_buffering(ffp,0);
                    toggle_pause(ffp,
                                 1);
                    if (ffp->error) {
                        ffp_notify_msg1(ffp, FFP_MSG_ERROR);
                    } else {
                        ffp_notify_msg1(ffp, FFP_MSG_COMPLETED);
                    }
                }
            }
        }

        // 读取数据,存入packet_queue
        int ret = av_read_frame(ic, pkt);

        if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
	        // 音频
            packet_queue_put(&is->audioq, pkt);
            SDL_SpeedSampler2Add(&ffp->stat.audio_bit_rate_sampler, pkt->size);
        } else if (pkt->stream_index == is->video_stream 
        			&& pkt_in_play_range 
        			&& !(is->video_st && (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))) { 
        	// 视频
            packet_queue_put(&is->videoq, pkt);
            SDL_SpeedSampler2Add(&ffp->stat.video_bit_rate_sampler, pkt->size);
        } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
	        // 字幕
            packet_queue_put(&is->subtitleq, pkt);
        } else {
            av_packet_unref(pkt);
        }

        /*
         * 开启缓冲机制
         * 在for循环中,每读到一个包,都会检查是否进行缓冲
         */
		if (ffp->packet_buffering) {
            io_tick_counter = SDL_GetTickHR();
            if ((!ffp->first_video_frame_rendered && is->video_st) ||
                (!ffp->first_audio_frame_rendered && is->audio_st)) {
                // 首帧未显示前,50ms检测一次
                if (abs((int) (io_tick_counter - prev_io_tick_counter)) > FAST_BUFFERING_CHECK_PER_MILLISECONDS) {
                    prev_io_tick_counter = io_tick_counter;
                    ffp->dcc.current_high_water_mark_in_ms = ffp->dcc.first_high_water_mark_in_ms;
                    ffp_check_buffering_l(ffp);
                }
            } else {
                if (abs((int) (io_tick_counter - prev_io_tick_counter)) > BUFFERING_CHECK_PER_MILLISECONDS) {
                    prev_io_tick_counter = io_tick_counter;
                    ffp_check_buffering_l(ffp);
                }
            }
        }
    }
}

完整代码如下
在这里插入图片描述

/* this thread gets the stream from the disk or the network */
static int read_thread(void *arg)
{
    inke_ijk_log(true, true, true, "ljc read_thread 1\n");
    FFPlayer *ffp = arg;
    VideoState *is = ffp->is;
    AVFormatContext *ic = NULL;
    int err = 0, i, ret __unused;
    int st_index[AVMEDIA_TYPE_NB];
    AVPacket pkt1, *pkt = &pkt1;
    int64_t stream_start_time;
    int completed = 0;
    int pkt_in_play_range = 0;
    AVDictionaryEntry *t;
    AVDictionary **opts;
    int orig_nb_streams;
    SDL_mutex *wait_mutex = SDL_CreateMutex();
    int scan_all_pmts_set = 0;
    int64_t pkt_ts;
    int last_error = 0;
    int64_t prev_io_tick_counter = 0;
    int64_t io_tick_counter = 0;
    #if defined(ENABLE_KRONOS_STREAM) || defined(ENABLE_QUIC_FLV_STREAM)
    // Kronos Stream
    int isKronosStream = 0;
    int isKronosFirstBuffering = 1;
    int kronosBufferingThreshold = 2;
    KronosMediaStream *kronos_media_stream = NULL;
    ffp->kronos_media_stream = NULL;
    ffp->inke_kronos_playback_pts = 0LL;
    ffp->inke_kronos_packet_duration = 0.0f;
    ffp->inke_sync_multi_playback_start_flag = 0;
    ffp->inke_sync_multi_playback_start_pts = 0LL;
    // QUIC FLV Stream
    #if defined(ENABLE_QUIC_FLV_STREAM)
    int isQuicFlvStream = 0;
    struct QuicFlvStream *quic_flv_stream = NULL;
    ffp->quic_flv_stream = NULL;
    #endif // defined(ENABLE_QUIC_FLV_STREAM)
    // FFmpeg AVIO
    uint8_t *avio_ctx_buffer = NULL;
    const size_t avio_ctx_buffer_size = 4096;
    AVIOContext *avio_ctx = NULL;

    // CDN cache monitor
    #define INKE_CDN_CACHE_MONITOR_DURATION 3000000LL
    struct {
        int     report_flag;
        int64_t video_frame_count;
        int64_t video_fps;
        int64_t data_size;
        int64_t pts_base_internal;
        int64_t pts_diff;
        int32_t zero_pts_frame_count;
        int64_t *sei_diff;
        int64_t *stream_connected_ts;
    } inke_cdn_cache_monitor;
    memset(&inke_cdn_cache_monitor, 0, sizeof(inke_cdn_cache_monitor));
    memset(&ffp->debug_info.cdn_cache_info, 0, sizeof(ffp->debug_info.cdn_cache_info));
    memset(&ffp->debug_info.cdn_cache_info, '-', 3);
    inke_cdn_cache_monitor.pts_base_internal = -1LL;
    ffp->inke_cdn_monitor_sei_diff = -1LL;
    inke_cdn_cache_monitor.sei_diff = &ffp->inke_cdn_monitor_sei_diff;
    inke_cdn_cache_monitor.stream_connected_ts = &ffp->debug_info.connect_end_time;

    if (ffp->inke_kronos_stream == 0LL) {
        if ((is != NULL) && (is->filename != NULL) \
            && (strncmp(is->filename, KronosProtocol, strlen(KronosProtocol)) == 0)) {
            ffp->inke_kronos_stream = 1LL;
        }
    }
    if (ffp->inke_kronos_stream != 0LL) {
        isKronosStream = 1;
        ffp->inke_live_stream = 1;
    }
    
    #if defined(ENABLE_QUIC_FLV_STREAM)
    if ((is != NULL) && (is->filename != NULL)
        && (strncmp(is->filename, QuicFlvProtocol, strlen(QuicFlvProtocol)) == 0)) {
        isQuicFlvStream = 1;
        ffp->inke_live_stream = 1;
    }
    #endif // defined(ENABLE_QUIC_FLV_STREAM)
    #endif // #if defined(ENABLE_KRONOS_STREAM) || defined(ENABLE_QUIC_FLV_STREAM)

    if (!wait_mutex) {
        av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError());
        inke_ijk_log(true, true, true, "SDL_CreateMutex(): %s  filename:%s \n", SDL_GetError(),is->filename);
        ret = AVERROR(ENOMEM);
        goto fail;
    }

    memset(st_index, -1, sizeof(st_index));
    is->last_video_stream = is->video_stream = -1;
    is->last_audio_stream = is->audio_stream = -1;
    is->last_subtitle_stream = is->subtitle_stream = -1;
    is->eof = 0;
#ifdef INKE_EXTENSION
    if ((is != NULL) && (is->filename != NULL)
        && ((strncmp(is->filename, QuicFlvProtocol, strlen(QuicFlvProtocol)) == 0) || strcasestr(is->filename, ".flv") != NULL)) {
        ffp->inke_cdn_stream = 1;
    }

    if ((is != NULL) && (is->filename != NULL)) {
        const char *urlParam = strchr(is->filename, '?');
        if ((urlParam == NULL) || (strcasestr(urlParam, "ikSyncBeta=") == NULL)) {
            ffp->inke_kronos_roomid = 0LL;
            ffp->inke_kronos_room_timestamp = 0LL;
            ffp->inke_kronos_master_flag = false;
        }
    }
    if ((ffp->inke_kronos_roomid != 0LL) \
        && (ffp->inke_kronos_room_timestamp != 0LL)) {
        SyncMultiAddInstance(ffp, ffp->inke_kronos_roomid, \
                             ffp->inke_kronos_room_timestamp, ffp->inke_kronos_master_flag, ffp->inke_kronos_stream_type);
    }
    if (PIStreamPullNodePref(ffp) != 0) {
        //av_log(NULL, AV_LOG_ERROR, "no chorus stream network preference optimize\n");
    }
    inke_read_frame_time = av_gettime();
	//TODO:: powerinfo delete this code (zhouliwei)
    ffp->audio_callback_time = av_gettime_relative();

#endif // INKE_EXTENSION
#ifdef INKE_DEBUG_IJKPLAYER
    ffp->aout->previousLogTime = 0LL;
    ffp->aout->previousAudioQueueLogTime = 0LL;
    ffp->vout->previousLogTime = 0.0f;
    ffp->vout->network_frame_count = 0LL;
    ffp->vout->decode_frame_count = 0LL;
    ffp->vout->decode_fps = 0;
    ffp->vout->decode_previous_log_time = 0LL;
    snprintf(ffp->aout->streamUrl, sizeof(ffp->aout->streamUrl), "%s", ffp->input_filename);
    snprintf(ffp->vout->streamUrl, sizeof(ffp->vout->streamUrl), "%s", ffp->input_filename);
#endif // INKE_DEBUG_IJKPLAYER

    ic = avformat_alloc_context();
    if (!ic) {
        av_log(NULL, AV_LOG_FATAL, "Could not allocate context.\n");
        inke_ijk_log(true, true, true, "Could not allocate context.  filename:%s\n",is->filename);
        ret = AVERROR(ENOMEM);
        goto fail;
    }
    ic->interrupt_callback.callback = decode_interrupt_cb;
    ic->interrupt_callback.opaque = is;
    //huangyao
    if(!listIsCreated){
        inke_ijk_log(true, true, true, "huangyao +++ Init_Array \n");
        arr = Init_Array();
        listIsCreated = true;
    }
    if(arr != NULL){
        VideoStateNode* videoStateNode = (VideoStateNode *)malloc(sizeof(VideoStateNode));
        videoStateNode->is = is;
        videoStateNode->state = 1;
        inke_ijk_log(true, true, true, "huangyao +++ PushBack_Array \n");
        PushBack_Array(arr, videoStateNode);
    }
    
    
    if (!av_dict_get(ffp->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
        av_dict_set(&ffp->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
        scan_all_pmts_set = 1;
    }
    if (av_stristart(is->filename, "rtmp", NULL) ||
        av_stristart(is->filename, "rtsp", NULL)) {
        // There is total different meaning for 'timeout' option in rtmp
        av_log(ffp, AV_LOG_WARNING, "remove 'timeout' option for rtmp.\n");
        av_dict_set(&ffp->format_opts, "timeout", NULL, 0);
    }
    if (ffp->iformat_name)
        is->iformat = av_find_input_format(ffp->iformat_name);
    ffp->debug_info.player_open_input_time = av_gettime();
    #if !defined(ENABLE_KRONOS_STREAM) && !defined(ENABLE_QUIC_FLV_STREAM)
    err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);
    #else
    if (isKronosStream == 1) {
        if (KronosCreateInstance(&kronos_media_stream, ffp) != 0) {
            inke_ijk_log(true, true, true, "KronosCreateInstance fail!. filename:%s \n",is->filename);
            ret = -1;
            goto fail;
        }
        char ip[128] = {'\0'};
        int  port = 0;
        uint32_t localASsrc = 0;
        uint32_t localVSsrc = 0;
        uint32_t  remoteASsrc = 0;
        char roomID[128] = {'\0'};
        bool isFM = (ffp->inke_kronos_fm!=0);
        inke_ijk_log(true, true, true, "setAudioOnly isFM=%d,val=%d filename:%s \n",isFM,ffp->inke_kronos_fm,is->filename);
        //krns://ip:port/channel/stream_id?L_SSRC_A=x&L_SSRC_V=x&R_SSRC_A=x&R_SSRC_V=x
        sscanf(is->filename,"%*[^0-9]%[^:]:%d/%*[^0-9]%[^?]%*[^0-9]%u%*[^0-9]%u%*[^0-9]%u",ip,&port,roomID,&localASsrc,&localVSsrc,&remoteASsrc);
        err = KronosMediaStreamSetLocalSSRC(kronos_media_stream, localASsrc, localASsrc+1);
        err += KronosMediaStreamSetRemoteSSRC(kronos_media_stream, remoteASsrc, remoteASsrc+1);
        err += KronosMediaStreamSetServerInfo(kronos_media_stream, ip, port);
        err += KronosMediaStreamSetAudioOnly(kronos_media_stream,isFM);
        err += KronosMediaStreamStartup(kronos_media_stream);
        if (err != 0) {
            assert(!"create kronos stream instance error");
            ret = -1;
            goto fail;
        }
        ffp->kronos_media_stream = kronos_media_stream;
        
        avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
        if (avio_ctx_buffer == NULL) {
            inke_ijk_log(true, true, true, "malloc avio_ctx_buffer fail filename:%s.\n",is->filename);
            ret = -1;
            goto fail;
        }
        avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, kronos_media_stream, kronos_read_packet, NULL, NULL);
        if (avio_ctx == NULL) {
            inke_ijk_log(true, true, true, "avio_alloc_context fail avio_ctx_buffer_size:%d\n",avio_ctx_buffer_size);
            ret = -1;
            goto fail;
        }
        ic->pb = avio_ctx;
        inke_ijk_log(true, true, true, "ljc read_thread krns open1\n");
        err = avformat_open_input(&ic, NULL, NULL, NULL);
        inke_ijk_log(true, true, true, "ljc read_thread krns open2\n");
    }
    #if defined(ENABLE_QUIC_FLV_STREAM)
    else if (isQuicFlvStream == 1) {
        if (QuicFlvCreateInstance(&quic_flv_stream, ffp) != 0) {
            ret = -1;
            goto fail;
        }
        if (QuicFlvStreamStartup(quic_flv_stream) != 0) {
            ret = -1;
            goto fail;
        }
        ffp->quic_flv_stream = quic_flv_stream;
        
        avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
        if (avio_ctx_buffer == NULL) {
            ret = -1;
            goto fail;
        }
        avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, quic_flv_stream, quic_flv_read_packet, NULL, NULL);
        if (avio_ctx == NULL) {
            ret = -1;
            goto fail;
        }
        ic->pb = avio_ctx;
        err = avformat_open_input(&ic, NULL, NULL, NULL);
    }
    #endif // defined(ENABLE_QUIC_FLV_STREAM)
    //for memory play
    else if ((is != NULL) && (is->filename != NULL)
             && (strncmp(is->filename, pipeProtocol, strlen(pipeProtocol)) == 0)) {
        avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
        if (avio_ctx_buffer == NULL) {
            ret = -1;
            goto fail;
        }
        
        struct memory_buffer_data mbuffer_data = { 0 };
        
        mbuffer_data.ptr = ffp->memory_data;
        mbuffer_data.size = ffp->memory_data_size;
        avio_ctx = avio_alloc_context(avio_ctx_buffer,
                                      avio_ctx_buffer_size,
                                      0,
                                      &mbuffer_data,
                                      memory_read_packet,
                                      NULL, NULL);
        if (avio_ctx == NULL) {
            ret = -1;
            goto fail;
        }
        ic->pb = avio_ctx;
        err = avformat_open_input(&ic, NULL, NULL, NULL);
    }
    else {
        err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);
    }
    #endif // !defined(ENABLE_KRONOS_STREAM) && !defined(ENABLE_QUIC_FLV_STREAM)
    if (err < 0) {
        //print_error(is->filename, err);//ljc 这个会崩溃
        char errbuf[128];
        const char *errbuf_ptr = errbuf;
        if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
            errbuf_ptr = strerror(AVUNERROR(err));
        inke_ijk_log(true, true, true, "ljc avformat_open_input ret=%d, %s: %s\n",err, is->filename, errbuf_ptr);
        ret = -1;

        goto fail;
    }
    if (scan_all_pmts_set)
        av_dict_set(&ffp->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);

    if ((t = av_dict_get(ffp->format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
        av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
#ifdef FFP_MERGE
        ret = AVERROR_OPTION_NOT_FOUND;
        goto fail;
#endif
    }
    is->ic = ic;

        av_log(NULL, AV_LOG_INFO,"filename not pzsp: %s",is->filename);

    if (ffp->genpts)
        ic->flags |= AVFMT_FLAG_GENPTS;

    av_format_inject_global_side_data(ic);

    opts = setup_find_stream_info_opts(ic, ffp->codec_opts);
    orig_nb_streams = ic->nb_streams;
    
#ifdef INKE_EXTENSION
        if (ffp->inke_live_stream == 1) {
            ic->probesize = 5000000; // TODO: wait for PowerInfo fix bug
            int64_t default_probo_size = ic->probesize;
            default_probo_size = default_probo_size > 5000000 ? default_probo_size : 5000000;
            if (strstr(is->filename, "&wsiphost=ipdbm") != NULL) {
                ic->probesize = 8192;
            }
            #define INKE_EXTENSION_CODEC_INFO "CodecInfo="
            char* codec_position = strcasestr(is->filename, INKE_EXTENSION_CODEC_INFO);
            if (codec_position != NULL) {
                codec_position += strlen(INKE_EXTENSION_CODEC_INFO);
                ic->probesize = atol(codec_position);
                ic->probesize = (ic->probesize > 0LL) && (ic->probesize <= default_probo_size) ? ic->probesize : default_probo_size;
            }
            #ifdef ENABLE_KRONOS_STREAM
            if (isKronosStream == 1) {
                ic->probesize = 4096;
            }
            #endif // ENABLE_KRONOS_STREAM
        }
#endif // INKE_EXTENSION
    err = avformat_find_stream_info(ic, opts);
    ffp->debug_info.open_success_time = av_gettime();
    
    
    
    for (i = 0; i < orig_nb_streams; i++)
        av_dict_free(&opts[i]);
    av_freep(&opts);

    if (err < 0) {
        av_log(NULL, AV_LOG_WARNING,
               "%s: could not find codec parameters\n", is->filename);
        inke_ijk_log(true, true, true, "%s: could not find codec parameters\n", is->filename);
        ret = -1;
        goto fail;
    }

    if (ic->pb)
        ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use avio_feof() to test for the end

    if (ffp->seek_by_bytes < 0)
        ffp->seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);

    is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
    is->max_frame_duration = 10.0;
    av_log(ffp, AV_LOG_INFO, "max_frame_duration: %.3f\n", is->max_frame_duration);

#ifdef FFP_MERGE
    if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
        window_title = av_asprintf("%s - %s", t->value, input_filename);

#endif
    /* if seeking requested, we execute it */
    if (ffp->start_time != AV_NOPTS_VALUE) {
        int64_t timestamp;

        timestamp = ffp->start_time;
        /* add the stream start time */
        if (ic->start_time != AV_NOPTS_VALUE)
            timestamp += ic->start_time;
        ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
        if (ret < 0) {
            av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
                    is->filename, (double)timestamp / AV_TIME_BASE);
        }
    }

    is->realtime = is_realtime(ic);

    if (true || ffp->show_status)
        av_dump_format(ic, 0, is->filename, 0);

    int video_stream_count = 0;
    int h264_stream_count = 0;
    int first_h264_stream = -1;
    inke_ijk_log(true, true, true, "ljc ic->nb_streams:%d\n", ic->nb_streams);
    for (i = 0; i < ic->nb_streams; i++) {
        AVStream *st = ic->streams[i];
        enum AVMediaType type = st->codecpar->codec_type;
        st->discard = AVDISCARD_ALL;
        if (type >= 0 && ffp->wanted_stream_spec[type] && st_index[type] == -1)
            if (avformat_match_stream_specifier(ic, st, ffp->wanted_stream_spec[type]) > 0)
                st_index[type] = i;

        // choose first h264

        if (type == AVMEDIA_TYPE_VIDEO) {
            enum AVCodecID codec_id = st->codecpar->codec_id;
            video_stream_count++;
            if (codec_id == AV_CODEC_ID_H264) {
                h264_stream_count++;
                if (first_h264_stream < 0)
                    first_h264_stream = i;
            }
            ffp->debug_info.has_video = 1;
        }else if(type == AVMEDIA_TYPE_AUDIO){
            ffp->debug_info.has_audio = 1;
        }
    }
    if (video_stream_count > 1 && st_index[AVMEDIA_TYPE_VIDEO] < 0) {
        st_index[AVMEDIA_TYPE_VIDEO] = first_h264_stream;
        av_log(NULL, AV_LOG_WARNING, "multiple video stream found, prefer first h264 stream: %d\n", first_h264_stream);
    }
    if (!ffp->video_disable)
        st_index[AVMEDIA_TYPE_VIDEO] =
            av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                                st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
    if (!ffp->audio_disable)
        st_index[AVMEDIA_TYPE_AUDIO] =
            av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                                st_index[AVMEDIA_TYPE_AUDIO],
                                st_index[AVMEDIA_TYPE_VIDEO],
                                NULL, 0);
    if (!ffp->video_disable && !ffp->subtitle_disable)
        st_index[AVMEDIA_TYPE_SUBTITLE] =
            av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
                                st_index[AVMEDIA_TYPE_SUBTITLE],
                                (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
                                 st_index[AVMEDIA_TYPE_AUDIO] :
                                 st_index[AVMEDIA_TYPE_VIDEO]),
                                NULL, 0);

    is->show_mode = ffp->show_mode;
#ifdef FFP_MERGE // bbc: dunno if we need this
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];
        AVCodecParameters *codecpar = st->codecpar;
        AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
        if (codecpar->width)
            set_default_window_size(codecpar->width, codecpar->height, sar);
    }
#endif

    /* open the streams */
    if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
        ret = stream_component_open(ffp, st_index[AVMEDIA_TYPE_AUDIO]);
    }
    if(!ffp->scale_tempo_info.isrunning){
        ffp->scale_tempo_info.in_pcm_format = VLC_PCM_S16L;
        ffp->scale_tempo_info.out_pcm_format = VLC_PCM_S16L;
        scaletempo_start(&ffp->scale_tempo_info, ffp->is->audio_tgt.freq, ffp->is->audio_tgt.channels, 16);
    }
    ret = -1;
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        ret = stream_component_open(ffp, st_index[AVMEDIA_TYPE_VIDEO]);
    }
    if (is->show_mode == SHOW_MODE_NONE)
        is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;

    if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
        stream_component_open(ffp, st_index[AVMEDIA_TYPE_SUBTITLE]);
    }

    ijkmeta_set_avformat_context_l(ffp->meta, ic);
    ffp->stat.bit_rate = ic->bit_rate;
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0)
        ijkmeta_set_int64_l(ffp->meta, IJKM_KEY_VIDEO_STREAM, st_index[AVMEDIA_TYPE_VIDEO]);
    if (st_index[AVMEDIA_TYPE_AUDIO] >= 0)
        ijkmeta_set_int64_l(ffp->meta, IJKM_KEY_AUDIO_STREAM, st_index[AVMEDIA_TYPE_AUDIO]);
    if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0)
        ijkmeta_set_int64_l(ffp->meta, IJKM_KEY_TIMEDTEXT_STREAM, st_index[AVMEDIA_TYPE_SUBTITLE]);

    if (is->video_stream < 0 && is->audio_stream < 0) {
        //av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",is->filename);
        inke_ijk_log(true, true, true, "Failed to open file '%s' or configure filtergraph\n",
        is->filename);
        ret = -1;
        goto fail;
    }

after_stream_comp_open:

    if (is->audio_stream >= 0) {
        is->audioq.is_buffer_indicator = 1;
        is->buffer_indicator_queue = &is->audioq;
    } else if (is->video_stream >= 0) {
        is->videoq.is_buffer_indicator = 1;
        is->buffer_indicator_queue = &is->videoq;
    } else {
        assert("invalid streams");
    }

    if (ffp->infinite_buffer < 0 && is->realtime)
        ffp->infinite_buffer = 1;

    if (!ffp->start_on_prepared)
        toggle_pause(ffp, 1);
    if (is->video_st && is->video_st->codecpar) {
        AVCodecParameters *codecpar = is->video_st->codecpar;
        ffp_notify_msg3(ffp, FFP_MSG_VIDEO_SIZE_CHANGED, codecpar->width, codecpar->height);
        ffp_notify_msg3(ffp, FFP_MSG_SAR_CHANGED, codecpar->sample_aspect_ratio.num, codecpar->sample_aspect_ratio.den);
#ifdef INKE_EXTENSION
        if(codecpar->width > codecpar->height)
        {
            inke_ijkplayer_msg_hook(ffp, 0x1500/*STREAM_RATIO_16_TO_9*/);
        }
#endif // INKE_EXTENSION
    }
    ffp->prepared = true;
    ffp_notify_msg1(ffp, FFP_MSG_PREPARED);
    if (!ffp->start_on_prepared) {
        while (is->pause_req && !is->abort_request) {
            SDL_Delay(100);
        }
    }
    ALOGI("ffp(%p) start after prepared", ffp);

    if (ffp->auto_resume) {
        ffp_notify_msg1(ffp, FFP_REQ_START);
        ffp->auto_resume = 0;
    }
    /* offset should be seeked*/
    if (ffp->seek_at_start > 0) {
        ffp_seek_to_l(ffp, ffp->seek_at_start);
    }

#ifdef INKE_EXTENSION
    #define INKE_INIT_RESERVE_DATA_TIME 3600    // units:ms
    #define INKE_MUST_DROP_DATA_TIME    8000    // units:ms
    #define INKE_AUDIO_BUFFER_TIME      1600    // units:ms
    #define INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT 15
    #define INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT 8
    #define INKE_PLAYBACK_BUFFER_AUDIO_PACKET_COUNT 50
    #define INKE_PLAYBACK_BUFFER_VIDEO_PACKET_COUNT 18
    #define INKE_PLAYBACK_BUFFER_AUDIO_MIN_THRESHOLD 2
    #define INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD 200
    //int video_fps = 24; // TODO: read from meta
    int64_t last_av_frame_base_time = av_gettime();
    //int audio_reserve_time = INKE_INIT_RESERVE_DATA_TIME * 1000; // TODO: danymic adjust reserve time
    int standard_audio_frame_interval = 0;
    int audio_last_delay_second = 0; //units:s
    int audio_queue_data_stability_time = 0; //units:ms
    int64_t av_frame_drop_until_pts = 0LL; //units:us
    int  display_initialize_buffering_time = 360; //units:ms
    bool drop_key_frame = false;
    bool powerinfo_has_reset_player_flag = false;
    int inke_buffer_audio_packet_count = INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT;
    int inke_buffer_video_packet_count = INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT;
    int64_t kronosLastReadTime = av_gettime_relative();
    srand((unsigned int)time(NULL));
    if (ffp->inke_live_stream == 1) {
        ffp->infinite_buffer = 1;
    }
    // quality
    int64_t quality_start_time = av_gettime();
    int64_t quality_total_network_data_size = 0LL;
    int64_t quality_fps_base_time = av_gettime();
    int     quality_fps_frame_count = 0;
    int64_t stream_start_playback_time = av_gettime();
    display_fps_base_time = av_gettime();
    ffp->audio_playback_alive_time = av_gettime();
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        AVRational avg_frame_rate = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]]->avg_frame_rate;
        inke_cdn_cache_monitor.video_fps = avg_frame_rate.num / (avg_frame_rate.den > 0 ? avg_frame_rate.den : 1);
    }
    bool isOnlyHasAudio = true;
    if(is->video_st && is->video_st->codecpar && is->video_st->codecpar->width > 16 && is->video_st->codecpar->height > 16){
        isOnlyHasAudio = false;
    }
#endif // INKE_EXTENSION
    int64_t read_loop_log_time = 0;
    int64_t kronosReadBlockTime = av_gettime_relative();
    int64_t kronosReadTime = av_gettime_relative();
    //read_thread_step 8  重复6、7不断获取待播放的数据
    for (;;) {
        if (is->abort_request)
            break;

#ifdef FFP_MERGE
        if (is->paused != is->last_paused) {
            is->last_paused = is->paused;
            if (is->paused)
                is->read_pause_return = av_read_pause(ic);
            else
                av_read_play(ic);
        }
#endif
#if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
        if (is->paused &&
                (!strcmp(ic->iformat->name, "rtsp") ||
                 (ic->pb && !strncmp(ffp->input_filename, "mmsh:", 5)))) {
            /* wait 10 ms to avoid trying to get another packet */
            /* XXX: horrible */
            SDL_Delay(10);
            continue;
        }
#endif
        if (is->seek_req) {
            inke_ijk_log(true, true, false, "[zlw] is->seek_req:%d", is->seek_req);
            int64_t seek_target = is->seek_pos;
            int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
            int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
// FIXME the +-2 is due to rounding being not done in the correct direction in generation
//      of the seek_pos/seek_rel variables

            ffp_toggle_buffering(ffp, 1);
            ffp_notify_msg3(ffp, FFP_MSG_BUFFERING_UPDATE, 0, 0);
            inke_ijk_log(true, true, false, "[zlw] avformat_seek_file start");
            ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
            inke_ijk_log(true, true, false, "[zlw] avformat_seek_file end");
            if (ret < 0) {
                av_log(NULL, AV_LOG_ERROR,
                       "%s: error while seeking\n", is->ic->filename);

#ifdef INKE_EXTENSION
                ffp_notify_msg1(ffp, FFP_MSG_SEEK_COMPLETE);
#endif // INKE_EXTENSION
            } else {
                if (is->audio_stream >= 0) {
                    packet_queue_flush(&is->audioq);
                    packet_queue_put(&is->audioq, &flush_pkt);
                }
                if (is->subtitle_stream >= 0) {
                    packet_queue_flush(&is->subtitleq);
                    packet_queue_put(&is->subtitleq, &flush_pkt);
                }
                if (is->video_stream >= 0) {
                    if (ffp->node_vdec) {
                        ffpipenode_flush(ffp->node_vdec);
                    }
                    packet_queue_flush(&is->videoq);
                    packet_queue_put(&is->videoq, &flush_pkt);
                }
                if (is->seek_flags & AVSEEK_FLAG_BYTE) {
                   set_clock(&is->extclk, NAN, 0);
                } else {
                   set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
                }

                is->latest_seek_load_serial = is->videoq.serial;
                is->latest_seek_load_start_at = av_gettime();
            }
            ffp->dcc.current_high_water_mark_in_ms = ffp->dcc.first_high_water_mark_in_ms;
            is->seek_req = 0;
            is->queue_attachments_req = 1;
            is->eof = 0;
#ifdef FFP_MERGE
            if (is->paused)
                step_to_next_frame(is);
#endif
            completed = 0;
            SDL_LockMutex(ffp->is->play_mutex);
            if (ffp->auto_resume) {
                is->pause_req = 0;
                if (ffp->packet_buffering)
                    is->buffering_on = 1;
                ffp->auto_resume = 0;
                stream_update_pause_l(ffp);
            }
            if (is->pause_req)
                step_to_next_frame_l(ffp);
            SDL_UnlockMutex(ffp->is->play_mutex);
            ffp_notify_msg3(ffp, FFP_MSG_SEEK_COMPLETE, (int)fftime_to_milliseconds(seek_target), ret);
            inke_ijk_log(true, true, false, "[zlw] FFP_MSG_SEEK_COMPLETE");
            ffp_toggle_buffering(ffp, 1);
        }
        if (is->queue_attachments_req) {
            if (is->video_st && (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
                AVPacket copy;
                if ((ret = av_copy_packet(&copy, &is->video_st->attached_pic)) < 0)
                    goto fail;
                packet_queue_put(&is->videoq, &copy);
                packet_queue_put_nullpacket(&is->videoq, is->video_stream);
            }
            is->queue_attachments_req = 0;
        }

        /* if the queue are full, no need to read more */
        if (ffp->infinite_buffer<1 && !is->seek_req &&
#ifdef FFP_MERGE
              (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
#else
              (is->audioq.size + is->videoq.size + is->subtitleq.size > ffp->dcc.max_buffer_size
#endif
            || (   stream_has_enough_packets(is->audio_st, is->audio_stream, &is->audioq, MIN_FRAMES)
                && stream_has_enough_packets(is->video_st, is->video_stream, &is->videoq, MIN_FRAMES)
                && stream_has_enough_packets(is->subtitle_st, is->subtitle_stream, &is->subtitleq, MIN_FRAMES)))) {
            if (!is->eof) {
                ffp_toggle_buffering(ffp, 0);
            }
            /* wait 10 ms */
            SDL_LockMutex(wait_mutex);
            SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
            SDL_UnlockMutex(wait_mutex);
            continue;
        }
        if ((!is->paused || completed) &&
            (!is->audio_st || (is->auddec.finished == is->audioq.serial && frame_queue_nb_remaining(&is->sampq) == 0))
            &&(!is->video_st || (is->viddec.finished == is->videoq.serial && frame_queue_nb_remaining(&is->pictq) == 0)
            || (is->video_st->codecpar->width == 16 && is->video_st->codecpar->height == 16))) {
            if (ffp->loop != 1 && (!ffp->loop || --ffp->loop)) {
                ffp->seek_to_start_by_loop = 1;
                stream_seek(is, ffp->start_time != AV_NOPTS_VALUE ? ffp->start_time : 0, 0, 0);
                inke_buffer_audio_packet_count = 10;//INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT;
                inke_buffer_video_packet_count = 3;//INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT;
                ffp_notify_msg1(ffp, INKE_STREAM_RESTART_PLAY);
            } else if (ffp->autoexit) {
                ret = AVERROR_EOF;
                goto fail;
            } else {
                ffp_statistic_l(ffp);
                if (completed) {
                    av_log(ffp, AV_LOG_INFO, "ffp_toggle_buffering: eof\n");
                    SDL_LockMutex(wait_mutex);
                    // infinite wait may block shutdown
                    while(!is->abort_request && !is->seek_req)
                        SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 100);
                    SDL_UnlockMutex(wait_mutex);
                    if (!is->abort_request)
                        continue;
                } else {
                    completed = 1;
                    ffp->auto_resume = 0;

                    // TODO: 0 it's a bit early to notify complete here
                    ffp_toggle_buffering(ffp, 0);
#ifndef INKE_EXTENSION
                    toggle_pause(ffp, 1);//LYN
#endif // INKE_EXTENSION
                    if (ffp->error) {
                        av_log(ffp, AV_LOG_INFO, "ffp_toggle_buffering: error: %d\n", ffp->error);
                        ffp_notify_msg1(ffp, FFP_MSG_ERROR);
                    } else {
                        av_log(ffp, AV_LOG_INFO, "ffp_toggle_buffering: completed: OK\n");
                        ffp_notify_msg1(ffp, FFP_MSG_COMPLETED);
                    }
#ifdef INKE_EXTENSION
                    toggle_pause(ffp, 1);//LYN
#endif // INKE_EXTENSION
                }
            }
        }
        pkt->flags = 0;
        
        ret = av_read_frame(ic, pkt);
        
        int minRiseVSize = 0;
        int minDeclineVSize = 0;
        double standardDecline = 0;
        uint16_t jitter_audio_count = 0;
    
        
        if(isKronosStream && ffp->kronos_media_stream && !isOnlyHasAudio){
            minRiseVSize = getMinVideoQueueSize(is->video_queue_size_rise, VIDEO_ARRAY_COUNT);
            minDeclineVSize = getMinVideoQueueSize(is->video_queue_size_decline, VIDEO_ARRAY_COUNT * 3);
            standardDecline = getStandardVideoQueueSize(is->video_queue_size_decline, VIDEO_ARRAY_COUNT * 3);
            jitter_audio_count = ffp->kronos_media_stream->jitterBufferMs/10;
            if(jitter_audio_count < INKE_PLAYBACK_BUFFER_AUDIO_MIN_THRESHOLD){
               jitter_audio_count = INKE_PLAYBACK_BUFFER_AUDIO_MIN_THRESHOLD;
            }
            if(jitter_audio_count > INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD){
               jitter_audio_count = INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD;
            }
            if(kronosBufferingThreshold < jitter_audio_count && minRiseVSize < 15){
                kronosBufferingThreshold = jitter_audio_count;
            }
            if(av_gettime_relative() - kronosLastReadTime > 50 * 1000){ //50 ms
                int aSize = is->audioq.nb_packets + frame_queue_nb_remaining(&is->sampq);
                int vSize = is->videoq.nb_packets + frame_queue_nb_remaining(&is->pictq);
                pushVideoQueueSize(is->video_queue_size_rise, VIDEO_ARRAY_COUNT,
                                   &is->video_array_rise_index, vSize);
                pushVideoQueueSize(is->video_queue_size_decline, VIDEO_ARRAY_COUNT * 3,
                                   &is->video_array_decline_index, vSize);
                if(jitter_audio_count > 35
                   && minRiseVSize < 1
                   && kronosBufferingThreshold < INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD){
                   kronosBufferingThreshold++;
                }
                
                //由于音频变速消耗cpu比较多,所以当实际的音频队列大于最大阀值的1.2倍时,就仅通过丢包限制
                if(aSize > kronosBufferingThreshold * 1.3 && aSize < INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD * 1.2){
                    ffp->scale_tempo_info.scale = 1.1; // 加速播放
                }else if(kronosBufferingThreshold - aSize > 2){
                    ffp->scale_tempo_info.scale = 0.9; // 减速播放
                }else{
                    ffp->scale_tempo_info.scale = 1.0;
                }
                kronosLastReadTime = av_gettime_relative();
            }
        }else{
            jitter_audio_count = INKE_PLAYBACK_BUFFER_AUDIO_MIN_THRESHOLD;
        }
        if (isKronosStream && pkt->stream_index == is->video_stream) {
//            int64_t kronosReadTimeInterval = av_gettime_relative() - kronosReadTime;
//            int aSize = is->audioq.nb_packets + frame_queue_nb_remaining(&is->sampq);
//            int vSize = is->videoq.nb_packets + frame_queue_nb_remaining(&is->pictq);
//            inke_ijk_log(false, true, false, "Tian read_thread | *** Interval = %lld, flag = %d, aSize = %d, vSize = %d, threshold = %d jitter:%d scale_tempo_info.scale=%f minRiseVSize:%d minDeclineVSize:%d standard:%0.4f\n", kronosReadTimeInterval, ffp->inke_kronos_player_flag, aSize, vSize, kronosBufferingThreshold, jitter_audio_count, ffp->scale_tempo_info.scale, minRiseVSize, minDeclineVSize, standardDecline);
//            kronosReadTime = av_gettime_relative();
        }
#ifdef INKE_EXTENSION
        inke_read_frame_time = av_gettime();
        if (inke_read_frame_time - *inke_cdn_cache_monitor.stream_connected_ts <= INKE_CDN_CACHE_MONITOR_DURATION) {
            if ((st_index[AVMEDIA_TYPE_VIDEO] >= 0) \
                && (pkt->stream_index == st_index[AVMEDIA_TYPE_VIDEO])) {
                ++inke_cdn_cache_monitor.video_frame_count;
                if (pkt->pts == 0LL) {
                    ++inke_cdn_cache_monitor.zero_pts_frame_count;
                }
            }
            if (inke_cdn_cache_monitor.pts_base_internal < 0LL) {
                inke_cdn_cache_monitor.pts_base_internal = pkt->pts;
                if ((ffp->input_filename != NULL) \
                    && (strncasecmp(ffp->input_filename, "http://", 7) == 0) \
                    && (strcasestr(ffp->input_filename, ".flv") != NULL)) {
                    ffp->debug_info.cdn_cache_info.stream_key = *inke_cdn_cache_monitor.stream_connected_ts;
                }
                ffp->debug_info.cdn_cache_info.monitor_duration = (uint32_t)(INKE_CDN_CACHE_MONITOR_DURATION / 1000LL);
            }
            inke_cdn_cache_monitor.data_size += pkt->size;
            inke_cdn_cache_monitor.pts_diff = pkt->pts - inke_cdn_cache_monitor.pts_base_internal;
            
            ffp->debug_info.cdn_cache_info.duration = (uint32_t)((inke_read_frame_time - *inke_cdn_cache_monitor.stream_connected_ts) / 1000LL);
            ffp->debug_info.cdn_cache_info.data_szie = (uint32_t)inke_cdn_cache_monitor.data_size;
            ffp->debug_info.cdn_cache_info.video_frames = (uint32_t)inke_cdn_cache_monitor.video_frame_count;
            ffp->debug_info.cdn_cache_info.pts_diff = (uint32_t)(inke_cdn_cache_monitor.pts_diff - ffp->debug_info.cdn_cache_info.duration);
            ffp->debug_info.cdn_cache_info.zero_pts_count = (uint32_t)inke_cdn_cache_monitor.zero_pts_frame_count;
        } else {
            if (!inke_cdn_cache_monitor.report_flag) {
                inke_cdn_cache_monitor.report_flag = 1;
                if ((ffp->input_filename != NULL) \
                    && (strncasecmp(ffp->input_filename, "http://", 7) == 0) \
                    && (strcasestr(ffp->input_filename, ".flv") != NULL)) {
                    int64_t correctedFps = inke_cdn_cache_monitor.video_fps != 0LL ? inke_cdn_cache_monitor.video_fps : 15LL;
                    double zero_frame_duration = 1.0f * (inke_cdn_cache_monitor.zero_pts_frame_count >= 1 ? (inke_cdn_cache_monitor.zero_pts_frame_count - 1) : 0) / correctedFps;
                    double monitor_duration = 1.0f * INKE_CDN_CACHE_MONITOR_DURATION / 1000000.0f;
                    ffp->debug_info.cdn_cache_info.duration = (uint32_t)(INKE_CDN_CACHE_MONITOR_DURATION / 1000LL);
                    ffp->debug_info.cdn_cache_info.pts_diff = (uint32_t)(inke_cdn_cache_monitor.pts_diff - ffp->debug_info.cdn_cache_info.duration);
                    snprintf(ffp->debug_info.cdn_cache_info.info_string, \
                             sizeof(ffp->debug_info.cdn_cache_info.info_string), \
                             "size:%dKB(%.1fs),pts_diff:%.1f,frame_diff:%.1f(%d/%s%d,%d),sei_diff:%0.1f", \
                             (int)(inke_cdn_cache_monitor.data_size / 1024), monitor_duration, \
                             1.0f * inke_cdn_cache_monitor.pts_diff / 1000.0f - monitor_duration + zero_frame_duration, \
                             1.0f * inke_cdn_cache_monitor.video_frame_count / correctedFps - monitor_duration, \
                             (int)inke_cdn_cache_monitor.video_frame_count, \
                             inke_cdn_cache_monitor.video_fps != 0LL ? "" : "+", \
                             (int)correctedFps, \
                             (int)inke_cdn_cache_monitor.zero_pts_frame_count, \
                             *inke_cdn_cache_monitor.sei_diff / 1000.0f);
                }
            }
        }
            
        if(!isKronosStream && !isOnlyHasAudio && ffp->inke_live_stream == 1
           && pkt->stream_index == is->video_stream && is->viddec.last_decode_time != 0
           && inke_read_frame_time - is->viddec.last_decode_time > 5 * 1000000){   // 5s
            inke_ijk_log(true, true, true, "ljc wait decoder timeout. FFP_MSG_ERROR");
            ffp_notify_msg1(ffp, FFP_MSG_ERROR);
            is->abort_request = 1;
        }
#endif // INKE_EXTENSION
        if (ret < 0) {
            int pb_eof = 0;
            int pb_error = 0;
            if ((ret == AVERROR_EOF || avio_feof(ic->pb)) && !is->eof) {
                pb_eof = 1;
                // check error later
            }
            if (ic->pb && ic->pb->error) {
                pb_eof = 1;
                pb_error = ic->pb->error;
            }
            if (ret == AVERROR_EXIT) {
                pb_eof = 1;
                pb_error = AVERROR_EXIT;
            }

            if (pb_eof) {
                if (is->video_stream >= 0)
                    packet_queue_put_nullpacket(&is->videoq, is->video_stream);
                if (is->audio_stream >= 0)
                    packet_queue_put_nullpacket(&is->audioq, is->audio_stream);
                if (is->subtitle_stream >= 0)
                    packet_queue_put_nullpacket(&is->subtitleq, is->subtitle_stream);
                is->eof = 1;
            }
            if (pb_error) {
                if (is->video_stream >= 0)
                    packet_queue_put_nullpacket(&is->videoq, is->video_stream);
                if (is->audio_stream >= 0)
                    packet_queue_put_nullpacket(&is->audioq, is->audio_stream);
                if (is->subtitle_stream >= 0)
                    packet_queue_put_nullpacket(&is->subtitleq, is->subtitle_stream);
                is->eof = 1;
                ffp->error = pb_error;
                av_log(ffp, AV_LOG_ERROR, "av_read_frame error: %x: %s\n", ffp->error,
                      ffp_get_error_string(ffp->error));
                print_error("av_read_frame error", ffp->error);
                // break;
#ifdef INKE_EXTENSION
                if (pb_error == AVERROR_EXIT) {
                    break;
                }
#endif // INKE_EXTENSION
            } else {
                ffp->error = 0;
            }
            if (is->eof) {
                ffp_toggle_buffering(ffp, 0);
                #ifndef INKE_EXTENSION
                SDL_Delay(1000);
                #else
                int ispzsp = 0;
                
                if (ispzsp || (ffp->inke_live_stream == 1) || (ffp->input_filename != NULL && (ffp->input_filename[0] != '/'))) {
                    int64_t duration = fftime_to_milliseconds(is->ic->duration);
                    int64_t remain_duration = duration - ffp_get_current_position_l(ffp);
//                    inke_ijk_log(true, true, false, "zlw remain_duration:%lld", remain_duration);
                    if (ffp->is->seek_req || remain_duration < 1000) {
                        if (remain_duration < 100) {
                            if (ffp->loop != 1 && (!ffp->loop || --ffp->loop)) {
                                inke_ijk_log(true, true, false, "[zlw] start seek to 0 start\n");
                                ffp->seek_to_start_by_loop = 1;
                                stream_seek(is, ffp->start_time != AV_NOPTS_VALUE ? ffp->start_time : 0, 0, 0);
                                inke_buffer_audio_packet_count = 10;//INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT;
                                inke_buffer_video_packet_count = 3;//INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT;
                                inke_ijk_log(true, true, false, "[zlw] start seek to 0 end\n");
                                ffp_notify_msg1(ffp, INKE_STREAM_RESTART_PLAY);
                            }
                        }
                        else {
                            SDL_Delay(1);
                        }
                    }
                    else {
                        SDL_Delay(500);
                    }
                } else {
                    if (!ffp->not_fast_stop&&(ic->duration >= 0) && (ic->duration < (15 * AV_TIME_BASE))) {
                        if (av_gettime() - stream_start_playback_time > ic->duration + 0.69 * AV_TIME_BASE) {
                            ffp_notify_msg1(ffp, FFP_MSG_COMPLETED);
                            is->abort_request = 1;
                            continue;
                        }
                        SDL_Delay(350);
                    } else {
                        if (ffp->loop == 0/*loop state on*/) {
                            SDL_Delay(200); // sleep short time when playback loop
                        } else {
                            SDL_Delay(1000);
                        }
                    }
                }
                #endif // INKE_EXTENSION
            }
            SDL_LockMutex(wait_mutex);
            SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
            SDL_UnlockMutex(wait_mutex);
            ffp_statistic_l(ffp);
            continue;
        } else {
            is->eof = 0;
        }

        if (pkt->flags & AV_PKT_FLAG_DISCONTINUITY) {
            if (is->audio_stream >= 0) {
                packet_queue_put(&is->audioq, &flush_pkt);
            }
            if (is->subtitle_stream >= 0) {
                packet_queue_put(&is->subtitleq, &flush_pkt);
            }
            if (is->video_stream >= 0) {
                packet_queue_put(&is->videoq, &flush_pkt);
            }
        }

        /* check if packet is in play range specified by user, then queue, otherwise discard */
        stream_start_time = ic->streams[pkt->stream_index]->start_time;
        pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
        pkt_in_play_range = ffp->duration == AV_NOPTS_VALUE ||
                (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
                av_q2d(ic->streams[pkt->stream_index]->time_base) -
                (double)(ffp->start_time != AV_NOPTS_VALUE ? ffp->start_time : 0) / 1000000
                <= ((double)ffp->duration / 1000000);
#ifdef INKE_EXTENSION
        if ((ffp->inke_live_stream == 1) || isKronosStream) {
            streamFlowmeter(ffp, &quality_start_time, &quality_total_network_data_size, pkt->size);
        }
        if (ffp->inke_cdn_stream == 1) {
            if (pkt->stream_index == is->video_stream) {
                is->bitrateV += pkt->size;
            } else {
                is->bitrateA += pkt->size;
            }
            stepIJKPlayerInfo(ffp);
        }
        if ((ffp->inke_live_stream == 1) && !isKronosStream&&!ffp->temp_disable_fast_chase) {
            int64_t pkt_pts_us = (int64_t)(pkt_ts * av_q2d(ic->streams[pkt->stream_index]->time_base) * 1000000);
            if (is->buffering_on || ((is->audioq.duration < INKE_INIT_RESERVE_DATA_TIME) \
                                     && (audio_last_delay_second > (int)(INKE_INIT_RESERVE_DATA_TIME / 1000)))) {
                av_frame_drop_until_pts = pkt_pts_us;
                last_av_frame_base_time = av_gettime();
                audio_last_delay_second = (int)(INKE_INIT_RESERVE_DATA_TIME / 1000);
                audio_queue_data_stability_time = 0;
            }
            if (av_frame_drop_until_pts > pkt_pts_us) {
                pkt_in_play_range = 0;
                last_av_frame_base_time = av_gettime();
                drop_key_frame = pkt->stream_index != is->video_stream ? drop_key_frame : true;
            } else {
                if ((pkt->stream_index == is->video_stream) && drop_key_frame) {
                    if(pkt->flags & AV_PKT_FLAG_KEY) {
                        drop_key_frame = false;
                    }
                    else {
                        last_av_frame_base_time = av_gettime();
                        pkt_in_play_range = 0;
                    }
                }
            }

            if ((pkt->stream_index == is->audio_stream) && pkt_in_play_range && !is->buffering_on) {
                int64_t now = av_gettime();
                int av_frame_interval = (int)((now - last_av_frame_base_time) / 1000);
                last_av_frame_base_time = now;
                standard_audio_frame_interval = ((standard_audio_frame_interval != 0)
                                                 || (is->audioq.nb_packets == 0)) ? standard_audio_frame_interval
                                                                                  : (int)(is->audioq.duration / is->audioq.nb_packets);
                int delay_second = (int)(is->audioq.duration / 1000);
                int audio_delay_drop_data_timeline = (int)(INKE_MUST_DROP_DATA_TIME / 1000);
//                av_log(NULL, AV_LOG_FATAL, "delay_second:%d, av_frame_interval:%d, standard_audio_frame_interval:%d, audio_last_delay_second:%d\n",
//                       delay_second, av_frame_interval, standard_audio_frame_interval, audio_last_delay_second);
                if ((av_frame_interval <= standard_audio_frame_interval * 16)
                    && ((audio_last_delay_second == delay_second)
                        || (delay_second >= audio_delay_drop_data_timeline)
                        || ((audio_last_delay_second >= 6) && (audio_last_delay_second < audio_delay_drop_data_timeline)
                             && (delay_second >= 6) && (delay_second < audio_delay_drop_data_timeline))
                        || ((audio_last_delay_second >= 5) && (delay_second >= 5))
                        || ((audio_last_delay_second > 3) && (delay_second > 3))
                        || ((audio_last_delay_second >= 2) && (delay_second >= 2))
                        || ((audio_last_delay_second >= 1) && (delay_second >= 1))
                        || (1.0 * delay_second / audio_delay_drop_data_timeline > 1.5f))) {
                    audio_queue_data_stability_time += av_frame_interval;
                }
                else {
                    audio_queue_data_stability_time = 0;
                }
                audio_last_delay_second = delay_second <= audio_delay_drop_data_timeline ? \
                                                                                        delay_second \
                                                                                      : audio_delay_drop_data_timeline;
                
                ffp->debug_info.audio_stability_time = audio_queue_data_stability_time;
                if (standard_audio_frame_interval != 0) {
                    int audio_packet_queue_keep_time = 0;
                    if (delay_second >= 9) {
                        audio_last_delay_second = delay_second;
                        audio_packet_queue_keep_time = 4;
                    }
                    else if ((audio_last_delay_second >= audio_delay_drop_data_timeline)
                        && (audio_queue_data_stability_time > 30 * 1000)) {
                        audio_packet_queue_keep_time = 5 + (rand() % 3);
                    }
                    else if ((audio_last_delay_second >= 6)
                             && (audio_queue_data_stability_time > 60 * 1000)) {
                        audio_packet_queue_keep_time = 5;
                    }
                    else if ((audio_last_delay_second >= 5)
                             && (audio_queue_data_stability_time > 75 * 1000)) {
                        audio_packet_queue_keep_time = 4;
                    }
//                    else if ((audio_last_delay_second > 3)
//                             && (audio_queue_data_stability_time > 30 * 1000)) {
//                        audio_packet_queue_keep_time = 3;
//                    }
//                    else if ((audio_last_delay_second >= 2)
//                             && (audio_queue_data_stability_time > 60 * 1000)) {
//                        audio_packet_queue_keep_time = 2;
//                    }
//                    else if ((audio_last_delay_second >= 1)
//                             && (audio_queue_data_stability_time > 180 * 1000)) {
//                        audio_packet_queue_keep_time = 1;
//                    }
                    if (audio_packet_queue_keep_time != 0) {
                        ffp->debug_info.drop_data_times++;
                        av_frame_drop_until_pts = (int64_t)(pkt_pts_us + (audio_last_delay_second - audio_packet_queue_keep_time) * 1000000);
                        audio_queue_data_stability_time = 0;
                    }
                }
            }
            if (pkt->stream_index == is->video_stream) {
                if (is->buffering_on || (pkt_in_play_range == 0) || (is->audioq.duration < INKE_AUDIO_BUFFER_TIME)) {
                    display_fps_base_time = av_gettime();
                    display_fps_frame_count = 0;
                    quality_fps_base_time = av_gettime();
                    quality_fps_frame_count = 0;
                }
                else {
                    ++quality_fps_frame_count;
                    if ((audio_queue_data_stability_time > 3000) && (av_gettime() - display_fps_base_time > 30000000)) {
                        int64_t now = av_gettime();
                        int display_fps_duration = (int)((now - display_fps_base_time) / 1000000);
                        int quality_fps_duration = (int)((now - quality_fps_base_time) / 1000000);
                        float display_fps = 1.0 * display_fps_frame_count / display_fps_duration;
//                        ffp->debug_info.video_display_fps = display_fps;
                        float quality_fps = 1.0 * quality_fps_frame_count / quality_fps_duration;
                        if ((0.8 * quality_fps > display_fps) && (0.8 * is->videoq.nb_packets > 1.0 * is->pictq.size)) {
                            if (display_fps >= 24.0 || display_fps < 3.0 || quality_fps < 18.0) {
                                display_fps_base_time = av_gettime();
                                display_fps_frame_count = 0;
                                quality_fps_base_time = av_gettime();
                                quality_fps_frame_count = 0;
                            }
                            else {
                                if (quality_fps_frame_count > 24 * 3 && display_fps_frame_count > 24 * 2) {
                                    ffp_notify_msg3(ffp, INKE_MSG_QUALITY, INKE_VIDEO_FPS_TOO_SLOW, (int)(display_fps * 100));
                                }
                            }
                        }
                    }
                }
            }
        }
#ifdef ENABLE_KRONOS_STREAM
            if (isKronosStream) {
                pkt_in_play_range = 1;
                int threshold = !is->buffering_on ? 4+kronosBufferingThreshold : 3+kronosBufferingThreshold;
                if (kronos_media_stream->audioInfo.format == AUDIO_INFO_FORMAT_PCM) {
                    threshold = !is->buffering_on ? 3+kronosBufferingThreshold : 2+kronosBufferingThreshold;
                }
                int64_t cur_time = av_gettime_relative();
            
                if(ffp->audio_callback_time!=0&&cur_time-ffp->audio_callback_time>3000000LL){
                    ffp->audio_callback_time=cur_time;
                    ffp_reset_audiounit(ffp);
                }
                if ((is->audioq.nb_packets + frame_queue_nb_remaining(&is->sampq) >= threshold) \
                    && (pkt->stream_index == is->audio_stream)) {
                    pkt_in_play_range = 0;
                }
            }
#endif // ENABLE_KRONOS_STREAM
#endif // INKE_EXTENSION

        if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
            packet_queue_put(&is->audioq, pkt);
            PacketQueue *q = &is->audioq;
            SDL_LockMutex(q->mutex);
            if (pkt->duration <= 0 && q->first_pkt) {
            	    q->duration = pkt->dts - q->first_pkt->pkt.dts;
            }
            SDL_UnlockMutex(q->mutex);
        } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
                   && !(is->video_st && (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))) {
            //printf("read sei frame pts = %lld\n",pkt->pts);
            packet_queue_put(&is->videoq, pkt);
#ifdef INKE_DEBUG_IJKPLAYER
            ++ffp->vout->network_frame_count;
#endif // INKE_DEBUG_IJKPLAYER
        } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
            packet_queue_put(&is->subtitleq, pkt);
        } else {
            av_packet_unref(pkt);
        }
 
        ffp_statistic_l(ffp);
#ifdef INKE_EXTENSION
        if (is->buffering_on) {
            #ifdef ENABLE_KRONOS_STREAM
            if (isKronosStream) {
                if (isKronosFirstBuffering && (is->audioq.nb_packets > 0)) {
                    isKronosFirstBuffering = 0;
                    ffp_toggle_buffering(ffp, 0);
                } else {
                    if (av_gettime_relative() - kronosReadBlockTime > 1 * 1000 * 1000) {
                        kronosBufferingThreshold = 1.5 * kronosBufferingThreshold;
                        kronosBufferingThreshold = kronosBufferingThreshold >= INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD ? INKE_PLAYBACK_BUFFER_AUDIO_MAX_THRESHOLD : kronosBufferingThreshold;

                        kronosReadBlockTime = av_gettime_relative();
                    }

                    if (is->audioq.nb_packets + frame_queue_nb_remaining(&is->sampq) >= kronosBufferingThreshold) {
                        ffp_toggle_buffering(ffp, 0);
                    }
                }
            } else if ((is->audioq.is_buffer_indicator && (is->audioq.nb_packets >= inke_buffer_audio_packet_count)) \
                    || (is->videoq.is_buffer_indicator && (is->videoq.nb_packets >= inke_buffer_video_packet_count))) {
                inke_buffer_audio_packet_count = inke_buffer_audio_packet_count == INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT ? INKE_PLAYBACK_BUFFER_AUDIO_PACKET_COUNT : inke_buffer_audio_packet_count;
                inke_buffer_video_packet_count = inke_buffer_video_packet_count == INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT ? INKE_PLAYBACK_BUFFER_VIDEO_PACKET_COUNT : inke_buffer_video_packet_count;
                ffp_toggle_buffering(ffp, 0);
            }
            #else
            if ((is->audioq.is_buffer_indicator && (is->audioq.nb_packets >= inke_buffer_audio_packet_count)) \
                || (is->videoq.is_buffer_indicator && (is->videoq.nb_packets >= inke_buffer_video_packet_count))) {
                inke_buffer_audio_packet_count = inke_buffer_audio_packet_count == INKE_STARTUP_BUFFER_AUDIO_PACKET_COUNT ? INKE_PLAYBACK_BUFFER_AUDIO_PACKET_COUNT : inke_buffer_audio_packet_count;
                inke_buffer_video_packet_count = inke_buffer_video_packet_count == INKE_STARTUP_BUFFER_VIDEO_PACKET_COUNT ? INKE_PLAYBACK_BUFFER_VIDEO_PACKET_COUNT : inke_buffer_video_packet_count;
                ffp_toggle_buffering(ffp, 0);
            }
            #endif // ENABLE_KRONOS_STREAM
            #ifdef ENABLE_KRONOS_STREAM
        } else {
            if (isKronosStream) {
                if (av_gettime_relative() - kronosReadBlockTime > 20 * 1000 * 1000) { // 20s
                    kronosReadBlockTime = av_gettime_relative() - 10 * 1000 * 1000;
                    if(minDeclineVSize > 0 && !isOnlyHasAudio){
                        if((kronosBufferingThreshold > jitter_audio_count * 2 && minDeclineVSize >= 15)
                           || standardDecline < 1.2
                           || jitter_audio_count < 20){
                            kronosBufferingThreshold = kronosBufferingThreshold * 0.9;
                        }else if(kronosBufferingThreshold > jitter_audio_count * 1.5 && minDeclineVSize > 3){
                            kronosBufferingThreshold = kronosBufferingThreshold * 0.97;
                        }else{
                            kronosBufferingThreshold--;
                        }
                        kronosBufferingThreshold = kronosBufferingThreshold <= jitter_audio_count ? jitter_audio_count: kronosBufferingThreshold;
                    }else{
                        kronosBufferingThreshold--;
                        kronosBufferingThreshold = kronosBufferingThreshold <= jitter_audio_count ? jitter_audio_count: kronosBufferingThreshold;
                    }
                }
            }
        }

#endif // ENABLE_KRONOS_STREAM
#endif // INKE_EXTENSION
        if (ffp->packet_buffering) {
            io_tick_counter = SDL_GetTickHR();
            if (abs((int)(io_tick_counter - prev_io_tick_counter)) > BUFFERING_CHECK_PER_MILLISECONDS) {
                prev_io_tick_counter = io_tick_counter;
                ffp_check_buffering_l(ffp);
            }
        }
#ifdef INKE_EXTENSION
        if (ffp->inke_live_stream == 1) {
            if (is->buffering_on && (is->audioq.duration >= display_initialize_buffering_time)) {
                if (display_initialize_buffering_time == 360) {
                    // time;is->audioq.duration;is->videoq.duration
                }
                display_initialize_buffering_time = INKE_AUDIO_BUFFER_TIME;
                ffp_toggle_buffering(ffp, 0);
                audio_last_delay_second = (int)(INKE_INIT_RESERVE_DATA_TIME / 1000);
            }
        }
#endif // INKE_EXTENSION
    }

    ret = 0;
 fail:

    if (ic && !is->ic)
        avformat_close_input(&ic);
    #ifdef INKE_EXTENSION
    if ((ffp->inke_kronos_roomid != 0LL) \
        && (ffp->inke_kronos_room_timestamp != 0LL)) {
        SyncMultiRemoveInstance(ffp);
    }
    if (avio_ctx != NULL) {
        if (avio_ctx->buffer != NULL) {
            av_freep(&avio_ctx->buffer);
        }
        av_freep(&avio_ctx);
    }
    if (kronos_media_stream != NULL) {
        err = KronosMediaStreamShutdown(kronos_media_stream);
        err += KronosDestroyInstance(&kronos_media_stream);
        assert(err == 0);
    }
    #if defined(ENABLE_QUIC_FLV_STREAM)
    if (quic_flv_stream != NULL) {
        err = QuicFlvStreamShutdown(quic_flv_stream);
        err += QuicFlvDestroyInstance(&quic_flv_stream);
        assert(err == 0);
    }
    #endif // defined(ENABLE_QUIC_FLV_STREAM)
    if(ffp->scale_tempo_info.isrunning){
        scaletempo_stop(&ffp->scale_tempo_info);
    }
    #endif // INKE_EXTENSION
    if (!ffp->prepared || !is->abort_request) {
        ffp->last_error = last_error;
        ffp_notify_msg2(ffp, FFP_MSG_ERROR, last_error);
    }
    SDL_DestroyMutex(wait_mutex);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端码匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值