• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_TS_RTP_CODEC_H
17 #define OHOS_SHARING_TS_RTP_CODEC_H
18 
19 #include <memory>
20 #include <queue>
21 #include <thread>
22 #include "frame/frame.h"
23 #include "frame/frame_merger.h"
24 #include "rtp_codec.h"
25 #include "rtp_maker.h"
26 extern "C" {
27 #include <libavformat/avformat.h>
28 }
29 
30 namespace OHOS {
31 namespace Sharing {
32 class RtpDecoderTs : public RtpDecoder {
33 public:
34     using Ptr = std::shared_ptr<RtpDecoderTs>;
35 
36     RtpDecoderTs();
37     ~RtpDecoderTs();
38 
39     void Release();
40 
41     void InputRtp(const RtpPacket::Ptr &rtp) override;
42     void SetOnFrame(const OnFrame &cb) override;
43 
44 private:
45     void StartDecoding();
46     int ReadPacket(uint8_t *buf, int buf_size);
47 
48     static int StaticReadPacket(void *opaque, uint8_t *buf, int buf_size);
49 
50 private:
51     constexpr static int64_t US_PER_SEC = 1000 * 1000;
52 
53     bool exit_ = false;
54     int videoStreamIndex_ = -1;
55     int audioStreamIndex_ = -1;
56 
57     std::mutex queueMutex_;
58     std::queue<RtpPacket::Ptr> dataQueue_;
59     std::unique_ptr<std::thread> decodeThread_;
60 
61     AVIOContext *avioContext_ = nullptr;
62     AVFormatContext *avFormatContext_ = nullptr;
63 };
64 
65 class RtpEncoderTs : public RtpEncoder,
66                      public RtpMaker {
67 public:
68     using Ptr = std::shared_ptr<RtpEncoderTs>;
69 
70     void Release();
71 
72     RtpEncoderTs(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType, uint16_t seq = 0);
73     ~RtpEncoderTs();
74 
75     void InputFrame(const Frame::Ptr &frame) override;
76     void SetOnRtpPack(const OnRtpPack &cb) override;
77 
78 private:
79     void StartEncoding();
80     void RemoveFrameAfterMuxing();
81     Frame::Ptr ReadFrame(AVPacket *packet);
82     void SaveFrame(Frame::Ptr frame);
83     static int WritePacket(void *opaque, uint8_t *buf, int buf_size);
84 
85 private:
86     bool exit_ = false;
87     uint8_t *avioCtxBuffer_ = nullptr;
88 
89     bool keyFrame_ = false;
90     uint32_t timeStamp_ = 0;
91     FrameMerger merger_;
92 
93     std::mutex queueMutex_;
94     std::mutex cbLockMutex_;
95     std::queue<Frame::Ptr> dataQueue_;
96     std::unique_ptr<std::thread> encodeThread_;
97 
98     AVCodecID audioCodeId_ = AV_CODEC_ID_NONE;
99     AVStream *videoStream = nullptr;
100     AVStream *audioStream = nullptr;
101     AVIOContext *avioContext_ = nullptr;
102     AVFormatContext *avFormatContext_ = nullptr;
103 };
104 } // namespace Sharing
105 } // namespace OHOS
106 #endif
107