Skip to content

Conversation

@oliviamiller
Copy link
Collaborator

@oliviamiller oliviamiller commented Jan 21, 2026

On MP3 files that had headers before the actual audio data, we would fail to decode and did not play the audio. This scans for the start of MP3 data and decodes from there. Also retrying the flush call up to 10 times to ensure we get all decoded data and don't fail too early.

Tested various MP3 files generated by TTS libraries

set(DISCOVERY_TIMEOUT_VALUE $ENV{GTEST_DISCOVERY_TIMEOUT})
else()
set(DISCOVERY_TIMEOUT_VALUE 5)
set(DISCOVERY_TIMEOUT_VALUE 15)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this kept timing out for some reason so increased it

@oliviamiller oliviamiller changed the title Mp3 id3 fix RSDK-13200 Make mp3 decoding more reliable Jan 23, 2026
@oliviamiller oliviamiller marked this pull request as ready for review January 26, 2026 17:01
}

// helper to skip id3 tag: https://id3.org/id3v2.3.0
static size_t get_id3v2_offset(const uint8_t* data, size_t size) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you passing the underlying pointer to data instead of a const reference to the buffer vector itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no particular reason, will change to pass vector

// ID3v2 header
if (data[0] == 'I' && data[1] == 'D' && data[2] == '3') {
// Size is stored as a "syncsafe" integer (7 bits per byte)
size_t tag_size = ((data[6] & 0x7F) << 21) | ((data[7] & 0x7F) << 14) | ((data[8] & 0x7F) << 7) | (data[9] & 0x7F);
Copy link

@SebastianMunozP SebastianMunozP Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's err on declaring all variables as const unless they are meant to be mutable.

}

VIAM_SDK_LOG(debug) << "Decoding " << (encoded_data.size()) << " bytes of MP3 data";
std::vector<uint8_t> buffer(encoded_data.begin(), encoded_data.end());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this logic here, you can calculate the offset directly on the encoded_data vector as you are not modifying it, no need to copy it at this point.

Then, later on, you can copy only the portion of the encoded_data you need and keep using this as a vector, instead of operating on raw pointers as in here:

    unsigned char* encoded_data_ptr = buffer.data() + offset;
    const size_t mp3_data_length = buffer.size() - offset;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, will rework this to not allocate extra memory

@hexbabe hexbabe removed their request for review January 26, 2026 18:50
@oliviamiller oliviamiller merged commit 932f82a into viam-modules:main Jan 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants