diff options
| author | Carl Hetherington <cth@carlh.net> | 2022-10-01 20:11:40 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-11-26 00:09:27 +0100 |
| commit | bc6fd8c020ad27c86c463876f76c8add042cb660 (patch) | |
| tree | fab3c338985e46a7297149c2b35f4c2f487f96f3 | |
| parent | 8dfef0f38cc3323bc4a5d90c9102e8d3965496a4 (diff) | |
Return AVERROR_EOF from the avio_read method when appropriate.
| -rw-r--r-- | src/lib/ffmpeg.cc | 6 | ||||
| -rw-r--r-- | src/lib/file_group.cc | 7 | ||||
| -rw-r--r-- | src/lib/file_group.h | 12 | ||||
| -rw-r--r-- | test/file_group_test.cc | 26 |
4 files changed, 33 insertions, 18 deletions
diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc index 6ec368ef6..39abfe2b8 100644 --- a/src/lib/ffmpeg.cc +++ b/src/lib/ffmpeg.cc @@ -273,7 +273,11 @@ FFmpeg::subtitle_codec_context () const int FFmpeg::avio_read (uint8_t* buffer, int const amount) { - return _file_group.read (buffer, amount); + auto result = _file_group.read(buffer, amount); + if (result.eof && result.bytes_read == 0) { + return AVERROR_EOF; + } + return result.bytes_read; } diff --git a/src/lib/file_group.cc b/src/lib/file_group.cc index 71faacc4c..f4d50028e 100644 --- a/src/lib/file_group.cc +++ b/src/lib/file_group.cc @@ -134,9 +134,8 @@ FileGroup::seek (int64_t pos, int whence) const /** Try to read some data from the current position into a buffer. * @param buffer Buffer to write data into. * @param amount Number of bytes to read. - * @return Number of bytes read. */ -int +FileGroup::Result FileGroup::read (uint8_t* buffer, int amount) const { DCPOMATIC_ASSERT (_current_file); @@ -173,13 +172,13 @@ FileGroup::read (uint8_t* buffer, int amount) const if (eof) { /* See if there is another file to use */ if ((_current_path + 1) >= _paths.size()) { - break; + return { read, true }; } ensure_open_path (_current_path + 1); } } - return read; + return { read, false }; } diff --git a/src/lib/file_group.h b/src/lib/file_group.h index 693bf89ba..aac72c228 100644 --- a/src/lib/file_group.h +++ b/src/lib/file_group.h @@ -49,8 +49,18 @@ public: void set_paths (std::vector<boost::filesystem::path> const &); + struct Result { + Result(int bytes_read_, bool eof_) + : bytes_read(bytes_read_) + , eof(eof_) + {} + + int bytes_read = 0; + bool eof = false; + }; + int64_t seek (int64_t, int) const; - int read (uint8_t*, int) const; + Result read (uint8_t*, int) const; int64_t length () const; private: diff --git a/test/file_group_test.cc b/test/file_group_test.cc index f974a3725..3fb31317c 100644 --- a/test/file_group_test.cc +++ b/test/file_group_test.cc @@ -79,51 +79,53 @@ BOOST_AUTO_TEST_CASE (file_group_test) int pos = 0; /* Basic read from 0 */ - BOOST_CHECK_EQUAL (fg.read(test, 64), 64); + BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64); BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0); pos += 64; /* Another read following the previous */ - BOOST_CHECK_EQUAL (fg.read(test, 4), 4); + BOOST_CHECK_EQUAL(fg.read(test, 4).bytes_read, 4); BOOST_CHECK_EQUAL (memcmp(data + pos, test, 4), 0); pos += 4; /* Read overlapping A and B */ - BOOST_CHECK_EQUAL (fg.read(test, 128), 128); + BOOST_CHECK_EQUAL(fg.read(test, 128).bytes_read, 128); BOOST_CHECK_EQUAL (memcmp(data + pos, test, 128), 0); pos += 128; /* Read overlapping B/C/D and over-reading by a lot */ - BOOST_CHECK_EQUAL (fg.read(test, total_length * 3), total_length - pos); + BOOST_CHECK_EQUAL(fg.read(test, total_length * 3).bytes_read, total_length - pos); BOOST_CHECK_EQUAL (memcmp(data + pos, test, total_length - pos), 0); /* Over-read by a little */ BOOST_CHECK_EQUAL (fg.seek(0, SEEK_SET), 0); - BOOST_CHECK_EQUAL (fg.read(test, total_length), total_length); - BOOST_CHECK_EQUAL (fg.read(test, 1), 0); + BOOST_CHECK_EQUAL(fg.read(test, total_length).bytes_read, total_length); + BOOST_CHECK_EQUAL(fg.read(test, 1).bytes_read, 0); /* Seeking off the end of the file should not give an error */ BOOST_CHECK_EQUAL (fg.seek(total_length * 2, SEEK_SET), total_length * 2); - /* and attempting to read should return nothing */ - BOOST_CHECK_EQUAL (fg.read(test, 64), 0); + /* and attempting to read should return nothing and EOF */ + auto result = fg.read(test, 64); + BOOST_CHECK_EQUAL(result.bytes_read, 0); + BOOST_CHECK(result.eof); /* but the requested seek should be remembered, so if we now go back (relatively) */ BOOST_CHECK_EQUAL (fg.seek(-total_length * 2, SEEK_CUR), 0); /* we should be at the start again */ - BOOST_CHECK_EQUAL (fg.read(test, 64), 64); + BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64); BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0); /* SEEK_SET */ BOOST_CHECK_EQUAL (fg.seek(999, SEEK_SET), 999); - BOOST_CHECK_EQUAL (fg.read(test, 64), 64); + BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64); BOOST_CHECK_EQUAL (memcmp(data + 999, test, 64), 0); /* SEEK_CUR */ BOOST_CHECK_EQUAL (fg.seek(42, SEEK_CUR), 999 + 64 + 42); - BOOST_CHECK_EQUAL (fg.read(test, 64), 64); + BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64); BOOST_CHECK_EQUAL (memcmp(data + 999 + 64 + 42, test, 64), 0); /* SEEK_END */ BOOST_CHECK_EQUAL (fg.seek(1077, SEEK_END), total_length - 1077); - BOOST_CHECK_EQUAL (fg.read(test, 256), 256); + BOOST_CHECK_EQUAL(fg.read(test, 256).bytes_read, 256); BOOST_CHECK_EQUAL (memcmp(data + total_length - 1077, test, 256), 0); } |
