Shaka Packager SDK
simple_mpd_notifier.cc
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/mpd/base/simple_mpd_notifier.h"
8 
9 #include <gflags/gflags.h>
10 
11 #include "packager/base/logging.h"
12 #include "packager/base/stl_util.h"
13 #include "packager/mpd/base/adaptation_set.h"
14 #include "packager/mpd/base/mpd_builder.h"
15 #include "packager/mpd/base/mpd_notifier_util.h"
16 #include "packager/mpd/base/mpd_utils.h"
17 #include "packager/mpd/base/period.h"
18 #include "packager/mpd/base/representation.h"
19 
20 DEFINE_int32(
21  pto_adjustment,
22  -1,
23  "There could be rounding errors in MSE which could cut the first key frame "
24  "of the representation and thus cut all the frames until the next key "
25  "frame, which then leads to a big gap in presentation timeline which "
26  "stalls playback. A small back off may be necessary to compensate for the "
27  "possible rounding error. It should not cause any playback issues if it is "
28  "small enough. The workaround can be removed once the problem is handled "
29  "in all players.");
30 
31 namespace shaka {
32 
33 SimpleMpdNotifier::SimpleMpdNotifier(const MpdOptions& mpd_options)
34  : MpdNotifier(mpd_options),
35  output_path_(mpd_options.mpd_params.mpd_output),
36  mpd_builder_(new MpdBuilder(mpd_options)),
37  content_protection_in_adaptation_set_(
38  mpd_options.mpd_params.generate_dash_if_iop_compliant_mpd) {
39  for (const std::string& base_url : mpd_options.mpd_params.base_urls)
40  mpd_builder_->AddBaseUrl(base_url);
41 }
42 
43 SimpleMpdNotifier::~SimpleMpdNotifier() {}
44 
46  return true;
47 }
48 
49 bool SimpleMpdNotifier::NotifyNewContainer(const MediaInfo& media_info,
50  uint32_t* container_id) {
51  DCHECK(container_id);
52 
53  ContentType content_type = GetContentType(media_info);
54  if (content_type == kContentTypeUnknown)
55  return false;
56 
57  MediaInfo adjusted_media_info(media_info);
58  MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
59  const Representation* kNoOriginalRepresentation = nullptr;
60  const double kPeriodStartTimeSeconds = 0.0;
61 
62  base::AutoLock auto_lock(lock_);
63  const Representation* representation = AddRepresentationToPeriod(
64  adjusted_media_info, kNoOriginalRepresentation, kPeriodStartTimeSeconds);
65  if (!representation)
66  return false;
67  *container_id = representation->id();
68  return true;
69 }
70 
71 bool SimpleMpdNotifier::NotifySampleDuration(uint32_t container_id,
72  uint32_t sample_duration) {
73  base::AutoLock auto_lock(lock_);
74  auto it = representation_map_.find(container_id);
75  if (it == representation_map_.end()) {
76  LOG(ERROR) << "Unexpected container_id: " << container_id;
77  return false;
78  }
79  it->second->SetSampleDuration(sample_duration);
80  return true;
81 }
82 
83 bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
84  uint64_t start_time,
85  uint64_t duration,
86  uint64_t size) {
87  base::AutoLock auto_lock(lock_);
88  auto it = representation_map_.find(container_id);
89  if (it == representation_map_.end()) {
90  LOG(ERROR) << "Unexpected container_id: " << container_id;
91  return false;
92  }
93  it->second->AddNewSegment(start_time, duration, size);
94  return true;
95 }
96 
97 bool SimpleMpdNotifier::NotifyCueEvent(uint32_t container_id,
98  uint64_t timestamp) {
99  base::AutoLock auto_lock(lock_);
100  auto it = representation_map_.find(container_id);
101  if (it == representation_map_.end()) {
102  LOG(ERROR) << "Unexpected container_id: " << container_id;
103  return false;
104  }
105  Representation* original_representation = it->second;
106  AdaptationSet* original_adaptation_set =
107  representation_id_to_adaptation_set_[container_id];
108 
109  const MediaInfo& media_info = original_representation->GetMediaInfo();
110  const double period_start_time_seconds =
111  static_cast<double>(timestamp) / media_info.reference_time_scale();
112  const Representation* new_representation = AddRepresentationToPeriod(
113  media_info, original_representation, period_start_time_seconds);
114  if (!new_representation)
115  return false;
116 
117  // TODO(kqyang): Pass the ID to GetOrCreateAdaptationSet instead?
118  AdaptationSet* new_adaptation_set =
119  representation_id_to_adaptation_set_[container_id];
120  DCHECK(new_adaptation_set);
121  new_adaptation_set->set_id(original_adaptation_set->id());
122  return true;
123 }
124 
126  uint32_t container_id,
127  const std::string& drm_uuid,
128  const std::vector<uint8_t>& new_key_id,
129  const std::vector<uint8_t>& new_pssh) {
130  base::AutoLock auto_lock(lock_);
131  auto it = representation_map_.find(container_id);
132  if (it == representation_map_.end()) {
133  LOG(ERROR) << "Unexpected container_id: " << container_id;
134  return false;
135  }
136 
137  if (content_protection_in_adaptation_set_) {
138  AdaptationSet* adaptation_set_for_representation =
139  representation_id_to_adaptation_set_[it->second->id()];
140  adaptation_set_for_representation->UpdateContentProtectionPssh(
141  drm_uuid, Uint8VectorToBase64(new_pssh));
142  } else {
143  it->second->UpdateContentProtectionPssh(drm_uuid,
144  Uint8VectorToBase64(new_pssh));
145  }
146  return true;
147 }
148 
150  base::AutoLock auto_lock(lock_);
151  return WriteMpdToFile(output_path_, mpd_builder_.get());
152 }
153 
154 Representation* SimpleMpdNotifier::AddRepresentationToPeriod(
155  const MediaInfo& media_info,
156  const Representation* original_representation,
157  double period_start_time_seconds) {
158  Period* period = mpd_builder_->GetOrCreatePeriod(period_start_time_seconds);
159  DCHECK(period);
160 
161  AdaptationSet* adaptation_set = period->GetOrCreateAdaptationSet(
162  media_info, content_protection_in_adaptation_set_);
163  DCHECK(adaptation_set);
164 
165  Representation* representation = nullptr;
166  if (original_representation) {
167  uint64_t presentation_time_offset =
168  period->start_time_in_seconds() * media_info.reference_time_scale();
169  if (presentation_time_offset > 0) {
170  presentation_time_offset += FLAGS_pto_adjustment;
171  }
172  representation = adaptation_set->CopyRepresentationWithTimeOffset(
173  *original_representation, presentation_time_offset);
174  } else {
175  representation = adaptation_set->AddRepresentation(media_info);
176  }
177  if (!representation)
178  return nullptr;
179 
180  if (content_protection_in_adaptation_set_) {
181  // ContentProtection elements are already added to AdaptationSet above.
182  // Use RepresentationId to AdaptationSet map to update ContentProtection
183  // in AdaptationSet in NotifyEncryptionUpdate.
184  representation_id_to_adaptation_set_[representation->id()] = adaptation_set;
185  } else {
186  AddContentProtectionElements(media_info, representation);
187  }
188  representation_map_[representation->id()] = representation;
189  return representation;
190 }
191 
192 } // namespace shaka
virtual const MediaInfo & GetMediaInfo() const
virtual AdaptationSet * GetOrCreateAdaptationSet(const MediaInfo &media_info, bool content_protection_in_adaptation_set)
Definition: period.cc:48
virtual Representation * AddRepresentation(const MediaInfo &media_info)
uint32_t id() const
All the methods that are virtual are virtual for mocking.
bool NotifyCueEvent(uint32_t container_id, uint64_t timestamp) override
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:368
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
std::string Uint8VectorToBase64(const std::vector< uint8_t > &input)
Converts uint8 vector into base64 encoded string.
bool NotifySampleDuration(uint32_t container_id, uint32_t sample_duration) override
bool NotifyEncryptionUpdate(uint32_t container_id, const std::string &drm_uuid, const std::vector< uint8_t > &new_key_id, const std::vector< uint8_t > &new_pssh) override
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:354
virtual Representation * CopyRepresentationWithTimeOffset(const Representation &representation, uint64_t presentation_time_offset)
double start_time_in_seconds() const
Definition: period.h:57
ContentType GetContentType(const MediaInfo &media_info)
bool NotifyNewSegment(uint32_t container_id, uint64_t start_time, uint64_t duration, uint64_t size) override
bool WriteMpdToFile(const std::string &output_path, MpdBuilder *mpd_builder)
void set_id(uint32_t id)
bool NotifyNewContainer(const MediaInfo &media_info, uint32_t *id) override