2013-10-11 21:44:55 +00:00
|
|
|
// Copyright (c) 2013 Google Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "media/base/muxer.h"
|
|
|
|
|
|
|
|
#include "media/base/encryptor_source.h"
|
|
|
|
#include "media/base/media_sample.h"
|
|
|
|
#include "media/base/media_stream.h"
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
Muxer::Muxer(const MuxerOptions& options)
|
2013-12-12 23:49:31 +00:00
|
|
|
: options_(options),
|
|
|
|
encryptor_source_(NULL),
|
|
|
|
clear_lead_in_seconds_(0),
|
|
|
|
muxer_listener_(NULL) {}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
Muxer::~Muxer() {}
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
void Muxer::SetEncryptorSource(EncryptorSource* encryptor_source,
|
|
|
|
double clear_lead_in_seconds) {
|
|
|
|
encryptor_source_ = encryptor_source;
|
|
|
|
clear_lead_in_seconds_ = clear_lead_in_seconds;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
Status Muxer::AddStream(MediaStream* stream) {
|
|
|
|
stream->Connect(this);
|
|
|
|
streams_.push_back(stream);
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Muxer::Run() {
|
|
|
|
DCHECK(!streams_.empty());
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
// Start the streams.
|
|
|
|
for (std::vector<MediaStream*>::iterator it = streams_.begin();
|
|
|
|
it != streams_.end();
|
|
|
|
++it) {
|
|
|
|
status = (*it)->Start(MediaStream::kPull);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
uint32 current_stream_id = 0;
|
2013-10-11 21:44:55 +00:00
|
|
|
while (status.ok()) {
|
|
|
|
scoped_refptr<MediaSample> sample;
|
2013-11-12 20:37:58 +00:00
|
|
|
status = streams_[current_stream_id]->PullSample(&sample);
|
2013-10-11 21:44:55 +00:00
|
|
|
if (!status.ok())
|
|
|
|
break;
|
2013-11-12 20:37:58 +00:00
|
|
|
status = AddSample(streams_[current_stream_id], sample);
|
|
|
|
|
|
|
|
// Switch to next stream if the current stream is ready for fragmentation.
|
|
|
|
if (status.Matches(Status(error::FRAGMENT_FINALIZED, ""))) {
|
|
|
|
current_stream_id = (current_stream_id + 1) % streams_.size();
|
|
|
|
status.Clear();
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
return status.Matches(Status(error::END_OF_STREAM, "")) ? Status::OK : status;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 23:49:31 +00:00
|
|
|
void Muxer::SetMuxerListener(media::event::MuxerListener* muxer_listener) {
|
|
|
|
muxer_listener_ = muxer_listener;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
} // namespace media
|