1 /*
2 * Copyright (c) 2021-2022 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 "client_trans_stream.h"
17
18 #include "client_trans_udp_stream_interface.h"
19 #include "session.h"
20 #include "softbus_errcode.h"
21 #include "softbus_log.h"
22 #include "softbus_utils.h"
23 #include "trans_server_proxy.h"
24
25 static const UdpChannelMgrCb *g_udpChannelMgrCb = NULL;
26
RegisterStreamCb(const UdpChannelMgrCb * streamCb)27 void RegisterStreamCb(const UdpChannelMgrCb *streamCb)
28 {
29 if (streamCb == NULL || streamCb->OnUdpChannelOpened == NULL ||
30 streamCb->OnUdpChannelClosed == NULL || streamCb->OnStreamReceived == NULL) {
31 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel callback is invalid");
32 return;
33 }
34
35 g_udpChannelMgrCb = streamCb;
36 }
37
UnregisterStreamCb(void)38 void UnregisterStreamCb(void)
39 {
40 g_udpChannelMgrCb = NULL;
41 }
42
SetStreamChannelStatus(int32_t channelId,int32_t status)43 static void SetStreamChannelStatus(int32_t channelId, int32_t status)
44 {
45 if (g_udpChannelMgrCb == NULL) {
46 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel callback is null.");
47 return;
48 }
49
50 switch (status) {
51 case STREAM_CONNECTED:
52 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream connected.", channelId);
53 if (g_udpChannelMgrCb->OnUdpChannelOpened == NULL) {
54 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel callback on udp channel opened is null.");
55 return;
56 }
57 g_udpChannelMgrCb->OnUdpChannelOpened(channelId);
58 break;
59 case STREAM_CLOSED:
60 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream closed.", channelId);
61 break;
62 case STREAM_INIT:
63 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream init.", channelId);
64 break;
65 case STREAM_OPENING:
66 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream opening.", channelId);
67 break;
68 case STREAM_CONNECTING:
69 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream connecting.", channelId);
70 break;
71 case STREAM_CLOSING:
72 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "cId=%d dstream closing.", channelId);
73 break;
74 default:
75 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "cId=%d unsupport stream status=%d.", channelId, status);
76 break;
77 }
78 }
79
OnStreamReceived(int32_t channelId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)80 static void OnStreamReceived(int32_t channelId, const StreamData *data, const StreamData *ext,
81 const StreamFrameInfo *param)
82 {
83 if ((g_udpChannelMgrCb == NULL) || (g_udpChannelMgrCb->OnStreamReceived == NULL)) {
84 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel callback on stream received is null.");
85 return;
86 }
87
88 g_udpChannelMgrCb->OnStreamReceived(channelId, data, ext, param);
89 }
90
OnQosEvent(int channelId,int eventId,int tvCount,const QosTv * tvList)91 NO_SANITIZE("cfi") static void OnQosEvent(int channelId, int eventId, int tvCount, const QosTv *tvList)
92 {
93 if ((g_udpChannelMgrCb == NULL) || (g_udpChannelMgrCb->OnQosEvent == NULL)) {
94 return;
95 }
96 g_udpChannelMgrCb->OnQosEvent(channelId, eventId, tvCount, tvList);
97 }
98
OnFrameStats(int32_t channelId,const StreamSendStats * data)99 static void OnFrameStats(int32_t channelId, const StreamSendStats *data)
100 {
101 int32_t ret = ServerIpcStreamStats(channelId, CHANNEL_TYPE_UDP, data);
102 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "notify frame stats to server, channelId:%d", channelId);
103 if ((ret != SOFTBUS_OK) && (ret != SOFTBUS_NOT_IMPLEMENT)) {
104 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ipc to server fail, reason:%d", ret);
105 return;
106 }
107 }
108
OnRippleStats(int32_t channelId,const TrafficStats * data)109 static void OnRippleStats(int32_t channelId, const TrafficStats *data)
110 {
111 int32_t ret = ServerIpcRippleStats(channelId, CHANNEL_TYPE_UDP, data);
112 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "notify ripple stats to server, channelId:%d", channelId);
113 if ((ret != SOFTBUS_OK) && (ret != SOFTBUS_NOT_IMPLEMENT)) {
114 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ipc to server fail, reason:%d", ret);
115 return;
116 }
117 }
118
119 static IStreamListener g_streamCallcb = {
120 .OnStatusChange = SetStreamChannelStatus,
121 .OnStreamReceived = OnStreamReceived,
122 .OnQosEvent = OnQosEvent,
123 .OnFrameStats = OnFrameStats,
124 .OnRippleStats = OnRippleStats,
125 };
126
TransOnstreamChannelOpened(const ChannelInfo * channel,int32_t * streamPort)127 int32_t TransOnstreamChannelOpened(const ChannelInfo *channel, int32_t *streamPort)
128 {
129 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OnstreamChannelOpened enter.");
130 if (channel == NULL || streamPort == NULL) {
131 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s:invalid param.", __func__);
132 return SOFTBUS_INVALID_PARAM;
133 }
134 StreamType streamType = (StreamType)channel->streamType;
135 if (streamType != RAW_STREAM && streamType != COMMON_VIDEO_STREAM && streamType != COMMON_AUDIO_STREAM) {
136 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "stream type invalid. type = %d", channel->streamType);
137 return SOFTBUS_INVALID_PARAM;
138 }
139 if (channel->isServer) {
140 VtpStreamOpenParam p1 = {
141 "DSOFTBUS_STREAM",
142 channel->myIp,
143 NULL,
144 -1,
145 streamType,
146 (uint8_t*)channel->sessionKey,
147 channel->keyLen,
148 };
149
150 int32_t port = StartVtpStreamChannelServer(channel->channelId, &p1, &g_streamCallcb);
151 if (port <= 0) {
152 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "start stream channel as server failed.");
153 return SOFTBUS_TRANS_UDP_START_STREAM_SERVER_FAILED;
154 }
155 *streamPort = port;
156 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "stream server success, listen port = %d.", port);
157 } else {
158 VtpStreamOpenParam p1 = {
159 "DSOFTBUS_STREAM",
160 channel->myIp,
161 channel->peerIp,
162 channel->peerPort,
163 streamType,
164 (uint8_t *)channel->sessionKey,
165 channel->keyLen,
166 };
167
168 int ret = StartVtpStreamChannelClient(channel->channelId, &p1, &g_streamCallcb);
169 if (ret <= 0) {
170 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "start stream channel as client failed.ret:%d", ret);
171 return SOFTBUS_TRANS_UDP_START_STREAM_CLIENT_FAILED;
172 }
173 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "stream start client success.");
174 if ((g_udpChannelMgrCb == NULL) || (g_udpChannelMgrCb->OnUdpChannelOpened == NULL)) {
175 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel callback on udp channel opened is null.");
176 return SOFTBUS_ERR;
177 }
178 g_udpChannelMgrCb->OnUdpChannelOpened(channel->channelId);
179 }
180 return SOFTBUS_OK;
181 }
182
TransSendStream(int32_t channelId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)183 int32_t TransSendStream(int32_t channelId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
184 {
185 return SendVtpStream(channelId, data, ext, param);
186 }
187
TransCloseStreamChannel(int32_t channelId)188 int32_t TransCloseStreamChannel(int32_t channelId)
189 {
190 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close stream channel.[channelId = %d]", channelId);
191 if (CloseVtpStreamChannel(channelId, "DSOFTBUS_STREAM") != SOFTBUS_OK) {
192 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "close stream channel failed.");
193 return SOFTBUS_ERR;
194 }
195 return SOFTBUS_OK;
196 }
197