Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chromecast/media/cma/backend/alsa/post_processing_pipeline.cc

Issue 2771143002: Implement runtime audio post-processing pipeline. See go/cast_audio.json (Closed)
Patch Set: Remove explicit dlsym include. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromecast/media/cma/backend/alsa/post_processing_pipeline.h"
6
7 #include <string>
8
9 #include "base/files/file_path.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/scoped_native_library.h"
12 #include "base/values.h"
13 #include "chromecast/base/serializers.h"
14 #include "chromecast/public/media/audio_post_processor_shlib.h"
15
16 namespace chromecast {
17 namespace media {
18
19 namespace {
20
21 const int kNoSampleRate = -1;
22 const char kSoCreateFunction[] = "AudioPostProcessorShlib_Create";
23
24 } // namespace
25
26 using CreatePostProcessor = AudioPostProcessor* (*)(const std::string&, int);
27
28 PostProcessingPipeline::PostProcessingPipeline(
29 const base::ListValue* filter_description_list,
30 int channels)
31 : sample_rate_(kNoSampleRate) {
32 if (!filter_description_list) {
33 return; // Warning logged.
34 }
35 for (size_t i = 0; i < filter_description_list->GetSize(); ++i) {
36 const base::DictionaryValue* processor_description_dict;
37 CHECK(
38 filter_description_list->GetDictionary(i, &processor_description_dict));
39 std::string library_path;
40 CHECK(processor_description_dict->GetString("processor", &library_path));
41 if (library_path == "null" || library_path == "none") {
42 continue;
43 }
44 const base::Value* processor_config_val;
45 CHECK(processor_description_dict->Get("config", &processor_config_val));
46 CHECK(processor_config_val->is_dict() || processor_config_val->is_string());
47 auto processor_config_string = SerializeToJson(*processor_config_val);
48
49 LOG(INFO) << "Creating an instance of " << library_path << "("
50 << *processor_config_string << ")";
51 if (libraries_.find(library_path) == libraries_.end()) {
52 libraries_[library_path] = base::MakeUnique<base::ScopedNativeLibrary>(
53 base::FilePath(library_path));
54 }
55 auto& library = libraries_[library_path];
56 CHECK(library->is_valid())
57 << "Could not open post processing library " << library_path;
58 CreatePostProcessor create = reinterpret_cast<CreatePostProcessor>(
59 library->GetFunctionPointer(kSoCreateFunction));
60
61 CHECK(create) << "Could not find " << kSoCreateFunction << "() in "
62 << library_path;
63 processors_.push_back(
64 base::WrapUnique(create(*processor_config_string, channels)));
65 }
66 }
67
68 PostProcessingPipeline::~PostProcessingPipeline() = default;
69
70 int PostProcessingPipeline::ProcessFrames(uint8_t* data,
71 int num_frames,
72 float current_volume,
73 bool is_silence) {
74 DCHECK_NE(sample_rate_, kNoSampleRate);
75 if (is_silence) {
76 if (!IsRinging()) {
77 return total_delay_frames_; // Output will be silence.
78 }
79 silence_frames_processed_ += num_frames;
80 } else {
81 silence_frames_processed_ = 0;
82 }
83
84 total_delay_frames_ = 0;
85 for (auto& processor : processors_) {
86 total_delay_frames_ +=
87 processor->ProcessFrames(data, num_frames, current_volume);
88 }
89 return total_delay_frames_;
90 }
91
92 bool PostProcessingPipeline::SetSampleRate(int sample_rate) {
93 sample_rate_ = sample_rate;
94 bool result = true;
95 for (auto& processor : processors_) {
96 result &= processor->SetSampleRate(sample_rate_);
97 }
98 ringing_time_in_frames_ = GetRingingTimeInFrames();
99 silence_frames_processed_ = 0;
100 return result;
101 }
102
103 bool PostProcessingPipeline::IsRinging() {
104 return silence_frames_processed_ < ringing_time_in_frames_;
105 }
106
107 int PostProcessingPipeline::GetRingingTimeInFrames() {
108 int memory_frames = 0;
109 for (auto& processor : processors_) {
110 memory_frames += processor->GetRingingTimeInFrames();
111 }
112 return memory_frames;
113 }
114
115 } // namespace media
116 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698