MimeTypeUtil.cpp revision fdd65a0fc7df2c878cc601e4c0f4021cb264f051
1/* 2 * Copyright (C) 2010 The Android Open Source Project 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#include <MimeTypeUtil.h> 18#include <utils/Log.h> 19 20namespace android { 21 22#undef LOG_TAG 23#define LOG_TAG "MimeTypeUtil" 24 25enum { 26 MIMETYPE_AUDIO = 0, 27 MIMETYPE_APPLICATION = 1, 28 MIMETYPE_IMAGE = 2, 29 MIMETYPE_VIDEO = 3, 30 MIMETYPE_LAST = -1, 31}; 32 33struct MimeGroup{ 34 int type; // Audio, video,.. use the enum values 35 const char* pGroup; // "audio/", "video/",.. should contain the last "/" 36 int size; // Number of bytes. e.g. "audio/" = 6 bytes 37}; 38 39struct MimeTypeList{ 40 int type; 41 const char* pMimeExt; // Everything after the '/' e.g. audio/x-mpeg -> "x-mpeg" 42 int size; // Number of bytes. e.g. "x-mpeg" = 6 bytes 43 const char* pMimeType; // Mimetype that should be returned 44}; 45 46 47// Known mimetypes by android 48static const char mime_type_audio_mpeg[] = "audio/mpeg"; 49static const char mime_type_audio_3gpp[] = "audio/3gpp"; 50static const char mime_type_audio_amr[] = "audio/amr-wb"; 51static const char mime_type_audio_aac[] = "audio/mp4a-latm"; 52static const char mime_type_audio_wav[] = "audio/wav"; 53 54static const char mime_type_video_mpeg4[] = "video/mpeg4"; 55static const char mime_type_video_3gpp[] = "video/3gpp"; 56 57// Known mimetype groups 58static const char mime_group_audio[] = "audio/"; 59static const char mime_group_application[] = "application/"; 60static const char mime_group_image[] = "image/"; 61static const char mime_group_video[] = "video/"; 62 63static struct MimeGroup mimeGroup[] = { 64 {MIMETYPE_AUDIO, mime_group_audio, sizeof(mime_group_audio)-1}, 65 {MIMETYPE_APPLICATION, mime_group_application, sizeof(mime_group_application)-1}, 66 {MIMETYPE_IMAGE, mime_group_image, sizeof(mime_group_image)-1}, 67 {MIMETYPE_VIDEO, mime_group_video, sizeof(mime_group_video)-1}, 68 {MIMETYPE_LAST, NULL, 0} // Must be last entry 69}; 70 71// List of all mimetypes that should be converted. 72static struct MimeTypeList mimeTypeList[] = { 73 // Mp3 mime types 74 {MIMETYPE_AUDIO, "mp3", sizeof("mp3")-1, mime_type_audio_mpeg}, 75 {MIMETYPE_AUDIO, "x-mpeg", sizeof("x-mpeg")-1, mime_type_audio_mpeg}, 76 {MIMETYPE_AUDIO, "x-mp3", sizeof("x-mp3")-1, mime_type_audio_mpeg}, 77 {MIMETYPE_AUDIO, "mpg", sizeof("mpg")-1, mime_type_audio_mpeg}, 78 {MIMETYPE_AUDIO, "mpg3", sizeof("mpg")-1, mime_type_audio_mpeg}, 79 {MIMETYPE_AUDIO, "x-mpg", sizeof("x-mpg")-1, mime_type_audio_mpeg}, 80 {MIMETYPE_AUDIO, "x-mpegaudio", sizeof("x-mpegaudio")-1, mime_type_audio_mpeg}, 81 82 // 3gpp audio mime types 83 {MIMETYPE_AUDIO, "3gp", sizeof("3gp")-1, mime_type_audio_3gpp}, 84 85 // Amr audio mime types 86 {MIMETYPE_AUDIO, "amr", sizeof("amr")-1, mime_type_audio_amr}, 87 88 // Aac audio mime types 89 {MIMETYPE_AUDIO, "aac", sizeof("aac")-1, mime_type_audio_aac}, 90 91 // Wav audio mime types 92 {MIMETYPE_AUDIO, "x-wav", sizeof("x-wav")-1, mime_type_audio_wav}, 93 94 // Mpeg4 video mime types 95 {MIMETYPE_VIDEO, "mpg4", sizeof("mpg4")-1, mime_type_video_mpeg4}, 96 {MIMETYPE_VIDEO, "mp4v-es", sizeof("mp4v-es")-1, mime_type_video_mpeg4}, 97 98 // 3gpp video mime types 99 {MIMETYPE_VIDEO, "3gp", sizeof("3gp")-1, mime_type_video_3gpp}, 100 101 // Must be last entry 102 {MIMETYPE_LAST, NULL, 0, NULL} 103}; 104 105/** 106 * May convert the mimetype if there is a well known 107 * replacement mimetype otherwise the original mimetype 108 * is returned. 109 * 110 * @param mimeType - mimetype in lower case to convert. 111 * 112 * @return mimetype or null. 113 */ 114String8 MimeTypeUtil::convertMimeType(String8& mimeType) { 115 String8 result = mimeType; 116 const char* pTmp; 117 const char* pMimeType; 118 struct MimeGroup* pGroup; 119 struct MimeTypeList* pMimeItem; 120 int len; 121 122 pMimeType = mimeType.string(); 123 if (NULL != pMimeType) { 124 /* Check which group the mimetype is */ 125 pGroup = mimeGroup; 126 127 while (MIMETYPE_LAST != pGroup->type) { 128 if (0 == strncmp(pMimeType, pGroup->pGroup, pGroup->size)) { 129 break; 130 } 131 pGroup++; 132 } 133 134 /* Go through the mimetype list. Only check items of the correct group */ 135 if (MIMETYPE_LAST != pGroup->type) { 136 pMimeItem = mimeTypeList; 137 len = strlen (pMimeType+pGroup->size); 138 139 while (MIMETYPE_LAST != pMimeItem->type) { 140 if ((len == pMimeItem->size) && 141 (0 == strcmp(pMimeType+pGroup->size, pMimeItem->pMimeExt))) { 142 result = String8(pMimeItem->pMimeType); 143 break; 144 } 145 pMimeItem++; 146 } 147 } 148 LOGI("convertMimeType got mimetype %s, converted into mimetype %s", 149 pMimeType, result.string()); 150 } 151 152 return result; 153} 154}; 155