4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
27 #include <sys/types.h>
35 #include "mm_source.h"
42 if(!filePath || filePath[0] == '\0')
44 debug_error("invalid argument\n");
48 p = (char*) strrchr(filePath, '.');
50 if( p && ((strncasecmp(p, ".odf", 4) == 0) || (strncasecmp(p, ".dcf", 4) == 0) || (strncasecmp(p, ".o4a", 4) == 0) || (strncasecmp(p, ".o4v", 4) == 0)))
58 int mm_source_open_file(const char *filename, MMSourceType *source, int drmsupport)
60 struct stat finfo = {0, };
62 void *mmap_buf = NULL;
66 debug_error("filename is null\n");
67 return MM_ERROR_SOUND_INVALID_FILE;
71 if(_is_drm_file(filename))
73 debug_error("%s is DRM contents\n", filename);
74 return MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
78 fd = open(filename, O_RDONLY);
80 debug_msg("Open file name is %s\n", filename);
81 unsigned int mediaSize,readSize,offSet=0;
86 debug_error("file open fail\n\n");
87 return MM_ERROR_SOUND_INTERNAL;
89 if (fstat(fd, &finfo) == -1)
91 debug_error("file get info fail\n");
93 return MM_ERROR_SOUND_INTERNAL;
96 mediaSize = (unsigned int)finfo.st_size;
99 /* get the extension (3 characters from last including NULL)*/
100 strncpy((void *)genStr,(void *)(filename+(strlen(filename)-2)),3);
102 debug_msg("ExtName of the Sound file %s\n",genStr);
105 if(strcasecmp (genStr, "dm") == 0)
107 debug_msg("It is DM file going ahead with special decoding\n");
109 /* Vlidation of the DM file */
110 readSize = read(fd,(void*)genStr,0x8);
113 debug_error("Error in Reading the file Header %x/0x8\n",readSize);
114 return MM_ERROR_SOUND_INTERNAL;
117 genStr[readSize] ='\0';
119 debug_msg("Header details of DM file %s\n",genStr);
121 if(strcasecmp (genStr, "--random") != 0)
123 debug_error("It is not a valied DM file");
124 return MM_ERROR_SOUND_INTERNAL;
127 /*checking the Media Type */
128 readSize = lseek(fd, 0x32, SEEK_SET);
131 debug_error("Error in Seeking the file to offset %x/0x32\n",readSize);
132 return MM_ERROR_SOUND_INTERNAL;
135 readSize = read(fd,(void*)genStr,0xf);
139 if(genStr[i] == (char)0xD)
146 debug_msg("Header details of DM file %s\n",genStr);
148 /*Finding the Media Offset */
150 if(strcasecmp (genStr, "audio/mpeg") == 0)
154 else if(strcasecmp (genStr, "audio/wav") == 0)
160 debug_error("It is not MP3/Wav DM file \n");
161 return MM_ERROR_SOUND_INTERNAL;
164 /*Finding the Media Size */
166 mediaSize -= (offSet + 0x28);
168 /*Seeking the file to start */
169 readSize = lseek(fd, 0x0, SEEK_SET);
173 debug_error("Error in Seeking the file to offset %x/0x32\n",readSize);
174 return MM_ERROR_SOUND_INTERNAL;
180 debug_msg("It is NON DM file going ahead with normal decoding\n");
184 mmap_buf = mmap(0, finfo.st_size, PROT_READ, MAP_SHARED, fd,0);
186 if (mmap_buf == NULL)
188 debug_error("MMAP fail\n");
190 return MM_ERROR_SOUND_INTERNAL;
192 source->ptr = mmap_buf+offSet;
193 source->medOffset = offSet;
194 debug_msg("source ptr = %p\n", source->ptr);
195 debug_msg("Med Offset Size : %d",source->medOffset);
196 source->tot_size = finfo.st_size;
197 source->cur_size = mediaSize;
198 source->type = MM_SOURCE_FILE;
201 return MM_ERROR_NONE;
205 int mm_source_open_full_memory(const void *ptr, int totsize, int alloc, MMSourceType *source)
207 int err = MM_ERROR_NONE;
210 debug_error("PTR is NULL\n");
211 return MM_ERROR_INVALID_ARGUMENT;
215 err = mm_source_open_memory(ptr, totsize, totsize, source);
220 source->tot_size = totsize;
221 source->cur_size = totsize;
223 source->type = MM_SOURCE_MEMORY_NOTALLOC;
230 int mm_source_open_memory(const void *ptr, int totsize, int size, MMSourceType *source)
232 source->ptr = (unsigned char*) malloc(totsize);
233 if (source->ptr == NULL)
235 debug_error("memory alloc fail\n");
236 return MM_ERROR_SOUND_NO_FREE_SPACE;
238 source->tot_size = totsize;
239 source->cur_size = 0;
240 source->type = MM_SOURCE_MEMORY;
243 return mm_source_append_memory(ptr, size, source);
247 int mm_source_append_memory(const void *ptr, int size, MMSourceType *source)
249 if (source->cur_size + size > source->tot_size)
251 debug_error("memory too large\n");
252 return MM_ERROR_SOUND_NO_FREE_SPACE;
255 memcpy(source->ptr + source->cur_size, ptr, size);
256 source->cur_size += size;
257 return MM_ERROR_NONE;
261 int mm_source_close(MMSourceType *source)
268 debug_critical("source is null %s\n", __func__);
269 return MM_ERROR_CLASS;
272 debug_msg("Source type = %d\n", source->type);
276 if(source->ptr != NULL)
278 source->ptr -= source->medOffset;
279 debug_msg("Med Offset Size : %d/%d",source->medOffset,source->tot_size);
280 if (munmap(source->ptr, source->tot_size) == -1) {
281 debug_error("MEM UNMAP fail\n\n");
286 case MM_SOURCE_MEMORY:
287 if(source->ptr != NULL)
290 case MM_SOURCE_MEMORY_NOTALLOC:
293 debug_critical("Unknown Source\n");
296 memset(source, 0, sizeof(MMSourceType));
297 return MM_ERROR_NONE;