1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef OBOE_OBOE_FLOW_GRAPH_H
18 #define OBOE_OBOE_FLOW_GRAPH_H
19 
20 #include <memory>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <flowgraph/ChannelCountConverter.h>
25 #include <flowgraph/MonoToMultiConverter.h>
26 #include <flowgraph/MultiToMonoConverter.h>
27 #include <flowgraph/SampleRateConverter.h>
28 #include <oboe/Definitions.h>
29 #include "AudioSourceCaller.h"
30 #include "FixedBlockWriter.h"
31 
32 namespace oboe {
33 
34 class AudioStream;
35 class AudioSourceCaller;
36 
37 /**
38  * Convert PCM channels, format and sample rate for optimal latency.
39  */
40 class DataConversionFlowGraph : public FixedBlockProcessor {
41 public:
42 
DataConversionFlowGraph()43     DataConversionFlowGraph()
44     : mBlockWriter(*this) {}
45 
46     void setSource(const void *buffer, int32_t numFrames);
47 
48     /** Connect several modules together to convert from source to sink.
49      * This should only be called once for each instance.
50      *
51      * @param sourceFormat
52      * @param sourceChannelCount
53      * @param sinkFormat
54      * @param sinkChannelCount
55      * @return
56      */
57     oboe::Result configure(oboe::AudioStream *sourceStream, oboe::AudioStream *sinkStream);
58 
59     int32_t read(void *buffer, int32_t numFrames, int64_t timeoutNanos);
60 
61     int32_t write(void *buffer, int32_t numFrames);
62 
63     int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
64 
getDataCallbackResult()65     DataCallbackResult getDataCallbackResult() {
66         return mCallbackResult;
67     }
68 
69 private:
70     std::unique_ptr<flowgraph::FlowGraphSourceBuffered>    mSource;
71     std::unique_ptr<AudioSourceCaller>                 mSourceCaller;
72     std::unique_ptr<flowgraph::MonoToMultiConverter>   mMonoToMultiConverter;
73     std::unique_ptr<flowgraph::MultiToMonoConverter>   mMultiToMonoConverter;
74     std::unique_ptr<flowgraph::ChannelCountConverter>  mChannelCountConverter;
75     std::unique_ptr<resampler::MultiChannelResampler>  mResampler;
76     std::unique_ptr<flowgraph::SampleRateConverter>    mRateConverter;
77     std::unique_ptr<flowgraph::FlowGraphSink>              mSink;
78 
79     FixedBlockWriter                                   mBlockWriter;
80     DataCallbackResult                                 mCallbackResult = DataCallbackResult::Continue;
81     AudioStream                                       *mFilterStream = nullptr;
82     std::unique_ptr<uint8_t[]>                         mAppBuffer;
83 };
84 
85 }
86 #endif //OBOE_OBOE_FLOW_GRAPH_H
87