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  const std::vector<std::string>& base_urls,
19  const std::string& output_path)
20  : MpdNotifier(mpd_options),
21  output_path_(output_path),
22  mpd_builder_(new MpdBuilder(mpd_options)) {
23  for (size_t i = 0; i < base_urls.size(); ++i)
24  mpd_builder_->AddBaseUrl(base_urls[i]);
25 }
26 
27 SimpleMpdNotifier::~SimpleMpdNotifier() {
28 }
29 
31  return true;
32 }
33 
34 bool SimpleMpdNotifier::NotifyNewContainer(const MediaInfo& media_info,
35  uint32_t* container_id) {
36  DCHECK(container_id);
37 
38  ContentType content_type = GetContentType(media_info);
39  if (content_type == kContentTypeUnknown)
40  return false;
41 
42  base::AutoLock auto_lock(lock_);
43 
44  // TODO(kqyang): Consider adding a new method MpdBuilder::AddRepresentation.
45  // Most of the codes here can be moved inside.
46  std::string key = GetAdaptationSetKey(media_info);
47  std::string lang = GetLanguage(media_info);
48  AdaptationSet** adaptation_set = &adaptation_set_map_[key];
49  if (*adaptation_set == NULL)
50  *adaptation_set = mpd_builder_->AddAdaptationSet(lang);
51 
52  DCHECK(*adaptation_set);
53  MediaInfo adjusted_media_info(media_info);
54  MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
55  Representation* representation =
56  (*adaptation_set)->AddRepresentation(adjusted_media_info);
57  if (representation == NULL)
58  return false;
59 
60  // For SimpleMpdNotifier, just put it in Representation. It should still
61  // generate a valid MPD.
62  AddContentProtectionElements(media_info, representation);
63  *container_id = representation->id();
64  DCHECK(!ContainsKey(representation_map_, representation->id()));
65  representation_map_[representation->id()] = representation;
66  return true;
67 }
68 
69 bool SimpleMpdNotifier::NotifySampleDuration(uint32_t container_id,
70  uint32_t sample_duration) {
71  base::AutoLock auto_lock(lock_);
72  RepresentationMap::iterator it = representation_map_.find(container_id);
73  if (it == representation_map_.end()) {
74  LOG(ERROR) << "Unexpected container_id: " << container_id;
75  return false;
76  }
77  it->second->SetSampleDuration(sample_duration);
78  return true;
79 }
80 
81 bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
82  uint64_t start_time,
83  uint64_t duration,
84  uint64_t size) {
85  base::AutoLock auto_lock(lock_);
86  RepresentationMap::iterator it = representation_map_.find(container_id);
87  if (it == representation_map_.end()) {
88  LOG(ERROR) << "Unexpected container_id: " << container_id;
89  return false;
90  }
91  it->second->AddNewSegment(start_time, duration, size);
92  return true;
93 }
94 
96  uint32_t container_id,
97  const std::string& drm_uuid,
98  const std::vector<uint8_t>& new_key_id,
99  const std::vector<uint8_t>& new_pssh) {
100  base::AutoLock auto_lock(lock_);
101  RepresentationMap::iterator it = representation_map_.find(container_id);
102  if (it == representation_map_.end()) {
103  LOG(ERROR) << "Unexpected container_id: " << container_id;
104  return false;
105  }
106  it->second->UpdateContentProtectionPssh(drm_uuid,
107  Uint8VectorToBase64(new_pssh));
108  return true;
109 }
110 
112  uint32_t container_id,
113  const ContentProtectionElement& content_protection_element) {
114  base::AutoLock auto_lock(lock_);
115  RepresentationMap::iterator it = representation_map_.find(container_id);
116  if (it == representation_map_.end()) {
117  LOG(ERROR) << "Unexpected container_id: " << container_id;
118  return false;
119  }
120  it->second->AddContentProtectionElement(content_protection_element);
121  return true;
122 }
123 
125  base::AutoLock auto_lock(lock_);
126  return WriteMpdToFile(output_path_, mpd_builder_.get());
127 }
128 
129 } // namespace shaka
uint32_t id() const
Definition: mpd_builder.h:498
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:658
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