Shaka Packager SDK
webm_muxer.cc
1 // Copyright 2015 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/webm/webm_muxer.h"
8 
9 #include "packager/media/base/fourccs.h"
10 #include "packager/media/base/media_sample.h"
11 #include "packager/media/base/stream_info.h"
12 #include "packager/media/formats/webm/mkv_writer.h"
13 #include "packager/media/formats/webm/multi_segment_segmenter.h"
14 #include "packager/media/formats/webm/single_segment_segmenter.h"
15 #include "packager/media/formats/webm/two_pass_single_segment_segmenter.h"
16 
17 namespace shaka {
18 namespace media {
19 namespace webm {
20 
21 WebMMuxer::WebMMuxer(const MuxerOptions& options) : Muxer(options) {}
22 WebMMuxer::~WebMMuxer() {}
23 
24 Status WebMMuxer::InitializeMuxer() {
25  CHECK_EQ(streams().size(), 1U);
26 
27  if (streams()[0]->is_encrypted() &&
28  streams()[0]->encryption_config().protection_scheme != FOURCC_cenc) {
29  LOG(ERROR) << "WebM does not support protection scheme other than 'cenc'.";
30  return Status(error::INVALID_ARGUMENT,
31  "WebM does not support protection scheme other than 'cenc'.");
32  }
33 
34  if (!options().segment_template.empty()) {
35  segmenter_.reset(new MultiSegmentSegmenter(options()));
36  } else {
37  segmenter_.reset(new TwoPassSingleSegmentSegmenter(options()));
38  }
39 
40  Status initialized = segmenter_->Initialize(
41  *streams()[0], progress_listener(), muxer_listener());
42  if (!initialized.ok())
43  return initialized;
44 
45  FireOnMediaStartEvent();
46  return Status::OK;
47 }
48 
49 Status WebMMuxer::Finalize() {
50  if (!segmenter_)
51  return Status::OK;
52  Status segmenter_finalized = segmenter_->Finalize();
53 
54  if (!segmenter_finalized.ok())
55  return segmenter_finalized;
56 
57  FireOnMediaEndEvent();
58  LOG(INFO) << "WEBM file '" << options().output_file_name << "' finalized.";
59  return Status::OK;
60 }
61 
62 Status WebMMuxer::AddMediaSample(size_t stream_id, const MediaSample& sample) {
63  DCHECK(segmenter_);
64  DCHECK_EQ(stream_id, 0u);
65  if (sample.pts() < 0) {
66  LOG(ERROR) << "Seeing negative timestamp " << sample.pts();
67  return Status(error::MUXER_FAILURE, "Unsupported negative timestamp.");
68  }
69  return segmenter_->AddSample(sample);
70 }
71 
72 Status WebMMuxer::FinalizeSegment(size_t stream_id,
73  const SegmentInfo& segment_info) {
74  DCHECK(segmenter_);
75  DCHECK_EQ(stream_id, 0u);
76 
77  if (segment_info.key_rotation_encryption_config) {
78  NOTIMPLEMENTED() << "Key rotation is not implemented for WebM.";
79  return Status(error::UNIMPLEMENTED,
80  "Key rotation is not implemented for WebM");
81  }
82  return segmenter_->FinalizeSegment(segment_info.start_timestamp,
83  segment_info.duration,
84  segment_info.is_subsegment);
85 }
86 
87 void WebMMuxer::FireOnMediaStartEvent() {
88  if (!muxer_listener())
89  return;
90 
91  DCHECK(!streams().empty()) << "Media started without a stream.";
92 
93  const uint32_t timescale = streams().front()->time_scale();
94  muxer_listener()->OnMediaStart(options(), *streams().front(), timescale,
95  MuxerListener::kContainerWebM);
96 }
97 
98 void WebMMuxer::FireOnMediaEndEvent() {
99  if (!muxer_listener())
100  return;
101 
102  MuxerListener::MediaRanges media_range;
103 
104  uint64_t init_range_start = 0;
105  uint64_t init_range_end = 0;
106  const bool has_init_range =
107  segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
108  if (has_init_range) {
109  Range r;
110  r.start = init_range_start;
111  r.end = init_range_end;
112  media_range.init_range = r;
113  }
114 
115  uint64_t index_range_start = 0;
116  uint64_t index_range_end = 0;
117  const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
118  &index_range_start, &index_range_end);
119  if (has_index_range) {
120  Range r;
121  r.start = index_range_start;
122  r.end = index_range_end;
123  media_range.index_range = r;
124  }
125 
126  media_range.subsegment_ranges = segmenter_->GetSegmentRanges();
127 
128  const float duration_seconds = segmenter_->GetDurationInSeconds();
129  muxer_listener()->OnMediaEnd(media_range, duration_seconds);
130 }
131 
132 } // namespace webm
133 } // namespace media
134 } // namespace shaka
virtual void OnMediaEnd(const MediaRanges &media_ranges, float duration_seconds)=0
virtual void OnMediaStart(const MuxerOptions &muxer_options, const StreamInfo &stream_info, uint32_t time_scale, ContainerType container_type)=0
WebMMuxer(const MuxerOptions &options)
Create a WebMMuxer object from MuxerOptions.
Definition: webm_muxer.cc:21
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20