1 /*
2  * Copyright (c) 2023-2023 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 FILTERS_SURFACE_ENCODER_FILTER_H
17 #define FILTERS_SURFACE_ENCODER_FILTER_H
18 
19 #include <cstring>
20 #include "filter/filter.h"
21 #include "surface.h"
22 #include "meta/meta.h"
23 #include "buffer/avbuffer.h"
24 #include "buffer/avallocator.h"
25 #include "buffer/avbuffer_queue.h"
26 #include "buffer/avbuffer_queue_producer.h"
27 #include "buffer/avbuffer_queue_consumer.h"
28 #include "common/status.h"
29 #include "avcodec_common.h"
30 
31 namespace OHOS {
32 namespace Media {
33 class SurfaceEncoderAdapter;
34 namespace Pipeline {
35 class SurfaceEncoderFilter : public Filter, public std::enable_shared_from_this<SurfaceEncoderFilter> {
36 public:
37     explicit SurfaceEncoderFilter(std::string name, FilterType type);
38     ~SurfaceEncoderFilter() override;
39     Status SetCodecFormat(const std::shared_ptr<Meta> &format);
40     void Init(const std::shared_ptr<EventReceiver> &receiver,
41         const std::shared_ptr<FilterCallback> &callback) override;
42     Status Configure(const std::shared_ptr<Meta> &parameter);
43     Status SetWatermark(std::shared_ptr<AVBuffer> &waterMarkBuffer);
44     Status SetInputSurface(sptr<Surface> surface);
45     Status SetTransCoderMode();
46     sptr<Surface> GetInputSurface();
47     Status DoPrepare() override;
48     Status DoStart() override;
49     Status DoPause() override;
50     Status DoResume() override;
51     Status Reset();
52     Status DoStop() override;
53     Status DoFlush() override;
54     Status DoRelease() override;
55     Status NotifyEos(int64_t pts);
56     void SetParameter(const std::shared_ptr<Meta> &parameter) override;
57     void GetParameter(std::shared_ptr<Meta> &parameter) override;
58     Status LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
59     Status UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
60     Status UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
61     FilterType GetFilterType();
62     void OnLinkedResult(const sptr<AVBufferQueueProducer> &outputBufferQueue, std::shared_ptr<Meta> &meta);
63     void OnUpdatedResult(std::shared_ptr<Meta> &meta);
64     void OnUnlinkedResult(std::shared_ptr<Meta> &meta);
65     void SetCallingInfo(int32_t appUid, int32_t appPid, const std::string &bundleName, uint64_t instanceId);
66     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
67     void OnReportKeyFramePts(std::string KeyFramePts);
68 
69 protected:
70     Status OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta,
71         const std::shared_ptr<FilterLinkCallback> &callback) override;
72     Status OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta,
73         const std::shared_ptr<FilterLinkCallback> &callback) override;
74     Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback) override;
75 
76 private:
77     std::string name_;
78     FilterType filterType_ = FilterType::FILTERTYPE_VENC;
79 
80     std::shared_ptr<EventReceiver> eventReceiver_;
81     std::shared_ptr<FilterCallback> filterCallback_;
82 
83     std::shared_ptr<FilterLinkCallback> onLinkedResultCallback_;
84 
85     std::shared_ptr<SurfaceEncoderAdapter> mediaCodec_;
86 
87     std::string codecMimeType_;
88     std::shared_ptr<Meta> configureParameter_;
89 
90     std::shared_ptr<Filter> nextFilter_;
91 
92     std::atomic<bool> isUpdateCodecNeeded_ = false;
93     sptr<Surface> surface_{nullptr};
94     std::string bundleName_;
95     uint64_t instanceId_{0};
96     int32_t appUid_ {0};
97     int32_t appPid_ {0};
98 };
99 } // namespace Pipeline
100 } // namespace MEDIA
101 } // namespace OHOS
102 #endif // FILTERS_SURFACE_ENCODER_FILTER_H
103