summaryrefslogtreecommitdiff
path: root/src/core/mp-file-tag-info.c
blob: af91c7033127b9aa2e54113df3a37aee85cf24e0 (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
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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 <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <glib.h>
#include <mm_file.h>
#include <mm_error.h>
#include "mp-file-tag-info.h"
#include "mp-player-debug.h"
#include "mp-define.h"

/* tag_info which must be freed with mp_file_tag_free() after use. */
int
mp_file_tag_info_get_all_tag(const char *filename, mp_tag_info_t * tag_info)
{
	MMHandleType tag_attrs = 0;
	MMHandleType content_attrs = 0;
	int ret = 0;

	if (!filename || !tag_info)
	{
		goto CATCH_ERROR;
	}

	memset(tag_info, 0x00, sizeof(mp_tag_info_t));

	ret = mm_file_create_content_attrs(&content_attrs, filename);
	if (ret == MM_ERROR_NONE)
	{
		char *error = NULL;
		int dur = 0;
		int audio_codec = 0;
		int audio_samplerate = 0;
		int audio_bitrate = 0;
		int audio_channel = 0;

		ret = mm_file_get_attrs(content_attrs,
					&error,
					MM_FILE_CONTENT_DURATION, &dur,
					MM_FILE_CONTENT_AUDIO_CODEC, &audio_codec,
					MM_FILE_CONTENT_AUDIO_SAMPLERATE, &audio_samplerate,
					MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
					MM_FILE_CONTENT_AUDIO_CHANNELS, &audio_channel, NULL);

		if (ret != MM_ERROR_NONE)
		{
			if (error)
			{
				ERROR_TRACE("fail to mm_file_get_attrs() - ret(%x), error(%s)", ret, error);
				free(error);
			}
			else
			{
				ERROR_TRACE("fail to mm_file_get_attrs() - ret(%x)", ret);
			}
			goto CATCH_ERROR;
		}
		tag_info->duration = dur;
		tag_info->audio_codec = audio_codec;
		tag_info->audio_samplerate = audio_samplerate;
		tag_info->audio_bitrate = audio_bitrate;
		tag_info->audio_channel = audio_channel;
	}
	else
	{
		ERROR_TRACE("fail to mm_file_create_content_attrs() - ret(%x)", ret);
		goto CATCH_ERROR;
	}


	ret = mm_file_create_tag_attrs(&tag_attrs, filename);
	if (ret == MM_ERROR_NONE)
	{
		char *error = NULL;
		char *album = NULL;
		int album_len = 0;
		char *artist = NULL;
		int artist_len = 0;
		char *title = NULL;
		int title_len = 0;
		void *albumart = NULL;
		int albumart_len = 0;
		int albumart_size = 0;
		char *genre = NULL;
		int genre_len = 0;
		char *copyright = NULL;
		int copyright_len = 0;
		char *date = NULL;
		int date_len = 0;
		char *desc = NULL;
		int desc_len = 0;
		char *author = NULL;
		int author_len = 0;
		char *track = NULL;
		int track_len = 0;
		char *rating = NULL;
		int rating_len = 0;

		ret = mm_file_get_attrs(tag_attrs,
					&error,
					MM_FILE_TAG_ARTIST, &artist, &artist_len,
					MM_FILE_TAG_ALBUM, &album, &album_len,
					MM_FILE_TAG_TITLE, &title, &title_len,
					MM_FILE_TAG_GENRE, &genre, &genre_len,
					MM_FILE_TAG_AUTHOR, &author, &author_len,
					MM_FILE_TAG_COPYRIGHT, &copyright, &copyright_len,
					MM_FILE_TAG_DATE, &date, &date_len,
					MM_FILE_TAG_DESCRIPTION, &desc, &desc_len,
					MM_FILE_TAG_ARTWORK, &albumart, &albumart_len,
					MM_FILE_TAG_ARTWORK_SIZE, &albumart_size,
					MM_FILE_TAG_TRACK_NUM, &track, &track_len,
					MM_FILE_TAG_RATING, &rating, &rating_len, NULL);

		if (ret != MM_ERROR_NONE)
		{
			if (error)
			{
				ERROR_TRACE("fail to mm_file_get_attrs() - ret(%x), error(%s)", ret, error);
				free(error);
			}
			else
			{
				ERROR_TRACE("fail to mm_file_get_attrs() - ret(%x)", ret);
			}
			goto CATCH_ERROR;
		}

		if (albumart)
		{
			gchar *path = NULL;
			int fd = g_file_open_tmp(NULL, &path, NULL);

			if (fd != -1)
			{
				FILE *fp = fdopen(fd, "w");
				if (fp == NULL)
				{
					ERROR_TRACE("fail to fdopen()");
					close(fd);
				}
				else
				{
					int n = fwrite((unsigned char *)albumart, 1, albumart_size, fp);
					if (n != albumart_size)
					{
						ERROR_TRACE("fail to fwrite()");
						fclose(fp);
						close(fd);
					}
					else
					{
						fflush(fp);
						fclose(fp);
						close(fd);
					}
				}
			}
			tag_info->albumart_path = path;
		}

		if (album)
			tag_info->album = strdup(album);
		if (artist)
			tag_info->artist = strdup(artist);
		if (title)
			tag_info->title = strdup(title);
		if (genre)
			tag_info->genre = strdup(genre);
		if (copyright)
			tag_info->copyright = strdup(copyright);
		if (date)
			tag_info->date = strdup(date);
		if (desc)
			tag_info->desc = strdup(desc);
		if (author)
			tag_info->author = strdup(author);
		if (track)
			tag_info->track = strdup(track);
		if (rating)
			tag_info->rating = strdup(rating);

	}
	else
	{
		ERROR_TRACE("fail to mm_file_create_tag_attrs() - ret(%x)", ret);
		goto CATCH_ERROR;
	}

	DEBUG_TRACE
		("file : %s\n duration: %d \n album: %s\n artist: %s\n title: %s\n genre: %s\n copyright:%s\n date: %s\n desc : %s\n author: %s\n albumart : %s",
		 filename, tag_info->duration, tag_info->album, tag_info->artist, tag_info->title, tag_info->genre,
		 tag_info->copyright, tag_info->date, tag_info->desc, tag_info->author, tag_info->albumart_path);

	if (content_attrs)
		mm_file_destroy_content_attrs(content_attrs);

	if (tag_attrs)
		mm_file_destroy_content_attrs(tag_attrs);

	return 0;

      CATCH_ERROR:
	if (content_attrs)
		mm_file_destroy_content_attrs(content_attrs);

	if (tag_attrs)
		mm_file_destroy_content_attrs(tag_attrs);

	return -1;
}


void
mp_file_tag_free(mp_tag_info_t * tag_info)
{
	if (tag_info == NULL)
		return;

	IF_FREE(tag_info->album);
	IF_FREE(tag_info->genre);
	IF_FREE(tag_info->author);
	IF_FREE(tag_info->artist);
	IF_FREE(tag_info->title);
	IF_FREE(tag_info->copyright);
	IF_FREE(tag_info->date);
	IF_FREE(tag_info->desc);
	IF_FREE(tag_info->albumart_path);
	IF_FREE(tag_info->track);
	IF_FREE(tag_info->rating);
	return;
}