aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/processing_component.cc
blob: 7abd8e2100547bd02c5f92dce005b6286d09bbdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/audio_processing/processing_component.h"

#include <assert.h>

#include "webrtc/modules/audio_processing/include/audio_processing.h"

namespace webrtc {

ProcessingComponent::ProcessingComponent()
  : initialized_(false),
    enabled_(false),
    num_handles_(0) {}

ProcessingComponent::~ProcessingComponent() {
  assert(initialized_ == false);
}

int ProcessingComponent::Destroy() {
  while (!handles_.empty()) {
    DestroyHandle(handles_.back());
    handles_.pop_back();
  }
  initialized_ = false;

  return AudioProcessing::kNoError;
}

int ProcessingComponent::EnableComponent(bool enable) {
  if (enable && !enabled_) {
    enabled_ = enable; // Must be set before Initialize() is called.

    int err = Initialize();
    if (err != AudioProcessing::kNoError) {
      enabled_ = false;
      return err;
    }
  } else {
    enabled_ = enable;
  }

  return AudioProcessing::kNoError;
}

bool ProcessingComponent::is_component_enabled() const {
  return enabled_;
}

void* ProcessingComponent::handle(size_t index) const {
  assert(index < num_handles_);
  return handles_[index];
}

size_t ProcessingComponent::num_handles() const {
  return num_handles_;
}

int ProcessingComponent::Initialize() {
  if (!enabled_) {
    return AudioProcessing::kNoError;
  }

  num_handles_ = num_handles_required();
  if (num_handles_ > handles_.size()) {
    handles_.resize(num_handles_, NULL);
  }

  assert(handles_.size() >= num_handles_);
  for (size_t i = 0; i < num_handles_; i++) {
    if (handles_[i] == NULL) {
      handles_[i] = CreateHandle();
      if (handles_[i] == NULL) {
        return AudioProcessing::kCreationFailedError;
      }
    }

    int err = InitializeHandle(handles_[i]);
    if (err != AudioProcessing::kNoError) {
      return GetHandleError(handles_[i]);
    }
  }

  initialized_ = true;
  return Configure();
}

int ProcessingComponent::Configure() {
  if (!initialized_) {
    return AudioProcessing::kNoError;
  }

  assert(handles_.size() >= num_handles_);
  for (size_t i = 0; i < num_handles_; i++) {
    int err = ConfigureHandle(handles_[i]);
    if (err != AudioProcessing::kNoError) {
      return GetHandleError(handles_[i]);
    }
  }

  return AudioProcessing::kNoError;
}
}  // namespace webrtc