Shaka Packager SDK
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(*streams()[0]);
26  FireOnMediaStartEvent();
27  return status;
28 }
29 
30 Status TsMuxer::Finalize() {
31  FireOnMediaEndEvent();
32  return segmenter_->Finalize();
33 }
34 
35 Status TsMuxer::AddMediaSample(size_t stream_id, const MediaSample& sample) {
36  DCHECK_EQ(stream_id, 0u);
37  if (num_samples_ < 2) {
38  sample_durations_[num_samples_] =
39  sample.duration() * kTsTimescale / streams().front()->time_scale();
40  if (num_samples_ == 1 && muxer_listener())
41  muxer_listener()->OnSampleDurationReady(sample_durations_[num_samples_]);
42  num_samples_++;
43  }
44  return segmenter_->AddSample(sample);
45 }
46 
47 Status TsMuxer::FinalizeSegment(size_t stream_id,
48  const SegmentInfo& segment_info) {
49  DCHECK_EQ(stream_id, 0u);
50  return segment_info.is_subsegment
51  ? Status::OK
52  : segmenter_->FinalizeSegment(segment_info.start_timestamp,
53  segment_info.duration);
54 }
55 
56 void TsMuxer::FireOnMediaStartEvent() {
57  if (!muxer_listener())
58  return;
59  muxer_listener()->OnMediaStart(options(), *streams().front(), kTsTimescale,
60  MuxerListener::kContainerMpeg2ts);
61 }
62 
63 void TsMuxer::FireOnMediaEndEvent() {
64  if (!muxer_listener())
65  return;
66 
67  // For now, there is no single file TS segmenter. So all the values passed
68  // here are left empty.
69  MuxerListener::MediaRanges range;
70  muxer_listener()->OnMediaEnd(range, 0);
71 }
72 
73 } // namespace mp2t
74 } // namespace media
75 } // namespace shaka
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::MuxerListener::OnSampleDurationReady
virtual void OnSampleDurationReady(uint32_t sample_duration)=0
shaka::media::MuxerListener::OnMediaEnd
virtual void OnMediaEnd(const MediaRanges &media_ranges, float duration_seconds)=0
shaka::media::MuxerListener::OnMediaStart
virtual void OnMediaStart(const MuxerOptions &muxer_options, const StreamInfo &stream_info, uint32_t time_scale, ContainerType container_type)=0