1 /*
2  * Copyright (C) 2022 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_profiles_impl.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "i_media_service.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_RECORDER, "RecorderProfilesImpl"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
CreateRecorderProfiles()27 RecorderProfiles& RecorderProfilesFactory::CreateRecorderProfiles()
28 {
29     return RecorderProfilesImpl::GetInstance();
30 }
31 
GetInstance()32 RecorderProfiles& RecorderProfilesImpl::GetInstance()
33 {
34     static RecorderProfilesImpl instance;
35     instance.Init();
36     return instance;
37 }
38 
Init()39 int32_t RecorderProfilesImpl::Init()
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     if (recorderProfilesService_ == nullptr) {
43         recorderProfilesService_ = MediaServiceFactory::GetInstance().CreateRecorderProfilesService();
44     }
45 
46     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr,
47         MSERR_NO_MEMORY, "failed to create RecorderProfiles service");
48     return MSERR_OK;
49 }
50 
RecorderProfilesImpl()51 RecorderProfilesImpl::RecorderProfilesImpl()
52 {
53     MEDIA_LOGD("RecorderProfilesImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
54 }
55 
~RecorderProfilesImpl()56 RecorderProfilesImpl::~RecorderProfilesImpl()
57 {
58     if (recorderProfilesService_ != nullptr) {
59         (void)MediaServiceFactory::GetInstance().DestroyMediaProfileService(recorderProfilesService_);
60         recorderProfilesService_ = nullptr;
61     }
62     MEDIA_LOGD("RecorderProfilesImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
63 }
64 
IsAudioRecorderConfigSupported(const AudioRecorderProfile & profile)65 bool RecorderProfilesImpl::IsAudioRecorderConfigSupported(const AudioRecorderProfile &profile)
66 {
67     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr, false, "RecorderProfiles service does not exist.");
68     RecorderProfilesData profileData;
69     profileData.recorderProfile.containerFormatType = profile.containerFormatType;
70     profileData.recorderProfile.audioCodec = profile.audioCodec;
71     profileData.recorderProfile.audioBitrate = profile.audioBitrate;
72     profileData.recorderProfile.audioSampleRate = profile.audioSampleRate;
73     profileData.recorderProfile.audioChannels = profile.audioChannels;
74     return recorderProfilesService_->IsAudioRecorderConfigSupported(profileData);
75 }
76 
HasVideoRecorderProfile(int32_t sourceId,int32_t qualityLevel)77 bool RecorderProfilesImpl::HasVideoRecorderProfile(int32_t sourceId, int32_t qualityLevel)
78 {
79     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr, false, "RecorderProfiles service does not exist.");
80     return recorderProfilesService_->HasVideoRecorderProfile(sourceId, qualityLevel);
81 }
82 
GetAudioRecorderCaps()83 std::vector<std::shared_ptr<AudioRecorderCaps>> RecorderProfilesImpl::GetAudioRecorderCaps()
84 {
85     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr,
86         std::vector<std::shared_ptr<AudioRecorderCaps>>(), "RecorderProfiles service does not exist.");
87     std::vector<RecorderProfilesData> capabilityArray = recorderProfilesService_->GetAudioRecorderCapsInfo();
88     std::vector<std::shared_ptr<AudioRecorderCaps>> audioRecorderCapsArray;
89     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
90         std::shared_ptr<AudioRecorderCaps> audioRecorderCaps = std::make_shared<AudioRecorderCaps>((*iter).audioCaps);
91         CHECK_AND_RETURN_RET_LOG(audioRecorderCaps != nullptr, audioRecorderCapsArray, "Is null mem");
92         audioRecorderCapsArray.push_back(audioRecorderCaps);
93     }
94     return audioRecorderCapsArray;
95 }
96 
GetVideoRecorderCaps()97 std::vector<std::shared_ptr<VideoRecorderCaps>> RecorderProfilesImpl::GetVideoRecorderCaps()
98 {
99     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr,
100         std::vector<std::shared_ptr<VideoRecorderCaps>>(), "RecorderProfiles service does not exist.");
101     std::vector<RecorderProfilesData> capabilityArray = recorderProfilesService_->GetVideoRecorderCapsInfo();
102     std::vector<std::shared_ptr<VideoRecorderCaps>> videoRecorderCapsArray;
103     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
104         std::shared_ptr<VideoRecorderCaps> videoRecorderCaps = std::make_shared<VideoRecorderCaps>((*iter).videoCaps);
105         CHECK_AND_RETURN_RET_LOG(videoRecorderCaps != nullptr, videoRecorderCapsArray, "Is null mem");
106         videoRecorderCapsArray.push_back(videoRecorderCaps);
107     }
108     return videoRecorderCapsArray;
109 }
110 
GetVideoRecorderProfile(int32_t sourceId,int32_t qualityLevel)111 std::shared_ptr<VideoRecorderProfile> RecorderProfilesImpl::GetVideoRecorderProfile(int32_t sourceId,
112     int32_t qualityLevel)
113 {
114     CHECK_AND_RETURN_RET_LOG(recorderProfilesService_ != nullptr,
115         nullptr, "RecorderProfiles service does not exist.");
116     RecorderProfilesData capability = recorderProfilesService_->GetVideoRecorderProfileInfo(sourceId, qualityLevel);
117     std::shared_ptr<VideoRecorderProfile> videoRecorderProfile =
118         std::make_shared<VideoRecorderProfile>(capability.recorderProfile);
119     CHECK_AND_RETURN_RET_LOG(videoRecorderProfile != nullptr, videoRecorderProfile, "Is null mem");
120     return videoRecorderProfile;
121 }
122 }  // namespace Media
123 }  // namespace OHOS
124