2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2015 Google LLC. All rights reserved.
|
2014-05-19 21:30:58 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/mpd/base/simple_mpd_notifier.h>
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
|
|
|
|
#include <packager/mpd/base/adaptation_set.h>
|
|
|
|
#include <packager/mpd/base/mpd_builder.h>
|
|
|
|
#include <packager/mpd/base/mpd_notifier_util.h>
|
|
|
|
#include <packager/mpd/base/mpd_utils.h>
|
|
|
|
#include <packager/mpd/base/period.h>
|
|
|
|
#include <packager/mpd/base/representation.h>
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-05-19 21:30:58 +00:00
|
|
|
|
2017-07-27 18:49:50 +00:00
|
|
|
SimpleMpdNotifier::SimpleMpdNotifier(const MpdOptions& mpd_options)
|
2016-12-21 23:28:56 +00:00
|
|
|
: MpdNotifier(mpd_options),
|
2017-07-27 18:49:50 +00:00
|
|
|
output_path_(mpd_options.mpd_params.mpd_output),
|
2017-12-19 20:02:28 +00:00
|
|
|
mpd_builder_(new MpdBuilder(mpd_options)),
|
|
|
|
content_protection_in_adaptation_set_(
|
|
|
|
mpd_options.mpd_params.generate_dash_if_iop_compliant_mpd) {
|
2017-07-27 18:49:50 +00:00
|
|
|
for (const std::string& base_url : mpd_options.mpd_params.base_urls)
|
|
|
|
mpd_builder_->AddBaseUrl(base_url);
|
2014-05-19 21:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:02:28 +00:00
|
|
|
SimpleMpdNotifier::~SimpleMpdNotifier() {}
|
2014-05-19 21:30:58 +00:00
|
|
|
|
|
|
|
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);
|
2015-07-22 06:57:21 +00:00
|
|
|
if (content_type == kContentTypeUnknown)
|
2014-05-19 21:30:58 +00:00
|
|
|
return false;
|
|
|
|
|
2014-12-16 01:32:19 +00:00
|
|
|
MediaInfo adjusted_media_info(media_info);
|
|
|
|
MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
|
2018-01-03 00:10:54 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock auto_lock(&lock_);
|
2018-01-25 23:04:59 +00:00
|
|
|
const double kPeriodStartTimeSeconds = 0.0;
|
|
|
|
Period* period = mpd_builder_->GetOrCreatePeriod(kPeriodStartTimeSeconds);
|
|
|
|
DCHECK(period);
|
|
|
|
AdaptationSet* adaptation_set = period->GetOrCreateAdaptationSet(
|
|
|
|
media_info, content_protection_in_adaptation_set_);
|
|
|
|
DCHECK(adaptation_set);
|
|
|
|
if (!adaptation_set->has_id())
|
|
|
|
adaptation_set->set_id(next_adaptation_set_id_++);
|
|
|
|
Representation* representation =
|
|
|
|
adaptation_set->AddRepresentation(adjusted_media_info);
|
2017-12-19 20:02:28 +00:00
|
|
|
if (!representation)
|
2014-05-19 21:30:58 +00:00
|
|
|
return false;
|
2018-01-25 23:04:59 +00:00
|
|
|
|
2014-05-19 21:30:58 +00:00
|
|
|
*container_id = representation->id();
|
2018-01-25 23:04:59 +00:00
|
|
|
if (content_protection_in_adaptation_set_) {
|
|
|
|
// ContentProtection elements are already added to AdaptationSet above.
|
|
|
|
// Use RepresentationId to AdaptationSet map to update ContentProtection
|
|
|
|
// in AdaptationSet in NotifyEncryptionUpdate.
|
|
|
|
representation_id_to_adaptation_set_[representation->id()] = adaptation_set;
|
|
|
|
} else {
|
|
|
|
AddContentProtectionElements(media_info, representation);
|
|
|
|
}
|
|
|
|
representation_map_[representation->id()] = representation;
|
2014-05-19 21:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:38:05 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyAvailabilityTimeOffset(uint32_t container_id) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2021-08-25 15:38:05 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
it->second->SetAvailabilityTimeOffset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-15 21:12:42 +00:00
|
|
|
bool SimpleMpdNotifier::NotifySampleDuration(uint32_t container_id,
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t sample_duration) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2017-12-19 20:02:28 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
2015-06-15 21:12:42 +00:00
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
it->second->SetSampleDuration(sample_duration);
|
2015-09-10 23:01:00 +00:00
|
|
|
return true;
|
2015-06-15 21:12:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:38:05 +00:00
|
|
|
bool SimpleMpdNotifier::NotifySegmentDuration(uint32_t container_id) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2021-08-25 15:38:05 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
it->second->SetSegmentDuration();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t start_time,
|
|
|
|
int64_t duration,
|
2024-05-02 20:25:49 +00:00
|
|
|
uint64_t size,
|
|
|
|
int64_t segment_number) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2017-12-19 20:02:28 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
2014-05-19 21:30:58 +00:00
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-02 20:25:49 +00:00
|
|
|
it->second->AddNewSegment(start_time, duration, size, segment_number);
|
2015-09-10 23:01:00 +00:00
|
|
|
return true;
|
2014-05-19 21:30:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 16:57:43 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyCompletedSegment(uint32_t container_id,
|
|
|
|
int64_t duration,
|
|
|
|
uint64_t size) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2021-09-03 16:57:43 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
it->second->UpdateCompletedSegment(duration, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:10:33 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyCueEvent(uint32_t container_id,
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t timestamp) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-01-03 00:10:54 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Representation* original_representation = it->second;
|
|
|
|
AdaptationSet* original_adaptation_set =
|
|
|
|
representation_id_to_adaptation_set_[container_id];
|
|
|
|
|
|
|
|
const MediaInfo& media_info = original_representation->GetMediaInfo();
|
|
|
|
const double period_start_time_seconds =
|
|
|
|
static_cast<double>(timestamp) / media_info.reference_time_scale();
|
2018-01-25 23:04:59 +00:00
|
|
|
|
|
|
|
Period* period = mpd_builder_->GetOrCreatePeriod(period_start_time_seconds);
|
|
|
|
DCHECK(period);
|
|
|
|
AdaptationSet* adaptation_set = period->GetOrCreateAdaptationSet(
|
|
|
|
media_info, content_protection_in_adaptation_set_);
|
|
|
|
DCHECK(adaptation_set);
|
|
|
|
if (!adaptation_set->has_id()) {
|
|
|
|
adaptation_set->set_id(original_adaptation_set->id());
|
|
|
|
} else {
|
|
|
|
DCHECK_EQ(adaptation_set->id(), original_adaptation_set->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
Representation* representation =
|
|
|
|
adaptation_set->CopyRepresentation(*original_representation);
|
|
|
|
if (!representation)
|
2018-01-03 00:10:54 +00:00
|
|
|
return false;
|
|
|
|
|
2018-01-25 23:04:59 +00:00
|
|
|
if (content_protection_in_adaptation_set_) {
|
|
|
|
// ContentProtection elements are already added to AdaptationSet above.
|
|
|
|
// Use RepresentationId to AdaptationSet map to update ContentProtection
|
|
|
|
// in AdaptationSet in NotifyEncryptionUpdate.
|
|
|
|
representation_id_to_adaptation_set_[representation->id()] = adaptation_set;
|
|
|
|
} else {
|
|
|
|
AddContentProtectionElements(media_info, representation);
|
|
|
|
}
|
|
|
|
representation_map_[representation->id()] = representation;
|
2018-01-03 00:10:54 +00:00
|
|
|
return true;
|
2018-01-03 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyEncryptionUpdate(
|
|
|
|
uint32_t container_id,
|
2015-09-24 22:03:52 +00:00
|
|
|
const std::string& drm_uuid,
|
2015-08-26 20:25:29 +00:00
|
|
|
const std::vector<uint8_t>& new_key_id,
|
|
|
|
const std::vector<uint8_t>& new_pssh) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2017-12-19 20:02:28 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
2015-08-26 20:25:29 +00:00
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:02:28 +00:00
|
|
|
if (content_protection_in_adaptation_set_) {
|
|
|
|
AdaptationSet* adaptation_set_for_representation =
|
|
|
|
representation_id_to_adaptation_set_[it->second->id()];
|
|
|
|
adaptation_set_for_representation->UpdateContentProtectionPssh(
|
|
|
|
drm_uuid, Uint8VectorToBase64(new_pssh));
|
|
|
|
} else {
|
|
|
|
it->second->UpdateContentProtectionPssh(drm_uuid,
|
|
|
|
Uint8VectorToBase64(new_pssh));
|
2015-07-21 21:52:20 +00:00
|
|
|
}
|
2015-09-10 23:01:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 00:39:21 +00:00
|
|
|
bool SimpleMpdNotifier::NotifyMediaInfoUpdate(uint32_t container_id,
|
|
|
|
const MediaInfo& media_info) {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-05-22 00:39:21 +00:00
|
|
|
auto it = representation_map_.find(container_id);
|
|
|
|
if (it == representation_map_.end()) {
|
|
|
|
LOG(ERROR) << "Unexpected container_id: " << container_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaInfo adjusted_media_info(media_info);
|
|
|
|
MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
|
|
|
|
|
|
|
|
it->second->set_media_info(adjusted_media_info);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:01:00 +00:00
|
|
|
bool SimpleMpdNotifier::Flush() {
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2015-07-21 21:52:20 +00:00
|
|
|
return WriteMpdToFile(output_path_, mpd_builder_.get());
|
2014-05-19 21:30:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|