1 /*
2 * Copyright (c) 2023 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 #ifndef LOG_TAG
16 #define LOG_TAG "CockpitPhoneRouter"
17 #endif
18
19 #include "cockpit_phone_router.h"
20
21 using namespace std;
22
23 namespace OHOS {
24 namespace AudioStandard {
25
GetBTCarDevices(const vector<shared_ptr<AudioDeviceDescriptor>> & descs)26 vector<shared_ptr<AudioDeviceDescriptor>> GetBTCarDevices(const vector<shared_ptr<AudioDeviceDescriptor>> &descs)
27 {
28 vector<shared_ptr<AudioDeviceDescriptor>> carDescs;
29 for (const auto &desc : descs) {
30 if (desc == nullptr || desc->deviceCategory_ != BT_CAR) {
31 continue;
32 }
33 carDescs.push_back(make_shared<AudioDeviceDescriptor>(*desc));
34 }
35 return carDescs;
36 }
37
GetMediaRenderDevice(StreamUsage streamUsage,int32_t clientUID)38 shared_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetMediaRenderDevice(StreamUsage streamUsage, int32_t clientUID)
39 {
40 return make_shared<AudioDeviceDescriptor>();
41 }
42
GetCallRenderDevice(StreamUsage streamUsage,int32_t clientUID)43 shared_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetCallRenderDevice(StreamUsage streamUsage, int32_t clientUID)
44 {
45 vector<shared_ptr<AudioDeviceDescriptor>> descs =
46 AudioDeviceManager::GetAudioDeviceManager().GetCommRenderPublicDevices();
47 vector<shared_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
48 shared_ptr<AudioDeviceDescriptor> desc = GetLatestNonExcludedConnectDevice(CALL_OUTPUT_DEVICES, carDescs);
49 AUDIO_DEBUG_LOG("streamUsage %{public}d clientUID %{public}d fetch device %{public}d", streamUsage,
50 clientUID, desc->deviceType_);
51 return desc;
52 }
53
GetCallCaptureDevice(SourceType sourceType,int32_t clientUID)54 shared_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetCallCaptureDevice(SourceType sourceType, int32_t clientUID)
55 {
56 vector<shared_ptr<AudioDeviceDescriptor>> descs =
57 AudioDeviceManager::GetAudioDeviceManager().GetCommCapturePublicDevices();
58 vector<shared_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
59 shared_ptr<AudioDeviceDescriptor> desc = GetLatestNonExcludedConnectDevice(CALL_INPUT_DEVICES, carDescs);
60 AUDIO_DEBUG_LOG("sourceType %{public}d clientUID %{public}d fetch device %{public}d", sourceType,
61 clientUID, desc->deviceType_);
62 return desc;
63 }
64
GetRingRenderDevices(StreamUsage streamUsage,int32_t clientUID)65 vector<std::shared_ptr<AudioDeviceDescriptor>> CockpitPhoneRouter::GetRingRenderDevices(StreamUsage streamUsage,
66 int32_t clientUID)
67 {
68 AudioRingerMode curRingerMode = audioPolicyManager_.GetRingerMode();
69 vector<shared_ptr<AudioDeviceDescriptor>> descs;
70 vector<shared_ptr<AudioDeviceDescriptor>> curDescs =
71 AudioDeviceManager::GetAudioDeviceManager().GetCommRenderPublicDevices();
72 vector<shared_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
73 shared_ptr<AudioDeviceDescriptor> latestConnDesc = GetLatestNonExcludedConnectDevice(CALL_OUTPUT_DEVICES, carDescs);
74 if (!latestConnDesc.get()) {
75 AUDIO_INFO_LOG("Have no latest connecte desc, dont add the other device.");
76 return descs;
77 }
78 if (latestConnDesc->getType() == DEVICE_TYPE_NONE) {
79 AUDIO_INFO_LOG("Latest connecte desc type is none, dont add the other device.");
80 return descs;
81 }
82
83 if (NeedLatestConnectWithDefaultDevices(latestConnDesc->getType())) {
84 // Add the latest connected device.
85 descs.push_back(move(latestConnDesc));
86 switch (streamUsage) {
87 case STREAM_USAGE_ALARM:
88 // Add default device at same time for alarm.
89 descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
90 break;
91 case STREAM_USAGE_VOICE_RINGTONE:
92 case STREAM_USAGE_RINGTONE:
93 if (curRingerMode == RINGER_MODE_NORMAL) {
94 // Add default devices at same time only in ringer normal mode.
95 descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
96 }
97 break;
98 default:
99 AUDIO_DEBUG_LOG("Don't add default device at the same time.");
100 break;
101 }
102 } else if (latestConnDesc->getType() != DEVICE_TYPE_NONE) {
103 descs.push_back(move(latestConnDesc));
104 } else {
105 descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
106 }
107 return descs;
108 }
109
GetRecordCaptureDevice(SourceType sourceType,int32_t clientUID)110 shared_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetRecordCaptureDevice(SourceType sourceType, int32_t clientUID)
111 {
112 return make_shared<AudioDeviceDescriptor>();
113 }
114
GetToneRenderDevice(StreamUsage streamUsage,int32_t clientUID)115 shared_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetToneRenderDevice(StreamUsage streamUsage, int32_t clientUID)
116 {
117 return make_shared<AudioDeviceDescriptor>();
118 }
119
120 } // namespace AudioStandard
121 } // namespace OHOS