DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
ts_muxer.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/mp2t/ts_muxer.h"
8 
9 namespace shaka {
10 namespace media {
11 namespace mp2t {
12 
13 namespace {
14 const uint32_t kTsTimescale = 90000;
15 } // namespace
16 
17 TsMuxer::TsMuxer(const MuxerOptions& muxer_options) : Muxer(muxer_options) {}
18 TsMuxer::~TsMuxer() {}
19 
20 Status TsMuxer::InitializeMuxer() {
21  if (streams().size() > 1u)
22  return Status(error::MUXER_FAILURE, "Cannot handle more than one streams.");
23 
24  segmenter_.reset(new TsSegmenter(options(), muxer_listener()));
25  Status status = segmenter_->Initialize(
26  *streams()[0], encryption_key_source(), max_sd_pixels(), max_hd_pixels(),
27  max_uhd1_pixels(), clear_lead_in_seconds());
28  FireOnMediaStartEvent();
29  return status;
30 }
31 
32 Status TsMuxer::Finalize() {
33  FireOnMediaEndEvent();
34  return segmenter_->Finalize();
35 }
36 
37 Status TsMuxer::AddSample(int stream_id, std::shared_ptr<MediaSample> sample) {
38  DCHECK_EQ(stream_id, 0);
39  return segmenter_->AddSample(sample);
40 }
41 
42 Status TsMuxer::FinalizeSegment(int stream_id,
43  std::shared_ptr<SegmentInfo> segment_info) {
44  DCHECK_EQ(stream_id, 0);
45  return segment_info->is_subsegment
46  ? Status::OK
47  : segmenter_->FinalizeSegment(segment_info->start_timestamp,
48  segment_info->duration);
49 }
50 
51 void TsMuxer::FireOnMediaStartEvent() {
52  if (!muxer_listener())
53  return;
54  muxer_listener()->OnMediaStart(options(), *streams().front(), kTsTimescale,
55  MuxerListener::kContainerWebM);
56 }
57 
58 void TsMuxer::FireOnMediaEndEvent() {
59  if (!muxer_listener())
60  return;
61 
62  // For now, there is no single file TS segmenter. So all the values passed
63  // here are false and 0. Called just to notify the MuxerListener.
64  const bool kHasInitRange = true;
65  const bool kHasIndexRange = true;
66  muxer_listener()->OnMediaEnd(!kHasInitRange, 0, 0, !kHasIndexRange, 0, 0, 0,
67  0);
68 }
69 
70 } // namespace mp2t
71 } // namespace media
72 } // namespace shaka
virtual void OnMediaEnd(bool has_init_range, uint64_t init_range_start, uint64_t init_range_end, bool has_index_range, uint64_t index_range_start, uint64_t index_range_end, float duration_seconds, uint64_t file_size)=0
virtual void OnMediaStart(const MuxerOptions &muxer_options, const StreamInfo &stream_info, uint32_t time_scale, ContainerType container_type)=0