1/*
2 * Copyright (c) 2023-2023 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 "osal/utils/util.h"
17#include <climits>
18
19#ifdef WIN32
20#include <windows.h>
21#include <io.h>
22#include "libavutil/error.h"
23
24void usleep(__int64 usec)
25{
26    HANDLE timer = CreateWaitableTimer(nullptr, TRUE, nullptr);
27    // set interval to 100 nanosecond, using relative time
28    LARGE_INTEGER dueTime;
29    dueTime.QuadPart = -(10 * usec); // 10
30    SetWaitableTimer(timer, &dueTime, 0, nullptr, nullptr, 0);
31    WaitForSingleObject(timer, INFINITE);
32    CloseHandle(timer);
33}
34
35char *realpath(const char *__restrict name, char *__restrict resolved)
36{
37    return _fullpath(resolved, name, PATH_MAX);
38}
39
40int access(const char *name, int type)
41{
42    return _access(name, type);
43}
44#else
45#include <unistd.h>
46#ifdef __cplusplus
47extern "C" {
48#endif
49#include "libavutil/error.h"
50#ifdef __cplusplus
51};
52#endif
53#endif
54
55#define ERROR_MAX_STRING_SIZE 64
56
57namespace OHOS {
58namespace Media {
59namespace OSAL {
60void SleepFor(unsigned ms)
61{
62    constexpr int factor = 1000;
63    usleep(ms * factor);
64}
65
66bool ConvertFullPath(const std::string& partialPath, std::string& fullPath)
67{
68    if (partialPath.empty() || partialPath.length() >= PATH_MAX) {
69        return false;
70    }
71    char tmpPath[PATH_MAX] = {0};
72    if (realpath(partialPath.c_str(), tmpPath) == nullptr) {
73        return false;
74    }
75    if (access(tmpPath, F_OK) || access(tmpPath, R_OK)) {
76        return false;
77    }
78    fullPath = tmpPath;
79    return true;
80}
81
82std::string AVStrError(int errnum)
83{
84#ifdef MEDIA_OHOS
85    char errbuf[ERROR_MAX_STRING_SIZE] = {0};
86    return std::string(errbuf);
87#else
88    return "AVStrError " + std::to_string(errnum);
89#endif
90}
91} // namespace OSAL
92} // namespace Media
93} // namespace OHOS