| 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 AvioContextPtr 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 AvioContextPtr(result); | |
| 41 } | |
| 42 | |
| 43 // Performs a read operation using the signature required for avio. | |
| 44 static int AvioRead(void* opaque, uint8_t* buf, int buf_size) { | |
| 45 Reader* reader = reinterpret_cast<Reader*>(opaque); | |
| 46 int result = reader->Read(buf, buf_size); | |
| 47 if (result < 0) { | |
| 48 result = AVERROR(EIO); | |
| 49 } | |
| 50 return result; | |
| 51 } | |
| 52 | |
| 53 // Performs a seek operation using the signature required for avio. | |
| 54 static int64_t AvioSeek(void* opaque, int64_t offset, int whence) { | |
| 55 Reader* reader = reinterpret_cast<Reader*>(opaque); | |
| 56 | |
| 57 if (whence == AVSEEK_SIZE) { | |
| 58 int64_t result = reader->GetSize(); | |
| 59 if (result == -1) { | |
| 60 return AVERROR(EIO); | |
| 61 } | |
| 62 return result; | |
| 63 } | |
| 64 | |
| 65 int64_t base; | |
| 66 switch (whence) { | |
| 67 case SEEK_SET: | |
| 68 base = 0; | |
| 69 break; | |
| 70 | |
| 71 case SEEK_CUR: | |
| 72 base = reader->GetPosition(); | |
| 73 if (base == -1) { | |
| 74 return AVERROR(EIO); | |
| 75 } | |
| 76 break; | |
| 77 | |
| 78 case SEEK_END: | |
| 79 base = reader->GetSize(); | |
| 80 if (base == -1) { | |
| 81 return AVERROR(EIO); | |
| 82 } | |
| 83 break; | |
| 84 | |
| 85 default: | |
| 86 NOTREACHED(); | |
| 87 return AVERROR(EIO); | |
| 88 } | |
| 89 | |
| 90 int64_t result = reader->SetPosition(base + offset); | |
| 91 if (result == -1) { | |
| 92 return AVERROR(EIO); | |
| 93 } | |
| 94 | |
| 95 return result; | |
| 96 } | |
| 97 | |
| 98 } // namespace media | |
| 99 } // namespace mojo | |
| OLD | NEW |