Shaka Packager SDK
media_sample.cc
1 // Copyright 2014 Google LLC. 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 <cinttypes>
10 
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 #include <absl/strings/str_format.h>
14 
15 namespace shaka {
16 namespace media {
17 
18 MediaSample::MediaSample(const uint8_t* data,
19  size_t data_size,
20  const uint8_t* side_data,
21  size_t side_data_size,
22  bool is_key_frame)
23  : is_key_frame_(is_key_frame) {
24  if (!data) {
25  CHECK_EQ(data_size, 0u);
26  }
27 
28  SetData(data, data_size);
29  if (side_data) {
30  std::shared_ptr<uint8_t> shared_side_data(new uint8_t[side_data_size],
31  std::default_delete<uint8_t[]>());
32  memcpy(shared_side_data.get(), side_data, side_data_size);
33  side_data_ = std::move(shared_side_data);
34  side_data_size_ = side_data_size;
35  }
36 }
37 
38 MediaSample::MediaSample() {}
39 
40 MediaSample::~MediaSample() {}
41 
42 // static
43 std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
44  size_t data_size,
45  bool is_key_frame) {
46  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
47  CHECK(data);
48  return std::shared_ptr<MediaSample>(
49  new MediaSample(data, data_size, nullptr, 0u, is_key_frame));
50 }
51 
52 // static
53 std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
54  size_t data_size,
55  const uint8_t* side_data,
56  size_t side_data_size,
57  bool is_key_frame) {
58  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
59  CHECK(data);
60  return std::shared_ptr<MediaSample>(new MediaSample(
61  data, data_size, side_data, side_data_size, is_key_frame));
62 }
63 
64 // static
65 std::shared_ptr<MediaSample> MediaSample::FromMetadata(const uint8_t* metadata,
66  size_t metadata_size) {
67  return std::shared_ptr<MediaSample>(
68  new MediaSample(nullptr, 0, metadata, metadata_size, false));
69 }
70 
71 // static
72 std::shared_ptr<MediaSample> MediaSample::CreateEmptyMediaSample() {
73  return std::shared_ptr<MediaSample>(new MediaSample);
74 }
75 
76 // static
77 std::shared_ptr<MediaSample> MediaSample::CreateEOSBuffer() {
78  return std::shared_ptr<MediaSample>(
79  new MediaSample(nullptr, 0, nullptr, 0, false));
80 }
81 
82 std::shared_ptr<MediaSample> MediaSample::Clone() const {
83  std::shared_ptr<MediaSample> new_media_sample(new MediaSample);
84  new_media_sample->dts_ = dts_;
85  new_media_sample->pts_ = pts_;
86  new_media_sample->duration_ = duration_;
87  new_media_sample->is_key_frame_ = is_key_frame_;
88  new_media_sample->is_encrypted_ = is_encrypted_;
89  new_media_sample->data_ = data_;
90  new_media_sample->data_size_ = data_size_;
91  new_media_sample->side_data_ = side_data_;
92  new_media_sample->side_data_size_ = side_data_size_;
93  new_media_sample->config_id_ = config_id_;
94  if (decrypt_config_) {
95  new_media_sample->decrypt_config_.reset(new DecryptConfig(
96  decrypt_config_->key_id(), decrypt_config_->iv(),
97  decrypt_config_->subsamples(), decrypt_config_->protection_scheme(),
98  decrypt_config_->crypt_byte_block(),
99  decrypt_config_->skip_byte_block()));
100  }
101  return new_media_sample;
102 }
103 
104 void MediaSample::TransferData(std::shared_ptr<uint8_t> data,
105  size_t data_size) {
106  data_ = std::move(data);
107  data_size_ = data_size;
108 }
109 
110 void MediaSample::SetData(const uint8_t* data, size_t data_size) {
111  std::shared_ptr<uint8_t> shared_data(new uint8_t[data_size],
112  std::default_delete<uint8_t[]>());
113  memcpy(shared_data.get(), data, data_size);
114  TransferData(std::move(shared_data), data_size);
115 }
116 
117 std::string MediaSample::ToString() const {
118  if (end_of_stream())
119  return "End of stream sample\n";
120  return absl::StrFormat(
121  "dts: %" PRId64 "\n pts: %" PRId64 "\n duration: %" PRId64
122  "\n "
123  "is_key_frame: %s\n size: %zu\n side_data_size: %zu\n",
124  dts_, pts_, duration_, is_key_frame_ ? "true" : "false", data_size_,
125  side_data_size_);
126 }
127 
128 } // namespace media
129 } // namespace shaka
Class to hold a media sample.
Definition: media_sample.h:25
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66