blob: 030c79f2ca3ab9e68573c95a6434ed148d269468 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_receiver_audio.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h> // assert
Yves Gerey665174f2018-06-19 15:03:05 +020014#include <math.h> // pow()
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015#include <string.h> // memcpy()
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000016
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
19#include "rtc_base/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000022RTPReceiverStrategy* RTPReceiverStrategy::CreateAudioStrategy(
solenberg1d031392016-03-30 02:42:32 -070023 RtpData* data_callback) {
24 return new RTPReceiverAudio(data_callback);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025}
26
solenberg1d031392016-03-30 02:42:32 -070027RTPReceiverAudio::RTPReceiverAudio(RtpData* data_callback)
Niels Möller3ed46bd2018-08-06 17:12:27 +020028 : RTPReceiverStrategy(data_callback) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010030RTPReceiverAudio::~RTPReceiverAudio() = default;
31
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000032// - Sample based or frame based codecs based on RFC 3551
33// -
34// - NOTE! There is one error in the RFC, stating G.722 uses 8 bits/samples.
35// - The correct rate is 4 bits/sample.
36// -
37// - name of sampling default
38// - encoding sample/frame bits/sample rate ms/frame ms/packet
39// -
40// - Sample based audio codecs
41// - DVI4 sample 4 var. 20
42// - G722 sample 4 16,000 20
43// - G726-40 sample 5 8,000 20
44// - G726-32 sample 4 8,000 20
45// - G726-24 sample 3 8,000 20
46// - G726-16 sample 2 8,000 20
47// - L8 sample 8 var. 20
48// - L16 sample 16 var. 20
49// - PCMA sample 8 var. 20
50// - PCMU sample 8 var. 20
51// -
52// - Frame based audio codecs
53// - G723 frame N/A 8,000 30 30
54// - G728 frame N/A 8,000 2.5 20
55// - G729 frame N/A 8,000 10 20
56// - G729D frame N/A 8,000 10 20
57// - G729E frame N/A 8,000 10 20
58// - GSM frame N/A 8,000 20 20
59// - GSM-EFR frame N/A 8,000 20 20
60// - LPC frame N/A 8,000 20 20
61// - MPA frame N/A var. var.
62// -
63// - G7221 frame N/A
niklase@google.com470e71d2011-07-07 08:21:25 +000064
wu@webrtc.org822fbd82013-08-15 23:38:54 +000065int32_t RTPReceiverAudio::ParseRtpPacket(WebRtcRTPHeader* rtp_header,
66 const PayloadUnion& specific_payload,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000067 const uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000068 size_t payload_length,
Niels Möllerbbf389c2017-09-26 14:05:05 +020069 int64_t timestamp_ms) {
skvlad98bb6642016-04-07 15:36:45 -070070 if (first_packet_received_()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_INFO) << "Received first audio RTP packet";
skvlad98bb6642016-04-07 15:36:45 -070072 }
73
Karl Wibergc856dc22017-09-28 20:13:59 +020074 return ParseAudioCodecSpecific(rtp_header, payload, payload_length,
Niels Möller31791e72018-03-14 11:27:26 +010075 specific_payload.audio_payload());
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000076}
77
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000078// We are not allowed to have any critsects when calling data_callback.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000079int32_t RTPReceiverAudio::ParseAudioCodecSpecific(
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000080 WebRtcRTPHeader* rtp_header,
pbos@webrtc.org2f446732013-04-08 11:08:41 +000081 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000082 size_t payload_length,
Niels Möller31791e72018-03-14 11:27:26 +010083 const AudioPayload& audio_specific) {
henrik.lundinb8c55b12017-05-10 07:38:01 -070084 RTC_DCHECK_GE(payload_length, rtp_header->header.paddingLength);
85 const size_t payload_data_length =
86 payload_length - rtp_header->header.paddingLength;
87 if (payload_data_length == 0) {
henrik.lundinb8c55b12017-05-10 07:38:01 -070088 rtp_header->frameType = kEmptyFrame;
89 return data_callback_->OnReceivedPayloadData(nullptr, 0, rtp_header);
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000090 }
91
henrik.lundinb8c55b12017-05-10 07:38:01 -070092 return data_callback_->OnReceivedPayloadData(payload_data,
93 payload_data_length, rtp_header);
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
phoglund@webrtc.orga7303bd2013-02-05 15:12:39 +000095} // namespace webrtc