Skip to content

Commit cfafb50

Browse files
U-THEFACEBOOK\motomthrok
authored andcommitted
Define TORCHAUDIO_API for better visbility control
y
1 parent d50ed52 commit cfafb50

File tree

5 files changed

+72
-40
lines changed

5 files changed

+72
-40
lines changed

torchaudio/csrc/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# the following line is added in order to export symbols when building on Windows
2-
# this approach has some limitations as documented in https://github.com/pytorch/pytorch/pull/3650
3-
if (MSVC)
4-
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
5-
endif()
6-
71
################################################################################
82
# libtorchaudio
93
################################################################################
@@ -205,7 +199,7 @@ if(USE_FFMPEG)
205199
"${LIBTORCHAUDIO_FFMPEG_SOURCES}"
206200
"${LIBTORCHAUDIO_INCLUDE_DIRS};${FFMPEG_INCLUDE_DIRS}"
207201
"torch;${FFMPEG_LIBRARIES}"
208-
"${LIBTORCHAUDIO_COMPILE_DEFINITIONS}"
202+
"${LIBTORCHAUDIO_COMPILE_DEFINITIONS};TORCHAUDIO_BUILD_MAIN_LIB"
209203
)
210204
endif()
211205

torchaudio/csrc/export.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
// Define the visibility of symbols.
4+
// The original logic and background can be found here.
5+
// https://github.com/pytorch/pytorch/blob/bcc02769bef1d7b89bec724223284958b7c5b564/c10/macros/Export.h#L49-L55
6+
//
7+
// In the context of torchaudio, the logic is simpler at the moment.
8+
//
9+
// The torchaudio custom operations are implemented in
10+
// `torchaudio/lib/libtorchaudio.[so|pyd]`. Some symbols are referred from
11+
// `torchaudio._torchaudio`.
12+
//
13+
// In Windows, default visibility of dynamically library are hidden, while in
14+
// Linux/macOS, they are visible.
15+
//
16+
// At the moment we do not expect torchaudio libraries to be built/linked
17+
// statically. We assume they are always shared.
18+
19+
#ifdef _WIN32
20+
#define TORCHAUDIO_EXPORT __declspec(dllexport)
21+
#define TORCHAUDIO_IMPORT __declspec(dllimport)
22+
#else // _WIN32
23+
#if defined(__GNUC__)
24+
#define TORCHAUDIO_EXPORT __attribute__((__visibility__("default")))
25+
#else // defined(__GNUC__)
26+
#define TORCHAUDIO_EXPORT
27+
#endif // defined(__GNUC__)
28+
#define TORCHAUDIO_IMPORT TORCHAUDIO_EXPORT
29+
#endif // _WIN32
30+
31+
#ifdef TORCHAUDIO_BUILD_MAIN_LIB
32+
#define TORCHAUDIO_API TORCHAUDIO_EXPORT
33+
#else
34+
#define TORCHAUDIO_API TORCHAUDIO_IMPORT
35+
#endif

