1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "APM_ClientDescriptor"
18 //#define LOG_NDEBUG 0
19 
20 #include <sstream>
21 
22 #include <android-base/stringprintf.h>
23 #include <TypeConverter.h>
24 #include <utils/Log.h>
25 #include <utils/String8.h>
26 
27 #include "AudioOutputDescriptor.h"
28 #include "AudioPatch.h"
29 #include "AudioPolicyMix.h"
30 #include "ClientDescriptor.h"
31 #include "DeviceDescriptor.h"
32 #include "HwModule.h"
33 #include "IOProfile.h"
34 
35 namespace android {
36 
toShortString() const37 std::string ClientDescriptor::toShortString() const
38 {
39     std::stringstream ss;
40 
41     ss << "PortId: " << mPortId << " SessionId: " << mSessionId << " Uid: " << mUid;
42     return ss.str();
43 }
44 
dump(String8 * dst,int spaces) const45 void ClientDescriptor::dump(String8 *dst, int spaces) const
46 {
47     dst->appendFormat("Port ID: %d; Session ID: %d; uid %d; State: %s\n",
48             mPortId, mSessionId, mUid, mActive ? "Active" : "Inactive");
49     dst->appendFormat("%*s%s; %d; Channel mask: 0x%x\n", spaces, "",
50             audio_format_to_string(mConfig.format), mConfig.sample_rate, mConfig.channel_mask);
51     dst->appendFormat("%*sAttributes: %s\n", spaces, "", toString(mAttributes).c_str());
52     if (mPreferredDeviceId != AUDIO_PORT_HANDLE_NONE) {
53         dst->appendFormat("%*sPreferred Device Port ID: %d;\n", spaces, "", mPreferredDeviceId);
54     }
55 }
56 
dump(String8 * dst,int spaces) const57 void TrackClientDescriptor::dump(String8 *dst, int spaces) const
58 {
59     ClientDescriptor::dump(dst, spaces);
60     dst->appendFormat("%*sStream: %d; Flags: %08x; Refcount: %d; InternalMute: %s\n",
61             spaces, "", mStream, mFlags, mActivityCount, mInternalMute ? "Yes" : "No");
62     dst->appendFormat("%*sDAP Primary Mix: %p\n", spaces, "", mPrimaryMix.promote().get());
63     if (!mSecondaryOutputs.empty()) {
64         dst->appendFormat("%*sDAP Secondary Outputs: ", spaces - 2, "");
65         for (auto desc : mSecondaryOutputs) {
66             dst->appendFormat("%d, ", desc.promote() == nullptr ? 0 : desc.promote()->mIoHandle);
67         }
68         dst->append("\n");
69     }
70 }
71 
toShortString() const72 std::string TrackClientDescriptor::toShortString() const
73 {
74     std::stringstream ss;
75     ss << ClientDescriptor::toShortString() << " Stream: " << mStream;
76     return ss.str();
77 }
78 
trackEffectEnabled(const sp<EffectDescriptor> & effect,bool enabled)79 void RecordClientDescriptor::trackEffectEnabled(const sp<EffectDescriptor> &effect, bool enabled)
80 {
81     if (enabled) {
82         mEnabledEffects.replaceValueFor(effect->mId, effect);
83     } else {
84         mEnabledEffects.removeItem(effect->mId);
85     }
86 }
87 
dump(String8 * dst,int spaces) const88 void RecordClientDescriptor::dump(String8 *dst, int spaces) const
89 {
90     ClientDescriptor::dump(dst, spaces);
91     dst->appendFormat("%*sSource: %d; Flags: %08x; is soundtrigger: %d\n",
92             spaces, "", mSource, mFlags, mIsSoundTrigger);
93     mEnabledEffects.dump(dst, spaces + 2 /*spaces*/, false /*verbose*/);
94 }
95 
SourceClientDescriptor(audio_port_handle_t portId,uid_t uid,audio_attributes_t attributes,const struct audio_port_config & config,const sp<DeviceDescriptor> & srcDevice,audio_stream_type_t stream,product_strategy_t strategy,VolumeSource volumeSource,bool isInternal,bool isCallRx,bool isCallTx)96 SourceClientDescriptor::SourceClientDescriptor(audio_port_handle_t portId, uid_t uid,
97          audio_attributes_t attributes, const struct audio_port_config &config,
98          const sp<DeviceDescriptor>& srcDevice, audio_stream_type_t stream,
99          product_strategy_t strategy, VolumeSource volumeSource,
100          bool isInternal, bool isCallRx, bool isCallTx) :
101     TrackClientDescriptor::TrackClientDescriptor(portId, uid, AUDIO_SESSION_NONE, attributes,
102         {config.sample_rate, config.channel_mask, config.format}, AUDIO_PORT_HANDLE_NONE,
103         stream, strategy, volumeSource, AUDIO_OUTPUT_FLAG_NONE, false,
104         {} /* Sources do not support secondary outputs*/, nullptr),
105     mSrcDevice(srcDevice), mIsInternal(isInternal),
106     mIsCallRx(isCallRx), mIsCallTx(isCallTx)
107 {
108 }
109 
setSwOutput(const sp<SwAudioOutputDescriptor> & swOutput,bool closeOutput)110 void SourceClientDescriptor::setSwOutput(
111         const sp<SwAudioOutputDescriptor>& swOutput, bool closeOutput)
112 {
113     mSwOutput = swOutput;
114     mCloseOutput = closeOutput;
115 }
116 
setHwOutput(const sp<HwAudioOutputDescriptor> & hwOutput)117 void SourceClientDescriptor::setHwOutput(const sp<HwAudioOutputDescriptor>& hwOutput)
118 {
119     mHwOutput = hwOutput;
120 }
121 
dump(String8 * dst,int spaces) const122 void SourceClientDescriptor::dump(String8 *dst, int spaces) const
123 {
124     TrackClientDescriptor::dump(dst, spaces);
125     const std::string prefix = base::StringPrintf("%*sDevice: ", spaces, "");
126     dst->appendFormat("%s", prefix.c_str());
127     mSrcDevice->dump(dst, prefix.size());
128 }
129 
dump(String8 * dst) const130 void SourceClientCollection::dump(String8 *dst) const
131 {
132     dst->appendFormat("\n Audio sources (%zu):\n", size());
133     for (size_t i = 0; i < size(); i++) {
134         const std::string prefix = base::StringPrintf("  %zu. ", i + 1);
135         dst->appendFormat("%s", prefix.c_str());
136         valueAt(i)->dump(dst, prefix.size());
137     }
138 }
139 
140 }; //namespace android
141