• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "media_utils.h"
17 #include <regex>
18 #include <sys/stat.h>
19 #include "media_errors.h"
20 #include "plugin/common/plugin_audio_tags.h"
21 namespace OHOS {
22 namespace Media {
23 namespace {
24 const std::pair<ErrorCode, int32_t> g_errorCodePair[] = {
25     {ErrorCode::SUCCESS, MSERR_OK},
26     {ErrorCode::ERROR_UNKNOWN, MSERR_UNKNOWN},
27     {ErrorCode::ERROR_AGAIN, MSERR_UNKNOWN},
28     {ErrorCode::ERROR_UNIMPLEMENTED, MSERR_UNSUPPORT},
29     {ErrorCode::ERROR_INVALID_PARAMETER_VALUE, MSERR_INVALID_VAL},
30     {ErrorCode::ERROR_INVALID_PARAMETER_TYPE, MSERR_INVALID_VAL},
31     {ErrorCode::ERROR_INVALID_OPERATION, MSERR_INVALID_OPERATION},
32     {ErrorCode::ERROR_UNSUPPORTED_FORMAT, MSERR_UNSUPPORT_CONTAINER_TYPE},
33     {ErrorCode::ERROR_NOT_EXISTED, MSERR_OPEN_FILE_FAILED},
34     {ErrorCode::ERROR_TIMED_OUT, MSERR_EXT_TIMEOUT},
35     {ErrorCode::ERROR_NO_MEMORY, MSERR_EXT_NO_MEMORY},
36     {ErrorCode::ERROR_INVALID_STATE, MSERR_INVALID_STATE},
37 };
38 const std::map<OutputFormatType, std::string> g_outputFormatToFormat = {
39     {OutputFormatType::FORMAT_DEFAULT, std::string("audio_%Y%m%d%H%M%S.m4a")},
40     {OutputFormatType::FORMAT_MPEG_4, std::string("video_%Y%m%d%H%M%S.mp4")},
41     {OutputFormatType::FORMAT_M4A, std::string("audio_%Y%m%d%H%M%S.m4a")},
42 };
43 }  // namespace
44 namespace Record {
TransErrorCode(ErrorCode errorCode)45 int TransErrorCode(ErrorCode errorCode)
46 {
47     for (const auto& errPair : g_errorCodePair) {
48         if (errPair.first == errorCode) {
49             return errPair.second;
50         }
51     }
52     return MSERR_UNKNOWN;
53 }
TransAudioInputType(OHOS::Media::AudioSourceType sourceType)54 Plugin::SrcInputType TransAudioInputType(OHOS::Media::AudioSourceType sourceType)
55 {
56     const static std::pair<OHOS::Media::AudioSourceType, Plugin::SrcInputType> mapArray[] = {
57         {OHOS::Media::AudioSourceType::AUDIO_MIC, Plugin::SrcInputType::AUD_MIC},
58         {OHOS::Media::AudioSourceType::AUDIO_SOURCE_DEFAULT, Plugin::SrcInputType::AUD_MIC},
59     };
60     for (const auto& item : mapArray) {
61         if (item.first == sourceType) {
62             return item.second;
63         }
64     }
65     return Plugin::SrcInputType::UNKNOWN;
66 }
TransVideoInputType(OHOS::Media::VideoSourceType sourceType)67 Plugin::SrcInputType TransVideoInputType(OHOS::Media::VideoSourceType sourceType)
68 {
69     const static std::pair<OHOS::Media::VideoSourceType, Plugin::SrcInputType> mapArray[] = {
70         {OHOS::Media::VideoSourceType::VIDEO_SOURCE_SURFACE_YUV, Plugin::SrcInputType::VID_SURFACE_YUV},
71         {OHOS::Media::VideoSourceType::VIDEO_SOURCE_SURFACE_ES, Plugin::SrcInputType::VID_SURFACE_ES},
72     };
73     for (const auto& item : mapArray) {
74         if (item.first == sourceType) {
75             return item.second;
76         }
77     }
78     return Plugin::SrcInputType::UNKNOWN;
79 }
TransAudioEncoderFmt(OHOS::Media::AudioCodecFormat aFormat,Plugin::Meta & encoderMeta)80 bool TransAudioEncoderFmt(OHOS::Media::AudioCodecFormat aFormat, Plugin::Meta& encoderMeta)
81 {
82     switch (aFormat) {
83         case OHOS::Media::AudioCodecFormat::AUDIO_DEFAULT:
84         case OHOS::Media::AudioCodecFormat::AAC_LC:
85             encoderMeta.SetString(Plugin::MetaID::MIME, MEDIA_MIME_AUDIO_AAC);
86             encoderMeta.SetData(Plugin::MetaID::AUDIO_AAC_PROFILE, Plugin::AudioAacProfile::LC);
87             return true;
88         default:
89             break;
90     }
91     return false;
92 }
93 
IsDirectory(const std::string & path)94 bool IsDirectory(const std::string& path)
95 {
96     struct stat s {};
97     return ((stat(path.c_str(), &s) == 0) && S_ISDIR(s.st_mode));
98 }
ConvertDirPathToFilePath(const std::string & dirPath,OutputFormatType outputFormatType,std::string & filePath)99 bool ConvertDirPathToFilePath(const std::string& dirPath, OutputFormatType outputFormatType, std::string& filePath)
100 {
101     std::regex reg("\\\\");
102     filePath = std::regex_replace(dirPath, reg, "/") ;
103     if (filePath[filePath.size() - 1] != '/') {
104         filePath += "/";
105     }
106     if (g_outputFormatToFormat.count(outputFormatType) != 0) {
107         char fileName[32] = { 0 }; /// magic number 32
108         auto tm = time(nullptr);
109         strftime(fileName, sizeof(fileName), g_outputFormatToFormat.at(outputFormatType).c_str(), localtime(&tm));
110         filePath += fileName;
111         return true;
112     } else {
113         return false;
114     }
115 }
116 }  // namespace Record
117 }  // namespace Media
118 }  // namespace OHOS