bitstream_buffer.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/base/bitstream_buffer.h"
  5. #include "base/numerics/checked_math.h"
  6. #include "media/base/decrypt_config.h"
  7. namespace media {
  8. BitstreamBuffer::BitstreamBuffer()
  9. : BitstreamBuffer(-1, base::UnsafeSharedMemoryRegion(), 0) {}
  10. BitstreamBuffer::BitstreamBuffer(int32_t id,
  11. base::UnsafeSharedMemoryRegion region,
  12. size_t size,
  13. uint64_t offset,
  14. base::TimeDelta presentation_timestamp)
  15. : id_(id),
  16. region_(std::move(region)),
  17. size_(size),
  18. offset_(offset),
  19. presentation_timestamp_(presentation_timestamp) {}
  20. BitstreamBuffer::BitstreamBuffer(BitstreamBuffer&&) = default;
  21. BitstreamBuffer& BitstreamBuffer::operator=(BitstreamBuffer&&) = default;
  22. BitstreamBuffer::~BitstreamBuffer() = default;
  23. scoped_refptr<DecoderBuffer> BitstreamBuffer::ToDecoderBuffer() {
  24. return ToDecoderBuffer(0, size_);
  25. }
  26. scoped_refptr<DecoderBuffer> BitstreamBuffer::ToDecoderBuffer(off_t offset,
  27. size_t size) {
  28. // We do allow mapping beyond the bounds of the original specified size in
  29. // order to deal with the OEMCrypto secure buffer format, but we do ensure
  30. // it stays within the shared memory region.
  31. base::CheckedNumeric<off_t> total_range(offset_);
  32. total_range += offset;
  33. if (!total_range.IsValid())
  34. return nullptr;
  35. const off_t final_offset = total_range.ValueOrDie();
  36. total_range += base::checked_cast<off_t>(size);
  37. if (!total_range.IsValid<size_t>())
  38. return nullptr;
  39. if (total_range.ValueOrDie<size_t>() > region_.GetSize())
  40. return nullptr;
  41. scoped_refptr<DecoderBuffer> buffer = DecoderBuffer::FromSharedMemoryRegion(
  42. std::move(region_), final_offset, size);
  43. if (!buffer)
  44. return nullptr;
  45. buffer->set_timestamp(presentation_timestamp_);
  46. if (!key_id_.empty()) {
  47. buffer->set_decrypt_config(
  48. DecryptConfig::CreateCencConfig(key_id_, iv_, subsamples_));
  49. }
  50. return buffer;
  51. }
  52. void BitstreamBuffer::SetDecryptionSettings(
  53. const std::string& key_id,
  54. const std::string& iv,
  55. const std::vector<SubsampleEntry>& subsamples) {
  56. key_id_ = key_id;
  57. iv_ = iv;
  58. subsamples_ = subsamples;
  59. }
  60. } // namespace media