2015-11-23 23:12:04 +00:00
|
|
|
// Copyright 2015 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
|
|
|
|
|
2017-02-13 22:25:34 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_SAMPLE_CONVERTER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_SAMPLE_CONVERTER_H_
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include "packager/media/base/status.h"
|
2017-02-13 22:25:34 +00:00
|
|
|
#include "packager/media/formats/mp4/box.h"
|
2015-11-23 23:12:04 +00:00
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
2017-02-13 22:25:34 +00:00
|
|
|
#include "packager/media/formats/webvtt/cue.h"
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
/// Appends box to vector.
|
|
|
|
/// @param box is the box to be serialized.
|
|
|
|
/// @param output_vector is where the data is appended.
|
2017-02-13 22:25:34 +00:00
|
|
|
void AppendBoxToVector(mp4::Box* box, std::vector<uint8_t>* output_vector);
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
/// According to the spec, when cues overlap, samples must be created.\n
|
|
|
|
/// The example below has 2 WebVTT cues:\n
|
|
|
|
/// 00:01:00.000 --> 00:02:00.000\n
|
|
|
|
/// hello\n
|
|
|
|
///\n
|
|
|
|
/// 00:01:15.000 --> 00:02:15.000\n
|
|
|
|
/// how are you?\n
|
|
|
|
///\n
|
|
|
|
/// These are added (AddSample()) as 2 samples but must be split into 3 samples
|
|
|
|
/// and 4 cues ('vttc' boxes).\n
|
|
|
|
/// First sample:\n
|
|
|
|
/// start_time: 00:01:00.000\n
|
|
|
|
/// duration: 15 seconds\n
|
|
|
|
/// cue payload: hello\n
|
|
|
|
///\n
|
|
|
|
/// Second sample:\n
|
|
|
|
/// start_time: 00:01:15.000\n
|
|
|
|
/// duration: 45 seconds\n
|
|
|
|
/// cue payload: hello\n
|
|
|
|
/// cue payload: how are you?\n
|
|
|
|
///\n
|
|
|
|
/// Third sample:\n
|
|
|
|
/// start_time: 00:02:00.000\n
|
|
|
|
/// duration: 15 seconds\n
|
|
|
|
/// cue payload: how are you?\n
|
|
|
|
///\n
|
|
|
|
/// This class buffers the samples that are passed to AddSample() and creates
|
|
|
|
/// more samples as necessary.
|
2017-02-13 22:25:34 +00:00
|
|
|
class WebVttSampleConverter {
|
2015-11-23 23:12:04 +00:00
|
|
|
public:
|
2017-02-13 22:25:34 +00:00
|
|
|
WebVttSampleConverter();
|
|
|
|
~WebVttSampleConverter();
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
/// Add a sample.
|
|
|
|
/// @param sample is the sample to be added. It should contain one VTT cue.
|
|
|
|
void PushSample(std::shared_ptr<MediaSample> sample);
|
|
|
|
|
|
|
|
/// Process all the buffered samples.
|
|
|
|
/// This finalizes the object and further calls to PushSample() may result in
|
|
|
|
/// an undefined behavior.
|
|
|
|
void Flush();
|
|
|
|
|
|
|
|
/// @return The number of samples that are processed and ready to be popped.
|
|
|
|
size_t ReadySamplesSize();
|
|
|
|
|
|
|
|
/// Returns a MediaSample that is non-overlapping with the previous samples
|
|
|
|
/// that it has output. The data in the sample is one or more ISO-BMFF boxes
|
|
|
|
/// for the duration of the sample.
|
|
|
|
/// @return The first sample that is ready to be processed.
|
|
|
|
std::shared_ptr<MediaSample> PopSample();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Handle |cues_| except the last item, and create samples from them.
|
|
|
|
// All cues that overlap with the latest cue are not processed.
|
|
|
|
// Usually the last cue (and cues that overlap with it) should not be
|
|
|
|
// processed right away because the following cues may overlap with the latest
|
|
|
|
// cue or the existing cues.
|
|
|
|
// If a cue has been proceessed, then this returns true.
|
|
|
|
bool HandleAllCuesButLatest();
|
|
|
|
|
|
|
|
// Same as HandleAllCuesButLatest() but it also includes the latest cue.
|
|
|
|
// If a cue has been processed, then this returns true.
|
|
|
|
bool HandleAllCues();
|
|
|
|
|
|
|
|
// Sweep line algorithm that handles the cues in |cues_|.
|
|
|
|
// This does not erase |cues_|.
|
|
|
|
// If a cue has been processed, this returns true.
|
|
|
|
// |sweep_line| is the start time and |sweep_stop_time| is when the sweep
|
|
|
|
// should stop.
|
|
|
|
bool SweepCues(uint64_t sweep_line, uint64_t sweep_stop_time);
|
|
|
|
|
|
|
|
// This is going to be in 'mdat' box. Keep this around until a sample is
|
|
|
|
// ready.
|
|
|
|
std::list<Cue> cues_;
|
|
|
|
|
|
|
|
// For comment samples.
|
2017-02-13 22:25:34 +00:00
|
|
|
std::list<mp4::VTTAdditionalTextBox> additional_texts_;
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
// Samples that are ready to be processed.
|
|
|
|
std::list<std::shared_ptr<MediaSample>> ready_samples_;
|
|
|
|
|
|
|
|
// This keeps track of the max end time of the processed cues which is the
|
|
|
|
// start time of the next cue. Used to check if cue_current_time has to be set
|
|
|
|
// or an empty cue (gap) has to be added.
|
|
|
|
uint64_t next_cue_start_time_;
|
|
|
|
|
2017-02-13 22:25:34 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WebVttSampleConverter);
|
2015-11-23 23:12:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2017-02-13 22:25:34 +00:00
|
|
|
} // namespace shaka
|
2015-11-23 23:12:04 +00:00
|
|
|
|
2017-02-13 22:25:34 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_SAMPLE_CONVERTER_H_
|