hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "utils.h"
 
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
 
#include <algorithm>
#include <sstream>
 
#ifdef RKMEDIA_SUPPORT_MINILOG
    #include "minilogger/log.h"
#endif
 
enum
{
    LOG_LEVEL_INFO,
    LOG_LEVEL_DBG,
};
 
enum
{
    LOG_METHOD_MINILOG,
    LOG_METHOD_PRINT
};
 
static int rkmedia_log_method = LOG_METHOD_PRINT;
static int rkmedia_log_level = LOG_LEVEL_INFO;
 
_API void LOG_INIT()
{
    char* ptr = NULL;
 
    rkmedia_log_method = LOG_METHOD_PRINT;
#ifdef RKMEDIA_SUPPORT_MINILOG
    ptr = getenv("RKMEDIA_LOG_METHOD");
    if (ptr && strstr(ptr, "MINILOG")) {
        fprintf(stderr, "##RKMEDIA Method: MINILOG\n");
        __minilog_log_init("RKMEDIA", NULL, false, false, "RKMEDIA", "0.1");
        rkmedia_log_method = LOG_METHOD_MINILOG;
    } else {
        fprintf(stderr, "##RKMEDIA Method: PRINT\n");
        rkmedia_log_method = LOG_METHOD_PRINT;
    }
#endif //#ifdef RKMEDIA_SUPPORT_MINILOG
 
    ptr = getenv("RKMEDIA_LOG_LEVEL");
    if (ptr && strstr(ptr, "DBG")) {
        fprintf(stderr, "##RKMEDIA Log level: DBG\n");
        rkmedia_log_level = LOG_LEVEL_DBG;
    } else {
        fprintf(stderr, "##RKMEDIA Log level: INFO\n");
        rkmedia_log_level = LOG_LEVEL_INFO;
    }
}
 
static void LogPrintf(const char* prefix, const char* fmt, va_list vl)
{
    char line[1024];
    if (rkmedia_log_level >= LOG_LEVEL_DBG) {
#ifdef RKMEDIA_SUPPORT_MINILOG
        if (rkmedia_log_method == LOG_METHOD_PRINT) {
#endif
            fprintf(stderr, "%s", (char*)prefix);
            vsnprintf(line, sizeof(line), fmt, vl);
            fprintf(stderr, "%s", line);
#ifdef RKMEDIA_SUPPORT_MINILOG
        } else {
            // minilog_debug("%s", (char *)prefix);
            vsnprintf(line, sizeof(line), fmt, vl);
            minilog_debug("%s%s", (char*)prefix, line);
        }
#endif
    }
}
 
void LOGD(const char* format, ...)
{
    va_list vl;
    va_start(vl, format);
    LogPrintf("Debug - ", format, vl);
    va_end(vl);
}
 
void LOG(const char* format, ...)
{
    do {
        va_list vl;
        va_start(vl, format);
        char line[1024];
        vsnprintf(line, sizeof(line), format, vl);
#ifdef RKMEDIA_SUPPORT_MINILOG
        if (rkmedia_log_method == LOG_METHOD_PRINT)
#endif
            fprintf(stderr, "%s", line);
#ifdef RKMEDIA_SUPPORT_MINILOG
        else {
            minilog_info("%s", line);
        }
#endif
        va_end(vl);
    } while (0);
}
 
namespace easymedia
{
 
    bool parse_media_param_map(const char* param, std::map<std::string, std::string>& map)
    {
        if (!param) {
            return false;
        }
 
        std::string token;
        std::istringstream tokenStream(param);
        while (std::getline(tokenStream, token)) {
            std::string key, value;
            std::istringstream subTokenStream(token);
            if (std::getline(subTokenStream, key, '=')) {
                std::getline(subTokenStream, value);
            }
            map[key] = value;
        }
 
        return true;
    }
 
    bool parse_media_param_list(const char* param, std::list<std::string>& list, const char delim)
    {
        if (!param) {
            return false;
        }
 
        std::string token;
        std::istringstream tokenStream(param);
        while (std::getline(tokenStream, token, delim)) {
            list.push_back(token);
        }
 
        return true;
    }
 
    int parse_media_param_match(const char* param, std::map<std::string, std::string>& map,
                                std::list<std::pair<const std::string, std::string&>>& list)
    {
        if (!easymedia::parse_media_param_map(param, map)) {
            return 0;
        }
        int match_num = 0;
        for (auto& key : list) {
            const std::string& req_key = key.first;
            for (auto& entry : map) {
                const std::string& entry_key = entry.first;
                if (req_key == entry_key) {
                    key.second = entry.second;
                    match_num++;
                    break;
                }
            }
        }
 
        return match_num;
    }
 
    bool has_intersection(const char* str, const char* expect, std::list<std::string>* expect_list)
    {
        std::list<std::string> request;
        // LOG("request: %s; expect: %s\n", str, expect);
 
        if (!expect || (strlen(expect) == 0)) {
            return true;
        }
 
        if (!parse_media_param_list(str, request, ',')) {
            return false;
        }
        if (expect_list->empty() && !parse_media_param_list(expect, *expect_list)) {
            return false;
        }
        for (auto& req : request) {
            if (std::find(expect_list->begin(), expect_list->end(), req) != expect_list->end()) {
                return true;
            }
        }
        return false;
    }
 
    std::string get_media_value_by_key(const char* param, const char* key)
    {
        std::map<std::string, std::string> param_map;
        if (!parse_media_param_map(param, param_map)) {
            return std::string();
        }
        return param_map[key];
    }
 
    bool string_start_withs(std::string const& fullString, std::string const& starting)
    {
        if (fullString.length() >= starting.length()) {
            return (0 == fullString.find(starting));
        } else {
            return false;
        }
    }
 
    bool string_end_withs(std::string const& fullString, std::string const& ending)
    {
        if (fullString.length() >= ending.length()) {
            return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
        } else {
            return false;
        }
    }
 
#ifndef NDEBUG
 
    #include <fcntl.h>
    #include <unistd.h>
 
    bool DumpToFile(std::string path, const char* ptr, size_t len)
    {
        int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_FSYNC);
        if (fd < 0) {
            LOG("Fail to open %s\n", path.c_str());
            return false;
        }
        LOGD("dump to %s, size %d\n", path.c_str(), (int)len);
        write(fd, ptr, len);
        close(fd);
        return true;
    }
 
#endif // #ifndef NDEBUG
 
} // namespace easymedia