DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
simple_mpd_notifier.cc
1 // Copyright 2014 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 "packager/base/logging.h"
10 #include "packager/base/stl_util.h"
11 #include "packager/mpd/base/mpd_builder.h"
12 #include "packager/mpd/base/mpd_notifier_util.h"
13 #include "packager/mpd/base/mpd_utils.h"
14 
15 namespace shaka {
16 
17 SimpleMpdNotifier::SimpleMpdNotifier(const MpdOptions& mpd_options)
18  : MpdNotifier(mpd_options),
19  output_path_(mpd_options.mpd_params.mpd_output),
20  mpd_builder_(new MpdBuilder(mpd_options)) {
21  for (const std::string& base_url : mpd_options.mpd_params.base_urls)
22  mpd_builder_->AddBaseUrl(base_url);
23 }
24 
25 SimpleMpdNotifier::~SimpleMpdNotifier() {
26 }
27 
29  return true;
30 }
31 
32 bool SimpleMpdNotifier::NotifyNewContainer(const MediaInfo& media_info,
33  uint32_t* container_id) {
34  DCHECK(container_id);
35 
36  ContentType content_type = GetContentType(media_info);
37  if (content_type == kContentTypeUnknown)
38  return false;
39 
40  base::AutoLock auto_lock(lock_);
41 
42  // TODO(kqyang): Consider adding a new method MpdBuilder::AddRepresentation.
43  // Most of the codes here can be moved inside.
44  std::string key = GetAdaptationSetKey(media_info);
45  std::string lang = GetLanguage(media_info);
46  AdaptationSet** adaptation_set = &adaptation_set_map_[key];
47  if (*adaptation_set == NULL)
48  *adaptation_set = mpd_builder_->AddAdaptationSet(lang);
49 
50  DCHECK(*adaptation_set);
51  MediaInfo adjusted_media_info(media_info);
52  MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
53  Representation* representation =
54  (*adaptation_set)->AddRepresentation(adjusted_media_info);
55  if (representation == NULL)
56  return false;
57 
58  // For SimpleMpdNotifier, just put it in Representation. It should still
59  // generate a valid MPD.
60  AddContentProtectionElements(media_info, representation);
61  *container_id = representation->id();
62  DCHECK(!ContainsKey(representation_map_, representation->id()));
63  representation_map_[representation->id()] = representation;
64  return true;
65 }
66 
67 bool SimpleMpdNotifier::NotifySampleDuration(uint32_t container_id,
68  uint32_t sample_duration) {
69  base::AutoLock auto_lock(lock_);
70  RepresentationMap::iterator it = representation_map_.find(container_id);
71  if (it == representation_map_.end()) {
72  LOG(ERROR) << "Unexpected container_id: " << container_id;
73  return false;
74  }
75  it->second->SetSampleDuration(sample_duration);
76  return true;
77 }
78 
79 bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
80  uint64_t start_time,
81  uint64_t duration,
82  uint64_t size) {
83  base::AutoLock auto_lock(lock_);
84  RepresentationMap::iterator it = representation_map_.find(container_id);
85  if (it == representation_map_.end()) {
86  LOG(ERROR) << "Unexpected container_id: " << container_id;
87  return false;
88  }
89  it->second->AddNewSegment(start_time, duration, size);
90  return true;
91 }
92 
94  uint32_t container_id,
95  const std::string& drm_uuid,
96  const std::vector<uint8_t>& new_key_id,
97  const std::vector<uint8_t>& new_pssh) {
98  base::AutoLock auto_lock(lock_);
99  RepresentationMap::iterator it = representation_map_.find(container_id);
100  if (it == representation_map_.end()) {
101  LOG(ERROR) << "Unexpected container_id: " << container_id;
102  return false;
103  }
104  it->second->UpdateContentProtectionPssh(drm_uuid,
105  Uint8VectorToBase64(new_pssh));
106  return true;
107 }
108 
110  uint32_t container_id,
111  const ContentProtectionElement& content_protection_element) {
112  base::AutoLock auto_lock(lock_);
113  RepresentationMap::iterator it = representation_map_.find(container_id);
114  if (it == representation_map_.end()) {
115  LOG(ERROR) << "Unexpected container_id: " << container_id;
116  return false;
117  }
118  it->second->AddContentProtectionElement(content_protection_element);
119  return true;
120 }
121 
123  base::AutoLock auto_lock(lock_);
124  return WriteMpdToFile(output_path_, mpd_builder_.get());
125 }
126 
127 } // namespace shaka
uint32_t id() const
Definition: mpd_builder.h:483
bool AddContentProtectionElement(uint32_t id, const ContentProtectionElement &content_protection_element) override
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:356
bool NotifyNewSegment(uint32_t id, uint64_t start_time, uint64_t duration, uint64_t size) override
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:621
ContentType GetContentType(const MediaInfo &media_info)
bool WriteMpdToFile(const std::string &output_path, MpdBuilder *mpd_builder)
bool NotifyNewContainer(const MediaInfo &media_info, uint32_t *id) override