Skip to content

Commit e3bde1b

Browse files
authored
Support compressed formats when building stream. (#2188)
This is part of the fix for #2180
1 parent 892a9b8 commit e3bde1b

5 files changed

Lines changed: 47 additions & 1 deletion

File tree

docs/FullGuide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ Oboe permits these sample formats:
5656
| I24 | N/A | 24-bit samples packed into 3 bytes, [Q0.23 format](https://source.android.com/devices/audio/data_formats#androidFormats). Added in API 31 |
5757
| I32 | int32_t | common 32-bit samples, [Q0.31 format](https://source.android.com/devices/audio/data_formats#androidFormats). Added in API 31 |
5858
| IEC61937 | N/A | compressed audio wrapped in IEC61937 for HDMI or S/PDIF passthrough. Added in API 34 |
59+
| MP3 | N/A | compressed audio format in MP3 format. Added in API36 |
60+
| AAC_LC | N/A | compressed audio format in AAC LC format. Added in API 36 |
61+
| AAC_HE_V1 | N/A | compressed audio format in AAC HE V1 format. Added in API 36 |
62+
| AAC_HE_V2 | N/A | compressed audio format in AAC HE V2 format. Added in API 36 |
63+
| AAC_ELD | N/A | compressed audio format in AAC ELD format. Added in API 36 |
64+
| AAC_XHE | N/A | compressed audio format in AAC XHE format. Added in API 36 |
65+
| OPUS | N/A | compressed audio format in OPUS format. Added in API 36 |
5966

6067
Oboe might perform sample conversion on its own. For example, if an app is writing AudioFormat::Float data but the HAL uses AudioFormat::I16, Oboe might convert the samples automatically. Conversion can happen in either direction. If your app processes audio input, it is wise to verify the input format and be prepared to convert data if necessary, as in this example:
6168

include/oboe/AudioStream.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "oboe/ResultWithValue.h"
2626
#include "oboe/AudioStreamBuilder.h"
2727
#include "oboe/AudioStreamBase.h"
28+
#include "oboe/Utilities.h"
2829

2930
namespace oboe {
3031

@@ -242,14 +243,20 @@ class AudioStream : public AudioStreamBase {
242243
* and the sample format. For example, a 2 channel floating point stream will have
243244
* 2 * 4 = 8 bytes per frame.
244245
*
246+
* Note for compressed formats, bytes per frames is treated as 1 by convention.
247+
*
245248
* @return number of bytes in each audio frame.
246249
*/
247-
int32_t getBytesPerFrame() const { return mChannelCount * getBytesPerSample(); }
250+
int32_t getBytesPerFrame() const {
251+
return isCompressedFormat(mFormat) ? 1 : mChannelCount * getBytesPerSample(); }
248252

249253
/**
250254
* Get the number of bytes per sample. This is calculated using the sample format. For example,
251255
* a stream using 16-bit integer samples will have 2 bytes per sample.
252256
*
257+
* Note for compressed formats, they may not have a fixed bytes per sample. In that case,
258+
* this method will return 0 for compressed format.
259+
*
253260
* @return the number of bytes per sample.
254261
*/
255262
int32_t getBytesPerSample() const;

include/oboe/AudioStreamBase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ class AudioStreamBase {
335335
case AudioFormat::I24:
336336
case AudioFormat::I32:
337337
case AudioFormat::IEC61937:
338+
case AudioFormat::MP3:
339+
case AudioFormat::AAC_LC:
340+
case AudioFormat::AAC_HE_V1:
341+
case AudioFormat::AAC_HE_V2:
342+
case AudioFormat::AAC_ELD:
343+
case AudioFormat::AAC_XHE:
344+
case AudioFormat::OPUS:
338345
break;
339346

340347
default:

src/common/Utilities.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ int32_t convertFormatToSizeInBytes(AudioFormat format) {
7070
case AudioFormat::IEC61937:
7171
size = sizeof(int16_t);
7272
break;
73+
case AudioFormat::MP3:
74+
case AudioFormat::AAC_LC:
75+
case AudioFormat::AAC_HE_V1:
76+
case AudioFormat::AAC_HE_V2:
77+
case AudioFormat::AAC_ELD:
78+
case AudioFormat::AAC_XHE:
79+
case AudioFormat::OPUS:
80+
// For compressed formats, set the size per sample as 0 as they may not have
81+
// fix size per sample.
82+
size = 0;
83+
break;
7384
default:
7485
break;
7586
}
@@ -111,6 +122,13 @@ const char *convertToText<AudioFormat>(AudioFormat format) {
111122
case AudioFormat::I24: return "I24";
112123
case AudioFormat::I32: return "I32";
113124
case AudioFormat::IEC61937: return "IEC61937";
125+
case AudioFormat::MP3: return "MP3";
126+
case AudioFormat::AAC_LC: return "AAC_LC";
127+
case AudioFormat::AAC_HE_V1: return "AAC_HE_V1";
128+
case AudioFormat::AAC_HE_V2: return "AAC_HE_V2";
129+
case AudioFormat::AAC_ELD: return "AAC_ELD";
130+
case AudioFormat::AAC_XHE: return "AAC_XHE";
131+
case AudioFormat::OPUS: return "OPUS";
114132
default: return "Unrecognized format";
115133
}
116134
}

src/opensles/OpenSLESUtilities.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ SLuint32 OpenSLES_ConvertFormatToRepresentation(AudioFormat format) {
8888
case AudioFormat::IEC61937:
8989
case AudioFormat::Invalid:
9090
case AudioFormat::Unspecified:
91+
case AudioFormat::MP3:
92+
case AudioFormat::AAC_LC:
93+
case AudioFormat::AAC_HE_V1:
94+
case AudioFormat::AAC_HE_V2:
95+
case AudioFormat::AAC_ELD:
96+
case AudioFormat::AAC_XHE:
97+
case AudioFormat::OPUS:
9198
default:
9299
return 0;
93100
}

0 commit comments

Comments
 (0)