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 #include "rtp_pack_impl.h"
17 #include "rtp_codec_g711.h"
18 #include "rtp_codec_h264.h"
19 #include "rtp_codec_ts.h"
20 namespace OHOS {
21 namespace Sharing {
RtpPackImpl(uint32_t ssrc, size_t mtuSize, uint32_t sampleRate, uint8_t pt, RtpPayloadStream ps, uint32_t channels)22 RtpPackImpl::RtpPackImpl(uint32_t ssrc, size_t mtuSize, uint32_t sampleRate, uint8_t pt, RtpPayloadStream ps,
23                          uint32_t channels)
24     : pt_(pt), ssrc_(ssrc), channels_(channels), sampleRate_(sampleRate), mtuSize_(mtuSize), ps_(ps)
25 {
26     InitEncoder();
27 }
28 
InputFrame(const Frame::Ptr &frame)29 void RtpPackImpl::InputFrame(const Frame::Ptr &frame)
30 {
31     if (rtpEncoder_) {
32         rtpEncoder_->InputFrame(frame);
33     }
34 }
35 
SetOnRtpPack(const OnRtpPack &cb)36 void RtpPackImpl::SetOnRtpPack(const OnRtpPack &cb)
37 {
38     onRtpPack_ = cb;
39 }
40 
InitEncoder()41 void RtpPackImpl::InitEncoder()
42 {
43     switch (ps_) {
44         case RtpPayloadStream::H264:
45             rtpEncoder_ = std::make_shared<RtpEncoderH264>(ssrc_, mtuSize_, sampleRate_, pt_, seq_);
46             break;
47         case RtpPayloadStream::MPEG4_GENERIC:
48             break;
49         case RtpPayloadStream::PCMA: // fall-through
50         case RtpPayloadStream::PCMU:
51             rtpEncoder_ = std::make_shared<RtpEncoderG711>(ssrc_, mtuSize_, sampleRate_, pt_, channels_, seq_);
52             break;
53         case RtpPayloadStream::MPEG2_TS:
54             rtpEncoder_ = std::make_shared<RtpEncoderTs>(ssrc_, mtuSize_, sampleRate_, pt_, seq_);
55             break;
56         case RtpPayloadStream::MPEG2_PS:
57             break;
58         default:
59             // todo log
60             break;
61     }
62     if (rtpEncoder_) {
63         rtpEncoder_->SetOnRtpPack([this](const RtpPacket::Ptr &rtp) { onRtpPack_(rtp); });
64     }
65 }
66 } // namespace Sharing
67 } // namespace OHOS
68