DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
media_sample.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/media/base/media_sample.h"
8 
9 #include <inttypes.h>
10 
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/stringprintf.h"
13 
14 namespace shaka {
15 namespace media {
16 
17 MediaSample::MediaSample(const uint8_t* data,
18  size_t size,
19  const uint8_t* side_data,
20  size_t side_data_size,
21  bool is_key_frame)
22  : dts_(0),
23  pts_(0),
24  duration_(0),
25  is_key_frame_(is_key_frame),
26  is_encrypted_(false) {
27  if (!data) {
28  CHECK_EQ(size, 0u);
29  }
30 
31  data_.assign(data, data + size);
32  if (side_data)
33  side_data_.assign(side_data, side_data + side_data_size);
34 }
35 
36 MediaSample::MediaSample() : dts_(0),
37  pts_(0),
38  duration_(0),
39  is_key_frame_(false),
40  is_encrypted_(false) {}
41 
42 MediaSample::~MediaSample() {}
43 
44 // static
45 std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
46  size_t data_size,
47  bool is_key_frame) {
48  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
49  CHECK(data);
50  return std::make_shared<MediaSample>(data, data_size, nullptr, 0u,
51  is_key_frame);
52 }
53 
54 // static
55 std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
56  size_t data_size,
57  const uint8_t* side_data,
58  size_t side_data_size,
59  bool is_key_frame) {
60  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
61  CHECK(data);
62  return std::make_shared<MediaSample>(data, data_size, side_data,
63  side_data_size, is_key_frame);
64 }
65 
66 // static
67 std::shared_ptr<MediaSample> MediaSample::CopyFrom(
68  const MediaSample& media_sample) {
69  std::shared_ptr<MediaSample> new_media_sample = CopyFrom(
70  media_sample.data(), media_sample.data_size(), media_sample.side_data(),
71  media_sample.side_data_size(), media_sample.is_key_frame());
72 
73  new_media_sample->set_dts(media_sample.dts());
74  new_media_sample->set_pts(media_sample.pts());
75  new_media_sample->set_is_encrypted(media_sample.is_encrypted());
76  new_media_sample->set_config_id(media_sample.config_id());
77  new_media_sample->set_duration(media_sample.duration());
78 
79  if (media_sample.decrypt_config()) {
80  std::unique_ptr<DecryptConfig> decrypt_config(
81  new DecryptConfig(media_sample.decrypt_config()->key_id(),
82  media_sample.decrypt_config()->iv(),
83  media_sample.decrypt_config()->subsamples(),
84  media_sample.decrypt_config()->protection_scheme(),
85  media_sample.decrypt_config()->crypt_byte_block(),
86  media_sample.decrypt_config()->skip_byte_block()));
87  new_media_sample->set_decrypt_config(std::move(decrypt_config));
88  }
89  return new_media_sample;
90 }
91 
92 // static
93 std::shared_ptr<MediaSample> MediaSample::FromMetadata(const uint8_t* metadata,
94  size_t metadata_size) {
95  return std::make_shared<MediaSample>(nullptr, 0, metadata, metadata_size,
96  false);
97 }
98 
99 // static
100 std::shared_ptr<MediaSample> MediaSample::CreateEmptyMediaSample() {
101  return std::make_shared<MediaSample>();
102 }
103 
104 // static
105 std::shared_ptr<MediaSample> MediaSample::CreateEOSBuffer() {
106  return std::make_shared<MediaSample>(nullptr, 0, nullptr, 0, false);
107 }
108 
109 std::string MediaSample::ToString() const {
110  if (end_of_stream())
111  return "End of stream sample\n";
112  return base::StringPrintf(
113  "dts: %" PRId64 "\n pts: %" PRId64 "\n duration: %" PRId64 "\n "
114  "is_key_frame: %s\n size: %zu\n side_data_size: %zu\n",
115  dts_,
116  pts_,
117  duration_,
118  is_key_frame_ ? "true" : "false",
119  data_.size(),
120  side_data_.size());
121 }
122 
123 } // namespace media
124 } // namespace shaka
static std::shared_ptr< MediaSample > CreateEOSBuffer()
static std::shared_ptr< MediaSample > CreateEmptyMediaSample()
Create a MediaSample object with default members.
std::string ToString() const
static std::shared_ptr< MediaSample > FromMetadata(const uint8_t *metadata, size_t metadata_size)
Definition: media_sample.cc:93
Class to hold a media sample.
Definition: media_sample.h:22
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45