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