Skip to content

Define TORCHAUDIO_API for better visbility control #2504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions torchaudio/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# the following line is added in order to export symbols when building on Windows
# this approach has some limitations as documented in https://github.com/pytorch/pytorch/pull/3650
if (MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

################################################################################
# libtorchaudio
################################################################################
Expand Down Expand Up @@ -205,7 +199,7 @@ if(USE_FFMPEG)
"${LIBTORCHAUDIO_FFMPEG_SOURCES}"
"${LIBTORCHAUDIO_INCLUDE_DIRS};${FFMPEG_INCLUDE_DIRS}"
"torch;${FFMPEG_LIBRARIES}"
"${LIBTORCHAUDIO_COMPILE_DEFINITIONS}"
"${LIBTORCHAUDIO_COMPILE_DEFINITIONS};TORCHAUDIO_BUILD_MAIN_LIB"
)
endif()

Expand Down
35 changes: 35 additions & 0 deletions torchaudio/csrc/export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

// Define the visibility of symbols.
// The original logic and background can be found here.
// https://github.com/pytorch/pytorch/blob/bcc02769bef1d7b89bec724223284958b7c5b564/c10/macros/Export.h#L49-L55
//
// In the context of torchaudio, the logic is simpler at the moment.
//
// The torchaudio custom operations are implemented in
// `torchaudio/lib/libtorchaudio.[so|pyd]`. Some symbols are referred from
// `torchaudio._torchaudio`.
//
// In Windows, default visibility of dynamically library are hidden, while in
// Linux/macOS, they are visible.
//
// At the moment we do not expect torchaudio libraries to be built/linked
// statically. We assume they are always shared.

#ifdef _WIN32
#define TORCHAUDIO_EXPORT __declspec(dllexport)
#define TORCHAUDIO_IMPORT __declspec(dllimport)
#else // _WIN32
#if defined(__GNUC__)
#define TORCHAUDIO_EXPORT __attribute__((__visibility__("default")))
#else // defined(__GNUC__)
#define TORCHAUDIO_EXPORT
#endif // defined(__GNUC__)
#define TORCHAUDIO_IMPORT TORCHAUDIO_EXPORT
#endif // _WIN32

#ifdef TORCHAUDIO_BUILD_MAIN_LIB
#define TORCHAUDIO_API TORCHAUDIO_EXPORT
#else
#define TORCHAUDIO_API TORCHAUDIO_IMPORT
#endif
19 changes: 10 additions & 9 deletions torchaudio/csrc/ffmpeg/ffmpeg.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// One stop header for all ffmepg needs
#pragma once
#include <torch/torch.h>
#include <torchaudio/csrc/export.h>
#include <cstdint>
#include <map>
#include <memory>
Expand Down Expand Up @@ -85,30 +86,30 @@ void clean_up_dict(AVDictionary* p);
// AVFormatContext
////////////////////////////////////////////////////////////////////////////////
struct AVFormatContextDeleter {
void operator()(AVFormatContext* p);
TORCHAUDIO_API void operator()(AVFormatContext* p);
};

struct AVFormatContextPtr
: public Wrapper<AVFormatContext, AVFormatContextDeleter> {
explicit AVFormatContextPtr(AVFormatContext* p);
TORCHAUDIO_API explicit AVFormatContextPtr(AVFormatContext* p);
};

////////////////////////////////////////////////////////////////////////////////
// AVIO
////////////////////////////////////////////////////////////////////////////////
struct AVIOContextDeleter {
void operator()(AVIOContext* p);
TORCHAUDIO_API void operator()(AVIOContext* p);
};

struct AVIOContextPtr : public Wrapper<AVIOContext, AVIOContextDeleter> {
explicit AVIOContextPtr(AVIOContext* p);
TORCHAUDIO_API explicit AVIOContextPtr(AVIOContext* p);
};

////////////////////////////////////////////////////////////////////////////////
// AVPacket
////////////////////////////////////////////////////////////////////////////////
struct AVPacketDeleter {
void operator()(AVPacket* p);
TORCHAUDIO_API void operator()(AVPacket* p);
};

