Shaka Packager SDK
period.cc
1 // Copyright 2017 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/period.h"
8 
9 #include "packager/base/stl_util.h"
10 #include "packager/mpd/base/adaptation_set.h"
11 #include "packager/mpd/base/mpd_options.h"
12 #include "packager/mpd/base/mpd_utils.h"
13 #include "packager/mpd/base/xml/xml_node.h"
14 
15 namespace shaka {
16 namespace {
17 
18 // The easiest way to check whether two protobufs are equal, is to compare the
19 // serialized version.
20 bool ProtectedContentEq(
21  const MediaInfo::ProtectedContent& content_protection1,
22  const MediaInfo::ProtectedContent& content_protection2) {
23  return content_protection1.SerializeAsString() ==
24  content_protection2.SerializeAsString();
25 }
26 
27 std::set<std::string> GetUUIDs(
28  const MediaInfo::ProtectedContent& protected_content) {
29  std::set<std::string> uuids;
30  for (const auto& entry : protected_content.content_protection_entry())
31  uuids.insert(entry.uuid());
32  return uuids;
33 }
34 
35 } // namespace
36 
37 Period::Period(uint32_t period_id,
38  double start_time_in_seconds,
39  const MpdOptions& mpd_options,
40  base::AtomicSequenceNumber* representation_counter)
41  : id_(period_id),
42  start_time_in_seconds_(start_time_in_seconds),
43  mpd_options_(mpd_options),
44  representation_counter_(representation_counter) {}
45 
47  const MediaInfo& media_info,
48  bool content_protection_in_adaptation_set) {
49  // Set duration if it is not set. It may be updated later from duration
50  // calculated from segments.
51  if (duration_seconds_ == 0)
52  duration_seconds_ = media_info.media_duration_seconds();
53 
54  // AdaptationSets with the same key should only differ in ContentProtection,
55  // which also means that if |content_protection_in_adaptation_set| is false,
56  // there should be at most one entry in |adaptation_sets|.
57  const std::string key = GetAdaptationSetKey(media_info);
58  std::list<AdaptationSet*>& adaptation_sets = adaptation_set_list_map_[key];
59  if (content_protection_in_adaptation_set) {
60  for (AdaptationSet* adaptation_set : adaptation_sets) {
61  if (protected_adaptation_set_map_.Match(*adaptation_set, media_info))
62  return adaptation_set;
63  }
64  } else {
65  if (!adaptation_sets.empty()) {
66  DCHECK_EQ(adaptation_sets.size(), 1u);
67  return adaptation_sets.front();
68  }
69  }
70  // None of the adaptation sets match with the new content protection.
71  // Need a new one.
72  const std::string language = GetLanguage(media_info);
73  std::unique_ptr<AdaptationSet> new_adaptation_set =
74  NewAdaptationSet(language, mpd_options_, representation_counter_);
75  if (!SetNewAdaptationSetAttributes(language, media_info, adaptation_sets,
76  new_adaptation_set.get())) {
77  return nullptr;
78  }
79 
80  if (content_protection_in_adaptation_set &&
81  media_info.has_protected_content()) {
82  protected_adaptation_set_map_.Register(*new_adaptation_set, media_info);
83  AddContentProtectionElements(media_info, new_adaptation_set.get());
84 
85  for (AdaptationSet* adaptation_set : adaptation_sets) {
86  if (protected_adaptation_set_map_.Switchable(*adaptation_set,
87  *new_adaptation_set)) {
88  adaptation_set->AddAdaptationSetSwitching(new_adaptation_set.get());
89  new_adaptation_set->AddAdaptationSetSwitching(adaptation_set);
90  }
91  }
92  }
93  AdaptationSet* adaptation_set_ptr = new_adaptation_set.get();
94  adaptation_sets.push_back(adaptation_set_ptr);
95  adaptation_sets_.emplace_back(std::move(new_adaptation_set));
96  return adaptation_set_ptr;
97 }
98 
99 xml::scoped_xml_ptr<xmlNode> Period::GetXml(bool output_period_duration) {
100  adaptation_sets_.sort(
101  [](const std::unique_ptr<AdaptationSet>& adaptation_set_a,
102  const std::unique_ptr<AdaptationSet>& adaptation_set_b) {
103  if (!adaptation_set_a->has_id())
104  return false;
105  if (!adaptation_set_b->has_id())
106  return true;
107  return adaptation_set_a->id() < adaptation_set_b->id();
108  });
109 
110  xml::XmlNode period("Period");
111 
112  // Required for 'dynamic' MPDs.
113  period.SetId(id_);
114  // Iterate thru AdaptationSets and add them to one big Period element.
115  for (const auto& adaptation_set : adaptation_sets_) {
116  xml::scoped_xml_ptr<xmlNode> child(adaptation_set->GetXml());
117  if (!child || !period.AddChild(std::move(child)))
118  return nullptr;
119  }
120 
121  if (output_period_duration) {
122  period.SetStringAttribute("duration",
123  SecondsToXmlDuration(duration_seconds_));
124  } else if (mpd_options_.mpd_type == MpdType::kDynamic) {
125  period.SetStringAttribute("start",
126  SecondsToXmlDuration(start_time_in_seconds_));
127  }
128  return period.PassScopedPtr();
129 }
130 
131 const std::list<AdaptationSet*> Period::GetAdaptationSets() const {
132  std::list<AdaptationSet*> adaptation_sets;
133  for (const auto& adaptation_set : adaptation_sets_) {
134  adaptation_sets.push_back(adaptation_set.get());
135  }
136  return adaptation_sets;
137 }
138 
139 std::unique_ptr<AdaptationSet> Period::NewAdaptationSet(
140  const std::string& language,
141  const MpdOptions& options,
142  base::AtomicSequenceNumber* representation_counter) {
143  return std::unique_ptr<AdaptationSet>(
144  new AdaptationSet(language, options, representation_counter));
145 }
146 
147 bool Period::SetNewAdaptationSetAttributes(
148  const std::string& language,
149  const MediaInfo& media_info,
150  const std::list<AdaptationSet*>& adaptation_sets,
151  AdaptationSet* new_adaptation_set) {
152  if (!language.empty() && language == mpd_options_.mpd_params.default_language)
153  new_adaptation_set->AddRole(AdaptationSet::kRoleMain);
154 
155  if (media_info.has_video_info()) {
156  // Because 'language' is ignored for videos, |adaptation_sets| must have
157  // all the video AdaptationSets.
158  if (adaptation_sets.size() > 1) {
159  new_adaptation_set->AddRole(AdaptationSet::kRoleMain);
160  } else if (adaptation_sets.size() == 1) {
161  (*adaptation_sets.begin())->AddRole(AdaptationSet::kRoleMain);
162  new_adaptation_set->AddRole(AdaptationSet::kRoleMain);
163  }
164 
165  if (media_info.video_info().has_playback_rate()) {
166  const AdaptationSet* trick_play_reference_adaptation_set =
167  FindOriginalAdaptationSetForTrickPlay(media_info);
168  if (!trick_play_reference_adaptation_set) {
169  LOG(ERROR) << "Failed to find original AdaptationSet for trick play.";
170  return false;
171  }
172  new_adaptation_set->AddTrickPlayReference(
173  trick_play_reference_adaptation_set);
174  }
175  } else if (media_info.has_text_info()) {
176  // IOP requires all AdaptationSets to have (sub)segmentAlignment set to
177  // true, so carelessly set it to true.
178  // In practice it doesn't really make sense to adapt between text tracks.
179  new_adaptation_set->ForceSetSegmentAlignment(true);
180  }
181  return true;
182 }
183 
184 const AdaptationSet* Period::FindOriginalAdaptationSetForTrickPlay(
185  const MediaInfo& media_info) {
186  MediaInfo media_info_no_trickplay = media_info;
187  media_info_no_trickplay.mutable_video_info()->clear_playback_rate();
188 
189  std::string key = GetAdaptationSetKey(media_info_no_trickplay);
190  const std::list<AdaptationSet*>& adaptation_sets =
191  adaptation_set_list_map_[key];
192  for (AdaptationSet* adaptation_set : adaptation_sets) {
193  if (protected_adaptation_set_map_.Match(*adaptation_set, media_info)) {
194  return adaptation_set;
195  }
196  }
197  return nullptr;
198 }
199 
200 void Period::ProtectedAdaptationSetMap::Register(
201  const AdaptationSet& adaptation_set,
202  const MediaInfo& media_info) {
203  DCHECK(!ContainsKey(protected_content_map_, &adaptation_set));
204  protected_content_map_[&adaptation_set] = media_info.protected_content();
205 }
206 
207 bool Period::ProtectedAdaptationSetMap::Match(
208  const AdaptationSet& adaptation_set,
209  const MediaInfo& media_info) {
210  const auto protected_content_it =
211  protected_content_map_.find(&adaptation_set);
212  // If the AdaptationSet ID is not registered in the map, then it is clear
213  // content.
214  if (protected_content_it == protected_content_map_.end())
215  return !media_info.has_protected_content();
216  if (!media_info.has_protected_content())
217  return false;
218  return ProtectedContentEq(protected_content_it->second,
219  media_info.protected_content());
220 }
221 
222 bool Period::ProtectedAdaptationSetMap::Switchable(
223  const AdaptationSet& adaptation_set_a,
224  const AdaptationSet& adaptation_set_b) {
225  const auto protected_content_it_a =
226  protected_content_map_.find(&adaptation_set_a);
227  const auto protected_content_it_b =
228  protected_content_map_.find(&adaptation_set_b);
229 
230  if (protected_content_it_a == protected_content_map_.end())
231  return protected_content_it_b == protected_content_map_.end();
232  if (protected_content_it_b == protected_content_map_.end())
233  return false;
234  // Get all the UUIDs of the AdaptationSet. If another AdaptationSet has the
235  // same UUIDs then those are switchable.
236  return GetUUIDs(protected_content_it_a->second) ==
237  GetUUIDs(protected_content_it_b->second);
238 }
239 
240 } // namespace shaka
std::string default_language
Definition: mpd_params.h:56
virtual AdaptationSet * GetOrCreateAdaptationSet(const MediaInfo &media_info, bool content_protection_in_adaptation_set)
Definition: period.cc:46
scoped_xml_ptr< xmlNode > PassScopedPtr()
Definition: xml_node.cc:169
All the methods that are virtual are virtual for mocking.
void SetStringAttribute(const char *attribute_name, const std::string &attribute)
Definition: xml_node.cc:137
bool AddChild(scoped_xml_ptr< xmlNode > child)
Definition: xml_node.cc:95
virtual void AddRole(Role role)
xml::scoped_xml_ptr< xmlNode > GetXml(bool output_period_duration)
Definition: period.cc:99
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:369
void SetId(uint32_t id)
Definition: xml_node.cc:160
virtual void ForceSetSegmentAlignment(bool segment_alignment)
const std::list< AdaptationSet * > GetAdaptationSets() const
Definition: period.cc:131
Defines Mpd Options.
Definition: mpd_options.h:25
Period(uint32_t period_id, double start_time_in_seconds, const MpdOptions &mpd_options, base::AtomicSequenceNumber *representation_counter)
Definition: period.cc:37
virtual void AddTrickPlayReference(const AdaptationSet *adaptation_set)