Skip to content

Commit

Permalink
Rename Streamer to StreamReader in C++ codebase
Browse files Browse the repository at this point in the history
`Streamer` has been renamed to `StreamReader` when it was moved from prototype to beta.

This commit applies the same name change to the C++ source code.
  • Loading branch information
mthrok committed May 22, 2022
1 parent a984872 commit d83caec
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions torchaudio/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ endif()
if(USE_FFMPEG)
set(
LIBTORCHAUDIO_FFMPEG_SOURCES
ffmpeg/prototype.cpp
ffmpeg/decoder.cpp
ffmpeg/ffmpeg.cpp
ffmpeg/filter_graph.cpp
ffmpeg/buffer.cpp
ffmpeg/sink.cpp
ffmpeg/stream_processor.cpp
ffmpeg/streamer.cpp
ffmpeg/stream_reader.cpp
ffmpeg/stream_reader_wrapper.cpp
ffmpeg/stream_reader_binding.cpp
)
message(STATUS "FFMPEG_ROOT=$ENV{FFMPEG_ROOT}")
find_package(FFMPEG 4.1 REQUIRED COMPONENTS avdevice avfilter avformat avcodec avutil)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <torchaudio/csrc/ffmpeg/ffmpeg.h>
#include <torchaudio/csrc/ffmpeg/streamer.h>
#include <torchaudio/csrc/ffmpeg/stream_reader.h>
#include <chrono>
#include <sstream>
#include <stdexcept>
Expand All @@ -13,23 +13,23 @@ using KeyType = StreamProcessor::KeyType;
//////////////////////////////////////////////////////////////////////////////
// Helper methods
//////////////////////////////////////////////////////////////////////////////
void Streamer::validate_open_stream() const {
void StreamReader::validate_open_stream() const {
if (!pFormatContext)
throw std::runtime_error("Stream is not open.");
}

void Streamer::validate_src_stream_index(int i) const {
void StreamReader::validate_src_stream_index(int i) const {
validate_open_stream();
if (i < 0 || i >= static_cast<int>(pFormatContext->nb_streams))
throw std::runtime_error("Source stream index out of range");
}

void Streamer::validate_output_stream_index(int i) const {
void StreamReader::validate_output_stream_index(int i) const {
if (i < 0 || i >= static_cast<int>(stream_indices.size()))
throw std::runtime_error("Output stream index out of range");
}

void Streamer::validate_src_stream_type(int i, AVMediaType type) {
void StreamReader::validate_src_stream_type(int i, AVMediaType type) {
validate_src_stream_index(i);
if (pFormatContext->streams[i]->codecpar->codec_type != type) {
std::ostringstream oss;
Expand All @@ -42,7 +42,7 @@ void Streamer::validate_src_stream_type(int i, AVMediaType type) {
//////////////////////////////////////////////////////////////////////////////
// Initialization / resource allocations
//////////////////////////////////////////////////////////////////////////////
Streamer::Streamer(AVFormatContextPtr&& p) : pFormatContext(std::move(p)) {
StreamReader::StreamReader(AVFormatContextPtr&& p) : pFormatContext(std::move(p)) {
if (avformat_find_stream_info(pFormatContext, nullptr) < 0) {
throw std::runtime_error("Failed to find stream information.");
}
Expand All @@ -63,11 +63,11 @@ Streamer::Streamer(AVFormatContextPtr&& p) : pFormatContext(std::move(p)) {
////////////////////////////////////////////////////////////////////////////////
// Query methods
////////////////////////////////////////////////////////////////////////////////
int64_t Streamer::num_src_streams() const {
int64_t StreamReader::num_src_streams() const {
return pFormatContext->nb_streams;
}

SrcStreamInfo Streamer::get_src_stream_info(int i) const {
SrcStreamInfo StreamReader::get_src_stream_info(int i) const {
validate_src_stream_index(i);
AVStream* stream = pFormatContext->streams[i];
AVCodecParameters* codecpar = stream->codecpar;
Expand Down Expand Up @@ -105,11 +105,11 @@ SrcStreamInfo Streamer::get_src_stream_info(int i) const {
return ret;
}

int64_t Streamer::num_out_streams() const {
int64_t StreamReader::num_out_streams() const {
return stream_indices.size();
}

OutputStreamInfo Streamer::get_out_stream_info(int i) const {
OutputStreamInfo StreamReader::get_out_stream_info(int i) const {
validate_output_stream_index(i);
OutputStreamInfo ret;
int i_src = stream_indices[i].first;
Expand All @@ -119,17 +119,17 @@ OutputStreamInfo Streamer::get_out_stream_info(int i) const {
return ret;
}

int64_t Streamer::find_best_audio_stream() const {
int64_t StreamReader::find_best_audio_stream() const {
return av_find_best_stream(
pFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
}

int64_t Streamer::find_best_video_stream() const {
int64_t StreamReader::find_best_video_stream() const {
return av_find_best_stream(
pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
}

bool Streamer::is_buffer_ready() const {
bool StreamReader::is_buffer_ready() const {
for (const auto& it : processors) {
if (it && !it->is_buffer_ready()) {
return false;
Expand All @@ -141,7 +141,7 @@ bool Streamer::is_buffer_ready() const {
////////////////////////////////////////////////////////////////////////////////
// Configure methods
////////////////////////////////////////////////////////////////////////////////
void Streamer::seek(double timestamp) {
void StreamReader::seek(double timestamp) {
if (timestamp < 0) {
throw std::runtime_error("timestamp must be non-negative.");
}
Expand All @@ -158,7 +158,7 @@ void Streamer::seek(double timestamp) {
}
}

void Streamer::add_audio_stream(
void StreamReader::add_audio_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
Expand All @@ -176,7 +176,7 @@ void Streamer::add_audio_stream(
torch::Device(torch::DeviceType::CPU));
}

void Streamer::add_video_stream(
void StreamReader::add_video_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
Expand Down Expand Up @@ -214,7 +214,7 @@ void Streamer::add_video_stream(
device);
}

void Streamer::add_stream(
void StreamReader::add_stream(
int i,
AVMediaType media_type,
int frames_per_chunk,
Expand Down Expand Up @@ -247,7 +247,7 @@ void Streamer::add_stream(
stream_indices.push_back(std::make_pair<>(i, key));
}

void Streamer::remove_stream(int64_t i) {
void StreamReader::remove_stream(int64_t i) {
validate_output_stream_index(i);
auto it = stream_indices.begin() + i;
int iP = it->first;
Expand All @@ -273,7 +273,7 @@ void Streamer::remove_stream(int64_t i) {
// 0: caller should keep calling this function
// 1: It's done, caller should stop calling
// <0: Some error happened
int Streamer::process_packet() {
int StreamReader::process_packet() {
int ret = av_read_frame(pFormatContext, pPacket);
if (ret == AVERROR_EOF) {
ret = drain();
Expand All @@ -293,7 +293,7 @@ int Streamer::process_packet() {
// it keeps retrying until timeout happens,
//
// timeout and backoff is given in millisecond
int Streamer::process_packet_block(double timeout, double backoff) {
int StreamReader::process_packet_block(double timeout, double backoff) {
auto dead_line = [&]() {
// If timeout < 0, then it repeats forever
if (timeout < 0) {
Expand Down Expand Up @@ -324,7 +324,7 @@ int Streamer::process_packet_block(double timeout, double backoff) {
}

// <0: Some error happened.
int Streamer::drain() {
int StreamReader::drain() {
int ret = 0, tmp = 0;
for (auto& p : processors) {
if (p) {
Expand All @@ -336,7 +336,7 @@ int Streamer::drain() {
return ret;
}

std::vector<c10::optional<torch::Tensor>> Streamer::pop_chunks() {
std::vector<c10::optional<torch::Tensor>> StreamReader::pop_chunks() {
std::vector<c10::optional<torch::Tensor>> ret;
for (auto& i : stream_indices) {
ret.push_back(processors[i.first]->pop_chunk(i.second));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace torchaudio {
namespace ffmpeg {

class Streamer {
class StreamReader {
AVFormatContextPtr pFormatContext;
AVPacketPtr pPacket;

Expand All @@ -19,14 +19,14 @@ class Streamer {
std::vector<std::pair<int, int>> stream_indices;

public:
explicit Streamer(AVFormatContextPtr&& p);
~Streamer() = default;
explicit StreamReader(AVFormatContextPtr&& p);
~StreamReader() = default;
// Non-copyable
Streamer(const Streamer&) = delete;
Streamer& operator=(const Streamer&) = delete;
StreamReader(const StreamReader&) = delete;
StreamReader& operator=(const StreamReader&) = delete;
// Movable
Streamer(Streamer&&) = default;
Streamer& operator=(Streamer&&) = default;
StreamReader(StreamReader&&) = default;
StreamReader& operator=(StreamReader&&) = default;

//////////////////////////////////////////////////////////////////////////////
// Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ c10::intrusive_ptr<StreamReaderBinding> init(
std::tuple<c10::optional<torch::Tensor>, int64_t> load(const std::string& src) {
StreamReaderBinding s{get_input_format_context(src, {}, {})};
int i = s.find_best_audio_stream();
auto sinfo = s.Streamer::get_src_stream_info(i);
auto sinfo = s.StreamReader::get_src_stream_info(i);
int64_t sample_rate = static_cast<int64_t>(sinfo.sample_rate);
s.add_audio_stream(i, -1, -1, {}, {}, {});
s.process_all_packets();
Expand All @@ -46,7 +46,7 @@ TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
av_log_set_level(AV_LOG_ERROR);
});
m.def("torchaudio::ffmpeg_load", load);
m.class_<StreamReaderBinding>("ffmpeg_Streamer")
m.class_<StreamReaderBinding>("ffmpeg_StreamReader")
.def(torch::init<>(init))
.def("num_src_streams", [](S self) { return self->num_src_streams(); })
.def("num_out_streams", [](S self) { return self->num_out_streams(); })
Expand Down
10 changes: 5 additions & 5 deletions torchaudio/csrc/ffmpeg/stream_reader_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ OutInfo convert(OutputStreamInfo osi) {
} // namespace

StreamReaderBinding::StreamReaderBinding(AVFormatContextPtr&& p)
: Streamer(std::move(p)) {}
: StreamReader(std::move(p)) {}

SrcInfo StreamReaderBinding::get_src_stream_info(int64_t i) {
return convert(Streamer::get_src_stream_info(i));
return convert(StreamReader::get_src_stream_info(i));
}

OutInfo StreamReaderBinding::get_out_stream_info(int64_t i) {
return convert(Streamer::get_out_stream_info(i));
return convert(StreamReader::get_out_stream_info(i));
}

int64_t StreamReaderBinding::process_packet(
const c10::optional<double>& timeout,
const double backoff) {
int64_t code = [&]() {
if (timeout.has_value()) {
return Streamer::process_packet_block(timeout.value(), backoff);
return StreamReader::process_packet_block(timeout.value(), backoff);
}
return Streamer::process_packet();
return StreamReader::process_packet();
}();
if (code < 0) {
throw std::runtime_error(
Expand Down
6 changes: 3 additions & 3 deletions torchaudio/csrc/ffmpeg/stream_reader_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <torch/script.h>
#include <torchaudio/csrc/ffmpeg/streamer.h>
#include <torchaudio/csrc/ffmpeg/stream_reader.h>

namespace torchaudio {
namespace ffmpeg {
Expand All @@ -25,9 +25,9 @@ using OutInfo = std::tuple<
std::string // filter description
>;

// Structure to implement wrapper API around Streamer, which is more suitable
// Structure to implement wrapper API around StreamReader, which is more suitable
// for Binding the code (i.e. it receives/returns pritimitves)
struct StreamReaderBinding : public Streamer, public torch::CustomClassHolder {
struct StreamReaderBinding : public StreamReader, public torch::CustomClassHolder {
explicit StreamReaderBinding(AVFormatContextPtr&& p);
SrcInfo get_src_stream_info(int64_t i);
OutInfo get_out_stream_info(int64_t i);
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/io/_stream_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def __init__(
buffer_size: int = 4096,
):
if isinstance(src, str):
self._be = torch.classes.torchaudio.ffmpeg_Streamer(src, format, option)
self._be = torch.classes.torchaudio.ffmpeg_StreamReader(src, format, option)
elif hasattr(src, "read"):
self._be = torchaudio._torchaudio_ffmpeg.StreamReaderFileObj(src, format, option, buffer_size)
else:
Expand Down

0 comments on commit d83caec

Please sign in to comment.