blob: 23bf22570b1e5bee698f1d2231bc371df06e46d1 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
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.org9fb16132013-05-28 08:11:59 +000011#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
pbos@webrtc.org3f45c2e2013-08-05 16:22:53 +000013#include <assert.h>
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000014
pbos@webrtc.org9fb16132013-05-28 08:11:59 +000015#include "webrtc/modules/audio_processing/audio_processing_impl.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000016
17namespace webrtc {
18
pbos@webrtc.org24add922013-08-02 11:44:11 +000019ProcessingComponent::ProcessingComponent() {}
20
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000021ProcessingComponent::ProcessingComponent(const AudioProcessingImpl* apm)
22 : apm_(apm),
23 initialized_(false),
24 enabled_(false),
25 num_handles_(0) {}
26
27ProcessingComponent::~ProcessingComponent() {
28 assert(initialized_ == false);
29}
30
31int 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
41int 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
57bool ProcessingComponent::is_component_enabled() const {
58 return enabled_;
59}
60
61void* ProcessingComponent::handle(int index) const {
62 assert(index < num_handles_);
63 return handles_[index];
64}
65
66int ProcessingComponent::num_handles() const {
67 return num_handles_;
68}
69
70int 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
99int 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