Skip to content

Commit

Permalink
Merge pull request #269 from phunkyfish/sync-ffmpeg6-seek-with-kodi
Browse files Browse the repository at this point in the history
Sync ffmpeg6 seek with kodi part2 - include the st->cur_pts replacement in FFmpeg6
  • Loading branch information
phunkyfish authored Jan 31, 2024
2 parents 36930e7 + 562c3cf commit 990ec69
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion inputstream.ffmpegdirect/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="inputstream.ffmpegdirect"
version="21.3.0"
version="21.3.1"
name="Inputstream FFmpeg Direct"
provider-name="Ross Nicholson">
<requires>@ADDON_DEPENDS@</requires>
Expand Down
4 changes: 4 additions & 0 deletions inputstream.ffmpegdirect/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v21.3.1
- Update seeking in FFmpegDirect to mimic the st->cur_pts replacement in FFmpeg6
- Sync FFmpegStream.cpp and DemuxStream.cpp with xbmc/cores/VideoPlayer/DVDDemuxers/DVDDemuxFFmpeg.cpp from https://github.com/xbmc/xbmc. Update for all commits up to the end of 2023 - part 2

v21.3.0
- Sync FFmpegStream.cpp and DemuxStream.cpp with xbmc/cores/VideoPlayer/DVDDemuxers/DVDDemuxFFmpeg.cpp from https://github.com/xbmc/xbmc. Update for all commits up to the end of 2023 part 1

Expand Down
4 changes: 2 additions & 2 deletions src/stream/FFmpegCatchupStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ bool FFmpegCatchupStream::GetTimes(kodi::addon::InputstreamTimes& times)
return true;
}

void FFmpegCatchupStream::UpdateCurrentPTS()
void FFmpegCatchupStream::CurrentPTSUpdated()
{
FFmpegStream::UpdateCurrentPTS();
FFmpegStream::CurrentPTSUpdated();
if (m_currentPts != STREAM_NOPTS_VALUE)
m_currentPts += m_seekOffset;
}
Expand Down
2 changes: 1 addition & 1 deletion src/stream/FFmpegCatchupStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FFmpegCatchupStream : public FFmpegStream
virtual bool IsRealTimeStream() override;

protected:
void UpdateCurrentPTS() override;
void CurrentPTSUpdated() override;
bool CheckReturnEmptyOnPacketResult(int result) override;

long long GetCurrentLiveOffset() { return std::time(nullptr) - m_catchupBufferStartTime; }
Expand Down
58 changes: 38 additions & 20 deletions src/stream/FFmpegStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,15 @@ DEMUX_PACKET* FFmpegStream::DemuxRead()

// used to guess streamlength
if (pPacket->dts != STREAM_NOPTS_VALUE && (pPacket->dts > m_currentPts || m_currentPts == STREAM_NOPTS_VALUE))
{
m_currentPts = pPacket->dts;
CurrentPTSUpdated();
}
else if (pPacket->pts != STREAM_NOPTS_VALUE && (pPacket->pts > m_currentPts || m_currentPts == STREAM_NOPTS_VALUE))
{
m_currentPts = pPacket->pts;
CurrentPTSUpdated();
}

// store internal id until we know the continuous id presented to player
// the stream might not have been created yet
Expand Down Expand Up @@ -795,8 +803,6 @@ bool FFmpegStream::Open(bool fileinfo)
// if format can be nonblocking, let's use that
m_pFormatContext->flags |= AVFMT_FLAG_NONBLOCK;

UpdateCurrentPTS();

// select the correct program if requested
m_initialProgramNumber = UINT_MAX;
CVariant programProp(m_programProperty.empty() ? CVariant::VariantTypeNull : CVariant(m_programProperty));
Expand Down Expand Up @@ -1145,20 +1151,8 @@ void FFmpegStream::ResetVideoStreams()
}
}

