1e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent/*
2e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *
4e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  Use of this source code is governed by a BSD-style license
5e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  that can be found in the LICENSE file in the root of the source
6e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  tree. An additional intellectual property rights grant can be found
7e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  in the file PATENTS.  All contributing project authors may
8e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent *  be found in the AUTHORS file in the root of the source tree.
9e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent */
10e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
11e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "processing_component.h"
12e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
13e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <cassert>
14e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
15e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "audio_processing_impl.h"
16e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
17e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentnamespace webrtc {
18e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
19e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentProcessingComponent::ProcessingComponent(const AudioProcessingImpl* apm)
20e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  : apm_(apm),
21e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    initialized_(false),
22e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    enabled_(false),
23e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    num_handles_(0) {}
24e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
25e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentProcessingComponent::~ProcessingComponent() {
26e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  assert(initialized_ == false);
27e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
28e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
29e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentint ProcessingComponent::Destroy() {
30e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  while (!handles_.empty()) {
31e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    DestroyHandle(handles_.back());
32e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    handles_.pop_back();
33e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
34e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  initialized_ = false;
35e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
36e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return apm_->kNoError;
37e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
38e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
39e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentint ProcessingComponent::EnableComponent(bool enable) {
40e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  if (enable && !enabled_) {
41e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    enabled_ = enable; // Must be set before Initialize() is called.
42e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
43e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    int err = Initialize();
44e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if (err != apm_->kNoError) {
45e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      enabled_ = false;
46e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      return err;
47e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
48e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  } else {
49e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    enabled_ = enable;
50e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
51e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
52e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return apm_->kNoError;
53e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
54e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
55e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentbool ProcessingComponent::is_component_enabled() const {
56e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return enabled_;
57e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
58e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
59e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentvoid* ProcessingComponent::handle(int index) const {
60e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  assert(index < num_handles_);
61e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return handles_[index];
62e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
63e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
64e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentint ProcessingComponent::num_handles() const {
65e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return num_handles_;
66e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
67e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
68e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentint ProcessingComponent::Initialize() {
69e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  if (!enabled_) {
70e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return apm_->kNoError;
71e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
72e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
73e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  num_handles_ = num_handles_required();
74e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  if (num_handles_ > static_cast<int>(handles_.size())) {
75e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    handles_.resize(num_handles_, NULL);
76e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
77e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
78e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  assert(static_cast<int>(handles_.size()) >= num_handles_);
79e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  for (int i = 0; i < num_handles_; i++) {
80e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if (handles_[i] == NULL) {
81e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      handles_[i] = CreateHandle();
82e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      if (handles_[i] == NULL) {
83e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent        return apm_->kCreationFailedError;
84e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      }
85e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
86e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
87e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    int err = InitializeHandle(handles_[i]);
88e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if (err != apm_->kNoError) {
89e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      return GetHandleError(handles_[i]);
90e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
91e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
92e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
93e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  initialized_ = true;
94e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return Configure();
95e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
96e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
97e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentint ProcessingComponent::Configure() {
98e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  if (!initialized_) {
99e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    return apm_->kNoError;
100e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
101e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
102e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  assert(static_cast<int>(handles_.size()) >= num_handles_);
103e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  for (int i = 0; i < num_handles_; i++) {
104e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    int err = ConfigureHandle(handles_[i]);
105e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    if (err != apm_->kNoError) {
106e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent      return GetHandleError(handles_[i]);
107e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent    }
108e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  }
109e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent
110e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent  return apm_->kNoError;
111e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}
112e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent}  // namespace webrtc
113