1 /*
2 * Copyright (c) 2021 Huawei Device 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 "stream_depacketizer.h"
17
18 #include "common_inner.h"
19 #include "i_stream.h"
20
21 namespace Communication {
22 namespace SoftBus {
DepacketizeHeader(const char * header)23 void StreamDepacketizer::DepacketizeHeader(const char *header)
24 {
25 if (streamType_ == COMMON_VIDEO_STREAM || streamType_ == COMMON_AUDIO_STREAM) {
26 const char *ptr = header;
27 header_.Depacketize(ptr);
28
29 TRANS_LOGD(TRANS_STREAM,
30 "streamPktHeader version=%{public}d, subVersion=%{public}d, extFlag=%{public}d, streamType=%{public}d, "
31 "marker=%{public}d, flag=%{public}d, streamId=%{public}d(%{public}x), timestamp=%{public}u(%{public}x), "
32 "dataLen=%{public}u(%{public}x), seqNum=%{public}d(%{public}x), subSeqNum=%{public}d(%{public}x)",
33 header_.GetVersion(), header_.GetSubVersion(), header_.GetExtFlag(), header_.GetStreamType(),
34 header_.GetMarker(), header_.GetFlag(), header_.GetStreamId(), header_.GetStreamId(),
35 header_.GetTimestamp(), header_.GetTimestamp(), header_.GetDataLen(), header_.GetDataLen(),
36 header_.GetSeqNum(), header_.GetSeqNum(), header_.GetSubSeqNum(), header_.GetSubSeqNum());
37 }
38 }
39
DepacketizeBuffer(char * buffer,uint32_t bufferSize)40 void StreamDepacketizer::DepacketizeBuffer(char *buffer, uint32_t bufferSize)
41 {
42 char *ptr = buffer;
43 uint32_t tlvTotalLen = 0;
44 if (header_.GetExtFlag() != 0) {
45 tlvs_.Depacketize(ptr, bufferSize);
46 TRANS_LOGD(TRANS_STREAM, "TLV version=%{public}d, num=%{public}d, extLen=%{public}zd, checksum=%{public}u",
47 tlvs_.GetVersion(), tlvs_.GetTlvNums(), tlvs_.GetExtLen(), tlvs_.GetCheckSum());
48 if (tlvs_.GetCheckSum() == 0) {
49 return;
50 }
51 tlvTotalLen = tlvs_.GetCheckSum() + sizeof(tlvs_.GetCheckSum());
52 ptr += tlvTotalLen;
53 }
54
55 dataLength_ = static_cast<int>(header_.GetDataLen() - tlvTotalLen);
56 if (dataLength_ <= 0 || dataLength_ > MAX_STREAM_LEN) {
57 TRANS_LOGE(
58 TRANS_STREAM,
59 "error. headerDataLen=%{public}u, tlvTotalLen=%{public}u", header_.GetDataLen(), tlvTotalLen);
60 return;
61 }
62
63 int remain = static_cast<int>(bufferSize - (ptr - buffer));
64 if (remain < dataLength_) {
65 TRANS_LOGE(TRANS_STREAM, "Data out of bounds, remain=%{public}d, dataLen=%{public}d", remain, dataLength_);
66 return;
67 }
68
69 data_ = std::make_unique<char[]>(dataLength_);
70 auto ret = memcpy_s(data_.get(), dataLength_, ptr, dataLength_);
71 if (ret != 0) {
72 TRANS_LOGE(TRANS_STREAM, "Failed to memcpy data_, ret=%{public}d", ret);
73 dataLength_ = -1;
74 }
75 }
76 } // namespace SoftBus
77 } // namespace Communication
78