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<unique_ptr<AudioDeviceDescriptor>> & descs)26 vector<unique_ptr<AudioDeviceDescriptor>> GetBTCarDevices(const vector<unique_ptr<AudioDeviceDescriptor>> &descs)
27 {
28     vector<unique_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_unique<AudioDeviceDescriptor>(*desc));
34     }
35     return carDescs;
36 }
37 
GetMediaRenderDevice(StreamUsage streamUsage,int32_t clientUID)38 unique_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetMediaRenderDevice(StreamUsage streamUsage, int32_t clientUID)
39 {
40     return make_unique<AudioDeviceDescriptor>();
41 }
42 
GetCallRenderDevice(StreamUsage streamUsage,int32_t clientUID)43 unique_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetCallRenderDevice(StreamUsage streamUsage, int32_t clientUID)
44 {
45     vector<unique_ptr<AudioDeviceDescriptor>> descs =
46         AudioDeviceManager::GetAudioDeviceManager().GetCommRenderPublicDevices();
47     vector<unique_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
48     unique_ptr<AudioDeviceDescriptor> desc = GetLatestConnectDeivce(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 unique_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetCallCaptureDevice(SourceType sourceType, int32_t clientUID)
55 {
56     vector<unique_ptr<AudioDeviceDescriptor>> descs =
57         AudioDeviceManager::GetAudioDeviceManager().GetCommCapturePublicDevices();
58     vector<unique_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
59     unique_ptr<AudioDeviceDescriptor> desc = GetLatestConnectDeivce(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::unique_ptr<AudioDeviceDescriptor>> CockpitPhoneRouter::GetRingRenderDevices(StreamUsage streamUsage,
66     int32_t clientUID)
67 {
68     AudioRingerMode curRingerMode = audioPolicyManager_.GetRingerMode();
69     vector<unique_ptr<AudioDeviceDescriptor>> descs;
70     vector<unique_ptr<AudioDeviceDescriptor>> curDescs =
71         AudioDeviceManager::GetAudioDeviceManager().GetCommRenderPublicDevices();
72     vector<unique_ptr<AudioDeviceDescriptor>> carDescs = GetBTCarDevices(descs);
73     unique_ptr<AudioDeviceDescriptor> latestConnDesc = GetLatestConnectDeivce(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 (latestConnDesc->getType() == DEVICE_TYPE_WIRED_HEADSET ||
84         latestConnDesc->getType() == DEVICE_TYPE_WIRED_HEADPHONES ||
85         latestConnDesc->getType() == DEVICE_TYPE_BLUETOOTH_SCO ||
86         latestConnDesc->getType() == DEVICE_TYPE_USB_HEADSET ||
87         latestConnDesc->getType() == DEVICE_TYPE_USB_ARM_HEADSET) {
88         // Add the latest connected device.
89         descs.push_back(move(latestConnDesc));
90         switch (streamUsage) {
91             case STREAM_USAGE_ALARM:
92                 // Add default device at same time for alarm.
93                 descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
94                 break;
95             case STREAM_USAGE_VOICE_RINGTONE:
96             case STREAM_USAGE_RINGTONE:
97                 if (curRingerMode == RINGER_MODE_NORMAL) {
98                     // Add default devices at same time only in ringer normal mode.
99                     descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
100                 }
101                 break;
102             default:
103                 AUDIO_DEBUG_LOG("Don't add default device at the same time.");
104                 break;
105         }
106     } else if (latestConnDesc->getType() != DEVICE_TYPE_NONE) {
107         descs.push_back(move(latestConnDesc));
108     } else {
109         descs.push_back(AudioDeviceManager::GetAudioDeviceManager().GetRenderDefaultDevice());
110     }
111     return descs;
112 }
113 
GetRecordCaptureDevice(SourceType sourceType,int32_t clientUID)114 unique_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetRecordCaptureDevice(SourceType sourceType, int32_t clientUID)
115 {
116     return make_unique<AudioDeviceDescriptor>();
117 }
118 
GetToneRenderDevice(StreamUsage streamUsage,int32_t clientUID)119 unique_ptr<AudioDeviceDescriptor> CockpitPhoneRouter::GetToneRenderDevice(StreamUsage streamUsage, int32_t clientUID)
120 {
121     return make_unique<AudioDeviceDescriptor>();
122 }
123 
124 } // namespace AudioStandard
125 } // namespace OHOS