|
172 | 172 | m_destinationFormat.mChannelsPerFrame = m_sourceFormat.mChannelsPerFrame;
|
173 | 173 | m_destinationFormat.mSampleRate = m_sourceFormat.mSampleRate;
|
174 | 174 | }
|
175 |
| - if (outputFormatID == kAudioFormatOpus) |
176 |
| - m_destinationFormat.mSampleRate = 48000; |
177 | 175 |
|
178 |
| - UInt32 size = sizeof(m_destinationFormat); |
179 |
| - if (auto error = PAL::AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &m_destinationFormat)) { |
| 176 | + if (auto error = [&](auto& destinationFormat) { |
| 177 | + auto originalDestinationFormat = destinationFormat; |
| 178 | + UInt32 size = sizeof(destinationFormat); |
| 179 | + auto result = PAL::AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &destinationFormat); |
| 180 | + if (result == kAudioCodecUnsupportedFormatError) { |
| 181 | + destinationFormat.mSampleRate = 48000; |
| 182 | + result = PAL::AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &destinationFormat); |
| 183 | + if (originalDestinationFormat.mFramesPerPacket) { |
| 184 | + // Adjust mFramesPerPacket to match new sampling rate |
| 185 | + destinationFormat.mFramesPerPacket = originalDestinationFormat.mFramesPerPacket / originalDestinationFormat.mSampleRate * destinationFormat.mSampleRate; |
| 186 | + } |
| 187 | + } |
| 188 | + return result; |
| 189 | + }(m_destinationFormat)) { |
180 | 190 | RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter AudioFormatGetProperty failed with %d", static_cast<int>(error));
|
181 | 191 | return error;
|
182 | 192 | }
|
|
209 | 219 | }
|
210 | 220 | }
|
211 | 221 |
|
212 |
| - size = sizeof(m_sourceFormat); |
| 222 | + UInt32 size = sizeof(m_sourceFormat); |
213 | 223 | if (auto error = PAL::AudioConverterGetProperty(m_converter, kAudioConverterCurrentInputStreamDescription, &size, &m_sourceFormat)) {
|
214 | 224 | RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter getting kAudioConverterCurrentInputStreamDescription failed with %d", static_cast<int>(error));
|
215 | 225 | return error;
|
|
231 | 241 | }
|
232 | 242 | if (shouldSetDefaultOutputBitRate) {
|
233 | 243 | auto outputBitRate = defaultOutputBitRate(m_destinationFormat);
|
234 |
| - size = sizeof(outputBitRate); |
235 |
| - if (auto error = PAL::AudioConverterSetProperty(m_converter, kAudioConverterEncodeBitRate, size, &outputBitRate)) |
| 244 | + if (auto error = PAL::AudioConverterSetProperty(m_converter, kAudioConverterEncodeBitRate, sizeof(outputBitRate), &outputBitRate)) |
236 | 245 | RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter setting default kAudioConverterEncodeBitRate failed with %d", static_cast<int>(error));
|
237 | 246 | else
|
238 | 247 | m_defaultBitRate = outputBitRate;
|
|
250 | 259 | UInt32 size = sizeof(primeInfo);
|
251 | 260 | if (auto error = PAL::AudioConverterGetProperty(m_converter, kAudioConverterPrimeInfo, &size, &primeInfo))
|
252 | 261 | RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter getting kAudioConverterPrimeInfo failed with %d", static_cast<int>(error));
|
| 262 | + else |
| 263 | + m_preSkip = primeInfo.leadingFrames; |
253 | 264 | m_remainingPrimeDuration = PAL::CMTimeMake(primeInfo.leadingFrames, m_destinationFormat.mSampleRate);
|
| 265 | + |
| 266 | + if (m_options.bitrateMode) { |
| 267 | + UInt32 bitrateMode = *m_options.bitrateMode == BitrateMode::Variable ? kAudioCodecBitRateControlMode_Variable : kAudioCodecBitRateControlMode_Constant; |
| 268 | + if (auto error = PAL::AudioConverterSetProperty(m_converter, kAudioCodecPropertyBitRateControlMode, sizeof(bitrateMode), &bitrateMode)) |
| 269 | + RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter setting kAudioCodecPropertyBitRateControlMode failed with %d", static_cast<int>(error)); |
| 270 | + } |
| 271 | + if (m_options.complexity) { |
| 272 | + if (auto error = PAL::AudioConverterSetProperty(m_converter, kAudioCodecPropertyQualitySetting, sizeof(*m_options.complexity), &m_options.complexity.value())) |
| 273 | + RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter setting kAudioCodecPropertyQualitySetting failed with %d", static_cast<int>(error)); |
| 274 | + } |
| 275 | + |
| 276 | + // Only operational with Opus encoder. |
| 277 | + if (m_options.packetlossperc) { |
| 278 | + if (auto error = PAL::AudioConverterSetProperty(m_converter, 'plsp', sizeof(*m_options.packetlossperc), &m_options.packetlossperc.value())) |
| 279 | + RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter setting packetlossperc failed with %d", static_cast<int>(error)); |
| 280 | + } |
| 281 | + if (m_options.useinbandfec) { |
| 282 | + if (auto error = PAL::AudioConverterSetProperty(m_converter, 'pfec', sizeof(*m_options.useinbandfec), &m_options.useinbandfec.value())) |
| 283 | + RELEASE_LOG_ERROR(MediaStream, "AudioSampleBufferConverter setting useinbandfec failed with %d", static_cast<int>(error)); |
| 284 | + } |
254 | 285 | }
|
255 | 286 |
|
256 | 287 | if (!m_destinationFormat.mBytesPerPacket) {
|
|
0 commit comments