• 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_CONSUMER_H
17 #define OHOS_SHARING_WFD_RTP_CONSUMER_H
18 
19 #include <chrono>
20 #include "common/common_macro.h"
21 #include "common/object.h"
22 #include "mediachannel/base_consumer.h"
23 #include "mediachannel/buffer_dispatcher.h"
24 #include "network/interfaces/iclient_callback.h"
25 #include "network/network_factory.h"
26 #include "protocol/rtcp/include/rtcp_context.h"
27 #include "protocol/rtp/include/rtp_def.h"
28 #include "protocol/rtp/include/rtp_factory.h"
29 #include "protocol/rtp/include/rtp_unpack.h"
30 
31 namespace OHOS {
32 namespace Sharing {
33 
34 class WfdRtpConsumer final : public BaseConsumer,
35                              public IServerCallback,
36                              public std::enable_shared_from_this<WfdRtpConsumer> {
37 public:
38     WfdRtpConsumer();
39     ~WfdRtpConsumer();
40 
41     // impl BaseConsumer
42     void UpdateOperation(ProsumerStatusMsg::Ptr &statusMsg) override;
43 
44     // impl IServerCallback
OnServerClose(int32_t fd)45     void OnServerClose(int32_t fd) override {}
OnServerWriteable(int32_t fd)46     void OnServerWriteable(int32_t fd) override {}
OnServerException(int32_t fd)47     void OnServerException(int32_t fd) override {}
OnAccept(std::weak_ptr<INetworkSession> session)48     void OnAccept(std::weak_ptr<INetworkSession> session) override {}
49     void OnServerReadData(int32_t fd, DataBuffer::Ptr buf, INetworkSession::Ptr session = nullptr) override;
50 
51     int32_t Release() override;
52     int32_t HandleEvent(SharingEvent &event) override;
53 
54 private:
55     void HandleProsumerInitState(SharingEvent &event);
56 
57     void OnRtpUnpackNotify(int32_t errCode);
58     void OnRtpUnpackCallback(uint32_t ssrc, const Frame::Ptr &frame);
59 
60     bool Init();
61     bool Stop();
62     bool Start();
63     bool InitRtpUnpacker();
64     bool StartNetworkServer(uint16_t port, NetworkFactory::ServerPtr &server, int32_t &fd);
65 
66 private:
67     bool isFirstPacket_ = true;
68     bool isFirstKeyFrame_ = true;
69 
70     uint16_t port_ = 0;
71     int32_t frameNums_ = 1;
72     uint32_t contextId_ = 0;
73 
74     std::chrono::steady_clock::time_point gopInterval_;
75     std::pair<int32_t, NetworkFactory::ServerPtr> rtpServer_ = {0, nullptr};
76 
77     MediaType mediaTypePaused_ = MEDIA_TYPE_AV;
78 
79     RtpUnpack::Ptr rtpUnpacker_ = nullptr;
80 };
81 
82 } // namespace Sharing
83 } // namespace OHOS
84 #endif
85