2014-05-19 21:30:58 +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
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/mpd/base/simple_mpd_notifier.h"
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/file/file.h"
|
|
|
|
#include "packager/mpd/base/mpd_builder.h"
|
|
|
|
#include "packager/mpd/base/mpd_utils.h"
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
using edash_packager::media::File;
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2014-05-16 23:32:10 +00:00
|
|
|
SimpleMpdNotifier::SimpleMpdNotifier(DashProfile dash_profile,
|
2014-05-30 05:01:09 +00:00
|
|
|
const MpdOptions& mpd_options,
|
2014-05-16 23:32:10 +00:00
|
|
|
const std::vector<std::string>& base_urls,
|
2014-05-19 21:30:58 +00:00
|
|
|
const std::string& output_path)
|
2014-05-16 23:32:10 +00:00
|
|
|
: MpdNotifier(dash_profile),
|
|
|
|
output_path_(output_path),
|
|
|
|
mpd_builder_(new MpdBuilder(dash_profile == kLiveProfile
|
|
|
|
? MpdBuilder::kDynamic
|
2014-05-22 02:16:17 +00:00
|
|
|
: MpdBuilder::kStatic,
|
2014-05-30 05:01:09 +00:00
|
|
|
mpd_options)) {
|
2014-05-16 23:32:10 +00:00
|
|
|
DCHECK(dash_profile == kLiveProfile || dash_profile == kOnDemandProfile);
|
|
|
|
for (size_t i = 0; i < base_urls.size(); ++i)
|
|
|
|
mpd_builder_->AddBaseUrl(base_urls[i]);
|
2014-05-19 21:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleMpdNotifier::~SimpleMpdNotifier() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleMpdNotifier::Init() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleMpdNotifier::NotifyNewContainer(const MediaInfo& media_info,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t* container_id) {
|
2014-05-19 21:30:58 +00:00
|
|
|
DCHECK(container_id);
|
|
|
|
|
|
|
|
ContentType content_type = GetContentType(media_info);
|
|
|
|
if (content_type == kUnknown)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
base::AutoLock auto_lock(lock_);
|
|
|
|
// TODO(kqyang): Consider adding a new method MpdBuilder::AddRepresentation.
|
|
|
|
// Most of the codes here can be moved inside.
|
|
|
|
AdaptationSet** adaptation_set = &adaptation_set_map_[content_type];
|
|
|
|
if (*adaptation_set == NULL)
|
|
|
|
*adaptation_set = mpd_builder_->AddAdaptationSet();
|
|
|
|
|
|
|
|
DCHECK(*adaptation_set);
|
|
|
|
Representation* representation =
|
|
|
|
(*adaptation_set)->AddRepresentation(media_info);
|
|
|
|
if (representation == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*container_id = representation->id();
|
|
|
|
|
2014-05-16 23:32:10 +00:00
|
|
|
if (mpd_builder_->type() == MpdBuilder::kStatic)
|
2014-05-19 21:30:58 +00:00
|
|
|
return WriteMpdToFile();
|
|
|
|
|
|
|
|
DCHECK(!ContainsKey(representation_map_, representation->id()));
|
|
|
|
representation_map_[representation->id()] = representation;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
|
|
|
|
uint64_t start_time,
|
|
|
|
uint64_t duration,
|
|
|
|
uint64_t size) {
|
2014-05-19 21:30:58 +00:00
|
|
|
base::AutoLock auto_lock(lock_);
|
|
|
|
|
|
|
|
RepresentationMap::iterator it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-30 05:01:09 +00:00
|
|
|
it->second->AddNewSegment(start_time, duration, size);
|
2014-05-19 21:30:58 +00:00
|
|
|
return WriteMpdToFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleMpdNotifier::AddContentProtectionElement(
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t container_id,
|
2014-05-19 21:30:58 +00:00
|
|
|
const ContentProtectionElement& content_protection_element) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleMpdNotifier::ContentType SimpleMpdNotifier::GetContentType(
|
|
|
|
const MediaInfo& media_info) {
|
|
|
|
const bool has_video = media_info.video_info().size() > 0;
|
|
|
|
const bool has_audio = media_info.audio_info().size() > 0;
|
|
|
|
const bool has_text = media_info.text_info().size() > 0;
|
|
|
|
|
|
|
|
if (MoreThanOneTrue(has_video, has_audio, has_text)) {
|
|
|
|
NOTIMPLEMENTED() << "MediaInfo with more than one stream is not supported.";
|
|
|
|
return kUnknown;
|
|
|
|
}
|
|
|
|
if (!AtLeastOneTrue(has_video, has_audio, has_text)) {
|
|
|
|
LOG(ERROR) << "MediaInfo should contain one audio, video, or text stream.";
|
|
|
|
return kUnknown;
|
|
|
|
}
|
|
|
|
return has_video ? kVideo : (has_audio ? kAudio : kText);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleMpdNotifier::WriteMpdToFile() {
|
|
|
|
CHECK(!output_path_.empty());
|
|
|
|
|
|
|
|
std::string mpd;
|
|
|
|
if (!mpd_builder_->ToString(&mpd)) {
|
|
|
|
LOG(ERROR) << "Failed to write MPD to string.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
File* file = File::Open(output_path_.c_str(), "w");
|
|
|
|
if (!file) {
|
|
|
|
LOG(ERROR) << "Failed to open file for writing: " << output_path_;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* mpd_char_ptr = mpd.data();
|
|
|
|
size_t mpd_bytes_left = mpd.size();
|
|
|
|
while (mpd_bytes_left > 0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t length = file->Write(mpd_char_ptr, mpd_bytes_left);
|
2014-05-19 21:30:58 +00:00
|
|
|
if (length <= 0) {
|
|
|
|
LOG(ERROR) << "Failed to write to file '" << output_path_ << "' ("
|
|
|
|
<< length << ").";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mpd_char_ptr += length;
|
|
|
|
mpd_bytes_left -= length;
|
|
|
|
}
|
|
|
|
return file->Close();
|
|
|
|
}
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|