Shaka Player Embedded
dynamic_buffer.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <glog/logging.h>
18 
19 #include <algorithm>
20 #include <cstring>
21 
22 namespace shaka {
23 namespace util {
24 
25 const size_t DynamicBuffer::kMinBufferSize;
26 
29 
32 
33 size_t DynamicBuffer::Size() const {
34  size_t size = 0;
35  for (auto& buffer : buffers_)
36  size += buffer.used;
37  return size;
38 }
39 
40 void DynamicBuffer::AppendCopy(const void* buffer, size_t size) {
41  if (!buffers_.empty()) {
42  auto* info = &buffers_.back();
43  const size_t to_copy = std::min(info->capacity - info->used, size);
44  std::memcpy(info->buffer.get() + info->used, buffer, to_copy);
45  info->used += to_copy;
46  buffer = reinterpret_cast<const uint8_t*>(buffer) + to_copy;
47  size -= to_copy;
48  }
49 
50  if (size > 0) {
51  const size_t capacity = std::max(kMinBufferSize, size);
52  auto* ptr = new uint8_t[capacity];
53  std::memcpy(ptr, buffer, size);
54  buffers_.emplace_back(ptr, size, capacity);
55  }
56 }
57 
58 std::string DynamicBuffer::CreateString() const {
59  std::string ret(Size(), '\0');
60  CopyDataTo(reinterpret_cast<uint8_t*>(&ret[0]), ret.size());
61  return ret;
62 }
63 
64 void DynamicBuffer::CopyDataTo(uint8_t* dest, size_t size) const {
65  for (auto& buffer : buffers_) {
66  CHECK_GE(size, buffer.used);
67  std::memcpy(dest, buffer.buffer.get(), buffer.used);
68  dest += buffer.used;
69  size -= buffer.used;
70  }
71 }
72 
73 DynamicBuffer::SubBuffer::SubBuffer(uint8_t* buffer, size_t used,
74  size_t capacity)
75  : buffer(buffer), used(used), capacity(capacity) {}
76 
77 DynamicBuffer::SubBuffer::~SubBuffer() {}
78 
79 } // namespace util
80 } // namespace shaka
const char * dest
Definition: media_utils.cc:31
void CopyDataTo(uint8_t *dest, size_t size) const
std::string CreateString() const
void AppendCopy(const void *buffer, size_t size)
DynamicBuffer & operator=(const DynamicBuffer &)=delete