• 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     bool exit_ = false;
52     int videoStreamIndex_ = -1;
53     int audioStreamIndex_ = -1;
54     uint8_t *avioCtxBuffer_ = nullptr;
55 
56     std::mutex queueMutex_;
57     std::queue<RtpPacket::Ptr> dataQueue_;
58     std::unique_ptr<std::thread> decodeThread_;
59 
60     AVIOContext *avioContext_ = nullptr;
61     AVFormatContext *avFormatContext_ = nullptr;
62 };
63 
64 class RtpEncoderTs : public RtpEncoder,
65                      public RtpMaker {
66 public:
67     using Ptr = std::shared_ptr<RtpEncoderTs>;
68 
69     void Release();
70 
71     RtpEncoderTs(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType, uint16_t seq = 0);
72     ~RtpEncoderTs();
73 
74     void InputFrame(const Frame::Ptr &frame) override;
75     void SetOnRtpPack(const OnRtpPack &cb) override;
76 
77 private:
78     void StartEncoding();
79     void RemoveFrameAfterMuxing();
80     int ReadFrame(AVPacket *packet);
81     void SaveFrame(Frame::Ptr frame);
82     static int WritePacket(void *opaque, uint8_t *buf, int buf_size);
83 
84 private:
85     bool exit_ = false;
86     uint8_t *avioCtxBuffer_ = nullptr;
87 
88     bool keyFrame_;
89     uint32_t timeStamp_;
90     FrameMerger merger_;
91 
92     std::mutex queueMutex_;
93     std::mutex cbLockMutex_;
94     std::queue<Frame::Ptr> dataQueue_;
95     std::unique_ptr<std::thread> encodeThread_;
96 
97     AVStream *videoStream = nullptr;
98     AVStream *audioStream = nullptr;
99     AVIOContext *avioContext_ = nullptr;
100     AVFormatContext *avFormatContext_ = nullptr;
101 };
102 } // namespace Sharing
103 } // namespace OHOS
104 #endif
105