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