|
| 1 | +#include "external_processing.hpp" |
| 2 | + |
| 3 | +#include <dlfcn.h> |
| 4 | +#include <syslog.h> |
| 5 | + |
| 6 | +#include "rtc_base/time_utils.h" |
| 7 | + |
| 8 | +namespace external { |
| 9 | + |
| 10 | +ExternalProcessing* ExternalProcessing::m_instance = nullptr; |
| 11 | + |
| 12 | +ExternalProcessing::ExternalProcessing() { |
| 13 | + ::syslog(LOG_INFO, "ExternalProcessing: #Constructor;"); |
| 14 | +} |
| 15 | + |
| 16 | +ExternalProcessing::~ExternalProcessing() { |
| 17 | + ::syslog(LOG_INFO, "ExternalProcessing: #Destructor;"); |
| 18 | + |
| 19 | + Destroy(); |
| 20 | +} |
| 21 | + |
| 22 | +bool ExternalProcessing::Create(const char* libname) { |
| 23 | + ::syslog(LOG_INFO, "ExternalProcessing: #Create; libname: %s", libname); |
| 24 | + |
| 25 | + // Load the shared library |
| 26 | + dlerror(); // Clear any existing errors |
| 27 | + void* handle = dlopen(libname, RTLD_LAZY); |
| 28 | + if (!handle) { |
| 29 | + ::syslog(LOG_ERR, "ExternalProcessing: #Create; Failed to load library: %s", |
| 30 | + dlerror()); |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + // Load external processor functions |
| 35 | + for (size_t functionId = 0; functionId < kFunctionCount; ++functionId) { |
| 36 | + const char* functionName = kFunctionNames[functionId]; |
| 37 | + syslog(LOG_INFO, "ExternalProcessing: #Create; Loading function: %s", |
| 38 | + functionName); |
| 39 | + void* functionPtr = dlsym(handle, functionName); |
| 40 | + const char* dlsym_error = dlerror(); |
| 41 | + if (dlsym_error) { |
| 42 | + syslog(LOG_ERR, |
| 43 | + "ExternalProcessing: #Create; Failed to load the function: %s", |
| 44 | + dlsym_error); |
| 45 | + return false; |
| 46 | + } |
| 47 | + m_functionPointers[functionId] = functionPtr; |
| 48 | + } |
| 49 | + |
| 50 | + void* createPtr = m_functionPointers[FunctionId::ExternalProcessorCreate]; |
| 51 | + if (!createPtr) { |
| 52 | + ::syslog(LOG_ERR, |
| 53 | + "ExternalProcessing: #Create; Failed to access " |
| 54 | + "ExternalProcessorCreate function"); |
| 55 | + dlclose(handle); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + auto createFunc = |
| 60 | + reinterpret_cast<ExternalProcessorCreateFuncType>(createPtr); |
| 61 | + if (!createFunc()) { |
| 62 | + ::syslog(LOG_ERR, |
| 63 | + "ExternalProcessing: #Create; Failed to invoke " |
| 64 | + "ExternalProcessorCreate function"); |
| 65 | + dlclose(handle); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + m_handle = handle; |
| 70 | + |
| 71 | + ::syslog(LOG_INFO, "ExternalProcessing: #Create; completed successfully"); |
| 72 | + |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +bool ExternalProcessing::Destroy() { |
| 77 | + ::syslog(LOG_INFO, "ExternalProcessing: #Destroy;"); |
| 78 | + |
| 79 | + void* destroyPtr = m_functionPointers[FunctionId::ExternalProcessorDestroy]; |
| 80 | + if (destroyPtr) { |
| 81 | + ::syslog(LOG_INFO, |
| 82 | + "ExternalProcessing: #Destroy; Invoke ExternalProcessorDestroy " |
| 83 | + "function"); |
| 84 | + |
| 85 | + auto destroyFunc = |
| 86 | + reinterpret_cast<ExternalProcessorDestroyFuncType>(destroyPtr); |
| 87 | + if (destroyFunc()) { |
| 88 | + ::syslog(LOG_INFO, |
| 89 | + "ExternalProcessing: #Destroy; Invoked ExternalProcessorDestroy " |
| 90 | + "successfully"); |
| 91 | + } |
| 92 | + } |
| 93 | + for (auto& functionPtr : m_functionPointers) { |
| 94 | + functionPtr = nullptr; |
| 95 | + } |
| 96 | + if (m_handle) { |
| 97 | + dlclose(m_handle); |
| 98 | + m_handle = nullptr; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +void ExternalProcessing::Initialize(int sample_rate_hz, int num_channels) { |
| 105 | + if (m_functionPointers.size() <= |
| 106 | + static_cast<size_t>(FunctionId::ExternalProcessorInitialize)) { |
| 107 | + ::syslog(LOG_ERR, |
| 108 | + "ExternalProcessing: #Initialize; m_functionPointers is not large " |
| 109 | + "enough"); |
| 110 | + return; |
| 111 | + } |
| 112 | + void* initializePtr = |
| 113 | + m_functionPointers[FunctionId::ExternalProcessorInitialize]; |
| 114 | + if (!initializePtr) { |
| 115 | + ::syslog(LOG_ERR, |
| 116 | + "ExternalProcessing: #Initialize; Failed to access " |
| 117 | + "ExternalProcessorInitialize function"); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + auto initializeFunc = |
| 122 | + reinterpret_cast<ExternalProcessorInitializeFuncType>(initializePtr); |
| 123 | + if (!initializeFunc(sample_rate_hz, num_channels)) { |
| 124 | + ::syslog(LOG_ERR, |
| 125 | + "ExternalProcessing: #Initialize; Failed to invoke " |
| 126 | + "ExternalProcessorInitialize function"); |
| 127 | + return; |
| 128 | + } |
| 129 | + ::syslog(LOG_INFO, |
| 130 | + "ExternalProcessing: #Initialize; Invoked " |
| 131 | + "ExternalProcessorInitialize; sample_rate_hz: %i, " |
| 132 | + "num_channels: %i", |
| 133 | + sample_rate_hz, num_channels); |
| 134 | +} |
| 135 | + |
| 136 | +void ExternalProcessing::Process(webrtc::AudioBuffer* audio) { |
| 137 | + float* const* channels = audio->channels(); |
| 138 | + size_t num_frames = audio->num_frames(); |
| 139 | + size_t num_bands = audio->num_bands(); |
| 140 | + size_t num_channels = audio->num_channels(); |
| 141 | + |
| 142 | + if (m_functionPointers.size() <= |
| 143 | + static_cast<size_t>(FunctionId::ExternalProcessorProcessFrame)) { |
| 144 | + ::syslog( |
| 145 | + LOG_ERR, |
| 146 | + "ExternalProcessing: #Process; m_functionPointers is not large enough"); |
| 147 | + return; |
| 148 | + } |
| 149 | + void* processPtr = |
| 150 | + m_functionPointers[FunctionId::ExternalProcessorProcessFrame]; |
| 151 | + if (!processPtr) { |
| 152 | + ::syslog(LOG_ERR, |
| 153 | + "ExternalProcessing: #Process; Failed to access " |
| 154 | + "ExternalProcessorProcessFrame function"); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + auto processFunc = |
| 159 | + reinterpret_cast<ExternalProcessorProcessFrameFuncType>(processPtr); |
| 160 | + if (!processFunc(channels, num_frames, num_bands, num_channels)) { |
| 161 | + ::syslog(LOG_ERR, |
| 162 | + "ExternalProcessing: #Process; Failed to invoke " |
| 163 | + "ExternalProcessorProcessFrame function"); |
| 164 | + return; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +std::string ExternalProcessing::ToString() const { |
| 169 | + return "ExternalProcessing"; |
| 170 | +} |
| 171 | + |
| 172 | +void ExternalProcessing::SetRuntimeSetting( |
| 173 | + webrtc::AudioProcessing::RuntimeSetting setting) { |
| 174 | + ::syslog(LOG_INFO, "ExternalProcessing: #SetRuntimeSetting;"); |
| 175 | +} |
| 176 | + |
| 177 | +} // end of namespace external |
0 commit comments