summaryrefslogtreecommitdiff
path: root/src/mm_player_utils.c
blob: 8bc83b418b1fda2b0d2d8149eab9dc19f98c3a7b (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * libmm-player
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>,
 * Seungbae Shin <seungbae.shin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unicode/ucsdet.h>
#include <dlog.h>
#include <storage.h>
#include <tzplatform_config.h>
#include "mm_player_utils.h"

#define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage */

int util_exist_file_path(const char *file_path)
{
	int fd = 0;
	struct stat stat_results = {0, };

	if (!file_path || !strlen(file_path))
		return MM_ERROR_PLAYER_FILE_NOT_FOUND;

	fd = open(file_path, O_RDONLY);

	if (fd < 0) {
		char str_error[256];
		strerror_r(errno, str_error, sizeof(str_error));
		LOGE("failed to open file by %s (%d)", str_error, errno);

		if (EACCES == errno)
			return MM_ERROR_PLAYER_PERMISSION_DENIED;

		return MM_ERROR_PLAYER_FILE_NOT_FOUND;
	}

	if (fstat(fd, &stat_results) < 0) {
		LOGE("failed to get file status");
	} else if (stat_results.st_size == 0) {
		LOGE("file size is zero");
		close(fd);
		return MM_ERROR_PLAYER_FILE_NOT_FOUND;
	} else
		LOGW("file size : %lld bytes", (long long)stat_results.st_size);

	close(fd);

	return MM_ERROR_NONE;
}

bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size)
{
	FILE *fp = NULL;
	int wsize = 0;

	fp = fopen(backup_path, "wb");
	if (!fp)
		return FALSE;

	wsize = fwrite(data_ptr, sizeof(char), data_size, fp);

	fclose(fp);

	if (wsize != data_size) {
		if (!access(backup_path, R_OK))
			remove(backup_path);

		LOGE("No space to write!\n");

		return FALSE;
	}

	return TRUE;
}

bool util_remove_file_backup(const char *backup_path)
{
	if (!backup_path || !strlen(backup_path))
		return FALSE;

	int res = access(backup_path, R_OK);
	if (!res) {
		if (remove(backup_path) == -1)
			return FALSE;
	}

	return TRUE;
}

#define DETECTION_PREFIX_SIZE	20
int util_is_midi_type_by_mem(void *mem, int size)
{
	const char *p = (const char *)mem;

	if (size < DETECTION_PREFIX_SIZE)
		return MM_AUDIO_CODEC_INVALID;

	/* mmf file detection */
	if (p[0] == 'M' && p[1] == 'M' && p[2] == 'M' && p[3] == 'D') {
		LOGD("MM_AUDIO_CODEC_MMF\n");
		return MM_AUDIO_CODEC_MMF;
	}

	/* midi file detection */
	if (p[0] == 'M' && p[1] == 'T' && p[2] == 'h' && p[3] == 'd') {
		LOGD("MM_AUDIO_CODEC_MIDI, %d\n", MM_AUDIO_CODEC_MIDI);
		return MM_AUDIO_CODEC_MIDI;
	}
	/* mxmf file detection */
	if (p[0] == 'X' && p[1] == 'M' && p[2] == 'F' && p[3] == '_') {
		LOGD("MM_AUDIO_CODEC_MXMF\n");
		return MM_AUDIO_CODEC_MXMF;
	}

	/* wave file detection */
	if (p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F' &&
		p[8] == 'W' && p[9] == 'A' && p[10] == 'V' && p[11] == 'E' &&
		p[12] == 'f' && p[13] == 'm' && p[14] == 't') {
		LOGD("MM_AUDIO_CODEC_WAVE\n");
		return MM_AUDIO_CODEC_WAVE;
	}
	/* i-melody file detection */
	if (memcmp(p, "BEGIN:IMELODY", 13) == 0) {
		LOGD("MM_AUDIO_CODEC_IMELODY\n");
		return MM_AUDIO_CODEC_IMELODY;
	}

	return MM_AUDIO_CODEC_INVALID;
}

