Skip to content

Commit

Permalink
Merge pull request #56 from ittiam-systems:rtp_wav
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 437783926
  • Loading branch information
icbaker committed Apr 6, 2022
2 parents 0096b40 + 3f8a680 commit e3a9ed6
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 3 deletions.
4 changes: 3 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
* RTSP:
* Add RTP reader for HEVC
([#36](https://github.com/androidx/media/pull/36)).
* Add RTP reader for AMR. Currently only mono-channel, non-interleaved
* Add RTP reader for AMR. Currently only mono-channel, non-interleaved
AMR streams are supported. Compound AMR RTP payload is not supported.
([#46](https://github.com/androidx/media/pull/46))
* Add RTP reader for VP8
([#47](https://github.com/androidx/media/pull/47)).
* Add RTP reader for WAV
([#56](https://github.com/androidx/media/pull/56)).
* Remove deprecated symbols:
* Remove `Player.Listener.onTracksChanged`. Use
`Player.Listener.onTracksInfoChanged` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package androidx.media3.exoplayer.rtsp;

import static androidx.media3.common.util.Assertions.checkArgument;

import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.UnstableApi;
Expand All @@ -42,6 +45,10 @@ public final class RtpPayloadFormat {
private static final String RTP_MEDIA_MPEG4_GENERIC = "MPEG4-GENERIC";
private static final String RTP_MEDIA_H264 = "H264";
private static final String RTP_MEDIA_H265 = "H265";
private static final String RTP_MEDIA_PCM_L8 = "L8";
private static final String RTP_MEDIA_PCM_L16 = "L16";
private static final String RTP_MEDIA_PCMA = "PCMA";
private static final String RTP_MEDIA_PCMU = "PCMU";
private static final String RTP_MEDIA_VP8 = "VP8";

/** Returns whether the format of a {@link MediaDescription} is supported. */
Expand All @@ -53,6 +60,10 @@ public static boolean isFormatSupported(MediaDescription mediaDescription) {
case RTP_MEDIA_H264:
case RTP_MEDIA_H265:
case RTP_MEDIA_MPEG4_GENERIC:
case RTP_MEDIA_PCM_L8:
case RTP_MEDIA_PCM_L16:
case RTP_MEDIA_PCMA:
case RTP_MEDIA_PCMU:
case RTP_MEDIA_VP8:
return true;
default:
Expand Down Expand Up @@ -81,13 +92,29 @@ public static String getMimeTypeFromRtpMediaType(String mediaType) {
return MimeTypes.VIDEO_H265;
case RTP_MEDIA_MPEG4_GENERIC:
return MimeTypes.AUDIO_AAC;
case RTP_MEDIA_PCM_L8:
case RTP_MEDIA_PCM_L16:
return MimeTypes.AUDIO_RAW;
case RTP_MEDIA_PCMA:
return MimeTypes.AUDIO_ALAW;
case RTP_MEDIA_PCMU:
return MimeTypes.AUDIO_MLAW;
case RTP_MEDIA_VP8:
return MimeTypes.VIDEO_VP8;
default:
throw new IllegalArgumentException(mediaType);
}
}

/** Returns the PCM encoding type for {@code mediaEncoding}. */
public static @C.PcmEncoding int getRawPcmEncodingType(String mediaEncoding) {
checkArgument(
mediaEncoding.equals(RTP_MEDIA_PCM_L8) || mediaEncoding.equals(RTP_MEDIA_PCM_L16));
return mediaEncoding.equals(RtpPayloadFormat.RTP_MEDIA_PCM_L8)
? C.ENCODING_PCM_8BIT
: C.ENCODING_PCM_16BIT_BIG_ENDIAN;
}

/** The payload type associated with this format. */
public final int rtpPayloadType;
/** The clock rate in Hertz, associated with the format. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ public int hashCode() {
}

int rtpPayloadType = mediaDescription.rtpMapAttribute.payloadType;
String mediaEncoding = mediaDescription.rtpMapAttribute.mediaEncoding;

String mimeType = getMimeTypeFromRtpMediaType(mediaDescription.rtpMapAttribute.mediaEncoding);
String mimeType = getMimeTypeFromRtpMediaType(mediaEncoding);
formatBuilder.setSampleMimeType(mimeType);

int clockRate = mediaDescription.rtpMapAttribute.clockRate;
Expand Down Expand Up @@ -168,8 +169,13 @@ public int hashCode() {
// width and height.
formatBuilder.setWidth(DEFAULT_VP8_WIDTH).setHeight(DEFAULT_VP8_HEIGHT);
break;
case MimeTypes.AUDIO_RAW:
formatBuilder.setPcmEncoding(RtpPayloadFormat.getRawPcmEncodingType(mediaEncoding));
break;
case MimeTypes.AUDIO_AC3:
// AC3 does not require a fmtp attribute. Fall through.
case MimeTypes.AUDIO_ALAW:
case MimeTypes.AUDIO_MLAW:
// Does not require a fmtp attribute. Fall through.
default:
// Do nothing.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public RtpPayloadReader createPayloadReader(RtpPayloadFormat payloadFormat) {
case MimeTypes.AUDIO_AMR_NB:
case MimeTypes.AUDIO_AMR_WB:
return new RtpAmrReader(payloadFormat);
case MimeTypes.AUDIO_RAW:
case MimeTypes.AUDIO_ALAW:
case MimeTypes.AUDIO_MLAW:
return new RtpPcmReader(payloadFormat);
case MimeTypes.VIDEO_H264:
return new RtpH264Reader(payloadFormat);
case MimeTypes.VIDEO_H265:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.checkNotNull;

import android.util.Log;
import androidx.media3.common.C;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.UnstableApi;
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 byte stream carried on RTP packets, and extracts PCM frames. Refer to RFC3551 for more
* details.
*/
@UnstableApi
/* package */ public final class RtpPcmReader implements RtpPayloadReader {

private static final String TAG = "RtpPcmReader";
private final RtpPayloadFormat payloadFormat;

private @MonotonicNonNull TrackOutput trackOutput;
private long firstReceivedTimestamp;
private long startTimeOffsetUs;
private int previousSequenceNumber;

public RtpPcmReader(RtpPayloadFormat payloadFormat) {
this.payloadFormat = payloadFormat;
firstReceivedTimestamp = C.TIME_UNSET;
// Start time offset must be 0 before the first seek.
startTimeOffsetUs = 0;
previousSequenceNumber = C.INDEX_UNSET;
}

@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) {
firstReceivedTimestamp = timestamp;
}

@Override
public void consume(
ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) {
checkNotNull(trackOutput);
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));
}
}

long sampleTimeUs =
toSampleUs(startTimeOffsetUs, timestamp, firstReceivedTimestamp, payloadFormat.clockRate);
int size = data.bytesLeft();
trackOutput.sampleData(data, size);
trackOutput.sampleMetadata(
sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, size, /* offset= */ 0, /* cryptoData= */ null);

previousSequenceNumber = sequenceNumber;
}

@Override
public void seek(long nextRtpTimestamp, long timeUs) {
// TODO(b/198620566) Rename firstReceivedTimestamp to timestampBase for all RtpPayloadReaders.
firstReceivedTimestamp = nextRtpTimestamp;
startTimeOffsetUs = timeUs;
}

/** Returns the correct sample time from RTP timestamp, accounting for the given clock rate. */
private static long toSampleUs(
long startTimeOffsetUs, long rtpTimestamp, long firstReceivedRtpTimestamp, int clockRate) {
return startTimeOffsetUs
+ Util.scaleLargeTimestamp(
rtpTimestamp - firstReceivedRtpTimestamp,
/* multiplier= */ C.MICROS_PER_SECOND,
/* divisor= */ clockRate);
}
}
Loading

0 comments on commit e3a9ed6

Please sign in to comment.