struct AVPacketPtr : public Wrapper<AVPacket, AVPacketDeleter> {
Expand Down Expand Up @@ -137,7 +138,7 @@ struct AutoPacketUnref {
// AVFrame
////////////////////////////////////////////////////////////////////////////////
struct AVFrameDeleter {
void operator()(AVFrame* p);
TORCHAUDIO_API void operator()(AVFrame* p);
};

struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
Expand All @@ -149,7 +150,7 @@ struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
// of AVBufferRefPtr.
////////////////////////////////////////////////////////////////////////////////
struct AutoBufferUnref {
void operator()(AVBufferRef* p);
TORCHAUDIO_API void operator()(AVBufferRef* p);
};

struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
Expand All @@ -161,7 +162,7 @@ struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
// AVCodecContext
////////////////////////////////////////////////////////////////////////////////
struct AVCodecContextDeleter {
void operator()(AVCodecContext* p);
TORCHAUDIO_API void operator()(AVCodecContext* p);
};
struct AVCodecContextPtr
: public Wrapper<AVCodecContext, AVCodecContextDeleter> {
Expand All @@ -172,7 +173,7 @@ struct AVCodecContextPtr
// AVFilterGraph
////////////////////////////////////////////////////////////////////////////////
struct AVFilterGraphDeleter {
void operator()(AVFilterGraph* p);
TORCHAUDIO_API void operator()(AVFilterGraph* p);
};
struct AVFilterGraphPtr : public Wrapper<AVFilterGraph, AVFilterGraphDeleter> {
AVFilterGraphPtr();
Expand Down
35 changes: 18 additions & 17 deletions torchaudio/csrc/ffmpeg/stream_reader.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <torchaudio/csrc/export.h>
#include <torchaudio/csrc/ffmpeg/decoder.h>
#include <torchaudio/csrc/ffmpeg/stream_processor.h>
#include <torchaudio/csrc/ffmpeg/typedefs.h>
Expand All @@ -18,7 +19,7 @@ class StreamReader {
std::vector<std::pair<int, int>> stream_indices;

public:
explicit StreamReader(AVFormatContextPtr&& p);
TORCHAUDIO_API explicit StreamReader(AVFormatContextPtr&& p);
~StreamReader() = default;
// Non-copyable
StreamReader(const StreamReader&) = delete;
Expand All @@ -41,40 +42,40 @@ class StreamReader {
//////////////////////////////////////////////////////////////////////////////
public:
// Find a suitable audio/video streams using heuristics from ffmpeg
int64_t find_best_audio_stream() const;
int64_t find_best_video_stream() const;
TORCHAUDIO_API int64_t find_best_audio_stream() const;
TORCHAUDIO_API int64_t find_best_video_stream() const;
// Fetch metadata of the source
c10::Dict<std::string, std::string> get_metadata() const;
TORCHAUDIO_API c10::Dict<std::string, std::string> get_metadata() const;
// Fetch information about source streams
int64_t num_src_streams() const;
SrcStreamInfo get_src_stream_info(int i) const;
TORCHAUDIO_API int64_t num_src_streams() const;
TORCHAUDIO_API SrcStreamInfo get_src_stream_info(int i) const;
// Fetch information about output streams
int64_t num_out_streams() const;
OutputStreamInfo get_out_stream_info(int i) const;
TORCHAUDIO_API int64_t num_out_streams() const;
TORCHAUDIO_API OutputStreamInfo get_out_stream_info(int i) const;
// Check if all the buffers of the output streams are ready.
bool is_buffer_ready() const;
TORCHAUDIO_API bool is_buffer_ready() const;

//////////////////////////////////////////////////////////////////////////////
// Configure methods
//////////////////////////////////////////////////////////////////////////////
void seek(double timestamp);
TORCHAUDIO_API void seek(double timestamp);

void add_audio_stream(
TORCHAUDIO_API void add_audio_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
const c10::optional<std::string>& filter_desc,
const c10::optional<std::string>& decoder,
const OptionDict& decoder_option);
void add_video_stream(
TORCHAUDIO_API void add_video_stream(
int64_t i,
int64_t frames_per_chunk,
int64_t num_chunks,
const c10::optional<std::string>& filter_desc,
const c10::optional<std::string>& decoder,
const OptionDict& decoder_option,
const c10::optional<std::string>& hw_accel);
void remove_stream(int64_t i);
TORCHAUDIO_API void remove_stream(int64_t i);

private:
void add_stream(
Expand All @@ -91,15 +92,15 @@ class StreamReader {
//////////////////////////////////////////////////////////////////////////////
// Stream methods
//////////////////////////////////////////////////////////////////////////////
int process_packet();
int process_packet_block(double timeout, double backoff);
TORCHAUDIO_API int process_packet();
TORCHAUDIO_API int process_packet_block(double timeout, double backoff);

int drain();
TORCHAUDIO_API int drain();

//////////////////////////////////////////////////////////////////////////////
// Retrieval
//////////////////////////////////////////////////////////////////////////////
std::vector<c10::optional<torch::Tensor>> pop_chunks();
TORCHAUDIO_API std::vector<c10::optional<torch::Tensor>> pop_chunks();
};

} // namespace ffmpeg
Expand Down
15 changes: 8 additions & 7 deletions torchaudio/csrc/ffmpeg/stream_reader_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once
#include <torch/script.h>
#include <torchaudio/csrc/export.h>
#include <torchaudio/csrc/ffmpeg/stream_reader.h>

namespace torchaudio {
namespace ffmpeg {

// create format context for reading media
AVFormatContextPtr get_input_format_context(
TORCHAUDIO_API AVFormatContextPtr get_input_format_context(
const std::string& src,
const c10::optional<std::string>& device,
const OptionDict& option,
Expand Down Expand Up @@ -65,16 +66,16 @@ using OutInfo = std::tuple<
// suitable for Binding the code (i.e. it receives/returns pritimitves)
struct StreamReaderBinding : public StreamReader,
public torch::CustomClassHolder {
explicit StreamReaderBinding(AVFormatContextPtr&& p);
SrcInfo get_src_stream_info(int64_t i);
SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
OutInfo get_out_stream_info(int64_t i);
TORCHAUDIO_API explicit StreamReaderBinding(AVFormatContextPtr&& p);
TORCHAUDIO_API SrcInfo get_src_stream_info(int64_t i);
TORCHAUDIO_API SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
TORCHAUDIO_API OutInfo get_out_stream_info(int64_t i);

int64_t process_packet(
TORCHAUDIO_API int64_t process_packet(
const c10::optional<double>& timeout = c10::optional<double>(),
const double backoff = 10.);

void process_all_packets();
TORCHAUDIO_API void process_all_packets();
};

} // namespace ffmpeg
Expand Down