Skip to content

Commit 9e06448

Browse files
U-THEFACEBOOK\motomthrok
authored andcommitted
Define TORCHAUDIO_API for better visbility control
1 parent 0dd5723 commit 9e06448

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>
@@ -76,16 +77,16 @@ void clean_up_dict(AVDictionary* p);
7677
// AVFormatContext
7778
////////////////////////////////////////////////////////////////////////////////
7879
struct AVFormatContextDeleter {
79-
void operator()(AVFormatContext* p);
80+
TORCHAUDIO_API void operator()(AVFormatContext* p);
8081
};
8182

8283
struct AVFormatContextPtr
8384
: public Wrapper<AVFormatContext, AVFormatContextDeleter> {
84-
explicit AVFormatContextPtr(AVFormatContext* p);
85+
TORCHAUDIO_API explicit AVFormatContextPtr(AVFormatContext* p);
8586
};
8687

8788
// create format context for reading media
88-
AVFormatContextPtr get_input_format_context(
89+
TORCHAUDIO_API AVFormatContextPtr get_input_format_context(
8990
const std::string& src,
9091
const c10::optional<std::string>& device,
9192
const OptionDict& option,
@@ -95,18 +96,18 @@ AVFormatContextPtr get_input_format_context(
9596
// AVIO
9697
////////////////////////////////////////////////////////////////////////////////
9798
struct AVIOContextDeleter {
98-
void operator()(AVIOContext* p);
99+
TORCHAUDIO_API void operator()(AVIOContext* p);
99100
};
100101

101102
struct AVIOContextPtr : public Wrapper<AVIOContext, AVIOContextDeleter> {
102-
explicit AVIOContextPtr(AVIOContext* p);
103+
TORCHAUDIO_API explicit AVIOContextPtr(AVIOContext* p);
103104
};
104105

105106
////////////////////////////////////////////////////////////////////////////////
106107
// AVPacket
107108
////////////////////////////////////////////////////////////////////////////////
108109
struct AVPacketDeleter {
109-
void operator()(AVPacket* p);
110+
TORCHAUDIO_API void operator()(AVPacket* p);
110111
};
111112

112113
struct AVPacketPtr : public Wrapper<AVPacket, AVPacketDeleter> {
@@ -135,7 +136,7 @@ struct AutoPacketUnref {
135136
// AVFrame
136137
////////////////////////////////////////////////////////////////////////////////
137138
struct AVFrameDeleter {
138-
void operator()(AVFrame* p);
139+
TORCHAUDIO_API void operator()(AVFrame* p);
139140
};
140141

141142
struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
@@ -147,7 +148,7 @@ struct AVFramePtr : public Wrapper<AVFrame, AVFrameDeleter> {
147148
// of AVBufferRefPtr.
148149
////////////////////////////////////////////////////////////////////////////////
149150
struct AutoBufferUnref {
150-
void operator()(AVBufferRef* p);
151+
TORCHAUDIO_API void operator()(AVBufferRef* p);
151152
};
152153

153154
struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
@@ -159,7 +160,7 @@ struct AVBufferRefPtr : public Wrapper<AVBufferRef, AutoBufferUnref> {
159160
// AVCodecContext
160161
////////////////////////////////////////////////////////////////////////////////
161162
struct AVCodecContextDeleter {
162-
void operator()(AVCodecContext* p);
163+
TORCHAUDIO_API void operator()(AVCodecContext* p);
163164
};
164165
struct AVCodecContextPtr
165166
: public Wrapper<AVCodecContext, AVCodecContextDeleter> {
@@ -184,7 +185,7 @@ void init_codec_context(
184185
// AVFilterGraph
185186
////////////////////////////////////////////////////////////////////////////////
186187
struct AVFilterGraphDeleter {
187-
void operator()(AVFilterGraph* p);
188+
TORCHAUDIO_API void operator()(AVFilterGraph* p);
188189
};
189190
struct AVFilterGraphPtr : public Wrapper<AVFilterGraph, AVFilterGraphDeleter> {
190191
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: 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)