|
| 1 | +/* |
| 2 | + * Copyright 2022 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.android.exoplayer2.source.rtsp.reader; |
| 17 | + |
| 18 | +import static com.google.android.exoplayer2.source.rtsp.reader.RtpReaderUtils.toSampleTimeUs; |
| 19 | +import static com.google.android.exoplayer2.util.Assertions.checkArgument; |
| 20 | +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; |
| 21 | +import static com.google.android.exoplayer2.util.Assertions.checkState; |
| 22 | +import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull; |
| 23 | +import static com.google.android.exoplayer2.util.Util.castNonNull; |
| 24 | + |
| 25 | +import androidx.annotation.Nullable; |
| 26 | +import com.google.android.exoplayer2.C; |
| 27 | +import com.google.android.exoplayer2.ParserException; |
| 28 | +import com.google.android.exoplayer2.extractor.ExtractorOutput; |
| 29 | +import com.google.android.exoplayer2.extractor.TrackOutput; |
| 30 | +import com.google.android.exoplayer2.source.rtsp.RtpPacket; |
| 31 | +import com.google.android.exoplayer2.source.rtsp.RtpPayloadFormat; |
| 32 | +import com.google.android.exoplayer2.util.ParsableBitArray; |
| 33 | +import com.google.android.exoplayer2.util.ParsableByteArray; |
| 34 | +import com.google.android.exoplayer2.util.Util; |
| 35 | +import com.google.common.collect.ImmutableMap; |
| 36 | +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
| 37 | + |
| 38 | +/** |
| 39 | + * Parses an MP4A-LATM byte stream carried on RTP packets, and extracts MP4A-LATM Access Units. |
| 40 | + * |
| 41 | + * <p>Refer to RFC3016 for more details. The LATM byte stream format is defined in ISO/IEC14496-3. |
| 42 | + */ |
| 43 | +/* package */ final class RtpMp4aReader implements RtpPayloadReader { |
| 44 | + private static final String TAG = "RtpMp4aReader"; |
| 45 | + |
| 46 | + private static final String PARAMETER_MP4A_CONFIG = "config"; |
| 47 | + |
| 48 | + private final RtpPayloadFormat payloadFormat; |
| 49 | + private final int numberOfSubframes; |
| 50 | + private @MonotonicNonNull TrackOutput trackOutput; |
| 51 | + private long firstReceivedTimestamp; |
| 52 | + private int previousSequenceNumber; |
| 53 | + /** The combined size of a sample that is fragmented into multiple subFrames. */ |
| 54 | + private int fragmentedSampleSizeBytes; |
| 55 | + |
| 56 | + private long startTimeOffsetUs; |
| 57 | + private long fragmentedSampleTimeUs; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates an instance. |
| 61 | + * |
| 62 | + * @throws IllegalArgumentException If {@link RtpPayloadFormat payloadFormat} is malformed. |
| 63 | + */ |
| 64 | + public RtpMp4aReader(RtpPayloadFormat payloadFormat) { |
| 65 | + this.payloadFormat = payloadFormat; |
| 66 | + try { |
| 67 | + numberOfSubframes = getNumOfSubframesFromMpeg4AudioConfig(payloadFormat.fmtpParameters); |
| 68 | + } catch (ParserException e) { |
| 69 | + throw new IllegalArgumentException(e); |
| 70 | + } |
| 71 | + firstReceivedTimestamp = C.TIME_UNSET; |
| 72 | + previousSequenceNumber = C.INDEX_UNSET; |
| 73 | + fragmentedSampleSizeBytes = 0; |
| 74 | + // The start time offset must be 0 until the first seek. |
| 75 | + startTimeOffsetUs = 0; |
| 76 | + fragmentedSampleTimeUs = C.TIME_UNSET; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void createTracks(ExtractorOutput extractorOutput, int trackId) { |
| 81 | + trackOutput = extractorOutput.track(trackId, C.TRACK_TYPE_VIDEO); |
| 82 | + castNonNull(trackOutput).format(payloadFormat.format); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onReceivingFirstPacket(long timestamp, int sequenceNumber) { |
| 87 | + checkState(firstReceivedTimestamp == C.TIME_UNSET); |
| 88 | + firstReceivedTimestamp = timestamp; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void consume( |
| 93 | + ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) { |
| 94 | + checkStateNotNull(trackOutput); |
| 95 | + |
| 96 | + int expectedSequenceNumber = RtpPacket.getNextSequenceNumber(previousSequenceNumber); |
| 97 | + if (fragmentedSampleSizeBytes > 0 && expectedSequenceNumber < sequenceNumber) { |
| 98 | + outputSampleMetadataForFragmentedPackets(); |
| 99 | + } |
| 100 | + |
| 101 | + for (int subFrameIndex = 0; subFrameIndex < numberOfSubframes; subFrameIndex++) { |
| 102 | + int sampleLength = 0; |
| 103 | + // Implements PayloadLengthInfo() in ISO/IEC14496-3 Chapter 1.7.3.1, it only supports one |
| 104 | + // program and one layer. Each subframe starts with a variable length encoding. |
| 105 | + while (data.getPosition() < data.limit()) { |
| 106 | + int payloadMuxLength = data.readUnsignedByte(); |
| 107 | + sampleLength += payloadMuxLength; |
| 108 | + if (payloadMuxLength != 0xff) { |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + trackOutput.sampleData(data, sampleLength); |
| 114 | + fragmentedSampleSizeBytes += sampleLength; |
| 115 | + } |
| 116 | + fragmentedSampleTimeUs = |
| 117 | + toSampleTimeUs( |
| 118 | + startTimeOffsetUs, timestamp, firstReceivedTimestamp, payloadFormat.clockRate); |
| 119 | + if (rtpMarker) { |
| 120 | + outputSampleMetadataForFragmentedPackets(); |
| 121 | + } |
| 122 | + previousSequenceNumber = sequenceNumber; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void seek(long nextRtpTimestamp, long timeUs) { |
| 127 | + firstReceivedTimestamp = nextRtpTimestamp; |
| 128 | + fragmentedSampleSizeBytes = 0; |
| 129 | + startTimeOffsetUs = timeUs; |
| 130 | + } |
| 131 | + |
| 132 | + // Internal methods. |
| 133 | + |
| 134 | + /** |
| 135 | + * Parses an MPEG-4 Audio Stream Mux configuration, as defined in ISO/IEC14496-3. |
| 136 | + * |
| 137 | + * <p>FMTP attribute {@code config} contains the MPEG-4 Audio Stream Mux configuration. |
| 138 | + * |
| 139 | + * @param fmtpAttributes The format parameters, mapped from the SDP FMTP attribute. |
| 140 | + * @return The number of subframes that is carried in each RTP packet. |
| 141 | + */ |
| 142 | + private static int getNumOfSubframesFromMpeg4AudioConfig( |
| 143 | + ImmutableMap<String, String> fmtpAttributes) throws ParserException { |
| 144 | + @Nullable String configInput = fmtpAttributes.get(PARAMETER_MP4A_CONFIG); |
| 145 | + int numberOfSubframes = 0; |
| 146 | + if (configInput != null && configInput.length() % 2 == 0) { |
| 147 | + byte[] configBuffer = Util.getBytesFromHexString(configInput); |
| 148 | + ParsableBitArray scratchBits = new ParsableBitArray(configBuffer); |
| 149 | + int audioMuxVersion = scratchBits.readBits(1); |
| 150 | + if (audioMuxVersion == 0) { |
| 151 | + checkArgument(scratchBits.readBits(1) == 1, "Only supports allStreamsSameTimeFraming."); |
| 152 | + numberOfSubframes = scratchBits.readBits(6); |
| 153 | + checkArgument(scratchBits.readBits(4) == 0, "Only suppors one program."); |
| 154 | + checkArgument(scratchBits.readBits(3) == 0, "Only suppors one layer."); |
| 155 | + } else { |
| 156 | + throw ParserException.createForMalformedDataOfUnknownType( |
| 157 | + "unsupported audio mux version: " + audioMuxVersion, null); |
| 158 | + } |
| 159 | + } |
| 160 | + // ISO/IEC14496-3 Chapter 1.7.3.2.3: The minimum value is 0 indicating 1 subframe. |
| 161 | + return numberOfSubframes + 1; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Outputs sample metadata. |
| 166 | + * |
| 167 | + * <p>Call this method only after receiving the end of an MPEG4 partition. |
| 168 | + */ |
| 169 | + private void outputSampleMetadataForFragmentedPackets() { |
| 170 | + checkNotNull(trackOutput) |
| 171 | + .sampleMetadata( |
| 172 | + fragmentedSampleTimeUs, |
| 173 | + C.BUFFER_FLAG_KEY_FRAME, |
| 174 | + fragmentedSampleSizeBytes, |
| 175 | + /* offset= */ 0, |
| 176 | + /* cryptoData= */ null); |
| 177 | + fragmentedSampleSizeBytes = 0; |
| 178 | + fragmentedSampleTimeUs = C.TIME_UNSET; |
| 179 | + } |
| 180 | +} |
0 commit comments