Fail streaming if no codec for a stream.

This commit is contained in:
John Preston 2019-03-06 11:07:33 +04:00
parent aade3d4f27
commit 518d1da736
2 changed files with 25 additions and 7 deletions

View File

@ -107,7 +107,7 @@ Stream File::Context::initStream(
nullptr,
0);
if (index < 0) {
return {};
return result;
}
const auto info = format->streams[index];
@ -117,25 +117,27 @@ Stream File::Context::initStream(
} else if (type == AVMEDIA_TYPE_AUDIO) {
result.frequency = info->codecpar->sample_rate;
if (!result.frequency) {
return {};
return result;
}
}
result.codec = MakeCodecPointer(info);
if (!result.codec) {
return {};
return result;
}
result.frame = MakeFramePointer();
if (!result.frame) {
return {};
result.codec = nullptr;
return result;
}
result.timeBase = info->time_base;
result.duration = (info->duration != AV_NOPTS_VALUE)
? PtsToTime(info->duration, result.timeBase)
: PtsToTime(format->duration, kUniversalTimeBase);
if (result.duration == kTimeUnknown || !result.duration) {
return {};
result.codec = nullptr;
return result;
}
// We want duration to be greater than any valid frame position.
// That way we can handle looping by advancing position by n * duration.

View File

@ -214,7 +214,13 @@ bool Player::fileReady(Stream &&video, Stream &&audio) {
};
};
const auto mode = _options.mode;
if (audio.codec && (mode == Mode::Audio || mode == Mode::Both)) {
if (mode != Mode::Audio && mode != Mode::Both) {
audio = Stream();
}
if (mode != Mode::Video && mode != Mode::Both) {
video = Stream();
}
if (audio.codec) {
if (_options.audioId.audio() != nullptr) {
_audioId = AudioMsgId(
_options.audioId.audio(),
@ -229,16 +235,26 @@ bool Player::fileReady(Stream &&video, Stream &&audio) {
_audioId,
ready,
error(_audio));
} else if (audio.index >= 0) {
LOG(("Streaming Error: No codec for audio stream %1, mode %2."
).arg(audio.index
).arg(int(mode)));
return false;
} else {
_audioId = AudioMsgId();
}
if (video.codec && (mode == Mode::Video || mode == Mode::Both)) {
if (video.codec) {
_video = std::make_unique<VideoTrack>(
_options,
std::move(video),
_audioId,
ready,
error(_video));
} else if (video.index >= 0) {
LOG(("Streaming Error: No codec for video stream %1, mode %2."
).arg(audio.index
).arg(int(mode)));
return false;
}
if ((mode == Mode::Audio && !_audio)
|| (mode == Mode::Video && !_video)