DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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  // TODO(kqyang): Consider adding a new method MpdBuilder::AddRepresentation.
48  // Most of the codes here can be moved inside.
49  std::string lang;
50  if (media_info.has_audio_info()) {
51  lang = media_info.audio_info().language();
52  } else if (media_info.has_text_info()) {
53  lang = media_info.text_info().language();
54  }
55  AdaptationSet** adaptation_set = &adaptation_set_map_[content_type][lang];
56  if (*adaptation_set == NULL)
57  *adaptation_set = mpd_builder_->AddAdaptationSet(lang);
58 
59  DCHECK(*adaptation_set);
60  MediaInfo adjusted_media_info(media_info);
61  MpdBuilder::MakePathsRelativeToMpd(output_path_, &adjusted_media_info);
62  Representation* representation =
63  (*adaptation_set)->AddRepresentation(adjusted_media_info);
64  if (representation == NULL)
65  return false;
66 
67  // For SimpleMpdNotifier, just put it in Representation. It should still
68  // generate a valid MPD.
69  AddContentProtectionElements(media_info, representation);
70  *container_id = representation->id();
71  DCHECK(!ContainsKey(representation_map_, representation->id()));
72  representation_map_[representation->id()] = representation;
73  return true;
74 }
75 
76 bool SimpleMpdNotifier::NotifySampleDuration(uint32_t container_id,
77  uint32_t sample_duration) {
78  base::AutoLock auto_lock(lock_);
79  RepresentationMap::iterator it = representation_map_.find(container_id);
80  if (it == representation_map_.end()) {
81  LOG(ERROR) << "Unexpected container_id: " << container_id;
82  return false;
83  }
84  it->second->SetSampleDuration(sample_duration);
85  return true;
86 }
87 
88 bool SimpleMpdNotifier::NotifyNewSegment(uint32_t container_id,
89  uint64_t start_time,
90  uint64_t duration,
91  uint64_t size) {
92  base::AutoLock auto_lock(lock_);
93  RepresentationMap::iterator it = representation_map_.find(container_id);
94  if (it == representation_map_.end()) {
95  LOG(ERROR) << "Unexpected container_id: " << container_id;
96  return false;
97  }
98  it->second->AddNewSegment(start_time, duration, size);
99  return true;
100 }
101 
103  uint32_t container_id,
104  const std::string& drm_uuid,
105  const std::vector<uint8_t>& new_key_id,
106  const std::vector<uint8_t>& new_pssh) {
107  base::AutoLock auto_lock(lock_);
108  RepresentationMap::iterator it = representation_map_.find(container_id);
109  if (it == representation_map_.end()) {
110  LOG(ERROR) << "Unexpected container_id: " << container_id;
111  return false;
112  }
113  it->second->UpdateContentProtectionPssh(drm_uuid,
114  Uint8VectorToBase64(new_pssh));
115  return true;
116 }
117 
119  uint32_t container_id,
120  const ContentProtectionElement& content_protection_element) {
121  base::AutoLock auto_lock(lock_);
122  RepresentationMap::iterator it = representation_map_.find(container_id);
123  if (it == representation_map_.end()) {
124  LOG(ERROR) << "Unexpected container_id: " << container_id;
125  return false;
126  }
127  it->second->AddContentProtectionElement(content_protection_element);
128  return true;
129 }
130 
132  base::AutoLock auto_lock(lock_);
133  return WriteMpdToFile(output_path_, mpd_builder_.get());
134 }
135 
136 } // 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:268
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:626
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)