1 /*
2  * Copyright (C) 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 "recorder_napi_utils.h"
17 #include <map>
18 #include <string>
19 #include "tokenid_kit.h"
20 #include "ipc_skeleton.h"
21 #include "access_token.h"
22 #include "accesstoken_kit.h"
23 #include "media_errors.h"
24 
25 using namespace OHOS::Security::AccessToken;
26 
27 namespace OHOS {
28 namespace Media {
29 using namespace MediaAVCodec;
30 const std::map<std::string_view, int32_t> g_mimeStrToCodecFormat = {
31     { CodecMimeType::AUDIO_AAC, AudioCodecFormat::AAC_LC },
32     { CodecMimeType::VIDEO_AVC, VideoCodecFormat::H264 },
33     { CodecMimeType::VIDEO_MPEG4, VideoCodecFormat::MPEG4 },
34 };
35 
36 const std::map<std::string, OutputFormatType> g_extensionToOutputFormat = {
37     { "mp4", OutputFormatType::FORMAT_MPEG_4 },
38     { "m4a", OutputFormatType::FORMAT_M4A },
39 };
40 
IsSystemApp()41 bool IsSystemApp()
42 {
43     uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
44     bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx);
45     return isSystemApp;
46 }
47 
SystemPermission()48 bool SystemPermission()
49 {
50     auto tokenId = IPCSkeleton::GetCallingTokenID();
51     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
52     if (tokenType == Security::AccessToken::TOKEN_NATIVE || tokenType == Security::AccessToken::TOKEN_SHELL) {
53         return true;
54     }
55     return IsSystemApp();
56 }
57 
MapMimeToAudioCodecFormat(const std::string & mime,AudioCodecFormat & codecFormat)58 int32_t MapMimeToAudioCodecFormat(const std::string &mime, AudioCodecFormat &codecFormat)
59 {
60     auto iter = g_mimeStrToCodecFormat.find(mime);
61     if (iter != g_mimeStrToCodecFormat.end()) {
62         codecFormat = static_cast<AudioCodecFormat>(iter->second);
63     }
64     return MSERR_INVALID_VAL;
65 }
66 
MapMimeToVideoCodecFormat(const std::string & mime,VideoCodecFormat & codecFormat)67 int32_t MapMimeToVideoCodecFormat(const std::string &mime, VideoCodecFormat &codecFormat)
68 {
69     auto iter = g_mimeStrToCodecFormat.find(mime);
70     if (iter != g_mimeStrToCodecFormat.end()) {
71         codecFormat = static_cast<VideoCodecFormat>(iter->second);
72     }
73     return MSERR_INVALID_VAL;
74 }
75 
MapExtensionNameToOutputFormat(const std::string & extension,OutputFormatType & type)76 int32_t MapExtensionNameToOutputFormat(const std::string &extension, OutputFormatType &type)
77 {
78     auto iter = g_extensionToOutputFormat.find(extension);
79     if (iter != g_extensionToOutputFormat.end()) {
80         type = iter->second;
81     }
82     return MSERR_INVALID_VAL;
83 }
84 } // namespace Media
85 } // namespace OHOS