Skip to content

Commit 3ac190b

Browse files
U-THEFACEBOOK\motomthrok
authored andcommitted
Define TORCHAUDIO_API for better visbility control
1 parent b2a90f9 commit 3ac190b

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: 10 additions & 9 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>
@@ -85,30 +86,30 @@ void clean_up_dict(AVDictionary* p);
8586
// AVFormatContext
8687
////////////////////////////////////////////////////////////////////////////////
8788
struct AVFormatContextDeleter {
88-
void operator()(AVFormatContext* p);
89+
TORCHAUDIO_API void operator()(AVFormatContext* p);
8990
};
9091

9192
struct AVFormatContextPtr
9293
: public Wrapper<AVFormatContext, AVFormatContextDeleter> {
93-
explicit AVFormatContextPtr(AVFormatContext* p);
94+
TORCHAUDIO_API explicit AVFormatContextPtr(AVFormatContext* p);
9495
};
9596

9697
////////////////////////////////////////////////////////////////////////////////
9798
// AVIO
9899
////////////////////////////////////////////////////////////////////////////////
99100
struct AVIOContextDeleter {
100-
void operator()(AVIOContext* p);
101+
TORCHAUDIO_API void operator()(AVIOContext* p);
101102
};
102103

103104
struct AVIOContextPtr : public Wrapper<AVIOContext, AVIOContextDeleter> {
104-
explicit AVIOContextPtr(AVIOContext* p);
105+
TORCHAUDIO_API explicit AVIOContextPtr(AVIOContext* p);
105106
};
106107

107108
////////////////////////////////////////////////////////////////////////////////
108109
// AVPacket
109110
////////////////////////////////////////////////////////////////////////////////
110111
struct AVPacketDeleter {
111-
void operator()(AVPacket* p);
112+
TORCHAUDIO_API void operator()(AVPacket* p);
112113
};
113114

114115
struct AVPacketPtr : public Wrapper<AVPacket, AVPacketDeleter> {
@@ -137,7 +138,7 @@ struct AutoPacketUnref {
137138
// AVFrame
138139
////////////////////////////////////////////////////////////////////////////////
139140
struct AVFrameDeleter {
140-
void operator()(AVFrame* p);
141+
TORCHAUDIO_API void operator()(AVFrame* p);
141142
};
142143

143144
struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
@@ -149,7 +150,7 @@ struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
149150
// of AVBufferRefPtr.
150151
////////////////////////////////////////////////////////////////////////////////
151152
struct AutoBufferUnref {
152-
void operator()(AVBufferRef* p);
153+
TORCHAUDIO_API void operator()(AVBufferRef* p);
153154
};
154155

155156
struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
@@ -161,7 +162,7 @@ struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
161162
// AVCodecContext
162163
////////////////////////////////////////////////////////////////////////////////
163164
struct AVCodecContextDeleter {
164-
void operator()(AVCodecContext* p);
165+
TORCHAUDIO_API void operator()(AVCodecContext* p);
165166
};
166167
struct AVCodecContextPtr
167168
: public Wrapper<AVCodecContext, AVCodecContextDeleter> {
@@ -172,7 +173,7 @@ struct AVCodecContextPtr
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/stream_processor.h>
45
#include <torchaudio/csrc/ffmpeg/typedefs.h>
@@ -18,7 +19,7 @@ class StreamReader {
1819
std::vector<std::pair<int, int>> stream_indices;
1920

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

5758
//////////////////////////////////////////////////////////////////////////////
5859
// Configure methods
5960
//////////////////////////////////////////////////////////////////////////////
60-
void seek(double timestamp);
61+
TORCHAUDIO_API void seek(double timestamp);
6162

62-
void add_audio_stream(
63+
TORCHAUDIO_API void add_audio_stream(
6364
int64_t i,
6465
int64_t frames_per_chunk,
6566
int64_t num_chunks,
6667
const c10::optional<std::string>& filter_desc,
6768
const c10::optional<std::string>& decoder,
6869
const OptionDict& decoder_option);
69-
void add_video_stream(
70+
TORCHAUDIO_API void add_video_stream(
7071
int64_t i,
7172
int64_t frames_per_chunk,
7273
int64_t num_chunks,
7374
const c10::optional<std::string>& filter_desc,
7475
const c10::optional<std::string>& decoder,
7576
const OptionDict& decoder_option,
7677
const c10::optional<std::string>& hw_accel);
77-
void remove_stream(int64_t i);
78+
TORCHAUDIO_API void remove_stream(int64_t i);
7879

7980
private:
8081
void add_stream(
@@ -91,15 +92,15 @@ class StreamReader {
9192
//////////////////////////////////////////////////////////////////////////////
9293
// Stream methods
9394
//////////////////////////////////////////////////////////////////////////////
94-
int process_packet();
95-
int process_packet_block(double timeout, double backoff);
95+
TORCHAUDIO_API int process_packet();
96+
TORCHAUDIO_API int process_packet_block(double timeout, double backoff);
9697

97-
int drain();
98+
TORCHAUDIO_API int drain();
9899

99100
//////////////////////////////////////////////////////////////////////////////
100101
// Retrieval
101102
//////////////////////////////////////////////////////////////////////////////
102-
std::vector<c10::optional<torch::Tensor>> pop_chunks();
103+
TORCHAUDIO_API std::vector<c10::optional<torch::Tensor>> pop_chunks();
103104
};
104105

105106
} // namespace ffmpeg

torchaudio/csrc/ffmpeg/stream_reader_wrapper.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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 {
67
namespace ffmpeg {
78

89
// create format context for reading media
9-
AVFormatContextPtr get_input_format_context(
10+
TORCHAUDIO_API AVFormatContextPtr get_input_format_context(
1011
const std::string& src,
1112
const c10::optional<std::string>& device,
1213
const OptionDict& option,
@@ -65,16 +66,16 @@ using OutInfo = std::tuple<
6566
// suitable for Binding the code (i.e. it receives/returns pritimitves)
6667
struct StreamReaderBinding : public StreamReader,
6768
public torch::CustomClassHolder {
68-
explicit StreamReaderBinding(AVFormatContextPtr&& p);
69-
SrcInfo get_src_stream_info(int64_t i);
70-
SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
71-
OutInfo get_out_stream_info(int64_t i);
69+
TORCHAUDIO_API explicit StreamReaderBinding(AVFormatContextPtr&& p);
70+
TORCHAUDIO_API SrcInfo get_src_stream_info(int64_t i);
71+
TORCHAUDIO_API SrcInfoPyBind get_src_stream_info_pybind(int64_t i);
72+
TORCHAUDIO_API OutInfo get_out_stream_info(int64_t i);
7273

73-
int64_t process_packet(
74+
TORCHAUDIO_API int64_t process_packet(
7475
const c10::optional<double>& timeout = c10::optional<double>(),
7576
const double backoff = 10.);
7677

77-
void process_all_packets();
78+
TORCHAUDIO_API void process_all_packets();
7879
};
7980

8081
} // namespace ffmpeg

0 commit comments

Comments
 (0)