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