nsresult AudioSink::InitializeAudioStream() { // AudioStream initialization can block for extended periods in unusual // circumstances, so we take care to drop the decoder monitor while // initializing. RefPtr<AudioStream> audioStream(new AudioStream()); audioStream->Init(mInfo.mChannels, mInfo.mRate, mChannel, AudioStream::HighLatency); // TODO: Check Init's return value and bail on error. Unfortunately this // causes some tests to fail due to playback failing. ReentrantMonitorAutoEnter mon(GetReentrantMonitor()); mAudioStream = audioStream; UpdateStreamSettings(); return NS_OK; }
nsresult DecodedAudioDataSink::InitializeAudioStream() { // AudioStream initialization can block for extended periods in unusual // circumstances, so we take care to drop the decoder monitor while // initializing. RefPtr<AudioStream> audioStream(new AudioStream()); nsresult rv = audioStream->Init(mInfo.mChannels, mInfo.mRate, mChannel, AudioStream::HighLatency); if (NS_FAILED(rv)) { audioStream->Shutdown(); return rv; } ReentrantMonitorAutoEnter mon(GetReentrantMonitor()); mAudioStream = audioStream; return NS_OK; }
/* static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_output) { char buf[256]; int flags = (is_output ? ic->oformat->flags : ic->iformat->flags); AVStream *st = ic->streams[i]; int g = av_gcd(st->time_base.num, st->time_base.den); AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0); avcodec_string(buf, sizeof(buf), st->codec, is_output); av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i); // the pid is an important information, so we display it // XXX: add a generic system if (flags & AVFMT_SHOW_IDS) av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id); if (lang) av_log(NULL, AV_LOG_INFO, "(%s)", lang->value); av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames, st->time_base.num/g, st->time_base.den/g); av_log(NULL, AV_LOG_INFO, ": %s", buf); if (st->sample_aspect_ratio.num && // default av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) { AVRational display_aspect_ratio; av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, st->codec->width*st->sample_aspect_ratio.num, st->codec->height*st->sample_aspect_ratio.den, 1024*1024); av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den); } if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){ if(st->avg_frame_rate.den && st->avg_frame_rate.num) print_fps(av_q2d(st->avg_frame_rate), "fps"); if(st->r_frame_rate.den && st->r_frame_rate.num) print_fps(av_q2d(st->r_frame_rate), "tbr"); if(st->time_base.den && st->time_base.num) print_fps(1/av_q2d(st->time_base), "tbn"); if(st->codec->time_base.den && st->codec->time_base.num) print_fps(1/av_q2d(st->codec->time_base), "tbc"); } if (st->disposition & AV_DISPOSITION_DEFAULT) av_log(NULL, AV_LOG_INFO, " (default)"); if (st->disposition & AV_DISPOSITION_DUB) av_log(NULL, AV_LOG_INFO, " (dub)"); if (st->disposition & AV_DISPOSITION_ORIGINAL) av_log(NULL, AV_LOG_INFO, " (original)"); if (st->disposition & AV_DISPOSITION_COMMENT) av_log(NULL, AV_LOG_INFO, " (comment)"); if (st->disposition & AV_DISPOSITION_LYRICS) av_log(NULL, AV_LOG_INFO, " (lyrics)"); if (st->disposition & AV_DISPOSITION_KARAOKE) av_log(NULL, AV_LOG_INFO, " (karaoke)"); if (st->disposition & AV_DISPOSITION_FORCED) av_log(NULL, AV_LOG_INFO, " (forced)"); if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) av_log(NULL, AV_LOG_INFO, " (hearing impaired)"); if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED) av_log(NULL, AV_LOG_INFO, " (visual impaired)"); if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS) av_log(NULL, AV_LOG_INFO, " (clean effects)"); av_log(NULL, AV_LOG_INFO, "\n"); dump_metadata(NULL, st->metadata, " "); }*/ void AVDemuxer::dump() { av_dump_format(format_context, 0, qPrintable(_file_name), false); fflush(0); qDebug("[AVFormatContext::duration = %lld]", duration()); qDebug("video format: %s [%s]", qPrintable(videoFormatName()), qPrintable(videoFormatLongName())); qDebug("Audio: %s [%s]", qPrintable(audioCodecName()), qPrintable(audioCodecLongName())); if (a_codec_context) qDebug("sample rate: %d, channels: %d", a_codec_context->sample_rate, a_codec_context->channels); struct stream_info { int index; AVCodecContext* ctx; const char* name; }; stream_info stream_infos[] = { {audioStream(), a_codec_context, "audio stream"} , {videoStream(), v_codec_context, "video_stream"} , {0, 0, 0} }; AVStream *stream = 0; for (int idx = 0; stream_infos[idx].name != 0; ++idx) { qDebug("%s: %d", stream_infos[idx].name, stream_infos[idx].index); if (stream_infos[idx].index < 0 || !(stream = format_context->streams[idx])) { qDebug("stream not available: index = %d, stream = %p", stream_infos[idx].index, stream); continue; } //why not fixed for video without audio? //qDebug("[AVStream::start_time = %lld]", stream->start_time); AVCodecContext *ctx = stream_infos[idx].ctx; if (ctx) { qDebug("[AVCodecContext::time_base = %d / %d = %f]", ctx->time_base.num, ctx->time_base.den, av_q2d(ctx->time_base)); } ////why avg_frame_rate is not fixed for the same video? //qDebug("[AVStream::avg_frame_rate = %d / %d = %f]", stream->avg_frame_rate.num, stream->avg_frame_rate.den, av_q2d(stream->avg_frame_rate)); //qDebug("[AVStream::time_base = %d / %d = %f]", stream->time_base.num, stream->time_base.den, av_q2d(stream->time_base)); } }
bool AVDemuxer::readFrame() { QMutexLocker lock(&mutex); Q_UNUSED(lock); AVPacket packet; //start timeout timer and timeout __interrupt_timer.start(); int ret = av_read_frame(format_context, &packet); //0: ok, <0: error/end //invalidate the timer __interrupt_timer.invalidate(); if (ret != 0) { if (ret == AVERROR_EOF) { //end of file. FIXME: why no eof if replaying by seek(0)? if (!eof) { eof = true; started_ = false; pkt->markEnd(); qDebug("End of file. %s %d", __FUNCTION__, __LINE__); emit finished(); return true; } //pkt->data = QByteArray(); //flush //return true; return false; //frames after eof are eof frames } else if (ret == AVERROR_INVALIDDATA) { qWarning("AVERROR_INVALIDDATA"); } qWarning("[AVDemuxer] error: %s", av_err2str(ret)); return false; } stream_idx = packet.stream_index; //TODO: check index //check whether the 1st frame is alreay got. emit only once if (!started_ && v_codec_context && v_codec_context->frame_number == 0) { started_ = true; emit started(); } else if (!started_ && a_codec_context && a_codec_context->frame_number == 0) { started_ = true; emit started(); } if (stream_idx != videoStream() && stream_idx != audioStream()) { //qWarning("[AVDemuxer] unknown stream index: %d", stream_idx); return false; } pkt->data = QByteArray((const char*)packet.data, packet.size); pkt->duration = packet.duration; //if (packet.dts == AV_NOPTS_VALUE && ) if (packet.dts != AV_NOPTS_VALUE) //has B-frames pkt->pts = packet.dts; else if (packet.pts != AV_NOPTS_VALUE) pkt->pts = packet.pts; else pkt->pts = 0; AVStream *stream = format_context->streams[stream_idx]; pkt->pts *= av_q2d(stream->time_base); if (stream->codec->codec_type == AVMEDIA_TYPE_SUBTITLE && (packet.flags & AV_PKT_FLAG_KEY) && packet.convergence_duration != AV_NOPTS_VALUE) pkt->duration = packet.convergence_duration * av_q2d(stream->time_base); else if (packet.duration > 0) pkt->duration = packet.duration * av_q2d(stream->time_base); else pkt->duration = 0; //qDebug("AVPacket.pts=%f, duration=%f, dts=%lld", pkt->pts, pkt->duration, packet.dts); av_free_packet(&packet); //important! return true; }