Chromium Code Reviews| 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 const int kBufferSize = 32 * 1024; | |
|
johngro
2016/03/21 21:06:08
constexpr
| |
| 28 | |
| 29 InitFfmpeg(); | |
|
johngro
2016/03/21 21:06:08
your other FFmpeg helper classes/structs seem to m
dalesat
2016/03/22 00:31:35
Done.
| |
| 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 DCHECK(size <= std::numeric_limits<int64_t>::max()); | |
|
johngro
2016/03/21 21:06:08
this DCHECK can never fail, so its probably OK to
dalesat
2016/03/22 00:31:35
Done.
| |
| 68 size_ = size == Reader::kFailed ? -1 : static_cast<int>(size); | |
| 69 } | |
| 70 | |
| 71 int AvIoContext::Read(uint8_t* buffer, size_t bytes_to_read) { | |
| 72 size_t bytes_read = reader_->Read(buffer, bytes_to_read); | |
| 73 if (bytes_read == Reader::kFailed) { | |
| 74 return AVERROR(EIO); | |
| 75 } | |
| 76 position_ += bytes_read; | |
| 77 return bytes_read; | |
| 78 } | |
| 79 | |
| 80 int64_t AvIoContext::Seek(int64_t offset, int whence) { | |
| 81 if (whence == AVSEEK_SIZE) { | |
| 82 if (size_ == -1) { | |
| 83 return AVERROR(EIO); | |
| 84 } | |
| 85 return size_; | |
| 86 } | |
| 87 | |
| 88 switch (whence) { | |
| 89 case SEEK_SET: | |
| 90 position_ = offset; | |
| 91 return position_; | |
| 92 case SEEK_CUR: | |
| 93 position_ += offset; | |
| 94 return position_; | |
| 95 case SEEK_END: | |
| 96 if (size_ == -1) { | |
| 97 return AVERROR(EIO); | |
| 98 } | |
| 99 position_ = size_ + offset; | |
| 100 return position_; | |
| 101 case AVSEEK_SIZE: | |
| 102 if (size_ == -1) { | |
| 103 return AVERROR(EIO); | |
| 104 } | |
| 105 return size_; | |
| 106 default: | |
| 107 NOTREACHED(); | |
| 108 return AVERROR(EIO); | |
| 109 } | |
| 110 | |
| 111 CHECK(size_ == -1 || position_ < size_) << "position out of range"; | |
| 112 return position_; | |
| 113 } | |
| 114 | |
| 115 | |
| 116 } // namespace media | |
| 117 } // namespace mojo | |
| OLD | NEW |