1 /*
2  *  Copyright (c) 2012 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 WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
13 
14 #include <set>
15 
16 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
18 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
19 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
21 #include "webrtc/typedefs.h"
22 
23 namespace webrtc {
24 
25 class CriticalSectionWrapper;
26 
27 // Handles audio RTP packets. This class is thread-safe.
28 class RTPReceiverAudio : public RTPReceiverStrategy,
29                          public TelephoneEventHandler {
30  public:
31   RTPReceiverAudio(RtpData* data_callback,
32                    RtpAudioFeedback* incoming_messages_callback);
~RTPReceiverAudio()33   virtual ~RTPReceiverAudio() {}
34 
35   // The following three methods implement the TelephoneEventHandler interface.
36   // Forward DTMFs to decoder for playout.
37   void SetTelephoneEventForwardToDecoder(bool forward_to_decoder);
38 
39   // Is forwarding of outband telephone events turned on/off?
40   bool TelephoneEventForwardToDecoder() const;
41 
42   // Is TelephoneEvent configured with payload type payload_type
43   bool TelephoneEventPayloadType(const int8_t payload_type) const;
44 
GetTelephoneEventHandler()45   TelephoneEventHandler* GetTelephoneEventHandler() { return this; }
46 
47   // Returns true if CNG is configured with payload type payload_type. If so,
48   // the frequency and cng_payload_type_has_changed are filled in.
49   bool CNGPayloadType(const int8_t payload_type,
50                       uint32_t* frequency,
51                       bool* cng_payload_type_has_changed);
52 
53   int32_t ParseRtpPacket(WebRtcRTPHeader* rtp_header,
54                          const PayloadUnion& specific_payload,
55                          bool is_red,
56                          const uint8_t* packet,
57                          size_t payload_length,
58                          int64_t timestamp_ms,
59                          bool is_first_packet) override;
60 
61   int GetPayloadTypeFrequency() const override;
62 
63   RTPAliveType ProcessDeadOrAlive(uint16_t last_payload_length) const override;
64 
65   bool ShouldReportCsrcChanges(uint8_t payload_type) const override;
66 
67   int32_t OnNewPayloadTypeCreated(
68       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
69       int8_t payload_type,
70       uint32_t frequency) override;
71 
72   int32_t InvokeOnInitializeDecoder(
73       RtpFeedback* callback,
74       int8_t payload_type,
75       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
76       const PayloadUnion& specific_payload) const override;
77 
78   // We do not allow codecs to have multiple payload types for audio, so we
79   // need to override the default behavior (which is to do nothing).
80   void PossiblyRemoveExistingPayloadType(
81       RtpUtility::PayloadTypeMap* payload_type_map,
82       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
83       size_t payload_name_length,
84       uint32_t frequency,
85       uint8_t channels,
86       uint32_t rate) const;
87 
88   // We need to look out for special payload types here and sometimes reset
89   // statistics. In addition we sometimes need to tweak the frequency.
90   void CheckPayloadChanged(int8_t payload_type,
91                            PayloadUnion* specific_payload,
92                            bool* should_discard_changes) override;
93 
94   int Energy(uint8_t array_of_energy[kRtpCsrcSize]) const override;
95 
96  private:
97   int32_t ParseAudioCodecSpecific(WebRtcRTPHeader* rtp_header,
98                                   const uint8_t* payload_data,
99                                   size_t payload_length,
100                                   const AudioPayload& audio_specific,
101                                   bool is_red);
102 
103   uint32_t last_received_frequency_;
104 
105   bool telephone_event_forward_to_decoder_;
106   int8_t telephone_event_payload_type_;
107   std::set<uint8_t> telephone_event_reported_;
108 
109   int8_t cng_nb_payload_type_;
110   int8_t cng_wb_payload_type_;
111   int8_t cng_swb_payload_type_;
112   int8_t cng_fb_payload_type_;
113   int8_t cng_payload_type_;
114 
115   // G722 is special since it use the wrong number of RTP samples in timestamp
116   // VS. number of samples in the frame
117   int8_t g722_payload_type_;
118   bool last_received_g722_;
119 
120   uint8_t num_energy_;
121   uint8_t current_remote_energy_[kRtpCsrcSize];
122 
123   RtpAudioFeedback* cb_audio_feedback_;
124 };
125 }  // namespace webrtc
126 
127 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
128