File tree Expand file tree Collapse file tree 4 files changed +20
-11
lines changed
Expand file tree Collapse file tree 4 files changed +20
-11
lines changed Original file line number Diff line number Diff 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,
Original file line number Diff line number Diff line change 99
1010namespace 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." );
Original file line number Diff line number Diff 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.
2021class __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);
Original file line number Diff line number Diff 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,
You can’t perform that action at this time.
0 commit comments