mkvmuxer: Add Init method to Cluster and Segment.

Remove asserts on the MkvWriter pointer in each constructor,
and assign the pointer in each Init method. Update Segment::Init
usage site in sample_muxer.

Change-Id: Id940f76b50dc15603742e76afc04cdffe0ec4990
This commit is contained in:
Tom Finegan
2012-01-19 20:19:37 -05:00
parent f7aa8ab33d
commit 69df730519
3 changed files with 56 additions and 19 deletions

View File

@@ -986,7 +986,7 @@ bool Tracks::Write(IMkvWriter* writer) const {
// //
// Cluster Class // Cluster Class
Cluster::Cluster(uint64 timecode, IMkvWriter* writer, int64 cues_pos) Cluster::Cluster(uint64 timecode, int64 cues_pos)
: blocks_added_(0), : blocks_added_(0),
finalized_(false), finalized_(false),
header_written_(false), header_written_(false),
@@ -994,13 +994,20 @@ Cluster::Cluster(uint64 timecode, IMkvWriter* writer, int64 cues_pos)
position_for_cues_(cues_pos), position_for_cues_(cues_pos),
size_position_(-1), size_position_(-1),
timecode_(timecode), timecode_(timecode),
writer_(writer) { writer_(NULL) {
assert(writer_);
} }
Cluster::~Cluster() { Cluster::~Cluster() {
} }
bool Cluster::Init(IMkvWriter* ptr_writer) {
if (!ptr_writer) {
return false;
}
writer_ = ptr_writer;
return true;
}
bool Cluster::AddFrame(const uint8* frame, bool Cluster::AddFrame(const uint8* frame,
uint64 length, uint64 length,
uint64 track_number, uint64 track_number,
@@ -1352,7 +1359,7 @@ void SegmentInfo::set_writing_app(const char* app) {
// //
// Segment Class // Segment Class
Segment::Segment(IMkvWriter* writer) Segment::Segment()
: chunk_count_(0), : chunk_count_(0),
chunk_name_(NULL), chunk_name_(NULL),
chunk_writer_cluster_(NULL), chunk_writer_cluster_(NULL),
@@ -1378,13 +1385,9 @@ Segment::Segment(IMkvWriter* writer)
output_cues_(true), output_cues_(true),
payload_pos_(0), payload_pos_(0),
size_position_(0), size_position_(0),
writer_cluster_(writer), writer_cluster_(NULL),
writer_cues_(writer), writer_cues_(NULL),
writer_header_(writer) { writer_header_(NULL) {
assert(writer_cluster_);
// TODO(fgalligan): Create an Init function for Segment.
segment_info_.Init();
} }
Segment::~Segment() { Segment::~Segment() {
@@ -1421,6 +1424,16 @@ Segment::~Segment() {
} }
} }
bool Segment::Init(IMkvWriter* ptr_writer) {
if (!ptr_writer) {
return false;
}
writer_cluster_ = ptr_writer;
writer_cues_ = ptr_writer;
writer_header_ = ptr_writer;
return segment_info_.Init();
}
bool Segment::Finalize() { bool Segment::Finalize() {
if (!WriteFramesAll()) if (!WriteFramesAll())
return false; return false;
@@ -1650,11 +1663,17 @@ bool Segment::AddFrame(const uint8* frame,
// are valid. // are valid.
cluster_list_[cluster_list_size_] = cluster_list_[cluster_list_size_] =
new (std::nothrow) Cluster(timecode, new (std::nothrow) Cluster(timecode, MaxOffset());
writer_cluster_,
MaxOffset());
if (!cluster_list_[cluster_list_size_]) if (!cluster_list_[cluster_list_size_])
return false; return false;
const bool cluster_init_ok =
cluster_list_[cluster_list_size_]->Init(writer_cluster_);
if (!cluster_init_ok)
return false;
cluster_list_size_ = new_size; cluster_list_size_ = new_size;
new_cluster_ = false; new_cluster_ = false;
} }

View File

@@ -436,13 +436,18 @@ class Tracks {
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
// Cluster element // Cluster element
//
// Notes:
// |Init| must be called before any other method in this class.
class Cluster { class Cluster {
public: public:
Cluster(uint64 timecode, int64 cues_pos);
~Cluster();
// |timecode| is the absolute timecode of the cluster. |cues_pos| is the // |timecode| is the absolute timecode of the cluster. |cues_pos| is the
// position for the cluster within the segment that should be written in // position for the cluster within the segment that should be written in
// the cues element. // the cues element.
Cluster(uint64 timecode, IMkvWriter* writer, int64 cues_pos); bool Init(IMkvWriter* ptr_writer);
~Cluster();
// Adds a frame to be output in the file. The frame is written out through // Adds a frame to be output in the file. The frame is written out through
// |writer_| if successful. Returns true on success. // |writer_| if successful. Returns true on success.
@@ -491,7 +496,7 @@ class Cluster {
uint64 payload_size_; uint64 payload_size_;
// The file position used for cue points. // The file position used for cue points.
int64 position_for_cues_; const int64 position_for_cues_;
// The file position of the cluster's size element. // The file position of the cluster's size element.
int64 size_position_; int64 size_position_;
@@ -591,6 +596,9 @@ class SegmentInfo {
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
// This class represents the main segment in a WebM file. Currently only // This class represents the main segment in a WebM file. Currently only
// supports one Segment element. // supports one Segment element.
//
// Notes:
// |Init| must be called before any other method in this class.
class Segment { class Segment {
public: public:
enum Mode { enum Mode {
@@ -600,9 +608,13 @@ class Segment {
const static uint64 kDefaultMaxClusterDuration = 30000000000ULL; const static uint64 kDefaultMaxClusterDuration = 30000000000ULL;
explicit Segment(IMkvWriter* writer); Segment();
virtual ~Segment(); virtual ~Segment();
// Initializes |SegmentInfo| and returns result. Always returns false when
// |ptr_writer| is NULL.
bool Init(IMkvWriter* ptr_writer);
// Adds an audio track to the segment. Returns the number of the track on // Adds an audio track to the segment. Returns the number of the track on
// success, 0 on error. // success, 0 on error.
uint64 AddAudioTrack(int32 sample_rate, int32 channels); uint64 AddAudioTrack(int32 sample_rate, int32 channels);

View File

@@ -171,7 +171,13 @@ int main(int argc, char* argv[]) {
} }
// Set Segment element attributes // Set Segment element attributes
mkvmuxer::Segment muxer_segment(&writer); mkvmuxer::Segment muxer_segment;
if (!muxer_segment.Init(&writer)) {
printf("\n Could not initialize muxer segment!\n");
return -1;
}
if (live_mode) if (live_mode)
muxer_segment.set_mode(mkvmuxer::Segment::kLive); muxer_segment.set_mode(mkvmuxer::Segment::kLive);
else else