DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/media_sample.h"
10 #include "packager/media/base/media_stream.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 edash_packager {
18 namespace media {
19 namespace webm {
20 
21 WebMMuxer::WebMMuxer(const MuxerOptions& options) : Muxer(options) {}
22 WebMMuxer::~WebMMuxer() {}
23 
24 Status WebMMuxer::Initialize() {
25  CHECK_EQ(streams().size(), 1U);
26 
27  scoped_ptr<MkvWriter> writer(new MkvWriter);
28  Status status = writer->Open(options().output_file_name);
29  if (!status.ok())
30  return status;
31 
32  if (!options().single_segment) {
33  segmenter_.reset(new MultiSegmentSegmenter(options()));
34  } else if (writer->Seekable()) {
35  segmenter_.reset(new SingleSegmentSegmenter(options()));
36  } else {
37  segmenter_.reset(new TwoPassSingleSegmentSegmenter(options()));
38  }
39 
40  Status initialized = segmenter_->Initialize(
41  writer.Pass(), streams()[0]->info().get(), progress_listener(),
42  muxer_listener(), encryption_key_source());
43 
44  if (!initialized.ok())
45  return initialized;
46 
47  FireOnMediaStartEvent();
48  return Status::OK;
49 }
50 
51 Status WebMMuxer::Finalize() {
52  DCHECK(segmenter_);
53  Status segmenter_finalized = segmenter_->Finalize();
54 
55  if (!segmenter_finalized.ok())
56  return segmenter_finalized;
57 
58  FireOnMediaEndEvent();
59  LOG(INFO) << "WEBM file '" << options().output_file_name << "' finalized.";
60  return Status::OK;
61 }
62 
63 Status WebMMuxer::DoAddSample(const MediaStream* stream,
64  scoped_refptr<MediaSample> sample) {
65  DCHECK(segmenter_);
66  DCHECK(stream == streams()[0]);
67  return segmenter_->AddSample(sample);
68 }
69 
70 void WebMMuxer::FireOnMediaStartEvent() {
71  if (!muxer_listener())
72  return;
73 
74  DCHECK(!streams().empty()) << "Media started without a stream.";
75 
76  const uint32_t timescale = streams().front()->info()->time_scale();
77  muxer_listener()->OnMediaStart(options(), *streams().front()->info(),
78  timescale, MuxerListener::kContainerWebM);
79 }
80 
81 void WebMMuxer::FireOnMediaEndEvent() {
82  if (!muxer_listener())
83  return;
84 
85  uint32_t init_range_start = 0;
86  uint32_t init_range_end = 0;
87  const bool has_init_range =
88  segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
89 
90  uint32_t index_range_start = 0;
91  uint32_t index_range_end = 0;
92  const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
93  &index_range_start, &index_range_end);
94 
95  const float duration_seconds = segmenter_->GetDuration();
96 
97  const int64_t file_size =
98  File::GetFileSize(options().output_file_name.c_str());
99  if (file_size <= 0) {
100  LOG(ERROR) << "Invalid file size: " << file_size;
101  return;
102  }
103 
104  muxer_listener()->OnMediaEnd(has_init_range, init_range_start, init_range_end,
105  has_index_range, index_range_start,
106  index_range_end, duration_seconds, file_size);
107 }
108 
109 } // namespace webm
110 } // namespace media
111 } // namespace edash_packager
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
WebMMuxer(const MuxerOptions &options)
Create a WebMMuxer object from MuxerOptions.
Definition: webm_muxer.cc:21
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:175