• 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_H264_RTP_CODEC_H
17 #define OHOS_SHARING_H264_RTP_CODEC_H
18 
19 #include "frame/frame.h"
20 #include "frame/h264_frame.h"
21 #include "rtp_codec.h"
22 #include "rtp_maker.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 class RtpDecoderH264 : public RtpDecoder {
27 public:
28     using Ptr = std::shared_ptr<RtpDecoderH264>;
29 
30     RtpDecoderH264();
31     ~RtpDecoderH264();
32 
33     void InputRtp(const RtpPacket::Ptr &rtp) override;
34     void SetOnFrame(const OnFrame &cb) override;
35 
36 private:
37     H264Frame::Ptr ObtainFrame();
38 
39     bool UnpackStapA(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp);
40     bool UnpackSingle(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp);
41     bool UnpackFuA(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp, uint16_t seq);
42 
43     void OutputFrame(const RtpPacket::Ptr &rtp, const H264Frame::Ptr &frame);
44 
45 private:
46     bool fuDropped_ = true;
47     bool gopDropped_ = false;
48 
49     uint16_t lastSeq_ = 0;
50 
51     H264Frame::Ptr frame_ = nullptr;
52 };
53 
54 class RtpEncoderH264 : public RtpEncoder,
55                        public RtpMaker {
56 public:
57     using Ptr = std::shared_ptr<RtpEncoderH264>;
58 
59     RtpEncoderH264(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType, uint16_t seq = 0);
60     ~RtpEncoderH264();
61 
62     void SetOnRtpPack(const OnRtpPack &cb) override;
63     void InputFrame(const Frame::Ptr &frame) override;
64 
65 private:
66     void InsertConfigFrame(uint32_t pts);
67     bool InputFrame(const Frame::Ptr &frame, bool isMark);
68     void PackRtp(const uint8_t *data, size_t len, uint32_t pts, bool isMark, bool gopPos);
69     void PackRtpFu(const uint8_t *data, size_t len, uint32_t pts, bool isMark, bool gopPos);
70     void PackSingle(const uint8_t *data, size_t len, uint32_t pts, bool isMark, bool gopPos);
71     void PackRtpStapA(const uint8_t *data, size_t len, uint32_t pts, bool isMark, bool gopPos);
72 
73 private:
74     Frame::Ptr sps_ = nullptr;
75     Frame::Ptr pps_ = nullptr;
76     Frame::Ptr lastFrame_ = nullptr;
77 };
78 } // namespace Sharing
79 } // namespace OHOS
80 #endif