• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WFD_RTP_PRODUCER_H
17 #define OHOS_SHARING_WFD_RTP_PRODUCER_H
18 
19 #include <atomic>
20 #include <string>
21 #include "buffer_dispatcher.h"
22 #include "codec/include/codec_factory.h"
23 #include "common/event_comm.h"
24 #include "event/handle_event_base.h"
25 #include "frame/frame.h"
26 #include "mediachannel/base_producer.h"
27 #include "mediachannel/media_channel_def.h"
28 #include "network/interfaces/iclient_callback.h"
29 #include "network/interfaces/inetwork_session.h"
30 #include "network/network_factory.h"
31 #include "protocol/rtcp/include/rtcp_context.h"
32 #include "protocol/rtp/include/rtp_def.h"
33 #include "protocol/rtp/include/rtp_factory.h"
34 #include "protocol/rtp/include/rtp_pack.h"
35 #include "wfd_media_def.h"
36 
37 namespace OHOS {
38 namespace Sharing {
39 
40 class WfdRtpProducer : public BaseProducer,
41                        public std::enable_shared_from_this<WfdRtpProducer> {
42 public:
43     using Ptr = std::shared_ptr<WfdRtpProducer>;
44 
45     class UdpClient : public IClientCallback,
46                       public std::enable_shared_from_this<UdpClient> {
47     public:
48         using Ptr = std::shared_ptr<UdpClient>;
49 
50         explicit UdpClient(bool rtcp);
51         ~UdpClient();
52 
53         void OnClientClose(int32_t fd) override;
54         void OnClientException(int32_t fd) override;
55         void OnClientWriteable(int32_t fd) override;
56         void OnClientConnect(bool isSuccess) override;
57         void OnClientReadData(int32_t fd, DataBuffer::Ptr buf) override;
58 
59         void Stop();
60         void Release();
61         void SetUdpDataListener(std::weak_ptr<WfdRtpProducer> udpDataListener);
62 
63         bool SendDataBuffer(const DataBuffer::Ptr &buf);
64         bool Connect(const std::string &peerIp, uint16_t peerPort, const std::string &localIp, uint16_t localPort);
65 
66     private:
67         bool rtcp_ = false;
68         std::weak_ptr<WfdRtpProducer> udpDataListener_;
69         NetworkFactory::ClientPtr networkClientPtr_ = nullptr;
70     };
71 
72     explicit WfdRtpProducer();
73     ~WfdRtpProducer();
74 
75     // impl BaseProducer
76     void StartDispatchThread() override;
77     void PublishOneFrame(const MediaData::Ptr &mediaData) override;
78     void UpdateOperation(ProsumerStatusMsg::Ptr &statusMsg) override;
79 
80     int32_t Release() override;
81     int32_t HandleEvent(SharingEvent &event) override;
82 
83     void OnRtcpReadData(int32_t fd, DataBuffer::Ptr buf);
84 
85 private:
86     bool ProducerInit();
87     bool SendDataBuffer(const DataBuffer::Ptr &buf, bool audio = true);
88 
89     int32_t Stop();
90     int32_t Connect();
91     int32_t InitUdpClients();
92     int32_t InitTsRtpPacker(uint32_t ssrc, size_t mtuSize = 1400, uint32_t sampleRate = 90000, uint8_t pt = 33,
93                             RtpPayloadStream ps = RtpPayloadStream::MPEG2_TS);
94 
95     void OnRtcpTimeOut();
96     void DispatchMediaData();
97     void HandleMediaInit(std::shared_ptr<WfdProducerEventMsg> msg);
98 
99 private:
100     std::atomic_bool dispatching_ = false;
101 
102     uint16_t localPort_ = MIN_PORT;
103     uint16_t primarySinkPort_ = MIN_PORT;
104 
105     uint32_t ssrc_ = 0x2000;
106     int32_t rtcpCheckInterval_ = 0;
107     std::atomic_uint32_t rtcpOvertimes_ = 0;
108 
109     std::string localIp_ = "127.0.0.1";
110     std::string primarySinkIp_ = "127.0.0.1";
111 
112     std::shared_ptr<UdpClient> tsUdpClient_ = nullptr;
113     std::shared_ptr<UdpClient> tsRtcpUdpClient_ = nullptr;
114     std::shared_ptr<std::thread> dispatchThread_ = nullptr;
115     std::shared_ptr<RtcpSenderContext> rtcpSendContext_ = nullptr;
116 
117     RtpPack::Ptr tsPacker_ = nullptr;
118 };
119 } // namespace Sharing
120 } // namespace OHOS
121 #endif // OHOS_SHARING_WFD_RTP_PRODUCER_H
122