| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "base/logging.h" |
| 6 #include "services/media/framework/parts/reader.h" |
| 7 #include "services/media/framework_ffmpeg/av_io_context.h" |
| 8 #include "services/media/framework_ffmpeg/ffmpeg_init.h" |
| 9 extern "C" { |
| 10 #include "third_party/ffmpeg/libavformat/avio.h" |
| 11 } |
| 12 |
| 13 namespace mojo { |
| 14 namespace media { |
| 15 |
| 16 void AVIOContextDeleter::operator()(AVIOContext* context) const { |
| 17 AvIoContext* av_io_context = reinterpret_cast<AvIoContext*>(context->opaque); |
| 18 DCHECK(av_io_context); |
| 19 delete av_io_context; |
| 20 av_free(context->buffer); |
| 21 av_free(context); |
| 22 } |
| 23 |
| 24 // static |
| 25 AvIoContextPtr AvIoContext::Create(std::shared_ptr<Reader> reader) { |
| 26 // Internal buffer size used by AVIO for reading. |
| 27 constexpr int kBufferSize = 32 * 1024; |
| 28 |
| 29 InitFfmpeg(); |
| 30 |
| 31 AVIOContext* result = avio_alloc_context( |
| 32 static_cast<unsigned char*>(av_malloc(kBufferSize)), |
| 33 kBufferSize, |
| 34 0, // write_flag |
| 35 new AvIoContext(reader), |
| 36 &Read, |
| 37 nullptr, |
| 38 &Seek); |
| 39 |
| 40 // Ensure FFmpeg only tries to seek when we know how. |
| 41 result->seekable = reader->CanSeek() ? AVIO_SEEKABLE_NORMAL : 0; |
| 42 |
| 43 // Ensure writing is disabled. |
| 44 result->write_flag = 0; |
| 45 |
| 46 return AvIoContextPtr(result); |
| 47 } |
| 48 |
| 49 // static |
| 50 int AvIoContext::Read(void* opaque, uint8_t* buf, int buf_size) { |
| 51 AvIoContext* av_io_context = reinterpret_cast<AvIoContext*>(opaque); |
| 52 return av_io_context->Read(buf, buf_size); |
| 53 } |
| 54 |
| 55 // static |
| 56 int64_t AvIoContext::Seek(void* opaque, int64_t offset, int whence) { |
| 57 AvIoContext* av_io_context = reinterpret_cast<AvIoContext*>(opaque); |
| 58 return av_io_context->Seek(offset, whence); |
| 59 } |
| 60 |
| 61 AvIoContext::~AvIoContext() {} |
| 62 |
| 63 AvIoContext::AvIoContext(std::shared_ptr<Reader> reader) : reader_(reader) { |
| 64 can_seek_ = reader_->CanSeek(); |
| 65 |
| 66 size_t size = reader_->GetSize(); |
| 67 size_ = size == Reader::kFailed ? -1 : static_cast<int>(size); |
| 68 } |
| 69 |
| 70 int AvIoContext::Read(uint8_t* buffer, size_t bytes_to_read) { |
| 71 size_t bytes_read = reader_->Read(buffer, bytes_to_read); |
| 72 if (bytes_read == Reader::kFailed) { |
| 73 return AVERROR(EIO); |
| 74 } |
| 75 position_ += bytes_read; |
| 76 return bytes_read; |
| 77 } |
| 78 |
| 79 int64_t AvIoContext::Seek(int64_t offset, int whence) { |
| 80 if (whence == AVSEEK_SIZE) { |
| 81 if (size_ == -1) { |
| 82 return AVERROR(EIO); |
| 83 } |
| 84 return size_; |
| 85 } |
| 86 |
| 87 switch (whence) { |
| 88 case SEEK_SET: |
| 89 position_ = offset; |
| 90 return position_; |
| 91 case SEEK_CUR: |
| 92 position_ += offset; |
| 93 return position_; |
| 94 case SEEK_END: |
| 95 if (size_ == -1) { |
| 96 return AVERROR(EIO); |
| 97 } |
| 98 position_ = size_ + offset; |
| 99 return position_; |
| 100 case AVSEEK_SIZE: |
| 101 if (size_ == -1) { |
| 102 return AVERROR(EIO); |
| 103 } |
| 104 return size_; |
| 105 default: |
| 106 NOTREACHED(); |
| 107 return AVERROR(EIO); |
| 108 } |
| 109 |
| 110 CHECK(size_ == -1 || position_ < size_) << "position out of range"; |
| 111 return position_; |
| 112 } |
| 113 |
| 114 |
| 115 } // namespace media |
| 116 } // namespace mojo |
| OLD | NEW |