Reinforce code for coverage
[platform/core/api/media-content.git] / src / media_audio.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
17
18 #include <media_info_private.h>
19
20 #define media_content_retv_free_audio_if(expr, val, p_str) do { \
21                                 if (expr) {     \
22                                         LOGE(FONT_COLOR_RED"Memory allocation failure"FONT_COLOR_RESET);        \
23                                         audio_meta_destroy(p_str);      \
24                                         return (val);   \
25                                 }       \
26                         } while (0)
27
28 int audio_meta_destroy(audio_meta_h audio)
29 {
30         audio_meta_s *_audio = (audio_meta_s*)audio;
31         media_content_retvm_if(_audio == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Audio handle is null");
32
33         SAFE_FREE(_audio->media_id);
34         SAFE_FREE(_audio->title);
35         SAFE_FREE(_audio->album);
36         SAFE_FREE(_audio->artist);
37         SAFE_FREE(_audio->album_artist);
38         SAFE_FREE(_audio->genre);
39         SAFE_FREE(_audio->composer);
40         SAFE_FREE(_audio->year);
41         SAFE_FREE(_audio->recorded_date);
42         SAFE_FREE(_audio->copyright);
43         SAFE_FREE(_audio->track_num);
44         SAFE_FREE(_audio);
45
46         return MEDIA_CONTENT_ERROR_NONE;
47 }
48
49 int audio_meta_clone(audio_meta_h *dst, audio_meta_h src)
50 {
51         audio_meta_s *_src = (audio_meta_s*)src;
52         media_content_retvm_if(_src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Source handle is null");
53
54         audio_meta_s *_dst = (audio_meta_s*)calloc(1, sizeof(audio_meta_s));
55         media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
56
57         if (_src->media_id != NULL) {
58                 _dst->media_id = g_strdup(_src->media_id);
59                 media_content_retv_free_audio_if(_dst->media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
60         }
61
62         if (_src->title != NULL) {
63                 _dst->title = g_strdup(_src->title);
64                 media_content_retv_free_audio_if(_dst->title == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
65         }
66
67         if (_src->album != NULL) {
68                 _dst->album = g_strdup(_src->album);
69                 media_content_retv_free_audio_if(_dst->album == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
70         }
71
72         if (_src->artist != NULL) {
73                 _dst->artist = g_strdup(_src->artist);
74                 media_content_retv_free_audio_if(_dst->artist == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
75         }
76
77         if (_src->album_artist != NULL) {
78                 _dst->album_artist = g_strdup(_src->album_artist);
79                 media_content_retv_free_audio_if(_dst->album_artist == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
80         }
81
82         if (_src->genre != NULL) {
83                 _dst->genre = g_strdup(_src->genre);
84                 media_content_retv_free_audio_if(_dst->genre == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
85         }
86
87         if (_src->composer != NULL) {
88                 _dst->composer = g_strdup(_src->composer);
89                 media_content_retv_free_audio_if(_dst->composer == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
90         }
91
92         if (_src->year != NULL) {
93                 _dst->year = g_strdup(_src->year);
94                 media_content_retv_free_audio_if(_dst->year == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
95         }
96
97         if (_src->recorded_date != NULL) {
98                 _dst->recorded_date = g_strdup(_src->recorded_date);
99                 media_content_retv_free_audio_if(_dst->recorded_date == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
100         }
101
102         if (_src->copyright != NULL) {
103                 _dst->copyright = g_strdup(_src->copyright);
104                 media_content_retv_free_audio_if(_dst->copyright == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
105         }
106
107         if (_src->track_num != NULL) {
108                 _dst->track_num = g_strdup(_src->track_num);
109                 media_content_retv_free_audio_if(_dst->track_num == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (audio_meta_h)_dst);
110         }
111
112         _dst->bitrate = _src->bitrate;
113         _dst->bitpersample = _src->bitpersample;
114         _dst->samplerate = _src->samplerate;
115         _dst->channel = _src->channel;
116         _dst->duration = _src->duration;
117
118         *dst = (audio_meta_h)_dst;
119
120         return MEDIA_CONTENT_ERROR_NONE;
121 }
122
123 int audio_meta_get_media_id(audio_meta_h audio, char **media_id)
124 {
125         int ret = MEDIA_CONTENT_ERROR_NONE;
126         audio_meta_s *_audio = (audio_meta_s*)audio;
127         if (_audio) {
128                 if (STRING_VALID(_audio->media_id)) {
129                         *media_id = strdup(_audio->media_id);
130                         media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
131                 } else {
132                         *media_id = NULL;
133                 }
134                 ret = MEDIA_CONTENT_ERROR_NONE;
135
136         } else {
137                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
138                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
139         }
140
141         return ret;
142 }
143
144 int audio_meta_get_album(audio_meta_h audio, char **album)
145 {
146         int ret = MEDIA_CONTENT_ERROR_NONE;
147         audio_meta_s *_audio = (audio_meta_s*)audio;
148         if (_audio) {
149                 if (_audio->album != NULL) {
150                         *album = g_strdup(_audio->album);               /*album can be empty string*/
151                         media_content_retvm_if(*album == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
152                 } else {
153                         *album = NULL;
154                 }
155                 ret = MEDIA_CONTENT_ERROR_NONE;
156
157         } else {
158                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
159                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
160         }
161
162         return ret;
163 }
164
165 int audio_meta_get_artist(audio_meta_h audio, char **artist)
166 {
167         int ret = MEDIA_CONTENT_ERROR_NONE;
168         audio_meta_s *_audio = (audio_meta_s*)audio;
169
170         if (_audio) {
171                 if (_audio->artist != NULL) {
172                         *artist = g_strdup(_audio->artist);     /*artist can be empty string*/
173                         media_content_retvm_if(*artist == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
174                 } else {
175                         *artist = NULL;
176                 }
177                 ret = MEDIA_CONTENT_ERROR_NONE;
178
179         } else {
180                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
181                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
182         }
183
184         return ret;
185 }
186
187 int audio_meta_get_album_artist(audio_meta_h audio, char **album_artist)
188 {
189         int ret = MEDIA_CONTENT_ERROR_NONE;
190         audio_meta_s *_audio = (audio_meta_s*)audio;
191
192         if (_audio) {
193                 if (_audio->album_artist != NULL) {
194                         *album_artist = g_strdup(_audio->album_artist); /*album_artist can be empty string*/
195                         media_content_retvm_if(*album_artist == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
196                 } else {
197                         *album_artist = NULL;
198                 }
199                 ret = MEDIA_CONTENT_ERROR_NONE;
200
201         } else {
202                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
203                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
204         }
205
206         return ret;
207 }
208
209 int audio_meta_get_genre(audio_meta_h audio, char **genre)
210 {
211         int ret = MEDIA_CONTENT_ERROR_NONE;
212         audio_meta_s *_audio = (audio_meta_s*)audio;
213
214         if (_audio) {
215                 if (_audio->genre != NULL) {
216                         *genre = g_strdup(_audio->genre);       /*genre can be empty string*/
217                         media_content_retvm_if(*genre == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
218                 } else {
219                         *genre = NULL;
220                 }
221
222                 ret = MEDIA_CONTENT_ERROR_NONE;
223         } else {
224                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
225                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
226         }
227
228         return ret;
229 }
230
231 int audio_meta_get_composer(audio_meta_h audio, char **composer)
232 {
233         int ret = MEDIA_CONTENT_ERROR_NONE;
234         audio_meta_s *_audio = (audio_meta_s*)audio;
235
236         if (_audio) {
237                 if (_audio->composer != NULL) {
238                         *composer = g_strdup(_audio->composer); /*composer can be empty string*/
239                         media_content_retvm_if(*composer == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
240                 } else {
241                         *composer = NULL;
242                 }
243
244                 ret = MEDIA_CONTENT_ERROR_NONE;
245         } else {
246                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
247                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
248         }
249
250         return ret;
251 }
252
253 int audio_meta_get_year(audio_meta_h audio, char **year)
254 {
255         int ret = MEDIA_CONTENT_ERROR_NONE;
256         audio_meta_s *_audio = (audio_meta_s*)audio;
257
258         if (_audio) {
259                 if (_audio->year != NULL) {
260                         *year = g_strdup(_audio->year); /*year can be empty string*/
261                         media_content_retvm_if(*year == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
262                 } else {
263                         *year = NULL;
264                 }
265
266                 ret = MEDIA_CONTENT_ERROR_NONE;
267         } else {
268                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
269                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
270         }
271
272         return ret;
273 }
274
275 int audio_meta_get_recorded_date(audio_meta_h audio, char **recorded_date)
276 {
277         int ret = MEDIA_CONTENT_ERROR_NONE;
278         audio_meta_s *_audio = (audio_meta_s*)audio;
279
280         if (_audio) {
281                 if (_audio->recorded_date != NULL) {
282                         *recorded_date = g_strdup(_audio->recorded_date);
283                         media_content_retvm_if(*recorded_date == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
284                 } else {
285                         *recorded_date = NULL;
286                 }
287
288                 ret = MEDIA_CONTENT_ERROR_NONE;
289         } else {
290                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
291                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
292         }
293
294         return ret;
295 }
296
297 int audio_meta_get_copyright(audio_meta_h audio, char **copyright)
298 {
299         int ret = MEDIA_CONTENT_ERROR_NONE;
300         audio_meta_s *_audio = (audio_meta_s*)audio;
301
302         if (_audio) {
303                 if (_audio->copyright != NULL) {
304                         *copyright = g_strdup(_audio->copyright);       /*copyright can be empty string*/
305                         media_content_retvm_if(*copyright == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
306                 } else {
307                         *copyright = NULL;
308                 }
309
310                 ret = MEDIA_CONTENT_ERROR_NONE;
311         } else {
312                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
313                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
314         }
315
316         return ret;
317 }
318
319 int audio_meta_get_track_num(audio_meta_h audio, char **track_num)
320 {
321         int ret = MEDIA_CONTENT_ERROR_NONE;
322         audio_meta_s *_audio = (audio_meta_s*)audio;
323
324         if (_audio) {
325                 if (_audio->track_num != NULL) {
326                         *track_num = g_strdup(_audio->track_num);       /*track_num can be empty string*/
327                         media_content_retvm_if(*track_num == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
328                 } else {
329                         *track_num = NULL;
330                 }
331
332                 ret = MEDIA_CONTENT_ERROR_NONE;
333         } else {
334                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
335                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
336         }
337
338         return ret;
339 }
340
341 int audio_meta_get_bit_rate(audio_meta_h audio, int *bit_rate)
342 {
343         int ret = MEDIA_CONTENT_ERROR_NONE;
344         audio_meta_s *_audio = (audio_meta_s*)audio;
345
346         if (_audio && bit_rate) {
347                 *bit_rate = _audio->bitrate;
348                 ret = MEDIA_CONTENT_ERROR_NONE;
349         } else {
350                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
351                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
352         }
353
354         return ret;
355 }
356
357 int audio_meta_get_bitpersample(audio_meta_h audio, int *bitpersample)
358 {
359         int ret = MEDIA_CONTENT_ERROR_NONE;
360         audio_meta_s *_audio = (audio_meta_s*)audio;
361
362         if (_audio && bitpersample) {
363                 *bitpersample = _audio->bitpersample;
364                 ret = MEDIA_CONTENT_ERROR_NONE;
365         } else {
366                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
367                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
368         }
369
370         return ret;
371 }
372
373 int audio_meta_get_sample_rate(audio_meta_h audio, int *sample_rate)
374 {
375         int ret = MEDIA_CONTENT_ERROR_NONE;
376         audio_meta_s *_audio = (audio_meta_s*)audio;
377
378         if (_audio && sample_rate) {
379                 *sample_rate = _audio->samplerate;
380                 ret = MEDIA_CONTENT_ERROR_NONE;
381         } else {
382                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
383                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
384         }
385
386         return ret;
387 }
388
389 int audio_meta_get_channel(audio_meta_h audio, int *channel)
390 {
391         int ret = MEDIA_CONTENT_ERROR_NONE;
392         audio_meta_s *_audio = (audio_meta_s*)audio;
393
394         if (_audio && channel) {
395                 *channel = _audio->channel;
396                 ret = MEDIA_CONTENT_ERROR_NONE;
397         } else {
398                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
399                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
400         }
401
402         return ret;
403 }
404
405 int audio_meta_get_duration(audio_meta_h audio, int *duration)
406 {
407         int ret = MEDIA_CONTENT_ERROR_NONE;
408         audio_meta_s *_audio = (audio_meta_s*)audio;
409
410         if (_audio) {
411                 *duration = _audio->duration;
412                 ret = MEDIA_CONTENT_ERROR_NONE;
413         } else {
414                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
415                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
416         }
417
418         return ret;
419 }
420
421 int audio_meta_update_to_db(audio_meta_h audio)
422 {
423         media_content_warn("DEPRECATION WARNING: audio_meta_update_to_db() is deprecated and will be removed from next release.");
424         audio_meta_s *_audio = (audio_meta_s*)audio;
425
426         if (_audio == NULL) {
427                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
428                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
429         }
430
431         return MEDIA_CONTENT_ERROR_NONE;
432 }