in_memory_url_protocol.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2011 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/filters/in_memory_url_protocol.h"
  5. #include "media/ffmpeg/ffmpeg_common.h"
  6. namespace media {
  7. InMemoryUrlProtocol::InMemoryUrlProtocol(const uint8_t* data,
  8. int64_t size,
  9. bool streaming)
  10. : data_(data),
  11. size_(size >= 0 ? size : 0),
  12. position_(0),
  13. streaming_(streaming) {}
  14. InMemoryUrlProtocol::~InMemoryUrlProtocol() = default;
  15. int InMemoryUrlProtocol::Read(int size, uint8_t* data) {
  16. // Not sure this can happen, but it's unclear from the ffmpeg code, so guard
  17. // against it.
  18. if (size < 0)
  19. return AVERROR(EIO);
  20. if (!size)
  21. return 0;
  22. const int64_t available_bytes = size_ - position_;
  23. if (available_bytes <= 0)
  24. return AVERROR_EOF;
  25. if (size > available_bytes)
  26. size = available_bytes;
  27. if (size > 0) {
  28. memcpy(data, data_ + position_, size);
  29. position_ += size;
  30. }
  31. return size;
  32. }
  33. bool InMemoryUrlProtocol::GetPosition(int64_t* position_out) {
  34. if (!position_out)
  35. return false;
  36. *position_out = position_;
  37. return true;
  38. }
  39. bool InMemoryUrlProtocol::SetPosition(int64_t position) {
  40. if (position < 0 || position > size_)
  41. return false;
  42. position_ = position;
  43. return true;
  44. }
  45. bool InMemoryUrlProtocol::GetSize(int64_t* size_out) {
  46. if (!size_out)
  47. return false;
  48. *size_out = size_;
  49. return true;
  50. }
  51. bool InMemoryUrlProtocol::IsStreaming() {
  52. return streaming_;
  53. }
  54. } // namespace media