6#include "playbackengine/qffmpegmediadataholder_p.h"
7#include <QtCore/qloggingcategory.h>
11Q_STATIC_LOGGING_CATEGORY(qLcStreamDecoder,
"qt.multimedia.ffmpeg.streamdecoder");
15StreamDecoder::StreamDecoder(
const PlaybackEngineObjectID &id,
const CodecContext &codecContext,
16 TrackPosition absSeekPos)
17 : PlaybackEngineObject(id),
18 m_codecContext(codecContext),
19 m_trackType(MediaDataHolder::trackTypeFromMediaType(codecContext.context()->codec_type)),
20 m_sessionCtx{ absSeekPos }
22 qCDebug(qLcStreamDecoder) <<
"Create stream decoder, trackType" << m_trackType
23 <<
"absSeekPos:" << absSeekPos.get();
24 Q_ASSERT(m_trackType != QPlatformMediaPlayer::NTrackTypes);
29 avcodec_flush_buffers(m_codecContext.context());
34 updateSession(sessionID, [
this, pos, offset]() {
35 m_sessionCtx = { offset.loopStartTimeUs.asDuration() + pos };
36 avcodec_flush_buffers(m_codecContext.context());
42 if (checkSessionID(sourceID.sessionID))
48 if (packet.isValid() && !checkSessionID(packet.sourceID().sessionID)) {
49 qCDebug(qLcStreamDecoder) <<
"Packet session outdated. Source id:" << packet.sourceID()
50 <<
"current id" << id();
55 m_sessionCtx.packets.enqueue(std::move(packet));
61 Packet packet = m_sessionCtx.packets.dequeue();
63 auto decodePacket = [
this](
const Packet &packet) {
64 if (trackType() == QPlatformMediaPlayer::SubtitleStream)
65 decodeSubtitle(packet);
70 if (packet.isValid() && packet.loopOffset().loopIndex != m_sessionCtx.offset.loopIndex) {
73 qCDebug(qLcStreamDecoder) <<
"flush buffers due to new loop:"
74 << packet.loopOffset().loopIndex;
76 avcodec_flush_buffers(m_codecContext.context());
77 m_sessionCtx.offset = packet.loopOffset();
82 setAtEnd(!packet.isValid());
85 emit packetProcessed(
std::move(packet));
112 if (!checkID(frame.sourceID()))
115 --m_sessionCtx.pendingFramesCount;
116 Q_ASSERT(m_sessionCtx.pendingFramesCount >= 0);
123 const qint32 maxCount = maxQueueSize(m_trackType);
125 return !m_sessionCtx.packets.empty() && m_sessionCtx.pendingFramesCount < maxCount
126 && PlaybackEngineObject::canDoNextStep();
131 if (frame.isValid() && frame.absoluteEnd() < m_sessionCtx.absSeekPos)
134 Q_ASSERT(m_sessionCtx.pendingFramesCount >= 0);
135 ++m_sessionCtx.pendingFramesCount;
136 emit requestHandleFrame(frame);
141 auto sendPacketResult = sendAVPacket(packet);
143 if (sendPacketResult == AVERROR(EAGAIN)) {
150 sendPacketResult = sendAVPacket(packet);
152 if (sendPacketResult != AVERROR(EAGAIN))
153 qWarning() <<
"Unexpected FFmpeg behavior";
156 if (sendPacketResult == 0)
157 receiveAVFrames(!packet.isValid());
162 return avcodec_send_packet(m_codecContext.context(), packet.isValid() ? packet.avPacket() :
nullptr);
168 auto avFrame = makeAVFrame();
170 const auto receiveFrameResult = avcodec_receive_frame(m_codecContext.context(), avFrame.get());
172 if (receiveFrameResult == AVERROR_EOF || receiveFrameResult == AVERROR(EAGAIN)) {
173 if (flushPacket && receiveFrameResult == AVERROR(EAGAIN)) {
181 qWarning() <<
"Unexpected FFmpeg behavior: EAGAIN state for avcodec_receive_frame "
182 <<
"at end of the stream";
189 if (receiveFrameResult < 0) {
190 emit error(QMediaPlayer::FormatError, err2str(receiveFrameResult));
196 if (m_trackType == QPlatformMediaPlayer::VideoStream)
197 avFrame = copyFromHwPool(
std::move(avFrame));
199 onFrameFound({ m_sessionCtx.offset, std::move(avFrame), m_codecContext, id() });
205 if (!packet.isValid())
210 memset(&subtitle, 0,
sizeof(subtitle));
214 avcodec_decode_subtitle2(m_codecContext.context(), &subtitle, &gotSubtitle, packet.avPacket());
217 if (res < 0 || !gotSubtitle)
222 TrackPosition start = 0, end = 0;
223 if (subtitle.pts == AV_NOPTS_VALUE) {
224 start = m_codecContext.toTrackPosition(AVStreamPosition(packet.avPacket()->pts));
225 end = start + m_codecContext.toTrackDuration(AVStreamDuration(packet.avPacket()->duration));
227 auto pts = timeStampUs(subtitle.pts, AVRational{ 1, AV_TIME_BASE });
228 start = TrackPosition(*pts + qint64(subtitle.start_display_time) * 1000);
229 end = TrackPosition(*pts + qint64(subtitle.end_display_time) * 1000);
233 qWarning() <<
"Invalid subtitle time";
238 for (uint i = 0; i < subtitle.num_rects; ++i) {
239 const auto *r = subtitle.rects[i];
242 text += QLatin1Char(
'\n');
244 text += QString::fromUtf8(r->text);
246 const char *ass = r->ass;
255 text += QString::fromUtf8(ass);
258 text.replace(QLatin1String(
"\\N"), QLatin1String(
"\n"));
259 text.replace(QLatin1String(
"\\n"), QLatin1String(
"\n"));
260 text.replace(QLatin1String(
"\r\n"), QLatin1String(
"\n"));
261 if (text.endsWith(QLatin1Char(
'\n')))
264 onFrameFound({ m_sessionCtx.offset, text, start, end - start, id() });
267 onFrameFound({ m_sessionCtx.offset, QString(), end, TrackDuration(0), id() });
273#include "moc_qffmpegstreamdecoder_p.cpp"
bool canDoNextStep() const override
~StreamDecoder() override
void doNextStep() override
void onFrameProcessed(const Frame &frame)
void onFinalPacketReceived(PlaybackEngineObjectID sourceID)
void seek(quint64 sessionID, TrackPosition pos, const LoopOffset &offset)
QPlatformMediaPlayer::TrackType trackType() const
QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) QMacScreenCaptureStreamDelegate
Combined button and popup list for selecting options.