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_codec_g711.h"
17 #include "common/common_macro.h"
18 #include "common/media_log.h"
19
20 namespace OHOS {
21 namespace Sharing {
RtpDecoderG711()22 RtpDecoderG711::RtpDecoderG711()
23 {
24 frame_ = ObtainFrame();
25 }
26
InputRtp(const RtpPacket::Ptr & rtp)27 void RtpDecoderG711::InputRtp(const RtpPacket::Ptr &rtp)
28 {
29 RETURN_IF_NULL(rtp);
30 RETURN_IF_NULL(frame_);
31 auto payload_size = rtp->GetPayloadSize();
32 if (payload_size <= 0) {
33 return;
34 }
35 auto payload = rtp->GetPayload();
36 auto stamp = rtp->GetStampMS();
37 auto seq = rtp->GetSeq();
38
39 if (frame_->dts_ != stamp || frame_->Size() > maxFrameSize_) {
40 if (frame_->Size()) {
41 onFrame_(frame_);
42 }
43
44 frame_ = ObtainFrame();
45 frame_->dts_ = stamp;
46
47 dropFlag_ = false;
48 } else if (lastSeq_ != 0 && (uint16_t)(lastSeq_ + 1) != seq) {
49 MEDIA_LOGD("rtp lose: %{public}d -> %{public}d.", lastSeq_, seq);
50 dropFlag_ = true;
51 frame_->Clear();
52 }
53
54 if (!dropFlag_) {
55 frame_->Append((char *)payload, payload_size);
56 }
57
58 lastSeq_ = seq;
59 return;
60 }
61
SetOnFrame(const OnFrame & cb)62 void RtpDecoderG711::SetOnFrame(const OnFrame &cb)
63 {
64 onFrame_ = cb;
65 }
66
ObtainFrame()67 FrameImpl::Ptr RtpDecoderG711::ObtainFrame()
68 {
69 auto frame = FrameImpl::Create();
70 frame->codecId_ = CODEC_G711A;
71 return frame;
72 }
73
RtpEncoderG711(uint32_t ssrc,uint32_t mtuSize,uint32_t sampleRate,uint8_t payloadType,uint32_t channels,uint16_t seq)74 RtpEncoderG711::RtpEncoderG711(uint32_t ssrc, uint32_t mtuSize, uint32_t sampleRate, uint8_t payloadType,
75 uint32_t channels, uint16_t seq)
76 : RtpMaker(ssrc, mtuSize, payloadType, sampleRate, seq)
77 {
78 cacheFrame_ = FrameImpl::Create();
79 cacheFrame_->codecId_ = CODEC_G711A;
80 channels_ = channels;
81 }
82
InputFrame(const Frame::Ptr & frame)83 void RtpEncoderG711::InputFrame(const Frame::Ptr &frame)
84 {
85 RETURN_IF_NULL(frame);
86 RETURN_IF_NULL(cacheFrame_);
87 MEDIA_LOGD("rtpEncoderG711::InputFrame enter.");
88 auto dur = (cacheFrame_->Size() - cacheFrame_->PrefixSize()) / (8 * channels_);
89 auto next_pts = cacheFrame_->Pts() + dur;
90 if (next_pts == 0) {
91 cacheFrame_->pts_ = frame->Pts();
92 } else {
93 if ((next_pts + 20) < frame->Pts()) { // 20:interval ms
94 cacheFrame_->pts_ = frame->Pts() - dur;
95 }
96 }
97
98 cacheFrame_->Append(frame->Data() + frame->PrefixSize(), frame->Size() - frame->PrefixSize());
99
100 auto stamp = cacheFrame_->Pts();
101 auto ptr = cacheFrame_->Data() + cacheFrame_->PrefixSize();
102 auto len = cacheFrame_->Size() - cacheFrame_->PrefixSize();
103 auto rtpPack = MakeRtp(ptr, len, false, stamp);
104 if (onRtpPack_) {
105 MEDIA_LOGD("rtpEncoderG711::InputFrame onRtpPack data %{public}p size: %{public}d.", ptr, (int32_t)len);
106 onRtpPack_(rtpPack);
107 }
108
109 cacheFrame_->Clear();
110 cacheFrame_->pts_ += 46; // 371 / 8k = 46.375ms
111 }
112
SetOnRtpPack(const OnRtpPack & cb)113 void RtpEncoderG711::SetOnRtpPack(const OnRtpPack &cb)
114 {
115 onRtpPack_ = cb;
116 }
117 } // namespace Sharing
118 } // namespace OHOS