torchaudio/csrc/ffmpeg/ffmpeg.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// One stop header for all ffmepg needs
22
#pragma once
33
#include <torch/torch.h>
4+
#include <torchaudio/csrc/export.h>
45
#include <cstdint>
56
#include <map>
67
#include <memory>
@@ -64,16 +65,16 @@ class Wrapper {
6465
// AVFormatContext
6566
////////////////////////////////////////////////////////////////////////////////
6667
struct AVFormatContextDeleter {
67-
void operator()(AVFormatContext* p);
68+
TORCHAUDIO_API void operator()(AVFormatContext* p);
6869
};
6970

7071
struct AVFormatContextPtr
7172
: public Wrapper<AVFormatContext, AVFormatContextDeleter> {
72-
explicit AVFormatContextPtr(AVFormatContext* p);
73+
TORCHAUDIO_API explicit AVFormatContextPtr(AVFormatContext* p);
7374
};
7475

7576
// create format context for reading media
76-
AVFormatContextPtr get_input_format_context(
77+
TORCHAUDIO_API AVFormatContextPtr get_input_format_context(
7778
const std::string& src,
7879
const c10::optional<std::string>& device,
7980
const OptionDict& option,
@@ -83,18 +84,18 @@ AVFormatContextPtr get_input_format_context(
8384
// AVIO
8485
////////////////////////////////////////////////////////////////////////////////
8586
struct AVIOContextDeleter {
86-
void operator()(AVIOContext* p);
87+
TORCHAUDIO_API void operator()(AVIOContext* p);
8788
};
8889

8990
struct AVIOContextPtr : public Wrapper<AVIOContext, AVIOContextDeleter> {
90-
explicit AVIOContextPtr(AVIOContext* p);
91+
TORCHAUDIO_API explicit AVIOContextPtr(AVIOContext* p);
9192
};
9293

9394
////////////////////////////////////////////////////////////////////////////////
9495
// AVPacket
9596
////////////////////////////////////////////////////////////////////////////////
9697
struct AVPacketDeleter {
97-
void operator()(AVPacket* p);
98+
TORCHAUDIO_API void operator()(AVPacket* p);
9899
};
99100

100101
struct AVPacketPtr : public Wrapper<AVPacket, AVPacketDeleter> {
@@ -123,7 +124,7 @@ struct AutoPacketUnref {
123124
// AVFrame
124125
////////////////////////////////////////////////////////////////////////////////
125126
struct AVFrameDeleter {
126-
void operator()(AVFrame* p);
127+
TORCHAUDIO_API void operator()(AVFrame* p);
127128
};
128129

129130
struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
@@ -135,7 +136,7 @@ struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
135136
// of AVBufferRefPtr.
136137
////////////////////////////////////////////////////////////////////////////////
137138
struct AutoBufferUnref {
138-
void operator()(AVBufferRef* p);
139+
TORCHAUDIO_API void operator()(AVBufferRef* p);
139140
};
140141

141142
struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
@@ -147,7 +148,7 @@ struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
147148
// AVCodecContext
148149
////////////////////////////////////////////////////////////////////////////////
149150
struct AVCodecContextDeleter {
150-
void operator()(AVCodecContext* p);
151+
TORCHAUDIO_API void operator()(AVCodecContext* p);
151152
};
152153
struct AVCodecContextPtr
153154
: public Wrapper<AVCodecContext, AVCodecContextDeleter> {
@@ -172,7 +173,7 @@ void init_codec_context(
172173
// AVFilterGraph
173174
////////////////////////////////////////////////////////////////////////////////
174175
struct AVFilterGraphDeleter {
175-
void operator()(AVFilterGraph* p);
176+
TORCHAUDIO_API void operator()(AVFilterGraph* p);
176177
};
177178
struct AVFilterGraphPtr : public Wrapper<AVFilterGraph, AVFilterGraphDeleter> {
178179
AVFilterGraphPtr();

torchaudio/csrc/ffmpeg/stream_reader.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <torchaudio/csrc/export.h>
23
#include <torchaudio/csrc/ffmpeg/decoder.h>
34
#include <torchaudio/csrc/ffmpeg/filter_graph.h>
45
#include <torchaudio/csrc/ffmpeg/stream_processor.h>
@@ -19,7 +20,7 @@ class StreamReader {
1920
std::vector<std::pair<int, int>> stream_indices;
2021

2122
public:
22-
explicit StreamReader(AVFormatContextPtr&& p);
23+
TORCHAUDIO_API explicit StreamReader(AVFormatContextPtr&& p);
2324
~StreamReader() = default;
2425
// Non-copyable
2526
StreamReader(const StreamReader&) = delete;
@@ -42,40 +43,40 @@ class StreamReader {
4243
//////////////////////////////////////////////////////////////////////////////
4344
public:
4445
// Find a suitable audio/video streams using heuristics from ffmpeg
45-
int64_t find_best_audio_stream() const;
46-
int64_t find_best_video_stream() const;
46+
TORCHAUDIO_API int64_t find_best_audio_stream() const;
47+
TORCHAUDIO_API int64_t find_best_video_stream() const;
4748
// Fetch metadata of the source
48-
c10::Dict<std::string, std::string> get_metadata() const;
49+
TORCHAUDIO_API c10::Dict<std::string, std::string> get_metadata() const;
4950
// Fetch information about source streams
50-
int64_t num_src_streams() const;
51-
SrcStreamInfo get_src_stream_info(int i) const;
51+
TORCHAUDIO_API int64_t num_src_streams() const;
52+
TORCHAUDIO_API SrcStreamInfo get_src_stream_info(int i) const;
5253
// Fetch information about output streams
53-
int64_t num_out_streams() const;
54-
OutputStreamInfo get_out_stream_info(int i) const;
54+
TORCHAUDIO_API int64_t num_out_streams() const;
55+
TORCHAUDIO_API OutputStreamInfo get_out_stream_info(int i) const;
5556
// Check if all the buffers of the output streams are ready.
56-
bool is_buffer_ready() const;
57+
TORCHAUDIO_API bool is_buffer_ready() const;
5758

5859
//////////////////////////////////////////////////////////////////////////////
5960
// Configure methods
6061
//////////////////////////////////////////////////////////////////////////////
61-
void seek(double timestamp);
62+
TORCHAUDIO_API void seek(double timestamp);
6263

63-
void add_audio_stream(
64+
TORCHAUDIO_API void add_audio_stream(
6465
int64_t i,
6566
int64_t frames_per_chunk,
6667
int64_t num_chunks,
6768
const c10::optional<std::string>& filter_desc,
6869
const c10::optional<std::string>& decoder,
6970
const OptionDict& decoder_option);
70-
void add_video_stream(
71+
TORCHAUDIO_API void add_video_stream(
7172
int64_t i,
7273
int64_t frames_per_chunk,
7374
int64_t num_chunks,
7475
const c10::optional<std::string>& filter_desc,
7576
const c10::optional<std::string>& decoder,
7677
const OptionDict& decoder_option,
7778
const c10::optional<std::string>& hw_accel);
78-
void remove_stream(int64_t i);
79+
TORCHAUDIO_API void remove_stream(int64_t i);
7980

8081
private:
8182
void add_stream(
@@ -92,15 +93,15 @@ class StreamReader {
9293
//////////////////////////////////////////////////////////////////////////////
9394
// Stream methods
9495
//////////////////////////////////////////////////////////////////////////////
95-
int process_packet();
96-
int process_packet_block(double timeout, double backoff);
96+
TORCHAUDIO_API int process_packet();
97+
TORCHAUDIO_API int process_packet_block(double timeout, double backoff);
9798

98-
int drain();
99+
TORCHAUDIO_API int drain();
99100

100101
//////////////////////////////////////////////////////////////////////////////
101102
// Retrieval
102103
//////////////////////////////////////////////////////////////////////////////
103-
std::vector<c10::optional<torch::Tensor>> pop_chunks();
104+
TORCHAUDIO_API std::vector<c10::optional<torch::Tensor>> pop_chunks();
104105
};
105106

106107
} // namespace ffmpeg

torchaudio/csrc/ffmpeg/stream_reader_wrapper.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <torch/script.h>
3+
#include <torchaudio/csrc/export.h>
34
#include <torchaudio/csrc/ffmpeg/stream_reader.h>
45

56
namespace torchaudio {
@@ -58,16 +59,16 @@ using OutInfo = std::tuple<
5859
// suitable for Binding the code (i.e. it receives/returns pritimitves)
5960
struct StreamReaderBinding : public StreamReader,
6061
public torch::CustomClassHolder {
61-
explicit StreamReaderBinding(AVFormatContextPtr&& p);
62-
SrcInfo get_src_stream_info(int64_t i);
63-
SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
64-
OutInfo get_out_stream_info(int64_t i);
62+
TORCHAUDIO_API explicit StreamReaderBinding(AVFormatContextPtr&& p);
63+
TORCHAUDIO_API SrcInfo get_src_stream_info(int64_t i);
64+
TORCHAUDIO_API SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
65+
TORCHAUDIO_API OutInfo get_out_stream_info(int64_t i);
6566

66-
int64_t process_packet(
67+
TORCHAUDIO_API int64_t process_packet(
6768
const c10::optional<double>& timeout = c10::optional<double>(),
6869
const double backoff = 10.);
6970

70-
void process_all_packets();
71+
TORCHAUDIO_API void process_all_packets();
7172
};
7273

7374
} // namespace ffmpeg

0 commit comments

Comments
 (0)