1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HISTREAMER_PIPELINE_FILTER_AUDIO_ENCODER_H
17 #define HISTREAMER_PIPELINE_FILTER_AUDIO_ENCODER_H
18 
19 #ifdef RECORDER_SUPPORT
20 
21 #include "foundation/utils/ring_buffer.h"
22 #include "pipeline/filters/codec/codec_filter_base.h"
23 #include "plugin/common/plugin_tags.h"
24 
25 namespace OHOS {
26 namespace Media {
27 namespace Pipeline {
28 class AudioEncoderFilter : public CodecFilterBase {
29 public:
30     explicit AudioEncoderFilter(const std::string& name);
31     ~AudioEncoderFilter() override;
32 
33     virtual ErrorCode SetAudioEncoder(int32_t sourceId, std::shared_ptr<Plugin::Meta> encoderMeta);
34 
35     ErrorCode Start() override;
36 
37     ErrorCode Stop() override;
38 
39     bool Negotiate(const std::string& inPort,
40                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
41                    Plugin::Capability& negotiatedCap,
42                    const Plugin::Meta& upstreamParams,
43                    Plugin::Meta& downstreamParams) override;
44 
45     uint32_t CalculateBufferSize(const std::shared_ptr<const Plugin::Meta>& meta) override;
46 
47     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
48                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
49 
50     /**
51      *
52      * @param inPort
53      * @param buffer
54      * @param offset always ignore this parameter
55      * @return
56      */
57     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
58 
59 private:
60     ErrorCode ConfigureToStartPluginLocked(const std::shared_ptr<const Plugin::Meta>& meta) override;
61 
62     ErrorCode HandleFrame(const std::shared_ptr<AVBuffer>& buffer);
63 
64     ErrorCode FinishFrame();
65 
66     ErrorCode Release();
67 
68     void OnInputBufferDone(const std::shared_ptr<Plugin::Buffer>& input) override;
69 
70     void OnOutputBufferDone(const std::shared_ptr<Plugin::Buffer>& output) override;
71 
72 private:
73     std::shared_ptr<BufferPool<AVBuffer>> outBufferPool_ {};
74     Capability capNegWithDownstream_;
75     Capability capNegWithUpstream_;
76     size_t frameSize_ {0};
77     std::string mime_;
78     std::shared_ptr<Plugin::Meta> encoderMeta_ {nullptr};
79     std::unique_ptr<RingBuffer> rb_ {};
80     AVBufferPtr cacheBuffer_ {nullptr};
81 };
82 } // OHOS
83 } // Media
84 } // Pipeline
85 #endif
86 #endif // HISTREAMER_PIPELINE_FILTER_AUDIO_ENCODER_H
87