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