1 /*
2  *  Copyright (c) 2016 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 
11 #ifndef MEDIA_ENGINE_PAYLOAD_TYPE_MAPPER_H_
12 #define MEDIA_ENGINE_PAYLOAD_TYPE_MAPPER_H_
13 
14 #include <map>
15 #include <set>
16 
17 #include "api/audio_codecs/audio_format.h"
18 #include "api/optional.h"
19 #include "media/base/codec.h"
20 
21 namespace cricket {
22 
23 webrtc::SdpAudioFormat AudioCodecToSdpAudioFormat(const AudioCodec& ac);
24 
25 class PayloadTypeMapper {
26  public:
27   PayloadTypeMapper();
28   ~PayloadTypeMapper();
29 
30   // Finds the current payload type for |format| or assigns a new one, if no
31   // current mapping exists. Will return an empty value if it was unable to
32   // create a mapping, i.e. if all dynamic payload type ids have been used up.
33   rtc::Optional<int> GetMappingFor(const webrtc::SdpAudioFormat& format);
34 
35   // Finds the current payload type for |format|, if any. Returns an empty value
36   // if no payload type mapping exists for the format.
37   rtc::Optional<int> FindMappingFor(const webrtc::SdpAudioFormat& format) const;
38 
39   // Like GetMappingFor, but fills in an AudioCodec structure with the necessary
40   // information instead.
41   rtc::Optional<AudioCodec> ToAudioCodec(const webrtc::SdpAudioFormat& format);
42 
43  private:
44   struct SdpAudioFormatOrdering {
45     bool operator()(const webrtc::SdpAudioFormat& a,
46                     const webrtc::SdpAudioFormat& b) const;
47   };
48 
49   int next_unused_payload_type_;
50   int max_payload_type_;
51   std::map<webrtc::SdpAudioFormat, int, SdpAudioFormatOrdering> mappings_;
52   std::set<int> used_payload_types_;
53 };
54 
55 }  // namespace cricket
56 #endif  // MEDIA_ENGINE_PAYLOAD_TYPE_MAPPER_H_
57