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 #ifndef STREAM_COMMON_DATA_H
17 #define STREAM_COMMON_DATA_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <sys/types.h>
24 
25 #include "i_stream.h"
26 #include "stream_common.h"
27 
28 namespace Communication {
29 namespace SoftBus {
30 class StreamCommonData : public IStream {
31 public:
32     StreamCommonData() = default;
33     StreamCommonData(uint32_t streamId, uint16_t seq, const StreamFrameInfo& frameInfo);
34 
35     ~StreamCommonData() override = default;
36 
37     // 应用调用生成流数据。
38     int InitStreamData(std::unique_ptr<char[]> inputBuf, ssize_t bufSize, std::unique_ptr<char[]> inputExt,
39         ssize_t extSize);
40 
SetTimeStamp(uint32_t timestamp)41     void SetTimeStamp(uint32_t timestamp) override
42     {
43         static_cast<void>(timestamp);
44     }
45 
GetTimeStamp()46     virtual uint32_t GetTimeStamp() const override
47     {
48         return 0;
49     }
50 
GetBuffer()51     std::unique_ptr<char[]> GetBuffer() override
52     {
53         return std::unique_ptr<char[]>(streamData_.release());
54     }
55 
GetBufferLen()56     ssize_t GetBufferLen() const override
57     {
58         return streamLen_;
59     }
60 
GetExtBuffer()61     std::unique_ptr<char[]> GetExtBuffer() override
62     {
63         return std::unique_ptr<char[]>(extBuf_.release());
64     }
65 
GetExtBufferLen()66     ssize_t GetExtBufferLen() const override
67     {
68         return extBufLen_;
69     }
70 
GetSeqNum()71     int GetSeqNum() const override
72     {
73         return curSeqNum_;
74     }
75 
GetStreamId()76     uint32_t GetStreamId() const override
77     {
78         return curStreamId_;
79     }
80 
GetStreamFrameInfo()81     const StreamFrameInfo* GetStreamFrameInfo() const override
82     {
83         return &streamFrameInfo_;
84     }
85 
86 protected:
87     std::unique_ptr<char[]> streamData_ = nullptr;
88     ssize_t streamLen_ = 0;
89 
90     /*
91      * 额外buffer信息,随着帧一起传输到对端
92      */
93     std::unique_ptr<char[]> extBuf_ = nullptr;
94     ssize_t extBufLen_ = 0;
95 
96     uint16_t curSeqNum_ = 0;
97     uint32_t curStreamId_ = 0;
98 
99     StreamFrameInfo streamFrameInfo_;
100 };
101 } // namespace SoftBus
102 } // namespace Communication
103 
104 #endif