2017-09-18 15:43:21 +00:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include "packager/media/formats/webvtt/webvtt_segmenter.h"
|
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
namespace {
|
|
|
|
const size_t kStreamIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebVttSegmenter::WebVttSegmenter(uint64_t segment_duration_ms)
|
|
|
|
: segment_duration_ms_(segment_duration_ms) {}
|
|
|
|
|
|
|
|
Status WebVttSegmenter::InitializeInternal() {
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WebVttSegmenter::Process(std::unique_ptr<StreamData> stream_data) {
|
|
|
|
switch (stream_data->stream_data_type) {
|
|
|
|
case StreamDataType::kStreamInfo:
|
|
|
|
return DispatchStreamInfo(kStreamIndex,
|
|
|
|
std::move(stream_data->stream_info));
|
|
|
|
case StreamDataType::kTextSample:
|
|
|
|
return OnTextSample(stream_data->text_sample);
|
|
|
|
default:
|
|
|
|
return Status(error::INTERNAL_ERROR,
|
|
|
|
"Invalid stream data type for this handler");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WebVttSegmenter::OnFlushRequest(size_t input_stream_index) {
|
2018-02-05 17:55:58 +00:00
|
|
|
while (segment_map_.size()) {
|
|
|
|
uint64_t segment = segment_map_.begin()->first;
|
|
|
|
|
|
|
|
// OnSegmentEnd will remove the segment from the map.
|
|
|
|
Status status = OnSegmentEnd(segment);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
2018-02-05 17:55:58 +00:00
|
|
|
|
|
|
|
return FlushAllDownstreams();
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status WebVttSegmenter::OnTextSample(std::shared_ptr<const TextSample> sample) {
|
|
|
|
const uint64_t start_segment = sample->start_time() / segment_duration_ms_;
|
|
|
|
|
|
|
|
// Find the last segment that overlaps the sample. Adjust the sample by one
|
|
|
|
// ms (smallest time unit) in case |EndTime| falls on the segment boundary.
|
|
|
|
DCHECK_GT(sample->duration(), 0u);
|
|
|
|
const uint64_t ending_segment =
|
|
|
|
(sample->EndTime() - 1) / segment_duration_ms_;
|
|
|
|
|
|
|
|
DCHECK_GE(ending_segment, start_segment);
|
|
|
|
|
|
|
|
// Samples must always be advancing. If a sample comes in out of order,
|
|
|
|
// skip the sample.
|
2018-02-05 17:55:58 +00:00
|
|
|
if (head_segment_ > start_segment) {
|
2017-09-18 15:43:21 +00:00
|
|
|
LOG(WARNING) << "New sample has arrived out of order. Skipping sample "
|
|
|
|
<< "as segment start is " << start_segment << " and segment "
|
2018-02-05 17:55:58 +00:00
|
|
|
<< "head is " << head_segment_ << ".";
|
2017-09-18 15:43:21 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:55:58 +00:00
|
|
|
// Now that we are accepting this new segment, its starting segment is our
|
|
|
|
// new head segment.
|
|
|
|
head_segment_ = start_segment;
|
2017-09-18 15:43:21 +00:00
|
|
|
|
2018-02-05 17:55:58 +00:00
|
|
|
// Add the sample to each segment it spans.
|
|
|
|
for (uint64_t segment = start_segment; segment <= ending_segment; segment++) {
|
|
|
|
segment_map_[segment].push_back(sample);
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:10:13 +00:00
|
|
|
// Output all the segments that come before the start of this cue's first
|
|
|
|
// segment.
|
2018-02-05 17:55:58 +00:00
|
|
|
while (segment_map_.size()) {
|
|
|
|
// Since the segments are in accending order, we can break out of the loop
|
|
|
|
// once we catch-up to the new samples starting segment.
|
|
|
|
const uint64_t segment = segment_map_.begin()->first;
|
|
|
|
if (segment >= start_segment) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the segment. If there is an error, there is no reason to continue.
|
|
|
|
Status status = OnSegmentEnd(segment);
|
2018-01-31 19:10:13 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:10:13 +00:00
|
|
|
return Status::OK;
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:10:13 +00:00
|
|
|
Status WebVttSegmenter::OnSegmentEnd(uint64_t segment) {
|
2018-01-02 21:58:47 +00:00
|
|
|
Status status;
|
2018-02-05 17:55:58 +00:00
|
|
|
|
|
|
|
const auto found = segment_map_.find(segment);
|
|
|
|
if (found != segment_map_.end()) {
|
|
|
|
for (const auto& sample : found->second) {
|
|
|
|
status.Update(DispatchTextSample(kStreamIndex, sample));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop storing the segment.
|
|
|
|
segment_map_.erase(found);
|
2017-09-18 15:43:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 21:58:47 +00:00
|
|
|
// Only send the segment info if all the samples were accepted.
|
|
|
|
if (status.ok()) {
|
|
|
|
std::shared_ptr<SegmentInfo> info = std::make_shared<SegmentInfo>();
|
|
|
|
info->start_timestamp = segment * segment_duration_ms_;
|
|
|
|
info->duration = segment_duration_ms_;
|
|
|
|
|
|
|
|
status.Update(DispatchSegmentInfo(kStreamIndex, std::move(info)));
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:43:21 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|