int util_is_midi_type_by_file(const char *file_path)
{
	struct stat file_attrib;
	FILE *fp = NULL;
	char prefix[DETECTION_PREFIX_SIZE] = {0,};
	int size;

	if (!file_path)
		return FALSE;

	fp = fopen(file_path, "r");

	if (!fp)
	return FALSE;

	memset(&file_attrib, 0, sizeof(file_attrib));

	if (stat(file_path, &file_attrib) != 0) {
		fclose(fp);
		return FALSE;
	}

	size = (int) file_attrib.st_size;

	if (size < DETECTION_PREFIX_SIZE) {
		fclose(fp);
		return FALSE;
	}

	size = fread(prefix, sizeof(char), DETECTION_PREFIX_SIZE, fp);

	fclose(fp);

	return util_is_midi_type_by_mem(prefix, size);
}

char**
util_get_cookie_list(const char *cookies)
{
	char **cookie_list = NULL;
	char *temp = NULL;
	gint i = 0;

	if (!cookies || !strlen(cookies))
		return NULL;

	SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);

	temp = g_strdup(cookies);

	/* trimming. it works inplace */
	g_strstrip(temp);

	/* split */
	cookie_list = g_strsplit(temp, ";", 100);

	for (i = 0; i < g_strv_length(cookie_list); i++) {
		if (cookie_list[i]) {
			if (strlen(cookie_list[i])) {
				g_strstrip(cookie_list[i]);
				SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
			} else
				cookie_list[i][0] = '\0';
		}
	}

	if (temp)
		g_free(temp);
	temp = NULL;

	return cookie_list;
}

bool util_check_valid_url(const char *proxy)
{
	struct in_addr proxy_addr;
	bool ret = TRUE;

	MMPLAYER_RETURN_VAL_IF_FAIL(proxy, FALSE);
	MMPLAYER_RETURN_VAL_IF_FAIL(strlen(proxy), FALSE);

	if (inet_aton(proxy, &proxy_addr) != 0) {
		LOGW("invalid proxy is set. \n");
		ret = FALSE;
	}

	return ret;
}

/* check the given path is indicating sdp file */
bool
util_is_sdp_file(const char *path)
{
	gboolean ret = FALSE;
	gchar* uri = NULL;

	MMPLAYER_FENTER();

	MMPLAYER_RETURN_VAL_IF_FAIL(path, FALSE);

	uri = g_ascii_strdown(path, -1);

	if (uri == NULL)
		return FALSE;

	/* trimming */
	g_strstrip(uri);

	/* strlen(".sdp") == 4 */
	if (strlen(uri) <= 4) {
		LOGW("path is too short.\n");
		g_free(uri);
		return ret;
	}

	/* first, check extension name */
	ret = g_str_has_suffix(uri, "sdp");

	/* second, if no suffix is there, check it's contents */
	if (!ret)
		/* FIXIT : do it soon */
		LOGD("no suffix");

	g_free(uri);
	uri = NULL;

	return ret;
}

int64_t
util_get_time(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}

int
util_get_rank_increase(const char *factory_class)
{
	gint rank_pri_inc = 20;
	gint rank_sec_inc = 10;
	gint ret = 0;

	if (g_strrstr(factory_class, "Dsp"))
		ret = rank_pri_inc;
	else if (g_strrstr(factory_class, "HW"))
		ret = rank_pri_inc;
	else if (g_strrstr(factory_class, "Arm"))
		ret = rank_sec_inc;

	return ret;
}

int
util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2)
{
	const gchar *klass;
	int f1_rank_inc = 0, f2_rank_inc = 0;

	klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f1));
	f1_rank_inc = util_get_rank_increase(klass);

	klass = gst_element_factory_get_klass(GST_ELEMENT_FACTORY(f2));
	f2_rank_inc = util_get_rank_increase(klass);

	return (gst_plugin_feature_get_rank(f2)+f2_rank_inc) - (gst_plugin_feature_get_rank(f1)+f1_rank_inc);
}

