Repository navigation
Conversation
The chunk player's audio readers (FFMpegFileReader for the V2 reader, SoftwareAudioSource for V1) decoded every track to one channel, so videos played in mono in the app: both ears got the same signal, while the saved file played in stereo in Photos and elsewhere. - Decode a track with two or more channels to stereo, and a mono track to mono (FFMpegAudioFrameDecoder.playbackChannelCount). - Give the output the matching channel layout tag (it was always Mono) and a sample count of bytes / (2 * channels) (it was bytes / 2, right for mono only, and twice the real length for the legacy player's stereo output). - Voice message conversion (convertOpusToAAC) still decodes to mono, which is what its encoder expects. Fixes https://bugs.telegram.org/c/66408
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes https://bugs.telegram.org/c/66408: videos with stereo audio play in mono in the app, with the same signal in both ears. The same video plays in stereo in Telegram Web, on Android and desktop, and on iOS after it is saved to Photos.
Cause
The chunk player decodes audio with FFmpeg through one of two readers, and both asked for a single output channel:
FFMpegFileReader(the V2 reader):FFMpegAudioFrameDecoder(codecContext:, sampleRate: 48000, channelCount: 1)SoftwareAudioSource(the V1 reader): the same callSo the resampler mixed left and right into one channel for every video. The decoder also tagged its output
kAudioChannelLayoutTag_Monowhatever the channel count, and created each sample buffer withsampleCount: data.count / 2, which is the frame count only for 16-bit mono.Change
FFMpegAudioFrameDecoder.playbackChannelCount(codecContext:): 2 for a track with two or more channels, 1 for a mono track. Both readers use it.data.count / (2 * channelCount)samples. That also fixes the legacy player (FFMpegMediaFrameSourceContext), which already decoded to stereo and so got buffers reporting twice their real length.SoftwareAudioSourcetakesisPlayback.MediaDataReaderpassestrue; the publicinit(path:)keeps mono output forconvertOpusToAAC, whose encoder is set up for one channel.Multichannel tracks (5.1 and so on) are downmixed to stereo by the resampler, as they were downmixed to mono before.
Testing
iOS 27 simulator, debug build, playing https://t.me/jingluoasmr/36373 in the chat's player. The simulator's audio output can't be recorded, so a temporary log in the decoder printed the RMS of each output channel and of their sum and difference once per second (removed before commit):
Not tested on a device or with the legacy player.