DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
segmenter.h
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 #ifndef MEDIA_FORMATS_WEBM_SEGMENTER_H_
8 #define MEDIA_FORMATS_WEBM_SEGMENTER_H_
9 
10 #include "packager/base/memory/ref_counted.h"
11 #include "packager/base/memory/scoped_ptr.h"
12 #include "packager/media/base/status.h"
13 #include "packager/media/formats/webm/mkv_writer.h"
14 #include "packager/media/formats/webm/seek_head.h"
15 #include "packager/third_party/libwebm/src/mkvmuxer.hpp"
16 
17 namespace edash_packager {
18 namespace media {
19 
20 struct MuxerOptions;
21 
22 class AudioStreamInfo;
23 class KeySource;
24 class MediaSample;
25 class MediaStream;
26 class MuxerListener;
27 class ProgressListener;
28 class StreamInfo;
29 class VideoStreamInfo;
30 
31 namespace webm {
32 
33 class Segmenter {
34  public:
35  explicit Segmenter(const MuxerOptions& options);
36  virtual ~Segmenter();
37 
48  Status Initialize(scoped_ptr<MkvWriter> writer,
49  MediaStream* streams,
50  ProgressListener* progress_listener,
51  MuxerListener* muxer_listener,
52  KeySource* encryption_key_source);
53 
56  Status Finalize();
57 
61  Status AddSample(scoped_refptr<MediaSample> sample);
62 
63  // TODO(modmaker): Add key source support.
64 
67  virtual bool GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) = 0;
68 
71  virtual bool GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) = 0;
72 
74  float GetDuration() const;
75 
76  protected:
79  uint64_t FromBMFFTimescale(uint64_t time_timescale);
81  uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
83  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
85  Status SetCluster(uint64_t start_webm_timecode,
86  uint64_t position,
87  MkvWriter* writer);
88 
90  void UpdateProgress(uint64_t progress);
91  void set_progress_target(uint64_t target) { progress_target_ = target; }
92 
93  const MuxerOptions& options() const { return options_; }
94  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
95  mkvmuxer::Cues* cues() { return &cues_; }
96  MuxerListener* muxer_listener() { return muxer_listener_; }
97  MediaStream* stream() { return stream_; }
98  SeekHead* seek_head() { return &seek_head_; }
99 
100  int track_id() const { return track_id_; }
101  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
102  double cluster_length_sec() const { return cluster_length_sec_; }
103 
104  virtual Status DoInitialize(scoped_ptr<MkvWriter> writer) = 0;
105  virtual Status DoFinalize() = 0;
106 
107  private:
108  Status CreateVideoTrack(VideoStreamInfo* info);
109  Status CreateAudioTrack(AudioStreamInfo* info);
110 
111  // This is called when there needs to be a new subsegment. This does nothing
112  // in single-segment mode. In multi-segment mode this creates a new Cluster
113  // element.
114  virtual Status NewSubsegment(uint64_t start_timescale) = 0;
115  // This is called when there needs to be a new segment. In single-segment
116  // mode, this creates a new Cluster element. In multi-segment mode this
117  // creates a new output file.
118  virtual Status NewSegment(uint64_t start_timescale) = 0;
119  // This is called when a segment ends. This is called right before a call
120  // to NewSegment and at the start of Finalize.
121  Status FinalizeSegment(uint64_t end_timescale);
122 
123  const MuxerOptions& options_;
124 
125  scoped_ptr<mkvmuxer::Cluster> cluster_;
126  mkvmuxer::Cues cues_;
127  SeekHead seek_head_;
128  mkvmuxer::SegmentInfo segment_info_;
129  mkvmuxer::Tracks tracks_;
130 
131  MediaStream* stream_;
132  MuxerListener* muxer_listener_;
133  ProgressListener* progress_listener_;
134  uint64_t progress_target_;
135  uint64_t accumulated_progress_;
136  uint64_t total_duration_;
137  uint64_t sample_duration_;
138  // The position (in bytes) of the start of the Segment payload in the init
139  // file. This is also the size of the header before the SeekHead.
140  uint64_t segment_payload_pos_;
141 
142  double cluster_length_sec_;
143  double segment_length_sec_;
144 
145  int track_id_;
146 
147  DISALLOW_COPY_AND_ASSIGN(Segmenter);
148 };
149 
150 } // namespace webm
151 } // namespace media
152 } // namespace edash_packager
153 
154 #endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
Holds audio stream information.
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
Status SetCluster(uint64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition: segmenter.cc:190
This class listens to progress updates events.
virtual bool GetIndexRangeStartAndEnd(uint32_t *start, uint32_t *end)=0
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
uint64_t FromWebMTimecode(uint64_t time_webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timescale.
Definition: segmenter.cc:148
virtual bool GetInitRangeStartAndEnd(uint32_t *start, uint32_t *end)=0
Holds video stream information.
uint64_t FromBMFFTimescale(uint64_t time_timescale)
Definition: segmenter.cc:141
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:154
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:199
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
Status Initialize(scoped_ptr< MkvWriter > writer, MediaStream *streams, ProgressListener *progress_listener, MuxerListener *muxer_listener, KeySource *encryption_key_source)
Definition: segmenter.cc:45
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: segmenter.cc:92