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