|
27 | 27 | #include "media/audio/AudioProcessing.h"
|
28 | 28 | #include "media/audio/AudioProcessingConfig.h"
|
29 | 29 | #include "media/audio/AudioProcessingStreamConfig.h"
|
| 30 | +#include "api/audio/audio_frame.h" |
30 | 31 | #include "api/scoped_refptr.h"
|
31 | 32 | #include "modules/audio_processing/include/audio_processing.h"
|
32 | 33 | #include "rtc_base/logging.h"
|
@@ -72,7 +73,35 @@ JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_audio_AudioProcessing_proces
|
72 | 73 | jbyte * srcPtr = env->GetByteArrayElements(src, nullptr);
|
73 | 74 | jbyte * dstPtr = env->GetByteArrayElements(dest, &isDstCopy);
|
74 | 75 |
|
75 |
| - int result = apm->ProcessStream(reinterpret_cast<const int16_t *>(srcPtr), srcConfig, dstConfig, reinterpret_cast<int16_t *>(dstPtr)); |
| 76 | + int result; |
| 77 | + |
| 78 | + if (srcConfig.num_channels() == 1 && dstConfig.num_channels() == 2) { |
| 79 | + // Up-mixing, only mono to stereo. |
| 80 | + // For complex channel layouts an audio converter is required. |
| 81 | + |
| 82 | + const size_t srcNumSamples = srcConfig.num_samples(); |
| 83 | + const size_t dstNumChannels = dstConfig.num_channels(); |
| 84 | + const size_t frameSize = srcNumSamples * dstNumChannels; |
| 85 | + |
| 86 | + if (frameSize > webrtc::AudioFrame::kMaxDataSizeSamples) { |
| 87 | + return -9; |
| 88 | + } |
| 89 | + |
| 90 | + const int16_t * srcFrame = reinterpret_cast<const int16_t *>(srcPtr); |
| 91 | + int16_t * dstFrame = reinterpret_cast<int16_t*>(dstPtr); |
| 92 | + |
| 93 | + for (int i = srcNumSamples - 1; i >= 0; i--) { |
| 94 | + for (size_t j = 0; j < dstNumChannels; ++j) { |
| 95 | + dstFrame[dstNumChannels * i + j] = srcFrame[i]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + result = apm->ProcessStream(dstFrame, srcConfig, dstConfig, dstFrame); |
| 100 | + } |
| 101 | + else { |
| 102 | + // Will also down-mix if required, e.g. from stereo to mono. |
| 103 | + result = apm->ProcessStream(reinterpret_cast<const int16_t *>(srcPtr), srcConfig, dstConfig, reinterpret_cast<int16_t *>(dstPtr)); |
| 104 | + } |
76 | 105 |
|
77 | 106 | if (isDstCopy == JNI_TRUE) {
|
78 | 107 | jsize dstLength = env->GetArrayLength(dest);
|
@@ -100,7 +129,35 @@ JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_audio_AudioProcessing_proces
|
100 | 129 | jbyte * srcPtr = env->GetByteArrayElements(src, nullptr);
|
101 | 130 | jbyte * dstPtr = env->GetByteArrayElements(dest, &isDstCopy);
|
102 | 131 |
|
103 |
| - int result = apm->ProcessReverseStream(reinterpret_cast<int16_t *>(srcPtr), srcConfig, dstConfig, reinterpret_cast<int16_t *>(dstPtr)); |
| 132 | + int result; |
| 133 | + |
| 134 | + if (srcConfig.num_channels() == 1 && dstConfig.num_channels() == 2) { |
| 135 | + // Up-mixing, only mono to stereo. |
| 136 | + // For complex channel layouts an audio converter is required. |
| 137 | + |
| 138 | + const size_t srcNumSamples = srcConfig.num_samples(); |
| 139 | + const size_t dstNumChannels = dstConfig.num_channels(); |
| 140 | + const size_t frameSize = srcNumSamples * dstNumChannels; |
| 141 | + |
| 142 | + if (frameSize > webrtc::AudioFrame::kMaxDataSizeSamples) { |
| 143 | + return -9; |
| 144 | + } |
| 145 | + |
| 146 | + const int16_t * srcFrame = reinterpret_cast<const int16_t *>(srcPtr); |
| 147 | + int16_t * dstFrame = reinterpret_cast<int16_t *>(dstPtr); |
| 148 | + |
| 149 | + for (int i = srcNumSamples - 1; i >= 0; i--) { |
| 150 | + for (size_t j = 0; j < dstNumChannels; ++j) { |
| 151 | + dstFrame[dstNumChannels * i + j] = srcFrame[i]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + result = apm->ProcessStream(dstFrame, srcConfig, dstConfig, dstFrame); |
| 156 | + } |
| 157 | + else { |
| 158 | + // Will also down-mix if required, e.g. from stereo to mono. |
| 159 | + result = apm->ProcessStream(reinterpret_cast<const int16_t *>(srcPtr), srcConfig, dstConfig, reinterpret_cast<int16_t *>(dstPtr)); |
| 160 | + } |
104 | 161 |
|
105 | 162 | if (isDstCopy == JNI_TRUE) {
|
106 | 163 | jsize dstLength = env->GetArrayLength(dest);
|
|
0 commit comments