Skip to content

Commit

Permalink
Merge pull request #1323 from CastagnaIT/fixes
Browse files Browse the repository at this point in the history
Fixes for buffer and recent regressions
  • Loading branch information
glennguy authored Jul 12, 2023
2 parents 70dd055 + 61f4fa5 commit 1e72d38
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 2 additions & 0 deletions inputstream.adaptive/resources/settings.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,13 @@
<setting id="ASSUREDBUFFERDURATION" type="integer" label="30200" help="30167">
<level>1</level>
<default>60</default>
<visible>false</visible> <!-- Working code disabled, rework needed -->
<control type="edit" format="integer" />
</setting>
<setting id="MAXBUFFERDURATION" type="integer" label="30201" help="30167">
<level>1</level>
<default>120</default>
<visible>false</visible> <!-- Working code disabled, rework needed -->
<control type="edit" format="integer" />
</setting>
<setting id="MEDIATYPE" type="integer" label="30112">
Expand Down
19 changes: 12 additions & 7 deletions src/common/AdaptiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -608,6 +612,7 @@ bool AdaptiveStream::start_stream()
max_buffer_length_ = std::ceil((max_buffer_length_ * segTemplate->GetTimescale()) /
static_cast<float>(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;
Expand Down Expand Up @@ -998,14 +1003,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<uint32_t>(pos - (absolute_position_ - segment_read_pos_));
segment_read_pos_ = static_cast<size_t>(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<uint32_t>(segment_buffers_[0]->buffer.size());
segment_read_pos_ = segment_buffers_[0]->buffer.size();
return false;
}
absolute_position_ = pos;
Expand Down
8 changes: 4 additions & 4 deletions src/common/AdaptiveStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -302,12 +302,27 @@ bool CInputStreamAdaptive::OpenStream(int streamid)
stream->SetReader(std::make_unique<CTSSampleReader>(
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<CADTSSampleReader>(stream->GetAdByteStream(), streamid));
}
else
{
stream->Disable();
return false;
}
m_session->OnSegmentChanged(&stream->m_adStream);
}
else if (reprContainerType == ContainerType::ADTS)
{
Expand Down

0 comments on commit 1e72d38

Please sign in to comment.