xref: /OK3568_Linux_fs/external/rockit/mpi/example/common/test_comm_utils.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2020 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <time.h>
19 #include <cstring>
20 #include <stdlib.h>
21 #include <cerrno>
22 #include <unistd.h>
23 
24 #include "test_comm_utils.h"
25 #include "rk_mpi_cal.h"
26 #include "rk_mpi_mb.h"
27 #include "rk_mpi_sys.h"
28 #include "rk_debug.h"
29 
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif
34 #endif /* End of #ifdef __cplusplus */
35 static RKSocType gEnRkSocType;
36 
TEST_COMM_GetNowUs()37 RK_U64 TEST_COMM_GetNowUs() {
38     struct timespec time = {0, 0};
39     clock_gettime(CLOCK_MONOTONIC, &time);
40     return (RK_U64)time.tv_sec * 1000000 + (RK_U64)time.tv_nsec / 1000; /* microseconds */
41 }
42 
TEST_COMM_GetUriSchemeType(const char * uri)43 URI_SCHEME_TYPE TEST_COMM_GetUriSchemeType(const char* uri) {
44     URI_SCHEME_TYPE schemeType = RK_URI_SCHEME_LOCAL;
45     if ((RK_NULL == uri) || (strlen(uri) < 4)) {
46         return schemeType;
47     }
48 
49     if (!strncmp("http://", uri, 7) || !strncmp("https://", uri, 8)) {
50         schemeType = RK_URI_SCHEME_HTTP;
51     } else if (!strncmp("rtsp://", uri, 7) || !strncmp("rtmp://", uri, 7)) {
52         RK_LOGD("uri is with rtsp or rtmp scheme type");
53         schemeType = RK_URI_SCHEME_RTSP;
54     } else if (!strncmp("/data/smb/", uri, 10)) {
55         RK_LOGD("uri is with /data/smb scheme type");
56         schemeType = RK_URI_SCHEME_SAMBA;
57     } else if (!strncmp("/data/nfs/", uri, 10)) {
58         RK_LOGD("uri is with /data/nfs schemeType (signed as samba)");
59         schemeType = RK_URI_SCHEME_SAMBA;
60     } else if (strstr(uri, "m3u8")) {
61         RK_LOGD("uri is with m3u8 scheme type");
62         schemeType = RK_URI_SCHEME_HLS;
63     } else if (!strncmp("rtp:", uri, 4)) {
64         RK_LOGD("uri is with rtp scheme type");
65         schemeType = RK_URI_SCHEME_RTP;
66     } else if (!strncmp("udp:", uri, 4)) {
67         RK_LOGD("uri is with udp scheme type");
68         schemeType = RK_URI_SCHEME_UDP;
69     } else if (!strncmp("mms://", uri, 6)) {
70         RK_LOGD("uri is with mms scheme type");
71         schemeType = RK_URI_SCHEME_MMS;
72     } else if (!strncmp("mmsh://", uri, 7)) {
73         RK_LOGD("uri is with mmsh scheme type");
74         schemeType = RK_URI_SCHEME_MMSH;
75     } else if (!strncmp("mmst://", uri, 7)) {
76         RK_LOGD("uri is with mmst scheme type");
77         schemeType = RK_URI_SCHEME_MMST;
78     } else if (strstr(uri, "app_tts-cache")) {
79         RK_LOGD("uri is with tts scheme type");
80         schemeType = RK_URI_SCHEME_TTS;
81     }  else if (strstr(uri, "cache://")) {
82         schemeType = RK_URI_SCHEME_IPTV;
83     }
84     return schemeType;
85 }
86 
TEST_COMM_OpenFileUris(const char * pCfgFileUri,char ** pFileUris,RK_U32 u32UriCount)87 RK_S32 TEST_COMM_OpenFileUris(const char *pCfgFileUri, char **pFileUris, RK_U32 u32UriCount) {
88     FILE *fp = RK_NULL;
89     RK_U32 u32Count = 0;
90     RK_U32 u32FileLen = 0;
91     RK_U32 u32Len = 0;
92     RK_S32 s32Ret = 0;
93     char *pLine = RK_NULL;
94     char *pBuffer = RK_NULL;
95     size_t sLen = 0;
96     size_t sReadLen = 0;
97 
98     if ((fp = fopen(pCfgFileUri, "r")) == RK_NULL) {
99         RK_LOGE("Error! opening file %s error is %s", pCfgFileUri, strerror(errno));
100         return RK_FAILURE;
101     }
102     fseek(fp, 0L, SEEK_END);
103     u32FileLen = ftell(fp);
104     fseek(fp, 0, SEEK_SET);
105 
106     while (sReadLen = getline(&pLine, &sLen, fp) != -1) {
107         RK_S32 len = strlen(pLine) + 1;
108         pBuffer = reinterpret_cast<char *>(malloc(len));
109         snprintf(pBuffer, len, "%s", pLine);
110 
111         while (len) {
112             len--;
113             if (pBuffer[len] == '\r') {
114                 pBuffer[len] = '\0';
115                 break;
116             }
117         }
118         pFileUris[u32Count] = pBuffer;
119         free(pLine);
120         sLen = RK_NULL;
121 
122         RK_LOGV("url %s", pFileUris[u32Count]);
123         u32Count++;
124 
125         if (u32Count >= u32UriCount)
126             break;
127     }
128 
129     fclose(fp);
130 
131     return RK_SUCCESS;
132 }
133 
TEST_COMM_CloseFileUris(char ** pFileUris,RK_U32 u32UriCount)134 void TEST_COMM_CloseFileUris(char **pFileUris, RK_U32 u32UriCount) {
135     RK_U32 u32Index = 0;
136 
137     for (u32Index = 0; u32Index < u32UriCount; u32Index++) {
138         if (pFileUris[u32Index] != RK_NULL) {
139             free(pFileUris[u32Index]);
140             pFileUris[u32Index] = RK_NULL;
141         }
142     }
143 }
144 
TEST_COMM_FileReadOneFrame(const char * pFileName,VIDEO_FRAME_INFO_S * pstVideoFrame)145 RK_S32 TEST_COMM_FileReadOneFrame(const char *pFileName, VIDEO_FRAME_INFO_S *pstVideoFrame) {
146     RK_U32 u32ReadSize = 0;
147     RK_S32 s32Ret = RK_SUCCESS;
148     FILE *fp = RK_NULL;
149     PIC_BUF_ATTR_S stPicBufAttr;
150     MB_PIC_CAL_S stMbPicCalResult;
151 
152     if (!pFileName || !pstVideoFrame) {
153         return RK_FAILURE;
154     }
155 
156     stPicBufAttr.u32Width = pstVideoFrame->stVFrame.u32VirWidth;
157     stPicBufAttr.u32Height = pstVideoFrame->stVFrame.u32VirHeight;
158     stPicBufAttr.enPixelFormat = pstVideoFrame->stVFrame.enPixelFormat;
159     stPicBufAttr.enCompMode = pstVideoFrame->stVFrame.enCompressMode;
160     s32Ret = RK_MPI_CAL_VGS_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
161     if (s32Ret != RK_SUCCESS) {
162         RK_LOGE("RK_MPI_CAL_VGS_GetPicBufferSize failed. err=0x%x", s32Ret);
163         return s32Ret;
164     }
165 
166     fp = fopen(pFileName, "rb");
167     if (fp == RK_NULL) {
168         RK_LOGE("fopen %s failed, error: %s", pFileName, strerror(errno));
169         s32Ret = RK_FAILURE;
170         goto __FAILED;
171     }
172     u32ReadSize = fread(RK_MPI_MB_Handle2VirAddr(pstVideoFrame->stVFrame.pMbBlk),
173                         1, stMbPicCalResult.u32MBSize, fp);
174     if (u32ReadSize == 0) {
175         RK_LOGE("fread %p failed! request %d", pFileName, stMbPicCalResult.u32MBSize);
176         s32Ret = RK_FAILURE;
177         goto __FAILED;
178     }
179 
180     RK_MPI_SYS_MmzFlushCache(pstVideoFrame->stVFrame.pMbBlk, RK_FALSE);
181 
182 __FAILED:
183     if (fp) {
184         fclose(fp);
185     }
186 
187     return s32Ret;
188 }
189 
TEST_COMM_FileWriteOneFrame(const char * pFileName,VIDEO_FRAME_INFO_S * pstVideoFrame)190 RK_S32 TEST_COMM_FileWriteOneFrame(const char *pFileName, VIDEO_FRAME_INFO_S *pstVideoFrame) {
191     RK_S32 s32Ret = RK_SUCCESS;
192     FILE *fp = RK_NULL;
193     PIC_BUF_ATTR_S stPicBufAttr;
194     MB_PIC_CAL_S stMbPicCalResult;
195 
196     if (!pFileName || !pstVideoFrame) {
197         return RK_FAILURE;
198     }
199 
200     stPicBufAttr.u32Width = pstVideoFrame->stVFrame.u32VirWidth;
201     stPicBufAttr.u32Height = pstVideoFrame->stVFrame.u32VirHeight;
202     stPicBufAttr.enPixelFormat = pstVideoFrame->stVFrame.enPixelFormat;
203     stPicBufAttr.enCompMode = pstVideoFrame->stVFrame.enCompressMode;
204     s32Ret = RK_MPI_CAL_VGS_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
205     if (s32Ret != RK_SUCCESS) {
206         RK_LOGE("RK_MPI_CAL_VGS_GetPicBufferSize failed. err=0x%x", s32Ret);
207         return s32Ret;
208     }
209 
210     RK_MPI_SYS_MmzFlushCache(pstVideoFrame->stVFrame.pMbBlk, RK_TRUE);
211 
212     fp = fopen(pFileName, "wb");
213     if (fp == RK_NULL) {
214         RK_LOGE("open %s failed, error: %s", pFileName, strerror(errno));
215         s32Ret = RK_FAILURE;
216         goto __FAILED;
217     }
218     fwrite(RK_MPI_MB_Handle2VirAddr(pstVideoFrame->stVFrame.pMbBlk),
219             1, stMbPicCalResult.u32MBSize, fp);
220     fflush(fp);
221 
222 __FAILED:
223     if (fp) {
224         fclose(fp);
225     }
226 
227     return RK_SUCCESS;
228 }
229 
TEST_COMM_MkDirs(const char * muldir,RK_S32 mode)230 void TEST_COMM_MkDirs(const char *muldir, RK_S32 mode) {
231     RK_S32 i, len;
232     char str[512];
233 
234     strncpy(str, muldir, 512);
235     len = strlen(str);
236     for (i = 0; i < len; i++) {
237         if (str[i] == '/') {
238             str[i] = '\0';
239             if (access(str, 0)) {
240                 mkdir(str, mode);
241             }
242             str[i]='/';
243         }
244     }
245     if (len > 0 && access(str, 0)) {
246         mkdir(str, mode);
247     }
248 
249     return;
250 }
251 
TEST_COMM_CheckFileSizeInRange(const char * pFilePath,RK_S32 minSize,RK_S32 maxSize)252 RK_S32 TEST_COMM_CheckFileSizeInRange(const char *pFilePath,
253                                                           RK_S32 minSize,
254                                                           RK_S32 maxSize) {
255     struct stat st;
256     FILE *fp;
257 
258     if (access(pFilePath, 0)) {
259         RK_LOGE("can't find file %s!", pFilePath);
260         return RK_FAILURE;
261     }
262 
263     if (stat(pFilePath, &st) == 0) {
264         if (st.st_size >= minSize && st.st_size <= maxSize) {
265             return RK_SUCCESS;
266         } else {
267             RK_LOGE("file %s size:%lld is out range[%d,%d]!",
268                      pFilePath, st.st_size, minSize, maxSize);
269             return RK_FAILURE;
270         }
271     } else {
272         RK_LOGE("can't determine size of %s: %s!", pFilePath, strerror(errno));
273         return RK_FAILURE;
274     }
275 }
276 
TEST_COMM_GetSocType()277 RKSocType TEST_COMM_GetSocType() {
278     char achBuf[1024] = "";
279     FILE *fd = NULL;
280     RKSocType socType = RK_SOC_1126;
281 
282     if (gEnRkSocType)
283         return gEnRkSocType;
284 
285     fd = fopen("/proc/device-tree/compatible", "r");
286     memset(achBuf, 0, sizeof(achBuf));
287     if (fd != NULL) {
288         fread(achBuf, 1, sizeof(achBuf), fd);
289         if ((strstr(achBuf, "rockchip")) ||
290             (strstr(achBuf, "Rockchip")) ||
291             (strstr(achBuf, "RK30board"))) {
292             if (strstr(achBuf, "1126") || (strstr(achBuf, "1109")))
293                 socType = RK_SOC_1126;
294             else if (strstr(achBuf, "3566") || (strstr(achBuf, "3568")))
295                 socType = RK_SOC_3568;
296             else if (strstr(achBuf, "3588"))
297                 socType = RK_SOC_3588;
298         } else {
299             RK_LOGE("not match rockchips device.");
300         }
301         fclose(fd);
302     } else {
303         RK_LOGE("open failed.");
304     }
305     gEnRkSocType = socType;
306     RK_LOGD("get the rkchip:%d.", socType);
307 
308     return socType;
309 }
310 
311 #ifdef __cplusplus
312 #if __cplusplus
313 }
314 #endif
315 #endif /* End of #ifdef __cplusplus */
316