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

View File

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

View File

@@ -171,7 +171,13 @@ int main(int argc, char* argv[]) {
}
// 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)
muxer_segment.set_mode(mkvmuxer::Segment::kLive);
else