• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 VIDEO_CODEC_SAMPLE_RECODER_H
17 #define VIDEO_CODEC_SAMPLE_RECODER_H
18 
19 #include <bits/alltypes.h>
20 #include <mutex>
21 #include <memory>
22 #include <atomic>
23 #include <thread>
24 #include <unistd.h>
25 #include "video_encoder.h"
26 #include "audio_encoder.h"
27 #include "muxer.h"
28 #include "sample_info.h"
29 #include "audio_capturer.h"
30 
31 class Recorder {
32 public:
Recorder()33     Recorder(){};
34     ~Recorder();
35 
GetInstance()36     static Recorder &GetInstance()
37     {
38         static Recorder recorder;
39         return recorder;
40     }
41 
42     int32_t Init(SampleInfo &sampleInfo);
43     int32_t Start();
44     int32_t Stop();
45 
46 private:
47     void EncOutputThread();
48     void AudioEncInputThread();
49     void AudioEncOutputThread();
50     void Release();
51     void StartRelease();
52     int32_t WaitForDone();
53 
54     int32_t CreateAudioEncoder();
55     int32_t CreateVideoEncoder();
56 
57     std::unique_ptr<VideoEncoder> videoEncoder_ = nullptr;
58     std::unique_ptr<AudioEncoder> audioEncoder_ = nullptr;
59     std::unique_ptr<Muxer> muxer_ = nullptr;
60 
61     std::mutex mutex_;
62     std::atomic<bool> isStarted_{false};
63     int32_t isFirstFrame_ = true;
64     std::unique_ptr<std::thread> encOutputThread_ = nullptr;
65     std::unique_ptr<std::thread> audioEncInputThread_ = nullptr;
66     std::unique_ptr<std::thread> audioEncOutputThread_ = nullptr;
67     std::unique_ptr<std::thread> releaseThread_ = nullptr;
68     std::condition_variable doneCond_;
69     SampleInfo sampleInfo_;
70     CodecUserData *encContext_ = nullptr;
71     CodecUserData *audioEncContext_ = nullptr;
72 
73     std::unique_ptr<AudioCapturer> audioCapturer_ = nullptr;
74 };
75 
76 #endif // VIDEO_CODEC_SAMPLE_RECODER_H
77