-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for RTSP AMR-NB/WB #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
* Copyright 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.exoplayer.rtsp.reader; | ||
|
||
import static androidx.media3.common.util.Assertions.checkArgument; | ||
import static androidx.media3.common.util.Assertions.checkNotNull; | ||
|
||
import androidx.media3.common.C; | ||
import androidx.media3.common.MimeTypes; | ||
import androidx.media3.common.util.Log; | ||
import androidx.media3.common.util.ParsableByteArray; | ||
import androidx.media3.common.util.Util; | ||
import androidx.media3.exoplayer.rtsp.RtpPacket; | ||
import androidx.media3.exoplayer.rtsp.RtpPayloadFormat; | ||
import androidx.media3.extractor.ExtractorOutput; | ||
import androidx.media3.extractor.TrackOutput; | ||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; | ||
|
||
/** | ||
* Parses an AMR byte stream carried on RTP packets and extracts individual samples. Interleaving | ||
ManishaJajoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* mode is not supported. Refer to RFC4867 for more details. | ||
*/ | ||
/* package */ final class RtpAmrReader implements RtpPayloadReader { | ||
private static final String TAG = "RtpAmrReader"; | ||
/** | ||
* The frame size in bytes, including header (1 byte), for each of the 16 frame types for AMR | ||
* narrow band. | ||
* AMR-NB is a multi-mode codec that supports eight narrow band speech encoding modes | ||
* with bit rates between 4.75 and 12.2 kbps RFC4867 Section 3.1 | ||
* Refer to table 1a in 3GPP TS 26.101 | ||
*/ | ||
private static final int[] amrNbFrameTypeIndexToFrameSize = { | ||
13, // 4.75kbps | ||
14, // 5.15kbps | ||
16, // 5.90kbps | ||
18, // 6.70kbps PDC-EFR | ||
20, // 7.40kbps TDMA-EFR | ||
21, // 7.95kbps | ||
27, // 10.2kbps | ||
32, // 12.2kbps GSM-EFR | ||
6, // AMR SID | ||
7, // GSM-EFR SID | ||
6, // TDMA-EFR SID | ||
6, // PDC-EFR SID | ||
1, // Future use | ||
1, // Future use | ||
1, // Future use | ||
1 // No data | ||
}; | ||
|
||
/** | ||
* The frame size in bytes, including header (1 byte), for each of the 16 frame types for AMR wide | ||
* band. | ||
* AMR-WB is a multi-mode codec that supports nine wide band speech encoding modes | ||
* with bit rates between 6.6 to 23.85 kbps RFC4867 Section 3.2 | ||
* Refer to table 1a in 3GPP TS 26.201 | ||
*/ | ||
private static final int[] amrWbFrameTypeIndexToFrameSize = { | ||
18, // 6.60kbps | ||
24, // 8.85kbps | ||
33, // 12.65kbps | ||
37, // 14.25kbps | ||
41, // 15.85kbps | ||
47, // 18.25kbps | ||
51, // 19.85kbps | ||
59, // 23.05kbps | ||
61, // 23.85kbps | ||
6, // AMR-WB SID | ||
1, // Future use | ||
1, // Future use | ||
1, // Future use | ||
1, // Future use | ||
1, // speech lost | ||
1 // No data | ||
}; | ||
|
||
private final RtpPayloadFormat payloadFormat; | ||
ManishaJajoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final int sampleRate; | ||
|
||
private @MonotonicNonNull TrackOutput trackOutput; | ||
private long firstReceivedTimestamp; | ||
private long startTimeOffsetUs; | ||
private int previousSequenceNumber; | ||
private boolean isWideBand; | ||
|
||
public RtpAmrReader(RtpPayloadFormat payloadFormat) { | ||
this.payloadFormat = payloadFormat; | ||
this.firstReceivedTimestamp = C.TIME_UNSET; | ||
this.previousSequenceNumber = C.INDEX_UNSET; | ||
this.sampleRate = this.payloadFormat.clockRate; | ||
|
||
checkNotNull(this.payloadFormat.format.sampleMimeType); | ||
this.isWideBand = this.payloadFormat.format.sampleMimeType == MimeTypes.AUDIO_AMR_WB; | ||
} | ||
|
||
// RtpPayloadReader implementation. | ||
@Override | ||
public void createTracks(ExtractorOutput extractorOutput, int trackId) { | ||
trackOutput = extractorOutput.track(trackId, C.TRACK_TYPE_AUDIO); | ||
trackOutput.format(payloadFormat.format); | ||
} | ||
|
||
@Override | ||
public void onReceivingFirstPacket(long timestamp, int sequenceNumber) { | ||
this.firstReceivedTimestamp = timestamp; | ||
} | ||
|
||
@Override | ||
public void consume( | ||
ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) { | ||
// Check that this packet is in the sequence of the previous packet. | ||
if (previousSequenceNumber != C.INDEX_UNSET) { | ||
int expectedSequenceNumber = RtpPacket.getNextSequenceNumber(previousSequenceNumber); | ||
if (sequenceNumber != expectedSequenceNumber) { | ||
Log.w( | ||
TAG, | ||
Util.formatInvariant( | ||
"Received RTP packet with unexpected sequence number. Expected: %d; received: %d.", | ||
expectedSequenceNumber, sequenceNumber)); | ||
} | ||
} | ||
checkNotNull(trackOutput); | ||
/** | ||
* AMR as RTP payload RFC4867 Section 4.2 | ||
* +----------------+-------------------+---------------- | ||
* | payload header | table of contents | speech data ... | ||
* +----------------+-------------------+---------------- | ||
* | ||
* Payload header RFC4867 Section 4.4.1 | ||
* As interleaving is not supported currently, our header won't contain ILL and ILP | ||
* +-+-+-+-+-+-+-+ | ||
* | CMR |R|R|R|R| | ||
* +-+-+-+-+-+-+-+ | ||
*/ | ||
// skip CMR and reserved bits | ||
data.skipBytes(1); | ||
// Loop over sampleSize to send multiple frames along with appropriate timestamp when compound | ||
// payload support is added | ||
int frameType = (data.peekUnsignedByte() >> 3) & 0x0f; | ||
int frameSize = getFrameSize(frameType, isWideBand); | ||
int sampleSize = data.bytesLeft(); | ||
ManishaJajoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
checkArgument(sampleSize == frameSize, "compound payload not supported currently"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, why is it not supported? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We haven't been able to get an compound payload input from the live555 server. Since this feature is not being tested, we are not supporting it currently. |
||
trackOutput.sampleData(data, sampleSize); | ||
long sampleTimeUs = | ||
toSampleTimeUs(startTimeOffsetUs, timestamp, firstReceivedTimestamp, sampleRate); | ||
trackOutput.sampleMetadata( | ||
sampleTimeUs, | ||
C.BUFFER_FLAG_KEY_FRAME, | ||
sampleSize, | ||
/* offset= */ 0, | ||
/* encryptionData= */ null); | ||
previousSequenceNumber = sequenceNumber; | ||
} | ||
|
||
@Override | ||
public void seek(long nextRtpTimestamp, long timeUs) { | ||
firstReceivedTimestamp = nextRtpTimestamp; | ||
startTimeOffsetUs = timeUs; | ||
} | ||
|
||
// Internal methods. | ||
|
||
public static int getFrameSize(int frameType, boolean isWideBand) { | ||
checkArgument( | ||
isValidFrameType(frameType), | ||
"Illegal AMR " + (isWideBand ? "WB" : "NB") + " frame type " + frameType); | ||
|
||
return isWideBand | ||
? amrWbFrameTypeIndexToFrameSize[frameType] | ||
: amrNbFrameTypeIndexToFrameSize[frameType]; | ||
} | ||
|
||
private static boolean isValidFrameType(int frameType) { | ||
if (frameType < 0 || frameType > 15) { | ||
return false; | ||
} | ||
return (frameType < 9 || frameType > 14); | ||
} | ||
|
||
/** Returns the correct sample time from RTP timestamp, accounting for the AMR sampling rate. */ | ||
private static long toSampleTimeUs( | ||
long startTimeOffsetUs, long rtpTimestamp, long firstReceivedRtpTimestamp, int sampleRate) { | ||
return startTimeOffsetUs | ||
+ Util.scaleLargeTimestamp( | ||
rtpTimestamp - firstReceivedRtpTimestamp, | ||
/* multiplier= */ C.MICROS_PER_SECOND, | ||
/* divisor= */ sampleRate); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. RTP_MEDIA_AMR (which is "AMR"), corresponds to MIME type AUDIO_AMR_NB ("audio/3gpp") and not to AUDIO_AMR ("audio/amr")?
Do you know what would happen if you to map RTP_MEDIA_AMR to MimeTypes.AUDIO_AMR? Would the decoder work?
I skimmed through rfc4867 and I see that both AMR and AMR_WB can be stored in 3gpp files so I'd be tempted to see what happens when AMR_WB is also mapped to audio/3gpp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon setting the mimeType as
AUDIO_AMR
, the player fails in selectTracks. The reason for this failure is: AUDIO_AMR is treated as an unsupported mime type and therefore result.second is set to null here:https://github.com/androidx/media/blob/main/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/MappingTrackSelector.java#L422
Regarding the second change, i.e. mapping
AMR_WB
toaudio/3gpp
, sinceAUDIO_AMR_NB
creates an AMR_NB decoder andAUDIO_AMR_WB
creates AMR_WB decoder we see the following playback error:MediaCodecDecoderException: Decoder failed: c2.android.amrnb.decoder