mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 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 | #include "video_engine/encoder_state_feedback.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | |
| 15 | #include "modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
| 16 | #include "system_wrappers/interface/critical_section_wrapper.h" |
| 17 | #include "video_engine/vie_encoder.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Helper class registered at the RTP module relaying callbacks to |
| 22 | // EncoderStatFeedback. |
| 23 | class EncoderStateFeedbackObserver : public RtcpIntraFrameObserver { |
| 24 | public: |
| 25 | explicit EncoderStateFeedbackObserver(EncoderStateFeedback* owner) |
| 26 | : owner_(owner) {} |
| 27 | ~EncoderStateFeedbackObserver() {} |
| 28 | |
| 29 | // Implements RtcpIntraFrameObserver. |
| 30 | virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) { |
| 31 | owner_->OnReceivedIntraFrameRequest(ssrc); |
| 32 | } |
| 33 | virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) { |
| 34 | owner_->OnReceivedSLI(ssrc, picture_id); |
| 35 | } |
| 36 | virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) { |
| 37 | owner_->OnReceivedRPSI(ssrc, picture_id); |
| 38 | } |
| 39 | |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 40 | virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) { |
| 41 | owner_->OnLocalSsrcChanged(old_ssrc, new_ssrc); |
| 42 | } |
| 43 | |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 44 | private: |
| 45 | EncoderStateFeedback* owner_; |
| 46 | }; |
| 47 | |
| 48 | EncoderStateFeedback::EncoderStateFeedback() |
| 49 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
| 50 | observer_(new EncoderStateFeedbackObserver(this)) {} |
| 51 | |
| 52 | EncoderStateFeedback::~EncoderStateFeedback() { |
| 53 | assert(encoders_.empty()); |
| 54 | } |
| 55 | |
| 56 | bool EncoderStateFeedback::AddEncoder(uint32_t ssrc, ViEEncoder* encoder) { |
| 57 | CriticalSectionScoped lock(crit_.get()); |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 58 | if (encoders_.find(ssrc) != encoders_.end()) { |
| 59 | // Two encoders must not have the same ssrc. |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 60 | return false; |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 61 | } |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 62 | |
| 63 | encoders_[ssrc] = encoder; |
| 64 | return true; |
| 65 | } |
| 66 | |
vikasmarwaha@webrtc.org | 8239ca5 | 2012-10-24 22:35:52 +0000 | [diff] [blame^] | 67 | void EncoderStateFeedback::RemoveEncoder(uint32_t ssrc) { |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 68 | CriticalSectionScoped lock(crit_.get()); |
vikasmarwaha@webrtc.org | 8239ca5 | 2012-10-24 22:35:52 +0000 | [diff] [blame^] | 69 | SsrcEncoderMap::iterator it = encoders_.find(ssrc); |
| 70 | if (it == encoders_.end()) |
| 71 | return; |
| 72 | |
| 73 | encoders_.erase(it); |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | RtcpIntraFrameObserver* EncoderStateFeedback::GetRtcpIntraFrameObserver() { |
| 77 | return observer_.get(); |
| 78 | } |
| 79 | |
| 80 | void EncoderStateFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
| 81 | CriticalSectionScoped lock(crit_.get()); |
| 82 | SsrcEncoderMap::iterator it = encoders_.find(ssrc); |
| 83 | if (it == encoders_.end()) |
| 84 | return; |
| 85 | |
| 86 | it->second->OnReceivedIntraFrameRequest(ssrc); |
| 87 | } |
| 88 | |
| 89 | void EncoderStateFeedback::OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) { |
| 90 | CriticalSectionScoped lock(crit_.get()); |
| 91 | SsrcEncoderMap::iterator it = encoders_.find(ssrc); |
| 92 | if (it == encoders_.end()) |
| 93 | return; |
| 94 | |
| 95 | it->second->OnReceivedSLI(ssrc, picture_id); |
| 96 | } |
| 97 | |
| 98 | void EncoderStateFeedback::OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) { |
| 99 | CriticalSectionScoped lock(crit_.get()); |
| 100 | SsrcEncoderMap::iterator it = encoders_.find(ssrc); |
| 101 | if (it == encoders_.end()) |
| 102 | return; |
| 103 | |
| 104 | it->second->OnReceivedRPSI(ssrc, picture_id); |
| 105 | } |
| 106 | |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 107 | void EncoderStateFeedback::OnLocalSsrcChanged(uint32_t old_ssrc, |
| 108 | uint32_t new_ssrc) { |
| 109 | CriticalSectionScoped lock(crit_.get()); |
| 110 | SsrcEncoderMap::iterator it = encoders_.find(old_ssrc); |
| 111 | if (it == encoders_.end() || encoders_.find(new_ssrc) != encoders_.end()) { |
| 112 | return; |
| 113 | } |
| 114 | |
vikasmarwaha@webrtc.org | 8239ca5 | 2012-10-24 22:35:52 +0000 | [diff] [blame^] | 115 | encoders_[new_ssrc] = it->second; |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 116 | encoders_.erase(it); |
| 117 | } |
| 118 | |
mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame] | 119 | } // namespace webrtc |