• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "audio_stream_descriptor.h"
17 
18 #include <cinttypes>
19 #include "audio_common_log.h"
20 #include "audio_utils.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 static const int32_t MAX_STREAM_DESCRIPTORS_SIZE = 1000;
25 
StreamStatusToString(AudioStreamStatus status)26 static const char *StreamStatusToString(AudioStreamStatus status)
27 {
28     switch (status) {
29         case STREAM_STATUS_NEW:
30             return "NEW";
31         case STREAM_STATUS_STARTED:
32             return "STARTED";
33         case STREAM_STATUS_PAUSED:
34             return "PAUSED";
35         case STREAM_STATUS_STOPPED:
36             return "STOPPED";
37         case STREAM_STATUS_RELEASED:
38             return "RELEASED";
39         default:
40             return "UNKNOWN";
41     }
42 }
43 
AudioStreamDescriptor()44 AudioStreamDescriptor::AudioStreamDescriptor()
45 {
46 }
47 
~AudioStreamDescriptor()48 AudioStreamDescriptor::~AudioStreamDescriptor()
49 {
50 }
51 
Marshalling(Parcel & parcel) const52 bool AudioStreamDescriptor::Marshalling(Parcel &parcel) const
53 {
54     return streamInfo_.Marshalling(parcel) &&
55         parcel.WriteUint32(audioMode_) &&
56         parcel.WriteUint32(audioFlag_) &&
57         parcel.WriteUint32(routeFlag_) &&
58         parcel.WriteInt64(createTimeStamp_) &&
59         parcel.WriteInt64(startTimeStamp_) &&
60         rendererInfo_.Marshalling(parcel) &&
61         capturerInfo_.Marshalling(parcel) &&
62         parcel.WriteInt32(appInfo_.appUid) &&
63         parcel.WriteUint32(appInfo_.appTokenId) &&
64         parcel.WriteInt32(appInfo_.appPid) &&
65         parcel.WriteUint64(appInfo_.appFullTokenId) &&
66         parcel.WriteUint32(sessionId_) &&
67         parcel.WriteInt32(callerUid_) &&
68         parcel.WriteInt32(callerPid_) &&
69         parcel.WriteUint32(streamAction_) &&
70         WriteDeviceDescVectorToParcel(parcel, oldDeviceDescs_) &&
71         WriteDeviceDescVectorToParcel(parcel, newDeviceDescs_);
72 }
73 
Unmarshalling(Parcel & parcel)74 AudioStreamDescriptor *AudioStreamDescriptor::Unmarshalling(Parcel &parcel)
75 {
76     auto info = new(std::nothrow) AudioStreamDescriptor();
77     if (info == nullptr) {
78         return nullptr;
79     }
80 
81     info->streamInfo_.UnmarshallingSelf(parcel);
82     info->audioMode_ = static_cast<AudioMode>(parcel.ReadUint32());
83     info->audioFlag_ = static_cast<AudioFlag>(parcel.ReadUint32());
84     info->routeFlag_ = static_cast<uint32_t>(parcel.ReadUint32());
85     info->createTimeStamp_ = parcel.ReadInt64();
86     info->startTimeStamp_ = parcel.ReadInt64();
87     info->rendererInfo_.UnmarshallingSelf(parcel);
88     info->capturerInfo_.UnmarshallingSelf(parcel);
89     info->appInfo_.appUid = parcel.ReadInt32();
90     info->appInfo_.appTokenId = parcel.ReadUint32();
91     info->appInfo_.appPid = parcel.ReadInt32();
92     info->appInfo_.appFullTokenId = parcel.ReadUint64();
93     info->sessionId_ = parcel.ReadUint32();
94     info->callerUid_ = parcel.ReadInt32();
95     info->callerPid_ = parcel.ReadInt32();
96     info->streamAction_ = static_cast<AudioStreamAction>(parcel.ReadUint32());
97     info->UnmarshallingDeviceDescVector(parcel, info->oldDeviceDescs_);
98     info->UnmarshallingDeviceDescVector(parcel, info->newDeviceDescs_);
99 
100     return info;
101 }
102 
WriteDeviceDescVectorToParcel(Parcel & parcel,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & descs) const103 bool AudioStreamDescriptor::WriteDeviceDescVectorToParcel(
104     Parcel &parcel, std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs) const
105 {
106     size_t size = descs.size();
107     if (size > MAX_STREAM_DESCRIPTORS_SIZE) {
108         return parcel.WriteInt32(-1);
109     }
110     bool ret = parcel.WriteInt32(static_cast<int32_t>(size));
111     CHECK_AND_RETURN_RET_LOG(ret, false, "write vector size failed");
112 
113     for (auto desc : descs) {
114         ret = desc->Marshalling(parcel);
115         CHECK_AND_RETURN_RET_LOG(ret, false, "Marshalling device desc failed");
116     }
117     return true;
118 }
119 
UnmarshallingDeviceDescVector(Parcel & parcel,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & descs)120 void AudioStreamDescriptor::UnmarshallingDeviceDescVector(
121     Parcel &parcel, std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs)
122 {
123     int32_t size = 0;
124     parcel.ReadInt32(size);
125     if (size == -1 || size > MAX_STREAM_DESCRIPTORS_SIZE) {
126         AUDIO_ERR_LOG("Invalid vector size");
127         return;
128     }
129     for (int32_t i = 0; i < size; i++) {
130         descs.push_back(std::shared_ptr<AudioDeviceDescriptor>(AudioDeviceDescriptor::Unmarshalling(parcel)));
131     }
132 }
133 
SetBunduleName(std::string & bundleName)134 void AudioStreamDescriptor::SetBunduleName(std::string &bundleName)
135 {
136     AUDIO_INFO_LOG("Bundle name: %{public}s", bundleName.c_str());
137     bundleName_ = bundleName;
138 }
139 
Dump(std::string & dumpString)140 void AudioStreamDescriptor::Dump(std::string &dumpString)
141 {
142     AppendFormat(dumpString, "  Stream %d:\n", sessionId_);
143 
144     DumpCommonAttrs(dumpString);
145 
146     if (audioMode_ == AUDIO_MODE_PLAYBACK) {
147         DumpRendererStreamAttrs(dumpString);
148     } else {
149         DumpCapturerStreamAttrs(dumpString);
150     }
151 
152     DumpDeviceAttrs(dumpString);
153 }
154 
DumpCommonAttrs(std::string & dumpString)155 void AudioStreamDescriptor::DumpCommonAttrs(std::string &dumpString)
156 {
157     AppendFormat(dumpString, "    - StreamStatus: %u (%s)\n",
158         streamStatus_, StreamStatusToString(streamStatus_));
159 
160     AppendFormat(dumpString, "    - CallerUid: %d CallerPid: %d AppUid: %d AppPid: %d\n",
161         callerUid_, callerPid_, appInfo_.appUid, appInfo_.appPid);
162 
163     AppendFormat(dumpString, "    - SampleRate: %d ChannelCount: %u ChannelLayout: %" PRIu64"" \
164         " Format: %u Encoding: %d\n",
165         streamInfo_.samplingRate, streamInfo_.channels, streamInfo_.channelLayout,
166         streamInfo_.format, streamInfo_.encoding);
167 
168     AppendFormat(dumpString, "    - AudioFlag: 0x%x RouteFlag: 0x%x\n", audioFlag_, routeFlag_);
169     AppendFormat(dumpString, "    - CreateTimestamp: %" PRId64"\n", createTimeStamp_);
170     AppendFormat(dumpString, "    - StartTimestamp: %" PRId64"\n", startTimeStamp_);
171 }
172 
DumpRendererStreamAttrs(std::string & dumpString)173 void AudioStreamDescriptor::DumpRendererStreamAttrs(std::string &dumpString)
174 {
175     AppendFormat(dumpString, "    - StreamUsage: %d\n", rendererInfo_.streamUsage);
176     AppendFormat(dumpString, "    - PlayerType: %d\n", rendererInfo_.playerType);
177     AppendFormat(dumpString, "    - OriginalFlag: %d RendererFlags: %d\n",
178         rendererInfo_.originalFlag, rendererInfo_.rendererFlags);
179     AppendFormat(dumpString, "    - OffloadAllowed: %d\n", rendererInfo_.isOffloadAllowed);
180 }
181 
DumpCapturerStreamAttrs(std::string & dumpString)182 void AudioStreamDescriptor::DumpCapturerStreamAttrs(std::string &dumpString)
183 {
184     AppendFormat(dumpString, "    - SourceType: %d\n", capturerInfo_.sourceType);
185     AppendFormat(dumpString, "    - RecorderType: %d\n", capturerInfo_.recorderType);
186     AppendFormat(dumpString, "    - OriginalFlag: %d CapturerFlags: %d\n",
187         capturerInfo_.originalFlag, capturerInfo_.capturerFlags);
188 }
189 
DumpDeviceAttrs(std::string & dumpString)190 void AudioStreamDescriptor::DumpDeviceAttrs(std::string &dumpString)
191 {
192     AppendFormat(dumpString, "    - OldDevices:\n");
193     for (auto &desc : oldDeviceDescs_) {
194         if (desc != nullptr) {
195             desc->Dump(dumpString);
196         }
197     }
198 
199     AppendFormat(dumpString, "    - NewDevices:\n");
200     for (auto &desc : newDeviceDescs_) {
201         if (desc != nullptr) {
202             desc->Dump(dumpString);
203         }
204     }
205 }
206 
GetNewDevicesTypeString()207 std::string AudioStreamDescriptor::GetNewDevicesTypeString()
208 {
209     std::string out = "";
210     for (auto &desc : newDeviceDescs_) {
211         if (desc != nullptr) {
212             out += (desc->GetDeviceTypeString() + ":");
213         }
214     }
215     return out;
216 }
217 
GetDeviceInfo(std::shared_ptr<AudioDeviceDescriptor> desc)218 std::string AudioStreamDescriptor::GetDeviceInfo(std::shared_ptr<AudioDeviceDescriptor> desc)
219 {
220     CHECK_AND_RETURN_RET_LOG(desc != nullptr, "", "desc is nullptr");
221     std::string out = "[";
222     out.append(std::to_string(static_cast<uint32_t>(desc->deviceType_)));
223     out.append(":" + std::to_string(static_cast<uint32_t>(desc->deviceId_)));
224     return out + "]";
225 }
226 
GetNewDevicesInfo()227 std::string AudioStreamDescriptor::GetNewDevicesInfo()
228 {
229     std::string out = "";
230     for (auto &desc : newDeviceDescs_) {
231         CHECK_AND_CONTINUE(desc != nullptr);
232         out += GetDeviceInfo(desc);
233     }
234     return out;
235 }
236 } // AudioStandard
237 } // namespace OHOS