Skip to content

Commit 558c8f7

Browse files
committed
Fix timeout
1 parent 843ff79 commit 558c8f7

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

src/torchcodec/_core/AVIOContextHolder.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void AVIOContextHolder::createAVIOContext(
1414
AVIOWriteFunction write,
1515
AVIOSeekFunction seek,
1616
void* heldData,
17+
bool isForWriting,
1718
int bufferSize) {
1819
TORCH_CHECK(
1920
bufferSize > 0,
@@ -23,14 +24,18 @@ void AVIOContextHolder::createAVIOContext(
2324
buffer != nullptr,
2425
"Failed to allocate buffer of size " + std::to_string(bufferSize));
2526

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");
27+
TORCH_CHECK(seek != nullptr, "seek method must be defined");
28+
29+
if (isForWriting) {
30+
TORCH_CHECK(write != nullptr, "write method must be defined for writing");
31+
} else {
32+
TORCH_CHECK(read != nullptr, "read method must be defined for reading");
33+
}
34+
3035
avioContext_.reset(avioAllocContext(
3136
buffer,
3237
bufferSize,
33-
/*write_flag=*/write != nullptr,
38+
/*write_flag=*/isForWriting,
3439
heldData,
3540
read,
3641
write,

src/torchcodec/_core/AVIOContextHolder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class AVIOContextHolder {
5151
AVIOWriteFunction write,
5252
AVIOSeekFunction seek,
5353
void* heldData,
54+
bool isForWriting,
5455
int bufferSize = defaultBufferSize);
5556

5657
private:

src/torchcodec/_core/AVIOFileLikeContext.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@
99

1010
namespace facebook::torchcodec {
1111

12-
AVIOFileLikeContext::AVIOFileLikeContext(
13-
py::object fileLike,
14-
std::string_view neededMethod)
12+
AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike, bool isForWriting)
1513
: fileLike_{UniquePyObject(new py::object(fileLike))} {
1614
{
1715
// TODO: Is it necessary to acquire the GIL here? Is it maybe even
1816
// harmful? At the moment, this is only called from within a pybind
1917
// function, and pybind guarantees we have the GIL.
2018
py::gil_scoped_acquire gil;
21-
TORCH_CHECK(
22-
py::hasattr(fileLike, neededMethod.data()),
23-
"File like object must implement a ",
24-
neededMethod,
25-
" method.");
19+
20+
if (isForWriting) {
21+
TORCH_CHECK(
22+
py::hasattr(fileLike, "write"),
23+
"File like object must implement a write method for writing.");
24+
} else {
25+
TORCH_CHECK(
26+
py::hasattr(fileLike, "read"),
27+
"File like object must implement a read method for reading.");
28+
}
29+
2630
TORCH_CHECK(
2731
py::hasattr(fileLike, "seek"),
2832
"File like object must implement a seek method.");
2933
}
30-
createAVIOContext(&read, &write, &seek, &fileLike_);
34+
createAVIOContext(&read, &write, &seek, &fileLike_, isForWriting);
3135
}
3236

3337
int AVIOFileLikeContext::read(void* opaque, uint8_t* buf, int buf_size) {

src/torchcodec/_core/AVIOFileLikeContext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ namespace facebook::torchcodec {
2121
class __attribute__((visibility("default"))) AVIOFileLikeContext
2222
: public AVIOContextHolder {
2323
public:
24-
explicit AVIOFileLikeContext(
25-
py::object fileLike,
26-
std::string_view neededMethod);
24+
explicit AVIOFileLikeContext(py::object fileLike, bool isForWriting);
2725

2826
private:
2927
static int read(void* opaque, uint8_t* buf, int buf_size);

src/torchcodec/_core/AVIOTensorContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,14 @@ AVIOFromTensorContext::AVIOFromTensorContext(torch::Tensor data)
105105
TORCH_CHECK(data.numel() > 0, "data must not be empty");
106106
TORCH_CHECK(data.is_contiguous(), "data must be contiguous");
107107
TORCH_CHECK(data.scalar_type() == torch::kUInt8, "data must be kUInt8");
108-
createAVIOContext(&read, nullptr, &seek, &tensorContext_);
108+
createAVIOContext(
109+
&read, nullptr, &seek, &tensorContext_, /*isForWriting=*/false);
109110
}
110111

111112
AVIOToTensorContext::AVIOToTensorContext()
112113
: tensorContext_{torch::empty({INITIAL_TENSOR_SIZE}, {torch::kUInt8}), 0} {
113-
createAVIOContext(nullptr, &write, &seek, &tensorContext_);
114+
createAVIOContext(
115+
nullptr, &write, &seek, &tensorContext_, /*isForWriting=*/true);
114116
}
115117

116118
torch::Tensor AVIOToTensorContext::getOutputTensor() {

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int64_t create_from_file_like(
3434
}
3535

3636
auto avioContextHolder =
37-
std::make_unique<AVIOFileLikeContext>(file_like, /*neededMethod=*/"read");
37+
std::make_unique<AVIOFileLikeContext>(file_like, /*isForWriting=*/false);
3838

3939
SingleStreamDecoder* decoder =
4040
new SingleStreamDecoder(std::move(avioContextHolder), realSeek);
@@ -61,8 +61,8 @@ void encode_audio_to_file_like(
6161
audioStreamOptions.bitRate = bit_rate;
6262
audioStreamOptions.numChannels = num_channels;
6363

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

6767
AudioEncoder encoder(
6868
samples,

0 commit comments

Comments
 (0)