2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 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 or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/media_stream.h"
|
|
|
|
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/media/base/demuxer.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/muxer.h"
|
|
|
|
#include "packager/media/base/stream_info.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
MediaStream::MediaStream(scoped_refptr<StreamInfo> info, Demuxer* demuxer)
|
|
|
|
: info_(info), demuxer_(demuxer), muxer_(NULL), state_(kIdle) {}
|
|
|
|
|
|
|
|
MediaStream::~MediaStream() {}
|
|
|
|
|
|
|
|
Status MediaStream::PullSample(scoped_refptr<MediaSample>* sample) {
|
|
|
|
DCHECK_EQ(state_, kPulling);
|
|
|
|
|
|
|
|
// Trigger a new parse in demuxer if no more samples.
|
|
|
|
while (samples_.empty()) {
|
|
|
|
Status status = demuxer_->Parse();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sample = samples_.front();
|
|
|
|
samples_.pop_front();
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status MediaStream::PushSample(const scoped_refptr<MediaSample>& sample) {
|
|
|
|
switch (state_) {
|
|
|
|
case kIdle:
|
|
|
|
case kPulling:
|
|
|
|
samples_.push_back(sample);
|
|
|
|
return Status::OK;
|
|
|
|
case kDisconnected:
|
|
|
|
return Status::OK;
|
|
|
|
case kPushing:
|
|
|
|
return muxer_->AddSample(this, sample);
|
|
|
|
default:
|
|
|
|
NOTREACHED() << "Unexpected State " << state_;
|
|
|
|
return Status::UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStream::Connect(Muxer* muxer) {
|
2014-03-31 22:25:45 +00:00
|
|
|
DCHECK(muxer);
|
|
|
|
DCHECK(!muxer_);
|
2013-10-11 21:44:55 +00:00
|
|
|
state_ = kConnected;
|
|
|
|
muxer_ = muxer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status MediaStream::Start(MediaStreamOperation operation) {
|
2014-03-31 22:25:45 +00:00
|
|
|
DCHECK(demuxer_);
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(operation == kPush || operation == kPull);
|
|
|
|
|
|
|
|
switch (state_) {
|
|
|
|
case kIdle:
|
|
|
|
// Disconnect the stream if it is not connected to a muxer.
|
|
|
|
state_ = kDisconnected;
|
|
|
|
samples_.clear();
|
|
|
|
return Status::OK;
|
|
|
|
case kConnected:
|
|
|
|
state_ = (operation == kPush) ? kPushing : kPulling;
|
|
|
|
if (operation == kPush) {
|
|
|
|
// Push samples in the queue to muxer if there is any.
|
|
|
|
while (!samples_.empty()) {
|
|
|
|
Status status = muxer_->AddSample(this, samples_.front());
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
samples_.pop_front();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We need to disconnect all its peer streams which are not connected
|
|
|
|
// to a muxer.
|
2014-03-21 17:26:49 +00:00
|
|
|
for (size_t i = 0; i < demuxer_->streams().size(); ++i) {
|
2013-11-12 20:32:44 +00:00
|
|
|
Status status = demuxer_->streams()[i]->Start(operation);
|
2013-10-11 21:44:55 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
case kPulling:
|
|
|
|
DCHECK(operation == kPull);
|
|
|
|
return Status::OK;
|
|
|
|
default:
|
|
|
|
NOTREACHED() << "Unexpected State " << state_;
|
|
|
|
return Status::UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
const scoped_refptr<StreamInfo> MediaStream::info() const { return info_; }
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
std::string MediaStream::ToString() const {
|
2014-03-31 22:25:45 +00:00
|
|
|
return base::StringPrintf("state: %d\n samples in the queue: %zu\n %s",
|
|
|
|
state_, samples_.size(), info_->ToString().c_str());
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|