Skip to content

Commit 88b335a

Browse files
committed
Simplify some stuff
1 parent 4d82cbb commit 88b335a

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/torchcodec/_core/AVIOContextHolder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ void AVIOContextHolder::createAVIOContext(
2323
buffer != nullptr,
2424
"Failed to allocate buffer of size " + std::to_string(bufferSize));
2525

26-
// TORCH_CHECK(
27-
// (seek != nullptr) && ((write != nullptr) ^ (read != nullptr)),
28-
// "seek method must be defined, and either write or read must be
29-
// defined. " "But not both!")
26+
TORCH_CHECK(
27+
(seek != nullptr) && ((write != nullptr) || (read != nullptr)),
28+
"seek method must be defined, and at least one of write or read must be "
29+
"defined too");
3030
avioContext_.reset(avioAllocContext(
3131
buffer,
3232
bufferSize,

src/torchcodec/_core/AVIOFileLikeContext.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99

1010
namespace facebook::torchcodec {
1111

12-
AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike)
12+
AVIOFileLikeContext::AVIOFileLikeContext(
13+
py::object fileLike,
14+
std::string_view neededMethod)
1315
: fileLike_{UniquePyObject(new py::object(fileLike))} {
1416
{
1517
// TODO: Is it necessary to acquire the GIL here? Is it maybe even
1618
// harmful? At the moment, this is only called from within a pybind
1719
// function, and pybind guarantees we have the GIL.
1820
py::gil_scoped_acquire gil;
19-
// TORCH_CHECK(
20-
// py::hasattr(fileLike, "read"),
21-
// "File like object must implement a read method.");
21+
TORCH_CHECK(
22+
py::hasattr(fileLike, neededMethod.data()),
23+
"File like object must implement a ",
24+
neededMethod,
25+
" method.");
2226
TORCH_CHECK(
2327
py::hasattr(fileLike, "seek"),
2428
"File like object must implement a seek method.");

src/torchcodec/_core/AVIOFileLikeContext.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ namespace facebook::torchcodec {
1717

1818
// Enables uers to pass in a Python file-like object. We then forward all read
1919
// and seek calls back up to the methods on the Python object.
20+
// TODO: explain this. We probably don't want it.
2021
class __attribute__((visibility("default"))) AVIOFileLikeContext
2122
: public AVIOContextHolder {
2223
public:
23-
explicit AVIOFileLikeContext(py::object fileLike);
24+
explicit AVIOFileLikeContext(
25+
py::object fileLike,
26+
std::string_view neededMethod);
2427

2528
private:
2629
static int read(void* opaque, uint8_t* buf, int buf_size);

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ int64_t create_from_file_like(
3333
realSeek = seekModeFromString(seek_mode.value());
3434
}
3535

36-
auto avioContextHolder = std::make_unique<AVIOFileLikeContext>(file_like);
36+
auto avioContextHolder =
37+
std::make_unique<AVIOFileLikeContext>(file_like, /*neededMethod=*/"read");
3738

3839
SingleStreamDecoder* decoder =
3940
new SingleStreamDecoder(std::move(avioContextHolder), realSeek);
@@ -60,7 +61,8 @@ void encode_audio_to_file_like(
6061
audioStreamOptions.bitRate = bit_rate;
6162
audioStreamOptions.numChannels = num_channels;
6263

63-
auto avioContextHolder = std::make_unique<AVIOFileLikeContext>(file_like);
64+
auto avioContextHolder = std::make_unique<AVIOFileLikeContext>(
65+
file_like, /*neededMethod=*/"write");
6466

6567
AudioEncoder encoder(
6668
samples,

0 commit comments

Comments
 (0)