blob: 942c4ff1e7b7bacef7000a371cbc737a53b99f5c [file] [log] [blame]
Henrik Kjellanderff761fb2015-11-04 08:31:52 +01001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
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#ifndef MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_
12#define MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010013
danilchapb8b6fbb2015-12-10 05:05:27 -080014#include <map>
brandtre6f98c72016-11-11 03:28:30 -080015#include <set>
danilchapb8b6fbb2015-12-10 05:05:27 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/audio_format.h"
Karl Wiberg73b60b82017-09-21 15:00:58 +020018#include "api/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/rtp_rtcp/source/rtp_utility.h"
20#include "rtc_base/criticalsection.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021
22namespace webrtc {
23
magjed56124bd2016-11-24 09:34:46 -080024class VideoCodec;
25
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010026class RTPPayloadRegistry {
27 public:
magjedf3feeff2016-11-25 06:40:25 -080028 RTPPayloadRegistry();
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029 ~RTPPayloadRegistry();
30
magjed56124bd2016-11-24 09:34:46 -080031 // TODO(magjed): Split RTPPayloadRegistry into separate Audio and Video class
magjedf3feeff2016-11-25 06:40:25 -080032 // and simplify the code. http://crbug/webrtc/6743.
kwiberg1c07c702017-03-27 07:15:49 -070033
34 // Replace all audio receive payload types with the given map.
35 void SetAudioReceivePayloads(std::map<int, SdpAudioFormat> codecs);
36
Karl Wibergc62f6c72017-10-04 12:38:53 +020037 int32_t RegisterReceivePayload(int payload_type,
38 const SdpAudioFormat& audio_format,
Sergey Ulanovec4f0682016-07-28 15:19:10 -070039 bool* created_new_payload_type);
magjed56124bd2016-11-24 09:34:46 -080040 int32_t RegisterReceivePayload(const VideoCodec& video_codec);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010041
Sergey Ulanovec4f0682016-07-28 15:19:10 -070042 int32_t DeRegisterReceivePayload(int8_t payload_type);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010043
Karl Wibergc62f6c72017-10-04 12:38:53 +020044 int32_t ReceivePayloadType(const SdpAudioFormat& audio_format,
magjed56124bd2016-11-24 09:34:46 -080045 int8_t* payload_type) const;
46 int32_t ReceivePayloadType(const VideoCodec& video_codec,
Sergey Ulanovec4f0682016-07-28 15:19:10 -070047 int8_t* payload_type) const;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010048
49 bool RtxEnabled() const;
50
51 void SetRtxSsrc(uint32_t ssrc);
52
53 bool GetRtxSsrc(uint32_t* ssrc) const;
54
55 void SetRtxPayloadType(int payload_type, int associated_payload_type);
56
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010057 bool IsRed(const RTPHeader& header) const;
58
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010059 int GetPayloadTypeFrequency(uint8_t payload_type) const;
60
Karl Wiberg73b60b82017-09-21 15:00:58 +020061 rtc::Optional<RtpUtility::Payload> PayloadTypeToPayload(
62 uint8_t payload_type) const;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010063
64 void ResetLastReceivedPayloadTypes() {
danilchap7c9426c2016-04-14 03:05:31 -070065 rtc::CritScope cs(&crit_sect_);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010066 last_received_payload_type_ = -1;
67 last_received_media_payload_type_ = -1;
68 }
69
70 // This sets the payload type of the packets being received from the network
71 // on the media SSRC. For instance if packets are encapsulated with RED, this
72 // payload type will be the RED payload type.
73 void SetIncomingPayloadType(const RTPHeader& header);
74
75 // Returns true if the new media payload type has not changed.
76 bool ReportMediaPayloadType(uint8_t media_payload_type);
77
magjedf3feeff2016-11-25 06:40:25 -080078 int8_t red_payload_type() const { return GetPayloadTypeWithName("red"); }
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010079 int8_t ulpfec_payload_type() const {
magjedf3feeff2016-11-25 06:40:25 -080080 return GetPayloadTypeWithName("ulpfec");
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010081 }
82 int8_t last_received_payload_type() const {
danilchap7c9426c2016-04-14 03:05:31 -070083 rtc::CritScope cs(&crit_sect_);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010084 return last_received_payload_type_;
85 }
86 void set_last_received_payload_type(int8_t last_received_payload_type) {
danilchap7c9426c2016-04-14 03:05:31 -070087 rtc::CritScope cs(&crit_sect_);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010088 last_received_payload_type_ = last_received_payload_type;
89 }
90
91 int8_t last_received_media_payload_type() const {
danilchap7c9426c2016-04-14 03:05:31 -070092 rtc::CritScope cs(&crit_sect_);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010093 return last_received_media_payload_type_;
danilchap5c1def82015-12-10 09:51:54 -080094 }
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010095
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010096 private:
97 // Prunes the payload type map of the specific payload type, if it exists.
98 void DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
Karl Wibergc62f6c72017-10-04 12:38:53 +020099 const SdpAudioFormat& audio_format);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100100
101 bool IsRtxInternal(const RTPHeader& header) const;
magjedf3feeff2016-11-25 06:40:25 -0800102 // Returns the payload type for the payload with name |payload_name|, or -1 if
103 // no such payload is registered.
104 int8_t GetPayloadTypeWithName(const char* payload_name) const;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100105
danilchap7c9426c2016-04-14 03:05:31 -0700106 rtc::CriticalSection crit_sect_;
magjedf3feeff2016-11-25 06:40:25 -0800107 std::map<int, RtpUtility::Payload> payload_type_map_;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100108 int8_t incoming_payload_type_;
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700109 int8_t last_received_payload_type_;
110 int8_t last_received_media_payload_type_;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100111 bool rtx_;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100112 // Mapping rtx_payload_type_map_[rtx] = associated.
113 std::map<int, int> rtx_payload_type_map_;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100114 uint32_t ssrc_rtx_;
brandtre6f98c72016-11-11 03:28:30 -0800115 // Only warn once per payload type, if an RTX packet is received but
116 // no associated payload type found in |rtx_payload_type_map_|.
danilchap56359be2017-09-07 07:53:45 -0700117 std::set<int> payload_types_with_suppressed_warnings_
118 RTC_GUARDED_BY(crit_sect_);
kwibergb0bf93a2017-03-23 00:10:09 -0700119
120 // As a first step in splitting this class up in separate cases for audio and
121 // video, DCHECK that no instance is used for both audio and video.
122#if RTC_DCHECK_IS_ON
danilchap56359be2017-09-07 07:53:45 -0700123 bool used_for_audio_ RTC_GUARDED_BY(crit_sect_) = false;
124 bool used_for_video_ RTC_GUARDED_BY(crit_sect_) = false;
kwibergb0bf93a2017-03-23 00:10:09 -0700125#endif
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100126};
127
128} // namespace webrtc
129
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200130#endif // MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_