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/ffmpeg_init.h" |
| 8 #include "services/media/framework_ffmpeg/ffmpeg_io.h" |
| 9 extern "C" { |
| 10 #include "third_party/ffmpeg/libavformat/avio.h" |
| 11 } |
| 12 |
| 13 namespace mojo { |
| 14 namespace media { |
| 15 |
| 16 static int AvioRead(void* opaque, uint8_t* buf, int buf_size); |
| 17 static int64_t AvioSeek(void* opaque, int64_t offset, int whence); |
| 18 |
| 19 AVIOContext* CreateAvioContext(Reader* reader) { |
| 20 // Internal buffer size used by AVIO for reading. |
| 21 const int kBufferSize = 32 * 1024; |
| 22 |
| 23 InitFfmpeg(); |
| 24 |
| 25 AVIOContext* result = avio_alloc_context( |
| 26 static_cast<unsigned char*>(av_malloc(kBufferSize)), |
| 27 kBufferSize, |
| 28 0, // write_flag |
| 29 reader, // opaque |
| 30 &AvioRead, |
| 31 nullptr, |
| 32 &AvioSeek); |
| 33 |
| 34 // Ensure FFmpeg only tries to seek when we know how. |
| 35 result->seekable = reader->CanSeek() ? AVIO_SEEKABLE_NORMAL : 0; |
| 36 |
| 37 // Ensure writing is disabled. |
| 38 result->write_flag = 0; |
| 39 |
| 40 return result; |
| 41 } |
| 42 |
| 43 void FreeAvioContext(AVIOContext* context) { |
| 44 av_free(context->buffer); |
| 45 av_free(context); |
| 46 } |
| 47 |
| 48 // Performs a read operation using the signature required for avio. |
| 49 static int AvioRead(void* opaque, uint8_t* buf, int buf_size) { |
| 50 Reader* reader = reinterpret_cast<Reader*>(opaque); |
| 51 int result = reader->Read(buf, buf_size); |
| 52 if (result < 0) { |
| 53 result = AVERROR(EIO); |
| 54 } |
| 55 return result; |
| 56 } |
| 57 |
| 58 // Performs a seek operation using the signature required for avio. |
| 59 static int64_t AvioSeek(void* opaque, int64_t offset, int whence) { |
| 60 Reader* reader = reinterpret_cast<Reader*>(opaque); |
| 61 |
| 62 if (whence == AVSEEK_SIZE) { |
| 63 int64_t result = reader->GetSize(); |
| 64 if (result == -1) { |
| 65 return AVERROR(EIO); |
| 66 } |
| 67 return result; |
| 68 } |
| 69 |
| 70 int64_t base; |
| 71 switch (whence) { |
| 72 case SEEK_SET: |
| 73 base = 0; |
| 74 break; |
| 75 |
| 76 case SEEK_CUR: |
| 77 base = reader->GetPosition(); |
| 78 if (base == -1) { |
| 79 return AVERROR(EIO); |
| 80 } |
| 81 break; |
| 82 |
| 83 case SEEK_END: |
| 84 base = reader->GetSize(); |
| 85 if (base == -1) { |
| 86 return AVERROR(EIO); |
| 87 } |
| 88 break; |
| 89 |
| 90 default: |
| 91 NOTREACHED(); |
| 92 return AVERROR(EIO); |
| 93 } |
| 94 |
| 95 int64_t result = reader->SetPosition(base + offset); |
| 96 if (result == -1) { |
| 97 return AVERROR(EIO); |
| 98 } |
| 99 |
| 100 return result; |
| 101 } |
| 102 |
| 103 } // namespace media |
| 104 } // namespace mojo |
OLD | NEW |