2016-03-21 19:16:58 +00:00
|
|
|
// Copyright 2016 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
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2016-03-21 19:16:58 +00:00
|
|
|
#include "packager/media/base/media_stream.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
|
|
|
#include "packager/media/base/status.h"
|
|
|
|
#include "packager/media/file/file.h"
|
|
|
|
#include "packager/media/formats/mp2t/pes_packet_generator.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_writer.h"
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-21 19:16:58 +00:00
|
|
|
namespace media {
|
2016-04-16 22:58:47 +00:00
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
class KeySource;
|
2016-04-16 22:58:47 +00:00
|
|
|
class MuxerListener;
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
// TODO(rkuroiwa): For now, this implements multifile segmenter. Like other
|
|
|
|
// make this an abstract super class and implement multifile and single file
|
|
|
|
// segmenters.
|
|
|
|
class TsSegmenter {
|
|
|
|
public:
|
2016-04-27 22:04:50 +00:00
|
|
|
// TODO(rkuroiwa): Add progress listener?
|
2016-03-21 19:16:58 +00:00
|
|
|
/// @param options is the options for this muxer. This must stay valid
|
|
|
|
/// throughout the life time of the instance.
|
2016-04-16 22:58:47 +00:00
|
|
|
/// @param listener is the MuxerListener that should be used to notify events.
|
|
|
|
/// This may be null, in which case no events are sent.
|
|
|
|
TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
|
2016-03-21 19:16:58 +00:00
|
|
|
~TsSegmenter();
|
|
|
|
|
|
|
|
/// Initialize the object.
|
2016-04-27 22:04:50 +00:00
|
|
|
/// Key rotation is not supported.
|
2016-03-21 19:16:58 +00:00
|
|
|
/// @param stream_info is the stream info for the segmenter.
|
|
|
|
/// @return OK on success.
|
2016-04-27 22:04:50 +00:00
|
|
|
Status Initialize(const StreamInfo& stream_info,
|
|
|
|
KeySource* encryption_key_source,
|
|
|
|
uint32_t max_sd_pixels,
|
2016-11-11 23:17:17 +00:00
|
|
|
uint32_t max_hd_pixels,
|
|
|
|
uint32_t max_uhd1_pixels,
|
2016-04-27 22:04:50 +00:00
|
|
|
double clear_lead_in_seconds);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Finalize the segmenter.
|
|
|
|
/// @return OK on success.
|
|
|
|
Status Finalize();
|
|
|
|
|
|
|
|
/// @param sample gets added to this object.
|
|
|
|
/// @return OK on success.
|
|
|
|
Status AddSample(scoped_refptr<MediaSample> sample);
|
|
|
|
|
|
|
|
/// Only for testing.
|
2016-08-17 17:41:40 +00:00
|
|
|
void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Only for testing.
|
|
|
|
void InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacketGenerator> generator);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Only for testing.
|
|
|
|
void SetTsWriterFileOpenedForTesting(bool value);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Status OpenNewSegmentIfClosed(uint32_t next_pts);
|
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
// Writes PES packets (carried in TsPackets) to a file. If a file is not open,
|
|
|
|
// it will open one. This will not close the file.
|
|
|
|
Status WritePesPacketsToFile();
|
|
|
|
|
|
|
|
// Flush all the samples that are (possibly) buffered and write them to the
|
|
|
|
// current segment, this will close the file. If a file is not already opened
|
|
|
|
// before calling this, this will open one and write them to file.
|
|
|
|
Status Flush();
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
// If conditions are met, notify objects that the data is encrypted.
|
|
|
|
Status NotifyEncrypted();
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
const MuxerOptions& muxer_options_;
|
2016-04-16 22:58:47 +00:00
|
|
|
MuxerListener* const listener_;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-04-19 01:00:06 +00:00
|
|
|
// Scale used to scale the input stream to TS's timesccale (which is 90000).
|
|
|
|
// Used for calculating the duration in seconds fo the current segment.
|
|
|
|
double timescale_scale_ = 1.0;
|
|
|
|
|
|
|
|
// This is the sum of the durations of the samples that were added to
|
|
|
|
// PesPacketGenerator for the current segment (in seconds). Note that this is
|
|
|
|
// not necessarily the same as the length of the PesPackets that have been
|
|
|
|
// written to the current segment in WritePesPacketsToFile().
|
2016-04-06 05:00:05 +00:00
|
|
|
double current_segment_total_sample_duration_ = 0.0;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
// Used for segment template.
|
|
|
|
uint64_t segment_number_ = 0;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsWriter> ts_writer_;
|
2016-03-21 19:16:58 +00:00
|
|
|
// Set to true if TsWriter::NewFile() succeeds, set to false after
|
|
|
|
// TsWriter::FinalizeFile() succeeds.
|
|
|
|
bool ts_writer_file_opened_ = false;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
// For OnNewSegment().
|
|
|
|
uint64_t current_segment_start_time_ = 0;
|
|
|
|
// Path of the current segment so that File::GetFileSize() can be used after
|
|
|
|
// the segment has been finalized.
|
|
|
|
std::string current_segment_path_;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EncryptionKey> encryption_key_;
|
2016-04-27 22:04:50 +00:00
|
|
|
double clear_lead_in_seconds_ = 0;
|
|
|
|
|
|
|
|
// The total duration of the segments that it has segmented. This only
|
|
|
|
// includes segments that have been finailzed. IOW, this does not count the
|
|
|
|
// current segments duration.
|
|
|
|
double total_duration_in_seconds_ = 0.0;
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2016-03-21 19:16:58 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|