From dffc23918fdfb0453977e27cba4355dc2738e415 Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Tue, 11 Jul 2023 14:35:33 +0200 Subject: [PATCH 1/3] Fallback to ADTS sample reader when TS initialization fails --- src/main.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index eb3fd023e..1c5a1b34a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -240,7 +240,7 @@ bool CInputStreamAdaptive::OpenStream(int streamid) bool needRefetch = false; //Make sure that Kodi fetches changes stream->m_isEnabled = true; - const CRepresentation* rep = stream->m_adStream.getRepresentation(); + CRepresentation* rep = stream->m_adStream.getRepresentation(); // If we select a dummy (=inside video) stream, open the video part // Dummy streams will be never enabled, they will only enable / activate audio track. @@ -302,12 +302,27 @@ bool CInputStreamAdaptive::OpenStream(int streamid) stream->SetReader(std::make_unique( stream->GetAdByteStream(), stream->m_info.GetStreamType(), streamid, mask)); - if (!stream->GetReader()->Initialize()) + if (stream->GetReader()->Initialize()) + { + m_session->OnSegmentChanged(&stream->m_adStream); + } + else if (stream->m_adStream.GetStreamType() == StreamType::AUDIO) + { + // If TSSampleReader fail, try fallback to ADTS + //! @todo: we should have an appropriate file type check + //! e.g. with HLS we determine the container type from file extension + //! in the url address, but .ts file could have ADTS + LOG::LogF(LOGWARNING, "Cannot initialize TS sample reader, fallback to ADTS sample reader"); + rep->SetContainerType(ContainerType::ADTS); + + stream->GetAdByteStream()->Seek(0); // Seek because bytes are consumed from previous reader + stream->SetReader(std::make_unique(stream->GetAdByteStream(), streamid)); + } + else { stream->Disable(); return false; } - m_session->OnSegmentChanged(&stream->m_adStream); } else if (reprContainerType == ContainerType::ADTS) { From cf3860b8e2d2eb9f5bbe7535f3d8c7010eb416da Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Tue, 11 Jul 2023 13:44:02 +0200 Subject: [PATCH 2/3] [AdaptiveStream] Fix wrong type cast --- src/common/AdaptiveStream.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/AdaptiveStream.cpp b/src/common/AdaptiveStream.cpp index 4a1423bd5..310b157d9 100644 --- a/src/common/AdaptiveStream.cpp +++ b/src/common/AdaptiveStream.cpp @@ -998,14 +998,14 @@ bool AdaptiveStream::seek(uint64_t const pos) // we seek only in the current segment if (state_ != STOPPED && pos >= absolute_position_ - segment_read_pos_) { - segment_read_pos_ = static_cast(pos - (absolute_position_ - segment_read_pos_)); + segment_read_pos_ = static_cast(pos - (absolute_position_ - segment_read_pos_)); while (segment_read_pos_ > segment_buffers_[0]->buffer.size() && worker_processing_) thread_data_->signal_rw_.wait(lckrw); if (segment_read_pos_ > segment_buffers_[0]->buffer.size()) { - segment_read_pos_ = static_cast(segment_buffers_[0]->buffer.size()); + segment_read_pos_ = segment_buffers_[0]->buffer.size(); return false; } absolute_position_ = pos; From 61f4fa52a3bbf9987e6fab10a2f7e98a0b5c53e8 Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Tue, 11 Jul 2023 14:07:55 +0200 Subject: [PATCH 3/3] [AdaptiveStream] Disabled adaptive/custom buffer length --- inputstream.adaptive/resources/settings.xml.in | 2 ++ src/common/AdaptiveStream.cpp | 15 ++++++++++----- src/common/AdaptiveStream.h | 8 ++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/inputstream.adaptive/resources/settings.xml.in b/inputstream.adaptive/resources/settings.xml.in index 1bf06ebb0..0a06d665b 100644 --- a/inputstream.adaptive/resources/settings.xml.in +++ b/inputstream.adaptive/resources/settings.xml.in @@ -174,11 +174,13 @@ 1 60 + false 1 120 + false diff --git a/src/common/AdaptiveStream.cpp b/src/common/AdaptiveStream.cpp index 310b157d9..9f5d2897e 100644 --- a/src/common/AdaptiveStream.cpp +++ b/src/common/AdaptiveStream.cpp @@ -44,8 +44,6 @@ AdaptiveStream::AdaptiveStream(AdaptiveTree& tree, current_period_(tree_.m_currentPeriod), current_adp_(adp), current_rep_(initialRepr), - available_segment_buffers_(0), - valid_segment_buffers_(0), m_streamParams(kodiProps.m_streamParams), m_streamHeaders(kodiProps.m_streamHeaders), segment_read_pos_(0), @@ -58,9 +56,7 @@ AdaptiveStream::AdaptiveStream(AdaptiveTree& tree, choose_rep_(choose_rep), rep_counter_(1), prev_rep_(0), - last_rep_(0), - assured_buffer_length_(5), - max_buffer_length_(10) + last_rep_(0) { current_rep_->current_segment_ = nullptr; @@ -598,8 +594,16 @@ bool AdaptiveStream::start_stream() //! a fixed duration of 1 sec moreover these properties currently works for //! the DASH manifest with "SegmentTemplate" tags defined only, //! in all other type of manifest cases always fallback on hardcoded values + /* + * Adaptive/custom buffering code disabled + * currently cause a bad memory management especially for 4k content + * too much buffer length leads to filling the RAM and cause kodi to crash + * required to implement a way to determine the max length of the buffer + * by taking in account also the device RAM + * assured_buffer_length_ = current_rep_->assured_buffer_duration_; max_buffer_length_ = current_rep_->max_buffer_duration_; + if (current_rep_->HasSegmentTemplate()) { const auto& segTemplate = current_rep_->GetSegmentTemplate(); @@ -608,6 +612,7 @@ bool AdaptiveStream::start_stream() max_buffer_length_ = std::ceil((max_buffer_length_ * segTemplate->GetTimescale()) / static_cast(segTemplate->GetDuration())); } + */ assured_buffer_length_ = assured_buffer_length_ <4 ? 4:assured_buffer_length_;//for incorrect settings input if(max_buffer_length_<=assured_buffer_length_)//for incorrect settings input max_buffer_length_=assured_buffer_length_+4u; diff --git a/src/common/AdaptiveStream.h b/src/common/AdaptiveStream.h index 9a6a7c37f..1347e3087 100644 --- a/src/common/AdaptiveStream.h +++ b/src/common/AdaptiveStream.h @@ -221,11 +221,11 @@ class AdaptiveStream; uint8_t m_decrypterIv[16]; // number of segmentbuffers whith valid segment, always >= valid_segment_buffers_ - size_t available_segment_buffers_; + size_t available_segment_buffers_{0}; // number of segment_buffers which are downloaded / downloading - uint32_t assured_buffer_length_; - uint32_t max_buffer_length_; - size_t valid_segment_buffers_; + uint32_t assured_buffer_length_{0}; + uint32_t max_buffer_length_{0}; + size_t valid_segment_buffers_{0}; uint32_t rep_counter_; PLAYLIST::CRepresentation* prev_rep_; // used for rep_counter_ PLAYLIST::CRepresentation* last_rep_; // used to align new live rep with old