andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
pbos@webrtc.org | 9fb1613 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/processing_component.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 3f45c2e | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | |
pbos@webrtc.org | 9fb1613 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_processing/audio_processing_impl.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
pbos@webrtc.org | 24add92 | 2013-08-02 11:44:11 +0000 | [diff] [blame] | 19 | ProcessingComponent::ProcessingComponent() {} |
| 20 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 21 | ProcessingComponent::ProcessingComponent(const AudioProcessingImpl* apm) |
| 22 | : apm_(apm), |
| 23 | initialized_(false), |
| 24 | enabled_(false), |
| 25 | num_handles_(0) {} |
| 26 | |
| 27 | ProcessingComponent::~ProcessingComponent() { |
| 28 | assert(initialized_ == false); |
| 29 | } |
| 30 | |
| 31 | int ProcessingComponent::Destroy() { |
| 32 | while (!handles_.empty()) { |
| 33 | DestroyHandle(handles_.back()); |
| 34 | handles_.pop_back(); |
| 35 | } |
| 36 | initialized_ = false; |
| 37 | |
| 38 | return apm_->kNoError; |
| 39 | } |
| 40 | |
| 41 | int ProcessingComponent::EnableComponent(bool enable) { |
| 42 | if (enable && !enabled_) { |
| 43 | enabled_ = enable; // Must be set before Initialize() is called. |
| 44 | |
| 45 | int err = Initialize(); |
| 46 | if (err != apm_->kNoError) { |
| 47 | enabled_ = false; |
| 48 | return err; |
| 49 | } |
| 50 | } else { |
| 51 | enabled_ = enable; |
| 52 | } |
| 53 | |
| 54 | return apm_->kNoError; |
| 55 | } |
| 56 | |
| 57 | bool ProcessingComponent::is_component_enabled() const { |
| 58 | return enabled_; |
| 59 | } |
| 60 | |
| 61 | void* ProcessingComponent::handle(int index) const { |
| 62 | assert(index < num_handles_); |
| 63 | return handles_[index]; |
| 64 | } |
| 65 | |
| 66 | int ProcessingComponent::num_handles() const { |
| 67 | return num_handles_; |
| 68 | } |
| 69 | |
| 70 | int ProcessingComponent::Initialize() { |
| 71 | if (!enabled_) { |
| 72 | return apm_->kNoError; |
| 73 | } |
| 74 | |
| 75 | num_handles_ = num_handles_required(); |
| 76 | if (num_handles_ > static_cast<int>(handles_.size())) { |
| 77 | handles_.resize(num_handles_, NULL); |
| 78 | } |
| 79 | |
| 80 | assert(static_cast<int>(handles_.size()) >= num_handles_); |
| 81 | for (int i = 0; i < num_handles_; i++) { |
| 82 | if (handles_[i] == NULL) { |
| 83 | handles_[i] = CreateHandle(); |
| 84 | if (handles_[i] == NULL) { |
| 85 | return apm_->kCreationFailedError; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int err = InitializeHandle(handles_[i]); |
| 90 | if (err != apm_->kNoError) { |
| 91 | return GetHandleError(handles_[i]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | initialized_ = true; |
| 96 | return Configure(); |
| 97 | } |
| 98 | |
| 99 | int ProcessingComponent::Configure() { |
| 100 | if (!initialized_) { |
| 101 | return apm_->kNoError; |
| 102 | } |
| 103 | |
| 104 | assert(static_cast<int>(handles_.size()) >= num_handles_); |
| 105 | for (int i = 0; i < num_handles_; i++) { |
| 106 | int err = ConfigureHandle(handles_[i]); |
| 107 | if (err != apm_->kNoError) { |
| 108 | return GetHandleError(handles_[i]); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return apm_->kNoError; |
| 113 | } |
| 114 | } // namespace webrtc |