DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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/media_stream.h"
12 #include "packager/media/base/stream_info.h"
13 #include "packager/media/formats/webm/mkv_writer.h"
14 #include "packager/media/formats/webm/multi_segment_segmenter.h"
15 #include "packager/media/formats/webm/single_segment_segmenter.h"
16 #include "packager/media/formats/webm/two_pass_single_segment_segmenter.h"
17 
18 namespace shaka {
19 namespace media {
20 namespace webm {
21 
22 WebMMuxer::WebMMuxer(const MuxerOptions& options) : Muxer(options) {}
23 WebMMuxer::~WebMMuxer() {}
24 
25 Status WebMMuxer::Initialize() {
26  CHECK_EQ(streams().size(), 1U);
27 
28  if (crypto_period_duration_in_seconds() > 0) {
29  NOTIMPLEMENTED() << "Key rotation is not implemented for WebM";
30  return Status(error::UNIMPLEMENTED,
31  "Key rotation is not implemented for WebM");
32  }
33 
34  if (encryption_key_source() && (protection_scheme() != FOURCC_cenc)) {
35  NOTIMPLEMENTED()
36  << "WebM does not support protection scheme other than 'cenc'.";
37  return Status(error::UNIMPLEMENTED,
38  "WebM does not support protection scheme other than 'cenc'.");
39  }
40 
41  std::unique_ptr<MkvWriter> writer(new MkvWriter);
42  Status status = writer->Open(options().output_file_name);
43  if (!status.ok())
44  return status;
45 
46  if (!options().segment_template.empty()) {
47  segmenter_.reset(new MultiSegmentSegmenter(options()));
48  } else {
49  segmenter_.reset(new TwoPassSingleSegmentSegmenter(options()));
50  }
51 
52  Status initialized = segmenter_->Initialize(
53  std::move(writer), streams()[0]->info().get(), progress_listener(),
54  muxer_listener(), encryption_key_source(), max_sd_pixels(),
55  max_hd_pixels(), max_uhd1_pixels(), clear_lead_in_seconds());
56 
57  if (!initialized.ok())
58  return initialized;
59 
60  FireOnMediaStartEvent();
61  return Status::OK;
62 }
63 
64 Status WebMMuxer::Finalize() {
65  DCHECK(segmenter_);
66  Status segmenter_finalized = segmenter_->Finalize();
67 
68  if (!segmenter_finalized.ok())
69  return segmenter_finalized;
70 
71  FireOnMediaEndEvent();
72  LOG(INFO) << "WEBM file '" << options().output_file_name << "' finalized.";
73  return Status::OK;
74 }
75 
76 Status WebMMuxer::DoAddSample(const MediaStream* stream,
77  scoped_refptr<MediaSample> sample) {
78  DCHECK(segmenter_);
79  DCHECK(stream == streams()[0]);
80  return segmenter_->AddSample(sample);
81 }
82 
83 void WebMMuxer::FireOnMediaStartEvent() {
84  if (!muxer_listener())
85  return;
86 
87  DCHECK(!streams().empty()) << "Media started without a stream.";
88 
89  const uint32_t timescale = streams().front()->info()->time_scale();
90  muxer_listener()->OnMediaStart(options(), *streams().front()->info(),
91  timescale, MuxerListener::kContainerWebM);
92 }
93 
94 void WebMMuxer::FireOnMediaEndEvent() {
95  if (!muxer_listener())
96  return;
97 
98  uint64_t init_range_start = 0;
99  uint64_t init_range_end = 0;
100  const bool has_init_range =
101  segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
102 
103  uint64_t index_range_start = 0;
104  uint64_t index_range_end = 0;
105  const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
106  &index_range_start, &index_range_end);
107 
108  const float duration_seconds = segmenter_->GetDuration();
109 
110  const int64_t file_size =
111  File::GetFileSize(options().output_file_name.c_str());
112  if (file_size <= 0) {
113  LOG(ERROR) << "Invalid file size: " << file_size;
114  return;
115  }
116 
117  muxer_listener()->OnMediaEnd(has_init_range, init_range_start, init_range_end,
118  has_index_range, index_range_start,
119  index_range_end, duration_seconds, file_size);
120 }
121 
122 } // namespace webm
123 } // namespace media
124 } // 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
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
static int64_t GetFileSize(const char *file_name)
Definition: file.cc:176
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:22