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