• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 AVCODEC_VENC_DEMO_H
17 #define AVCODEC_VENC_DEMO_H
18 
19 #include <atomic>
20 #include <thread>
21 #include <queue>
22 #include <string>
23 #include "avcodec_video_encoder.h"
24 #include "nocopyable.h"
25 
26 namespace OHOS {
27 namespace Media {
28 class VEncSignal {
29 public:
30     std::mutex mutex_;
31     std::condition_variable cond_;
32     std::queue<uint32_t> bufferQueue_;
33 };
34 
35 class VEncDemoCallback : public AVCodecCallback, public NoCopyable {
36 public:
37     explicit VEncDemoCallback(std::shared_ptr<VEncSignal> signal);
38     virtual ~VEncDemoCallback() = default;
39 
40     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
41     void OnOutputFormatChanged(const Format &format) override;
42     void OnInputBufferAvailable(uint32_t index) override;
43     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
44 
45 private:
46     std::shared_ptr<VEncSignal> signal_;
47 };
48 
49 class VEncDemo : public NoCopyable {
50 public:
51     VEncDemo() = default;
52     virtual ~VEncDemo() = default;
53 
54     void RunCase(bool enableProp);
55     void GenerateData(uint32_t count, uint32_t fps);
56 
57 private:
58     int32_t CreateVenc();
59     int32_t Configure(const Format &format);
60     int32_t Prepare();
61     int32_t Start();
62     int32_t SetParameter(int32_t suspend, int32_t maxFps, int32_t repeatMs);
63     int32_t Stop();
64     int32_t Flush();
65     int32_t Reset();
66     int32_t Release();
67     sptr<Surface> GetVideoSurface();
68     void LoopFunc();
69 
70     std::atomic<bool> isRunning_ = false;
71     std::unique_ptr<std::thread> readLoop_;
72     std::shared_ptr<AVCodecVideoEncoder> venc_;
73     std::shared_ptr<VEncSignal> signal_;
74     std::shared_ptr<VEncDemoCallback> cb_;
75     sptr<Surface> surface_ = nullptr;
76     int64_t timestampNs_ = 0;
77 };
78 } // namespace Media
79 } // namespace OHOS
80 #endif // AVCODEC_VENC_DEMO_H
81