void FFmpegStream::UpdateCurrentPTS()
void FFmpegStream::CurrentPTSUpdated()
{
m_currentPts = STREAM_NOPTS_VALUE;

int idx = av_find_default_stream_index(m_pFormatContext);
if (idx >= 0)
{
AVStream* stream = m_pFormatContext->streams[idx];
if (stream && m_pkt.pkt.dts != (int64_t)AV_NOPTS_VALUE)
{
double ts = ConvertTimestamp(m_pkt.pkt.dts, stream->time_base.den, stream->time_base.num);
m_currentPts = ts;
}
}
}

double FFmpegStream::ConvertTimestamp(int64_t pts, int den, int num)
Expand Down Expand Up @@ -1541,7 +1535,25 @@ bool FFmpegStream::SeekTime(double time, bool backwards, double* startpts)
if (m_pFormatContext->iformat->read_seek)
m_seekToKeyFrame = true;

UpdateCurrentPTS();
m_currentPts = STREAM_NOPTS_VALUE;
}
}

if (ret >= 0)
{
kodi::tools::CEndTime timer(1000);
while (m_currentPts == STREAM_NOPTS_VALUE && !timer.IsTimePast())
{
m_pkt.result = -1;
av_packet_unref(&m_pkt.pkt);

DEMUX_PACKET* pkt = DemuxRead();
if (!pkt)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
m_demuxPacketManager->FreeDemuxPacketFromInputStreamAPI(pkt);
}
}

Expand Down Expand Up @@ -1771,7 +1783,7 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamAudioState()
st = m_pFormatContext->streams[idx];
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
if (st->start_time != AV_NOPTS_VALUE)
if (idx == m_pkt.pkt.stream_index && m_pkt.pkt.dts != AV_NOPTS_VALUE)
{
if (!m_startTime)
{
Expand All @@ -1791,7 +1803,7 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamAudioState()
st = m_pFormatContext->streams[i];
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
if (st->start_time != AV_NOPTS_VALUE)
if (static_cast<int>(i) == m_pkt.pkt.stream_index && m_pkt.pkt.dts != AV_NOPTS_VALUE)
{
if (!m_startTime)
{
Expand All @@ -1804,6 +1816,8 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamAudioState()
}
}
}
if (hasAudio && m_startTime)
return TRANSPORT_STREAM_STATE::READY;

return (hasAudio) ? TRANSPORT_STREAM_STATE::NOTREADY : TRANSPORT_STREAM_STATE::NONE;
}
Expand All @@ -1824,7 +1838,8 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamVideoState()
st = m_pFormatContext->streams[idx];
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
if (st->codecpar->extradata)
if (idx == m_pkt.pkt.stream_index && m_pkt.pkt.dts != AV_NOPTS_VALUE &&
st->codecpar->extradata)
{
if (!m_startTime)
{
Expand All @@ -1844,7 +1859,8 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamVideoState()
st = m_pFormatContext->streams[i];
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
if (st->codecpar->extradata)
if (static_cast<int>(i) == m_pkt.pkt.stream_index && m_pkt.pkt.dts != AV_NOPTS_VALUE &&
st->codecpar->extradata)
{
if (!m_startTime)
{
Expand All @@ -1857,6 +1873,8 @@ TRANSPORT_STREAM_STATE FFmpegStream::TransportStreamVideoState()
}
}
}
if (hasVideo && m_startTime)
return TRANSPORT_STREAM_STATE::READY;

return (hasVideo) ? TRANSPORT_STREAM_STATE::NOTREADY : TRANSPORT_STREAM_STATE::NONE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/stream/FFmpegStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class FFmpegStream

protected:
virtual std::string GetStreamCodecName(int iStreamId);
virtual void UpdateCurrentPTS();
virtual void CurrentPTSUpdated();
bool IsPaused() { return m_speed == STREAM_PLAYSPEED_PAUSE; }
virtual bool CheckReturnEmptyOnPacketResult(int result);

Expand Down

0 comments on commit 990ec69

Please sign in to comment.