DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
mpd_notifier_util.cc
1 // Copyright 2015 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/mpd_notifier_util.h"
8 
9 #include "packager/base/strings/string_number_conversions.h"
10 #include "packager/base/strings/string_util.h"
11 #include "packager/media/file/file.h"
12 #include "packager/media/file/file_closer.h"
13 #include "packager/mpd/base/mpd_utils.h"
14 
15 namespace shaka {
16 
17 using media::File;
18 using media::FileCloser;
19 
20 bool WriteMpdToFile(const std::string& output_path, MpdBuilder* mpd_builder) {
21  CHECK(!output_path.empty());
22 
23  std::string mpd;
24  if (!mpd_builder->ToString(&mpd)) {
25  LOG(ERROR) << "Failed to write MPD to string.";
26  return false;
27  }
28 
29  std::unique_ptr<File, FileCloser> file(File::Open(output_path.c_str(), "w"));
30  if (!file) {
31  LOG(ERROR) << "Failed to open file for writing: " << output_path;
32  return false;
33  }
34 
35  const char* mpd_char_ptr = mpd.data();
36  size_t mpd_bytes_left = mpd.size();
37  while (mpd_bytes_left > 0) {
38  int64_t length = file->Write(mpd_char_ptr, mpd_bytes_left);
39  if (length <= 0) {
40  LOG(ERROR) << "Failed to write to file '" << output_path << "' ("
41  << length << ").";
42  return false;
43  }
44  mpd_char_ptr += length;
45  mpd_bytes_left -= length;
46  }
47  // Release the pointer because Close() destructs itself.
48  return file.release()->Close();
49 }
50 
51 ContentType GetContentType(const MediaInfo& media_info) {
52  const bool has_video = media_info.has_video_info();
53  const bool has_audio = media_info.has_audio_info();
54  const bool has_text = media_info.has_text_info();
55 
56  if (MoreThanOneTrue(has_video, has_audio, has_text)) {
57  NOTIMPLEMENTED() << "MediaInfo with more than one stream is not supported.";
58  return kContentTypeUnknown;
59  }
60  if (!AtLeastOneTrue(has_video, has_audio, has_text)) {
61  LOG(ERROR) << "MediaInfo should contain one audio, video, or text stream.";
62  return kContentTypeUnknown;
63  }
64  return has_video ? kContentTypeVideo
65  : (has_audio ? kContentTypeAudio : kContentTypeText);
66 }
67 
68 std::string Uint8VectorToBase64(const std::vector<uint8_t>& input) {
69  std::string output;
70  std::string input_in_string(input.begin(), input.end());
71  base::Base64Encode(input_in_string, &output);
72  return output;
73 }
74 
75 } // namespace shaka
This class generates DASH MPDs (Media Presentation Descriptions).
Definition: mpd_builder.h:57
std::string Uint8VectorToBase64(const std::vector< uint8_t > &input)
Converts uint8 vector into base64 encoded string.
virtual bool ToString(std::string *output)
Definition: mpd_builder.cc:431
ContentType GetContentType(const MediaInfo &media_info)
bool WriteMpdToFile(const std::string &output_path, MpdBuilder *mpd_builder)