• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VIDEO_CAPTURE_PLUGIN_H
17 #define HISTREAMER_VIDEO_CAPTURE_PLUGIN_H
18 
19 #if !defined(OHOS_LITE) && defined(RECORDER_SUPPORT) && defined(VIDEO_SUPPORT)
20 
21 #include <atomic>
22 #include "foundation/osal/thread/condition_variable.h"
23 #include "foundation/osal/thread/mutex.h"
24 #include "plugin/common/plugin_types.h"
25 #include "plugin/interface/source_plugin.h"
26 #include "refbase.h"
27 #include "surface/surface.h"
28 
29 namespace OHOS {
30 namespace Media {
31 namespace Plugin {
32 namespace VideoCapture {
33 class VideoCaptureAllocator : public Plugin::Allocator {
34 public:
35     VideoCaptureAllocator() = default;
36     ~VideoCaptureAllocator() override = default;
37 
38     void* Alloc(size_t size) override;
39     void Free(void* ptr) override; // NOLINT: void*
40 };
41 class VideoCapturePlugin : public Plugin::SourcePlugin {
42 public:
43     explicit VideoCapturePlugin(std::string name);
44     ~VideoCapturePlugin() override;
45 
46     Plugin::Status Init() override;
47     Plugin::Status Deinit() override;
48     Plugin::Status Prepare() override;
49     Plugin::Status Reset() override;
50     Plugin::Status Start() override;
51     Plugin::Status Stop() override;
52     bool IsParameterSupported(Plugin::Tag tag) override;
53     Plugin::Status GetParameter(Plugin::Tag tag, Plugin::ValueType& value) override;
54     Plugin::Status SetParameter(Plugin::Tag tag, const Plugin::ValueType& value) override;
55     std::shared_ptr<Plugin::Allocator> GetAllocator() override;
56     Plugin::Status SetCallback(Plugin::Callback* cb) override;
57     Plugin::Status SetSource(std::shared_ptr<Plugin::MediaSource> source) override;
58     Plugin::Status Read(std::shared_ptr<Plugin::Buffer>& buffer, size_t expectedLen) override;
59     Plugin::Status GetSize(size_t& size) override;
60     bool IsSeekable() override;
61     Plugin::Status SeekTo(uint64_t offset) override;
62 
63 protected:
64     class SurfaceConsumerListener : public IBufferConsumerListener {
65     public:
SurfaceConsumerListener(VideoCapturePlugin & owner)66         explicit SurfaceConsumerListener(VideoCapturePlugin &owner) : owner_(owner) {}
67         ~SurfaceConsumerListener() = default;
68         void OnBufferAvailable() override;
69     private:
70         VideoCapturePlugin &owner_;
71     };
72 
73 private:
74     void ConfigSurfaceConsumer();
75     Status AcquireSurfaceBuffer();
76     void OnBufferAvailable();
77 
78     OHOS::Media::OSAL::Mutex mutex_ {};
79     OSAL::ConditionVariable readCond_;
80     std::shared_ptr<VideoCaptureAllocator> mAllocator_ {nullptr};
81     sptr<Surface> surfaceConsumer_ {nullptr};
82     sptr<Surface> surfaceProducer_ {nullptr};
83     std::atomic<bool> isStop_ {false};
84     uint32_t width_ {0};
85     uint32_t height_ {0};
86     uint32_t bufferCnt_ {0};
87     uint64_t curTimestampNs_ {0};
88     uint64_t stopTimestampNs_ {0};
89     uint64_t totalPauseTimeNs_ {0};
90 
91     sptr<SurfaceBuffer> surfaceBuffer_ {nullptr};
92     int32_t fence_ {-1};
93     int64_t timestamp_ {0};
94     int32_t bufferSize_ {0};
95     Rect damage_;
96     int32_t isKeyFrame_ {0};
97 };
98 } // namespace VideoCapture
99 } // namespace Plugin
100 } // namespace Media
101 } // namespace OHOS
102 #endif
103 #endif // HISTREAMER_VIDEO_CAPTURE_PLUGIN_H
104 
105