const char*
util_get_charset(const char *file_path)
{
	UCharsetDetector* ucsd;
	const UCharsetMatch* ucm;
	UErrorCode status = U_ZERO_ERROR;

	const char* charset = NULL;
	char *buf = NULL;
	FILE* fin = 0;
	size_t n_size = 0;

	fin = fopen(file_path, "r");
	if (!fin) {
		SECURE_LOGE("fail to open file %s\n", file_path);
		return NULL;
	}

	ucsd = ucsdet_open(&status);
	if (U_FAILURE(status)) {
		LOGE("fail to ucsdet_open\n");
		goto done;
	}

	ucsdet_enableInputFilter(ucsd, TRUE);

	buf = g_malloc(1024*1024);
	if (!buf) {
		LOGE("fail to alloc\n");
		goto done;
	}

	n_size = fread(buf, 1, 1024*1024, fin);

	if (!n_size)
		goto done;

	ucsdet_setText(ucsd, buf, strlen(buf), &status);
	if (U_FAILURE(status)) {
		LOGE("fail to ucsdet_setText\n");
		goto done;
	}

	ucm = ucsdet_detect(ucsd, &status);
	if (U_FAILURE(status)) {
		LOGE("fail to ucsdet_detect\n");
		goto done;
	}

	charset = ucsdet_getName(ucm, &status);
	if (U_FAILURE(status)) {
		LOGE("fail to ucsdet_getName\n");
		goto done;
	}

	/* CP949 encoding is an extension of the EUC-KR and it is backwards compatible.*/
	if (charset && !strcmp(charset, "EUC-KR"))
		charset = "CP949";

done:
	if (fin)
		fclose(fin);

	if (ucsd)
		ucsdet_close(ucsd);

	if (buf)
		g_free(buf);

	return charset;
}

int util_get_pixtype(unsigned int fourcc)
{
	int pixtype = MM_PIXEL_FORMAT_INVALID;

    /*
	char *pfourcc = (char*)&fourcc;

	LOGD("fourcc(%c%c%c%c)",
		pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
    */

	switch (fourcc) {
	case GST_MAKE_FOURCC('S', 'N', '1', '2'):
	case GST_MAKE_FOURCC('N', 'V', '1', '2'):
		pixtype = MM_PIXEL_FORMAT_NV12;
		break;
	case GST_MAKE_FOURCC('S', 'T', '1', '2'):
		pixtype = MM_PIXEL_FORMAT_NV12T;
		break;
	case GST_MAKE_FOURCC('S', 'N', '2', '1'):
	case GST_MAKE_FOURCC('N', 'V', '2', '1'):
		pixtype = MM_PIXEL_FORMAT_NV21;
		break;
	case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
	case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
	case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
		pixtype = MM_PIXEL_FORMAT_YUYV;
		break;
	case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
	case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
		pixtype = MM_PIXEL_FORMAT_UYVY;
		break;
	case GST_MAKE_FOURCC('S', '4', '2', '0'):
	case GST_MAKE_FOURCC('I', '4', '2', '0'):
		pixtype = MM_PIXEL_FORMAT_I420;
		break;
	case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
		pixtype = MM_PIXEL_FORMAT_YV12;
		break;
	case GST_MAKE_FOURCC('4', '2', '2', 'P'):
		pixtype = MM_PIXEL_FORMAT_422P;
		break;
	case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
		pixtype = MM_PIXEL_FORMAT_RGB565;
		break;
	case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
		pixtype = MM_PIXEL_FORMAT_RGB888;
		break;
	case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
	case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
		pixtype = MM_PIXEL_FORMAT_ARGB;
		break;
	case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
	case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
	case GST_MAKE_FOURCC('S', 'R', '3', '2'):
		pixtype = MM_PIXEL_FORMAT_RGBA;
		break;
	case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
	case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
		pixtype = MM_PIXEL_FORMAT_ENCODED;
		break;
	case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
		pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
		break;
	default:
		LOGE("Not supported fourcc type(%c%c%c%c)",
			fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
		pixtype = MM_PIXEL_FORMAT_INVALID;
		break;
	}

	return pixtype;
}

static int _util_storage_supported_cb(int storage_id, storage_type_e type,
	storage_state_e state, const char *path, void *user_data)
{
	MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;

	MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);

	if (type == storage_info->type) {
		storage_info->id = storage_id;
		storage_info->state = state;
		return FALSE;
	}

	return TRUE;
}

bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
{
	int ret = 0;
	const char *file_path = path;

	MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);

	if (strncmp(file_path, "file://", strlen("file://")) == 0)
		file_path = path+7; /* remove file prefix */

	if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0)
		storage_info->type = STORAGE_TYPE_EXTERNAL;
	else
		storage_info->type = STORAGE_TYPE_INTERNAL;

	ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
	if (ret != STORAGE_ERROR_NONE) {
		LOGE("storage_foreach_device_supported failed 0x%x", ret);
		return false;
	}

	if ((storage_info->type == STORAGE_TYPE_EXTERNAL) &&
		(storage_info->state <= STORAGE_STATE_REMOVED)) {
		LOGE("need to check the storage state : %d", storage_info->state);
		return false;
	}

	LOGD("storage info: type %d, id %d", storage_info->type, storage_info->id);
	return true;
}