|
| 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.util.Assertions.checkArgument; |
| 19 | +import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull; |
| 20 | + |
| 21 | +import com.google.android.exoplayer2.C; |
| 22 | +import com.google.android.exoplayer2.Format; |
| 23 | +import com.google.android.exoplayer2.audio.OpusUtil; |
| 24 | +import com.google.android.exoplayer2.extractor.ExtractorOutput; |
| 25 | +import com.google.android.exoplayer2.extractor.TrackOutput; |
| 26 | +import com.google.android.exoplayer2.source.rtsp.RtpPacket; |
| 27 | +import com.google.android.exoplayer2.source.rtsp.RtpPayloadFormat; |
| 28 | +import com.google.android.exoplayer2.util.Log; |
| 29 | +import com.google.android.exoplayer2.util.ParsableByteArray; |
| 30 | +import com.google.android.exoplayer2.util.Util; |
| 31 | +import java.util.List; |
| 32 | +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
| 33 | + |
| 34 | +/** |
| 35 | + * Parses an OPUS byte stream carried on RTP packets and extracts individual samples. Refer to |
| 36 | + * RFC7845 for more details. |
| 37 | + */ |
| 38 | +/* package */ final class RtpOpusReader implements RtpPayloadReader { |
| 39 | + private static final String TAG = "RtpOpusReader"; |
| 40 | + /* Opus uses a fixed 48KHz media clock RFC7845 Section 4. */ |
| 41 | + private static final long MEDIA_CLOCK_FREQUENCY = 48_000; |
| 42 | + |
| 43 | + private final RtpPayloadFormat payloadFormat; |
| 44 | + |
| 45 | + private @MonotonicNonNull TrackOutput trackOutput; |
| 46 | + |
| 47 | + /** |
| 48 | + * First received RTP timestamp. All RTP timestamps are dimension-less, the time base is defined |
| 49 | + * by {@link #MEDIA_CLOCK_FREQUENCY}. |
| 50 | + */ |
| 51 | + private long firstReceivedTimestamp; |
| 52 | + |
| 53 | + private long startTimeOffsetUs; |
| 54 | + private int previousSequenceNumber; |
| 55 | + private boolean foundOpusIDHeader; |
| 56 | + private boolean foundOpusCommentHeader; |
| 57 | + |
| 58 | + /** Creates an instance. */ |
| 59 | + public RtpOpusReader(RtpPayloadFormat payloadFormat) { |
| 60 | + this.payloadFormat = payloadFormat; |
| 61 | + this.firstReceivedTimestamp = C.INDEX_UNSET; |
| 62 | + this.previousSequenceNumber = C.INDEX_UNSET; |
| 63 | + } |
| 64 | + |
| 65 | + // RtpPayloadReader implementation. |
| 66 | + |
| 67 | + @Override |
| 68 | + public void createTracks(ExtractorOutput extractorOutput, int trackId) { |
| 69 | + trackOutput = extractorOutput.track(trackId, C.TRACK_TYPE_AUDIO); |
| 70 | + trackOutput.format(payloadFormat.format); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onReceivingFirstPacket(long timestamp, int sequenceNumber) { |
| 75 | + this.firstReceivedTimestamp = timestamp; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void consume( |
| 80 | + ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) { |
| 81 | + checkStateNotNull(trackOutput); |
| 82 | + |
| 83 | + /* RFC7845 Section 3. |
| 84 | + * +---------+ +----------------+ +--------------------+ +----- |
| 85 | + * |ID Header| | Comment Header | |Audio Data Packet 1 | | ... |
| 86 | + * +---------+ +----------------+ +--------------------+ +----- |
| 87 | + */ |
| 88 | + if (!foundOpusIDHeader) { |
| 89 | + validateOpusIdHeader(data); |
| 90 | + List<byte[]> initializationData = OpusUtil.buildInitializationData(data.getData()); |
| 91 | + Format.Builder formatBuilder = payloadFormat.format.buildUpon(); |
| 92 | + formatBuilder.setInitializationData(initializationData); |
| 93 | + trackOutput.format(formatBuilder.build()); |
| 94 | + foundOpusIDHeader = true; |
| 95 | + } else if (!foundOpusCommentHeader) { |
| 96 | + // Comment Header RFC7845 Section 5.2. |
| 97 | + int sampleSize = data.limit(); |
| 98 | + checkArgument(sampleSize >= 8, "Comment Header has insufficient data"); |
| 99 | + String header = data.readString(8); |
| 100 | + checkArgument(header.equals("OpusTags"), "Comment Header should follow ID Header"); |
| 101 | + foundOpusCommentHeader = true; |
| 102 | + } else { |
| 103 | + // Check that this packet is in the sequence of the previous packet. |
| 104 | + int expectedSequenceNumber = RtpPacket.getNextSequenceNumber(previousSequenceNumber); |
| 105 | + if (sequenceNumber != expectedSequenceNumber) { |
| 106 | + Log.w( |
| 107 | + TAG, |
| 108 | + Util.formatInvariant( |
| 109 | + "Received RTP packet with unexpected sequence number. Expected: %d; received: %d.", |
| 110 | + expectedSequenceNumber, sequenceNumber)); |
| 111 | + } |
| 112 | + |
| 113 | + // sending opus data. |
| 114 | + int size = data.bytesLeft(); |
| 115 | + trackOutput.sampleData(data, size); |
| 116 | + long timeUs = toSampleTimeUs(startTimeOffsetUs, timestamp, firstReceivedTimestamp); |
| 117 | + trackOutput.sampleMetadata( |
| 118 | + timeUs, C.BUFFER_FLAG_KEY_FRAME, size, /* offset*/ 0, /* cryptoData*/ null); |
| 119 | + } |
| 120 | + previousSequenceNumber = sequenceNumber; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void seek(long nextRtpTimestamp, long timeUs) { |
| 125 | + firstReceivedTimestamp = nextRtpTimestamp; |
| 126 | + startTimeOffsetUs = timeUs; |
| 127 | + } |
| 128 | + |
| 129 | + // Internal methods. |
| 130 | + |
| 131 | + /** |
| 132 | + * Validates the OPUS ID Header at {@code data}'s current position, throws {@link |
| 133 | + * IllegalArgumentException} if the header is invalid. |
| 134 | + * |
| 135 | + * <p>{@code data}'s position does not change after returning. |
| 136 | + */ |
| 137 | + private static void validateOpusIdHeader(ParsableByteArray data) { |
| 138 | + int currPosition = data.getPosition(); |
| 139 | + int sampleSize = data.limit(); |
| 140 | + checkArgument(sampleSize > 18, "ID Header has insufficient data"); |
| 141 | + String header = data.readString(8); |
| 142 | + // Identification header RFC7845 Section 5.1. |
| 143 | + checkArgument(header.equals("OpusHead"), "ID Header missing"); |
| 144 | + checkArgument(data.readUnsignedByte() == 1, "version number must always be 1"); |
| 145 | + data.setPosition(currPosition); |
| 146 | + } |
| 147 | + |
| 148 | + /** Returns the correct sample time from RTP timestamp, accounting for the OPUS sampling rate. */ |
| 149 | + private static long toSampleTimeUs( |
| 150 | + long startTimeOffsetUs, long rtpTimestamp, long firstReceivedRtpTimestamp) { |
| 151 | + return startTimeOffsetUs |
| 152 | + + Util.scaleLargeTimestamp( |
| 153 | + rtpTimestamp - firstReceivedRtpTimestamp, |
| 154 | + /* multiplier= */ C.MICROS_PER_SECOND, |
| 155 | + /* divisor= */ MEDIA_CLOCK_FREQUENCY); |
| 156 | + } |
| 157 | +} |
0 commit comments