1 /*
2 * Copyright (c) 2021-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 AUDIO_INFO_H
16 #define AUDIO_INFO_H
17
18 #ifdef __MUSL__
19 #include <stdint.h>
20 #endif // __MUSL__
21
22 #include <cmath>
23 #include <limits>
24 #include <string>
25 #include <vector>
26 #include <array>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include <parcel.h>
30 #include <audio_source_type.h>
31 #include <audio_device_info.h>
32 #include <audio_interrupt_info.h>
33 #include <audio_session_info.h>
34 #include <audio_stream_info.h>
35 #include <audio_asr.h>
36
37 namespace OHOS {
38 namespace AudioStandard {
39 constexpr int32_t MAX_NUM_STREAMS = 3;
40 constexpr int32_t RENDERER_STREAM_USAGE_SHIFT = 16;
41 constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5;
42 constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20;
43 constexpr int32_t MIN_SERVICE_COUNT = 2;
44 constexpr int32_t ROOT_UID = 0;
45 constexpr int32_t INVALID_UID = -1;
46 constexpr int32_t INTELL_VOICE_SERVICR_UID = 1042;
47 constexpr int32_t RSS_UID = 1096;
48 constexpr int32_t NETWORK_ID_SIZE = 80;
49 constexpr int32_t DEFAULT_VOLUME_GROUP_ID = 1;
50 constexpr int32_t DEFAULT_VOLUME_INTERRUPT_ID = 1;
51 constexpr int32_t AUDIO_FLAG_INVALID = -1;
52 constexpr int32_t AUDIO_FLAG_NORMAL = 0;
53 constexpr int32_t AUDIO_FLAG_MMAP = 1;
54 constexpr int32_t AUDIO_FLAG_VOIP_FAST = 2;
55 constexpr int32_t AUDIO_FLAG_DIRECT = 3;
56 constexpr int32_t AUDIO_FLAG_VOIP_DIRECT = 4;
57 constexpr int32_t AUDIO_FLAG_FORCED_NORMAL = 10;
58 constexpr int32_t AUDIO_USAGE_NORMAL = 0;
59 constexpr int32_t AUDIO_USAGE_VOIP = 1;
60 constexpr uint32_t STREAM_FLAG_NORMAL = 0;
61 constexpr uint32_t STREAM_FLAG_FAST = 1;
62 constexpr uint32_t STREAM_FLAG_DIRECT = 2;
63 constexpr float MAX_STREAM_SPEED_LEVEL = 4.0f;
64 constexpr float MIN_STREAM_SPEED_LEVEL = 0.125f;
65 constexpr int32_t EMPTY_UID = 0;
66 constexpr int32_t AUDIO_NORMAL_MANAGER_TYPE = 0;
67 constexpr int32_t AUDIO_DIRECT_MANAGER_TYPE = 2;
68
69 constexpr uint32_t MIN_SESSIONID = 100000;
70 constexpr uint32_t MAX_SESSIONID = UINT32_MAX - MIN_SESSIONID;
71
72 const float MIN_FLOAT_VOLUME = 0.0f;
73 const float MAX_FLOAT_VOLUME = 1.0f;
74
75 const std::string MICROPHONE_PERMISSION = "ohos.permission.MICROPHONE";
76 const std::string MANAGE_INTELLIGENT_VOICE_PERMISSION = "ohos.permission.MANAGE_INTELLIGENT_VOICE";
77 const std::string MANAGE_AUDIO_CONFIG = "ohos.permission.MANAGE_AUDIO_CONFIG";
78 const std::string MICROPHONE_CONTROL_PERMISSION = "ohos.permission.MICROPHONE_CONTROL";
79 const std::string MODIFY_AUDIO_SETTINGS_PERMISSION = "ohos.permission.MODIFY_AUDIO_SETTINGS";
80 const std::string ACCESS_NOTIFICATION_POLICY_PERMISSION = "ohos.permission.ACCESS_NOTIFICATION_POLICY";
81 const std::string USE_BLUETOOTH_PERMISSION = "ohos.permission.USE_BLUETOOTH";
82 const std::string CAPTURER_VOICE_DOWNLINK_PERMISSION = "ohos.permission.CAPTURE_VOICE_DOWNLINK_AUDIO";
83 const std::string RECORD_VOICE_CALL_PERMISSION = "ohos.permission.RECORD_VOICE_CALL";
84 const std::string MANAGE_SYSTEM_AUDIO_EFFECTS = "ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS";
85 const std::string CAST_AUDIO_OUTPUT_PERMISSION = "ohos.permission.CAST_AUDIO_OUTPUT";
86 const std::string DUMP_AUDIO_PERMISSION = "ohos.permission.DUMP_AUDIO";
87 const std::string CAPTURE_PLAYBACK_PERMISSION = "ohos.permission.CAPTURE_PLAYBACK";
88
89 constexpr std::string_view PRIMARY_WAKEUP = "Built_in_wakeup";
90 constexpr std::string_view VOICE_CALL_REC_NAME = "Voice_call_rec";
91
92 const std::string INNER_CAPTURER_SOURCE = "Speaker.monitor";
93 const std::string INNER_CAPTURER_SINK = "InnerCapturerSink";
94 const std::string NEW_INNER_CAPTURER_SOURCE = "InnerCapturerSink.monitor";
95 const std::string REMOTE_CAST_INNER_CAPTURER_SINK_NAME = "RemoteCastInnerCapturer";
96 const std::string MONITOR_SOURCE_SUFFIX = ".monitor";
97 const std::string DUP_STREAM = "DupStream";
98 const std::string DUAL_TONE_STREAM = "DualToneStream";
99 const std::string NORMAL_STREAM = "NormalStream";
100
101 #ifdef FEATURE_DTMF_TONE
102 // Maximun number of sine waves in a tone segment
103 constexpr uint32_t TONEINFO_MAX_WAVES = 3;
104
105 // Maximun number of segments in a tone descriptor
106 constexpr uint32_t TONEINFO_MAX_SEGMENTS = 12;
107 constexpr uint32_t TONEINFO_INF = 0xFFFFFFFF;
108 class ToneSegment : public Parcelable {
109 public:
110 uint32_t duration;
111 uint16_t waveFreq[TONEINFO_MAX_WAVES+1];
112 uint16_t loopCnt;
113 uint16_t loopIndx;
Marshalling(Parcel & parcel)114 bool Marshalling(Parcel &parcel) const override
115 {
116 parcel.WriteUint32(duration);
117 parcel.WriteUint16(loopCnt);
118 parcel.WriteUint16(loopIndx);
119 for (uint32_t i = 0; i < TONEINFO_MAX_WAVES + 1; i++) {
120 parcel.WriteUint16(waveFreq[i]);
121 }
122 return true;
123 }
Unmarshalling(Parcel & parcel)124 void Unmarshalling(Parcel &parcel)
125 {
126 duration = parcel.ReadUint32();
127 loopCnt = parcel.ReadUint16();
128 loopIndx = parcel.ReadUint16();
129 for (uint32_t i = 0; i < TONEINFO_MAX_WAVES + 1; i++) {
130 waveFreq[i] = parcel.ReadUint16();
131 }
132 }
133 };
134
135 class ToneInfo : public Parcelable {
136 public:
137 ToneSegment segments[TONEINFO_MAX_SEGMENTS+1];
138 uint32_t segmentCnt;
139 uint32_t repeatCnt;
140 uint32_t repeatSegment;
Marshalling(Parcel & parcel)141 bool Marshalling(Parcel &parcel) const override
142 {
143 parcel.WriteUint32(segmentCnt);
144 parcel.WriteUint32(repeatCnt);
145 parcel.WriteUint32(repeatSegment);
146 if (!(segmentCnt >= 0 && segmentCnt <= TONEINFO_MAX_SEGMENTS + 1)) {
147 return false;
148 }
149 for (uint32_t i = 0; i < segmentCnt; i++) {
150 segments[i].Marshalling(parcel);
151 }
152 return true;
153 }
Unmarshalling(Parcel & parcel)154 void Unmarshalling(Parcel &parcel)
155 {
156 segmentCnt = parcel.ReadUint32();
157 repeatCnt = parcel.ReadUint32();
158 repeatSegment = parcel.ReadUint32();
159 if (!(segmentCnt >= 0 && segmentCnt <= TONEINFO_MAX_SEGMENTS + 1)) {
160 return;
161 }
162 for (uint32_t i = 0; i < segmentCnt; i++) {
163 segments[i].Unmarshalling(parcel);
164 }
165 }
166 };
167 #endif
168
169 enum VolumeAdjustType {
170 /**
171 * Adjust volume up
172 */
173 VOLUME_UP = 0,
174 /**
175 * Adjust volume down
176 */
177 VOLUME_DOWN = 1,
178 };
179
180 enum ChannelBlendMode {
181 /**
182 * No channel process.
183 */
184 MODE_DEFAULT = 0,
185 /**
186 * Blend left and right channel.
187 */
188 MODE_BLEND_LR = 1,
189 /**
190 * Replicate left to right channel.
191 */
192 MODE_ALL_LEFT = 2,
193 /**
194 * Replicate right to left channel.
195 */
196 MODE_ALL_RIGHT = 3,
197 };
198
199 enum ConnectType {
200 /**
201 * Group connect type of local device
202 */
203 CONNECT_TYPE_LOCAL = 0,
204 /**
205 * Group connect type of distributed device
206 */
207 CONNECT_TYPE_DISTRIBUTED
208 };
209
210 typedef AudioStreamType AudioVolumeType;
211
212 enum VolumeFlag {
213 /**
214 * Show system volume bar
215 */
216 FLAG_SHOW_SYSTEM_UI = 1,
217 };
218
219 enum AudioOffloadType {
220 /**
221 * Indicates audio offload state default.
222 */
223 OFFLOAD_DEFAULT = -1,
224 /**
225 * Indicates audio offload state : screen is active & app is foreground.
226 */
227 OFFLOAD_ACTIVE_FOREGROUND = 0,
228 /**
229 * Indicates audio offload state : screen is active & app is background.
230 */
231 OFFLOAD_ACTIVE_BACKGROUND = 1,
232 /**
233 * Indicates audio offload state : screen is inactive & app is background.
234 */
235 OFFLOAD_INACTIVE_BACKGROUND = 3,
236 };
237
238 enum FocusType {
239 /**
240 * Recording type.
241 */
242 FOCUS_TYPE_RECORDING = 0,
243 };
244
245 enum AudioErrors {
246 /**
247 * Common errors.
248 */
249 ERROR_INVALID_PARAM = 6800101,
250 ERROR_NO_MEMORY = 6800102,
251 ERROR_ILLEGAL_STATE = 6800103,
252 ERROR_UNSUPPORTED = 6800104,
253 ERROR_TIMEOUT = 6800105,
254 /**
255 * Audio specific errors.
256 */
257 ERROR_STREAM_LIMIT = 6800201,
258 /**
259 * Default error.
260 */
261 ERROR_SYSTEM = 6800301
262 };
263
264 // Ringer Mode
265 enum AudioRingerMode {
266 RINGER_MODE_SILENT = 0,
267 RINGER_MODE_VIBRATE = 1,
268 RINGER_MODE_NORMAL = 2
269 };
270
271 /**
272 * Enumerates audio stream privacy type for playback capture.
273 */
274 enum AudioPrivacyType {
275 PRIVACY_TYPE_PUBLIC = 0,
276 PRIVACY_TYPE_PRIVATE = 1
277 };
278
279 /**
280 * Enumerates the renderer playback speed.
281 */
282 enum AudioRendererRate {
283 RENDER_RATE_NORMAL = 0,
284 RENDER_RATE_DOUBLE = 1,
285 RENDER_RATE_HALF = 2,
286 };
287
288 /**
289 * media safe volume status
290 */
291 enum SafeStatus : int32_t {
292 SAFE_UNKNOWN = -1,
293 SAFE_INACTIVE = 0,
294 SAFE_ACTIVE = 1,
295 };
296
297 enum CallbackChange : int32_t {
298 CALLBACK_UNKNOWN = 0,
299 CALLBACK_FOCUS_INFO_CHANGE,
300 CALLBACK_RENDERER_STATE_CHANGE,
301 CALLBACK_CAPTURER_STATE_CHANGE,
302 CALLBACK_MICMUTE_STATE_CHANGE,
303 CALLBACK_AUDIO_SESSION,
304 CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE,
305 CALLBACK_PREFERRED_INPUT_DEVICE_CHANGE,
306 CALLBACK_SET_VOLUME_KEY_EVENT,
307 CALLBACK_SET_DEVICE_CHANGE,
308 CALLBACK_SET_RINGER_MODE,
309 CALLBACK_SET_MIC_STATE_CHANGE,
310 CALLBACK_SPATIALIZATION_ENABLED_CHANGE,
311 CALLBACK_HEAD_TRACKING_ENABLED_CHANGE,
312 CALLBACK_SET_MICROPHONE_BLOCKED,
313 CALLBACK_DEVICE_CHANGE_WITH_INFO,
314 CALLBACK_HEAD_TRACKING_DATA_REQUESTED_CHANGE,
315 CALLBACK_MAX,
316 };
317
318 constexpr CallbackChange CALLBACK_ENUMS[] = {
319 CALLBACK_UNKNOWN,
320 CALLBACK_FOCUS_INFO_CHANGE,
321 CALLBACK_RENDERER_STATE_CHANGE,
322 CALLBACK_CAPTURER_STATE_CHANGE,
323 CALLBACK_MICMUTE_STATE_CHANGE,
324 CALLBACK_AUDIO_SESSION,
325 CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE,
326 CALLBACK_PREFERRED_INPUT_DEVICE_CHANGE,
327 CALLBACK_SET_VOLUME_KEY_EVENT,
328 CALLBACK_SET_DEVICE_CHANGE,
329 CALLBACK_SET_RINGER_MODE,
330 CALLBACK_SET_MIC_STATE_CHANGE,
331 CALLBACK_SPATIALIZATION_ENABLED_CHANGE,
332 CALLBACK_HEAD_TRACKING_ENABLED_CHANGE,
333 CALLBACK_SET_MICROPHONE_BLOCKED,
334 CALLBACK_DEVICE_CHANGE_WITH_INFO,
335 CALLBACK_HEAD_TRACKING_DATA_REQUESTED_CHANGE,
336 };
337
338 static_assert((sizeof(CALLBACK_ENUMS) / sizeof(CallbackChange)) == static_cast<size_t>(CALLBACK_MAX),
339 "check CALLBACK_ENUMS");
340
341 struct VolumeEvent {
342 AudioVolumeType volumeType;
343 int32_t volume;
344 bool updateUi;
345 int32_t volumeGroupId;
346 std::string networkId;
347 };
348
349 struct AudioParameters {
350 AudioSampleFormat format;
351 AudioChannel channels;
352 AudioSamplingRate samplingRate;
353 AudioEncodingType encoding;
354 ContentType contentType;
355 StreamUsage usage;
356 DeviceRole deviceRole;
357 DeviceType deviceType;
358 };
359
360 struct A2dpDeviceConfigInfo {
361 DeviceStreamInfo streamInfo;
362 bool absVolumeSupport = false;
363 int32_t volumeLevel;
364 bool mute = false;
365 };
366
367 struct AudioRendererInfo {
368 ContentType contentType = CONTENT_TYPE_UNKNOWN;
369 StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
370 int32_t rendererFlags = AUDIO_FLAG_NORMAL;
371 std::string sceneType = "";
372 bool spatializationEnabled = false;
373 bool headTrackingEnabled = false;
374 int32_t originalFlag = AUDIO_FLAG_NORMAL;
375 AudioPipeType pipeType = PIPE_TYPE_UNKNOWN;
376 AudioSamplingRate samplingRate = SAMPLE_RATE_8000;
377 AudioSampleFormat format = SAMPLE_S16LE;
378 uint8_t encodingType = 0;
379 uint64_t channelLayout = 0ULL;
380 bool isOffloadAllowed = true;
381
MarshallingAudioRendererInfo382 bool Marshalling(Parcel &parcel) const
383 {
384 return parcel.WriteInt32(static_cast<int32_t>(contentType))
385 && parcel.WriteInt32(static_cast<int32_t>(streamUsage))
386 && parcel.WriteInt32(rendererFlags)
387 && parcel.WriteInt32(originalFlag)
388 && parcel.WriteString(sceneType)
389 && parcel.WriteBool(spatializationEnabled)
390 && parcel.WriteBool(headTrackingEnabled)
391 && parcel.WriteInt32(static_cast<int32_t>(pipeType))
392 && parcel.WriteInt32(static_cast<int32_t>(samplingRate))
393 && parcel.WriteInt32(format)
394 && parcel.WriteUint8(encodingType)
395 && parcel.WriteUint64(channelLayout)
396 && parcel.WriteBool(isOffloadAllowed);
397 }
UnmarshallingAudioRendererInfo398 void Unmarshalling(Parcel &parcel)
399 {
400 contentType = static_cast<ContentType>(parcel.ReadInt32());
401 streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
402 rendererFlags = parcel.ReadInt32();
403 originalFlag = parcel.ReadInt32();
404 sceneType = parcel.ReadString();
405 spatializationEnabled = parcel.ReadBool();
406 headTrackingEnabled = parcel.ReadBool();
407 pipeType = static_cast<AudioPipeType>(parcel.ReadInt32());
408 samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
409 format = static_cast<AudioSampleFormat>(parcel.ReadInt32());
410 encodingType = parcel.ReadUint8();
411 channelLayout = parcel.ReadUint64();
412 isOffloadAllowed = parcel.ReadBool();
413 }
414 };
415
416 class AudioCapturerInfo {
417 public:
418 SourceType sourceType = SOURCE_TYPE_INVALID;
419 int32_t capturerFlags = 0;
420 AudioPipeType pipeType = PIPE_TYPE_UNKNOWN;
421 AudioSamplingRate samplingRate = SAMPLE_RATE_8000;
422 uint8_t encodingType = 0;
423 uint64_t channelLayout = 0ULL;
424 std::string sceneType = "";
425 int32_t originalFlag = AUDIO_FLAG_NORMAL;
AudioCapturerInfo(SourceType sourceType_,int32_t capturerFlags_)426 AudioCapturerInfo(SourceType sourceType_, int32_t capturerFlags_) : sourceType(sourceType_),
427 capturerFlags(capturerFlags_) {}
AudioCapturerInfo(const AudioCapturerInfo & audioCapturerInfo)428 AudioCapturerInfo(const AudioCapturerInfo &audioCapturerInfo)
429 {
430 *this = audioCapturerInfo;
431 }
432 AudioCapturerInfo() = default;
433 ~AudioCapturerInfo()= default;
Marshalling(Parcel & parcel)434 bool Marshalling(Parcel &parcel) const
435 {
436 return parcel.WriteInt32(static_cast<int32_t>(sourceType))
437 && parcel.WriteInt32(capturerFlags)
438 && parcel.WriteInt32(static_cast<int32_t>(pipeType))
439 && parcel.WriteInt32(static_cast<int32_t>(samplingRate))
440 && parcel.WriteUint8(encodingType)
441 && parcel.WriteUint64(channelLayout)
442 && parcel.WriteString(sceneType)
443 && parcel.WriteInt32(originalFlag);
444 }
Unmarshalling(Parcel & parcel)445 void Unmarshalling(Parcel &parcel)
446 {
447 sourceType = static_cast<SourceType>(parcel.ReadInt32());
448 capturerFlags = parcel.ReadInt32();
449 pipeType = static_cast<AudioPipeType>(parcel.ReadInt32());
450 samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
451 encodingType = parcel.ReadUint8();
452 channelLayout = parcel.ReadUint64();
453 sceneType = parcel.ReadString();
454 originalFlag = parcel.ReadInt32();
455 }
456 };
457
458 struct AudioRendererDesc {
459 ContentType contentType = CONTENT_TYPE_UNKNOWN;
460 StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
461 };
462
463 struct AudioRendererOptions {
464 AudioStreamInfo streamInfo;
465 AudioRendererInfo rendererInfo;
466 AudioPrivacyType privacyType = PRIVACY_TYPE_PUBLIC;
467 AudioSessionStrategy strategy = { AudioConcurrencyMode::INVALID };
468 };
469
470 struct MicStateChangeEvent {
471 bool mute;
472 };
473
474 enum AudioScene : int32_t {
475 /**
476 * Invalid
477 */
478 AUDIO_SCENE_INVALID = -1,
479 /**
480 * Default audio scene
481 */
482 AUDIO_SCENE_DEFAULT,
483 /**
484 * Ringing audio scene
485 * Only available for system api.
486 */
487 AUDIO_SCENE_RINGING,
488 /**
489 * Phone call audio scene
490 * Only available for system api.
491 */
492 AUDIO_SCENE_PHONE_CALL,
493 /**
494 * Voice chat audio scene
495 */
496 AUDIO_SCENE_PHONE_CHAT,
497 /**
498 * AvSession set call start flag
499 */
500 AUDIO_SCENE_CALL_START,
501 /**
502 * AvSession set call end flag
503 */
504 AUDIO_SCENE_CALL_END,
505 /**
506 * Voice ringing audio scene
507 * Only available for system api.
508 */
509 AUDIO_SCENE_VOICE_RINGING,
510 /**
511 * Max
512 */
513 AUDIO_SCENE_MAX,
514 };
515
516 enum AudioDeviceUsage : uint32_t {
517 /**
518 * Media output devices.
519 * @syscap SystemCapability.Multimedia.Audio.Device
520 * @systemapi
521 * @since 11
522 */
523 MEDIA_OUTPUT_DEVICES = 1,
524 /**
525 * Media input devices.
526 * @syscap SystemCapability.Multimedia.Audio.Device
527 * @systemapi
528 * @since 11
529 */
530 MEDIA_INPUT_DEVICES = 2,
531 /**
532 * All media devices.
533 * @syscap SystemCapability.Multimedia.Audio.Device
534 * @systemapi
535 * @since 11
536 */
537 ALL_MEDIA_DEVICES = 3,
538 /**
539 * Call output devices.
540 * @syscap SystemCapability.Multimedia.Audio.Device
541 * @systemapi
542 * @since 11
543 */
544 CALL_OUTPUT_DEVICES = 4,
545 /**
546 * Call input devices.
547 * @syscap SystemCapability.Multimedia.Audio.Device
548 * @systemapi
549 * @since 11
550 */
551 CALL_INPUT_DEVICES = 8,
552 /**
553 * All call devices.
554 * @syscap SystemCapability.Multimedia.Audio.Device
555 * @systemapi
556 * @since 11
557 */
558 ALL_CALL_DEVICES = 12,
559 /**
560 * All devices.
561 * @syscap SystemCapability.Multimedia.Audio.Device
562 * @systemapi
563 * @since 11
564 */
565 D_ALL_DEVICES = 15,
566 };
567
568 enum FilterMode : uint32_t {
569 INCLUDE = 0,
570 EXCLUDE,
571 MAX_FILTER_MODE
572 };
573
574 // 1.If the size of usages or pids is 0, FilterMode will not work.
575 // 2.Filters will only works with FileterMode INCLUDE or EXCLUDE while the vector size is not zero.
576 // 3.If usages and pids are both not empty, the result is the intersection of the two Filter.
577 // 4.If usages.size() == 0, defalut usages will be filtered with FilterMode::INCLUDE.
578 // 5.Default usages are MEDIA MUSIC MOVIE GAME and BOOK.
579 struct CaptureFilterOptions {
580 std::vector<StreamUsage> usages;
581 FilterMode usageFilterMode {FilterMode::INCLUDE};
582 std::vector<int32_t> pids;
583 FilterMode pidFilterMode {FilterMode::INCLUDE};
584 };
585
586 struct AudioPlaybackCaptureConfig {
587 CaptureFilterOptions filterOptions;
588 bool silentCapture {false}; // To be deprecated since 12
589 };
590
591 struct AudioCapturerOptions {
592 AudioStreamInfo streamInfo;
593 AudioCapturerInfo capturerInfo;
594 AudioPlaybackCaptureConfig playbackCaptureConfig;
595 AudioSessionStrategy strategy = { AudioConcurrencyMode::INVALID };
596 };
597
598 struct AppInfo {
599 int32_t appUid { INVALID_UID };
600 uint32_t appTokenId { 0 };
601 int32_t appPid { 0 };
602 uint64_t appFullTokenId { 0 };
603 };
604
605 struct BufferQueueState {
606 uint32_t numBuffers;
607 uint32_t currentIndex;
608 };
609
610 enum AudioRenderMode {
611 RENDER_MODE_NORMAL,
612 RENDER_MODE_CALLBACK
613 };
614
615 enum AudioCaptureMode {
616 CAPTURE_MODE_NORMAL,
617 CAPTURE_MODE_CALLBACK
618 };
619
620 struct SinkInfo {
621 uint32_t sinkId; // sink id
622 std::string sinkName;
623 std::string adapterName;
624 };
625
626 struct SinkInput {
627 int32_t streamId;
628 AudioStreamType streamType;
629
630 // add for routing stream.
631 int32_t uid; // client uid
632 int32_t pid; // client pid
633 uint32_t paStreamId; // streamId
634 uint32_t deviceSinkId; // sink id
635 std::string sinkName; // sink name
636 int32_t statusMark; // mark the router status
637 uint64_t startTime; // when this router is created
638 };
639
640 struct SourceOutput {
641 int32_t streamId;
642 AudioStreamType streamType;
643
644 // add for routing stream.
645 int32_t uid; // client uid
646 int32_t pid; // client pid
647 uint32_t paStreamId; // streamId
648 uint32_t deviceSourceId; // sink id
649 int32_t statusMark; // mark the router status
650 uint64_t startTime; // when this router is created
651 };
652
653 typedef uint32_t AudioIOHandle;
654
FLOAT_COMPARE_EQ(const float & x,const float & y)655 static inline bool FLOAT_COMPARE_EQ(const float& x, const float& y)
656 {
657 return (std::abs((x) - (y)) <= (std::numeric_limits<float>::epsilon()));
658 }
659
660 enum AudioServiceIndex {
661 HDI_SERVICE_INDEX = 0,
662 AUDIO_SERVICE_INDEX
663 };
664
665 /**
666 * @brief Enumerates the rendering states of the current device.
667 */
668 enum RendererState {
669 /** INVALID state */
670 RENDERER_INVALID = -1,
671 /** Create New Renderer instance */
672 RENDERER_NEW,
673 /** Reneder Prepared state */
674 RENDERER_PREPARED,
675 /** Rendere Running state */
676 RENDERER_RUNNING,
677 /** Renderer Stopped state */
678 RENDERER_STOPPED,
679 /** Renderer Released state */
680 RENDERER_RELEASED,
681 /** Renderer Paused state */
682 RENDERER_PAUSED
683 };
684
685 /**
686 * @brief Enumerates the capturing states of the current device.
687 */
688 enum CapturerState {
689 /** Capturer INVALID state */
690 CAPTURER_INVALID = -1,
691 /** Create new capturer instance */
692 CAPTURER_NEW,
693 /** Capturer Prepared state */
694 CAPTURER_PREPARED,
695 /** Capturer Running state */
696 CAPTURER_RUNNING,
697 /** Capturer Stopped state */
698 CAPTURER_STOPPED,
699 /** Capturer Released state */
700 CAPTURER_RELEASED,
701 /** Capturer Paused state */
702 CAPTURER_PAUSED
703 };
704
705 enum State {
706 /** INVALID */
707 INVALID = -1,
708 /** New */
709 NEW,
710 /** Prepared */
711 PREPARED,
712 /** Running */
713 RUNNING,
714 /** Stopped */
715 STOPPED,
716 /** Released */
717 RELEASED,
718 /** Paused */
719 PAUSED,
720 /** Stopping */
721 STOPPING
722 };
723
724 struct AudioRegisterTrackerInfo {
725 uint32_t sessionId;
726 int32_t clientPid;
727 State state;
728 AudioRendererInfo rendererInfo;
729 AudioCapturerInfo capturerInfo;
730 int32_t channelCount;
731 uint32_t appTokenId;
732 };
733
734 enum StateChangeCmdType {
735 CMD_FROM_CLIENT = 0,
736 CMD_FROM_SYSTEM = 1
737 };
738
739 enum AudioMode {
740 AUDIO_MODE_PLAYBACK,
741 AUDIO_MODE_RECORD
742 };
743
744 // LEGACY_INNER_CAP: Called from hap build with api < 12, work normally.
745 // LEGACY_MUTE_CAP: Called from hap build with api >= 12, will cap mute data.
746 // MODERN_INNER_CAP: Called from SA with inner-cap right, work with filter.
747 enum InnerCapMode : uint32_t {
748 LEGACY_INNER_CAP = 0,
749 LEGACY_MUTE_CAP,
750 MODERN_INNER_CAP,
751 INVALID_CAP_MODE
752 };
753
754 struct AudioProcessConfig {
755 int32_t callerUid = INVALID_UID;
756
757 AppInfo appInfo;
758
759 AudioStreamInfo streamInfo;
760
761 AudioMode audioMode = AUDIO_MODE_PLAYBACK;
762
763 AudioRendererInfo rendererInfo;
764
765 AudioCapturerInfo capturerInfo;
766
767 AudioStreamType streamType = STREAM_DEFAULT;
768
769 DeviceType deviceType = DEVICE_TYPE_INVALID;
770
771 bool isInnerCapturer = false;
772
773 bool isWakeupCapturer = false;
774
775 uint32_t originalSessionId = 0;
776
777 AudioPrivacyType privacyType = PRIVACY_TYPE_PUBLIC;
778
779 InnerCapMode innerCapMode {InnerCapMode::INVALID_CAP_MODE};
780 };
781
782 struct Volume {
783 bool isMute = false;
784 float volumeFloat = 1.0f;
785 uint32_t volumeInt = 0;
786 };
787
788 enum StreamSetState {
789 STREAM_PAUSE,
790 STREAM_RESUME
791 };
792
793 struct StreamSetStateEventInternal {
794 StreamSetState streamSetState;
795 StreamUsage streamUsage;
796 };
797
798 class AudioRendererChangeInfo {
799 public:
800 int32_t createrUID;
801 int32_t clientUID;
802 int32_t sessionId;
803 int32_t callerPid;
804 int32_t clientPid;
805 int32_t tokenId;
806 int32_t channelCount;
807 AudioRendererInfo rendererInfo;
808 RendererState rendererState;
809 DeviceInfo outputDeviceInfo;
810 bool prerunningState = false;
811
AudioRendererChangeInfo(const AudioRendererChangeInfo & audioRendererChangeInfo)812 AudioRendererChangeInfo(const AudioRendererChangeInfo &audioRendererChangeInfo)
813 {
814 *this = audioRendererChangeInfo;
815 }
816 AudioRendererChangeInfo() = default;
817 ~AudioRendererChangeInfo() = default;
Marshalling(Parcel & parcel)818 bool Marshalling(Parcel &parcel) const
819 {
820 return parcel.WriteInt32(createrUID)
821 && parcel.WriteInt32(clientUID)
822 && parcel.WriteInt32(sessionId)
823 && parcel.WriteInt32(callerPid)
824 && parcel.WriteInt32(clientPid)
825 && parcel.WriteInt32(tokenId)
826 && parcel.WriteInt32(channelCount)
827 && rendererInfo.Marshalling(parcel)
828 && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.contentType))
829 && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.streamUsage))
830 && parcel.WriteInt32(rendererInfo.rendererFlags)
831 && parcel.WriteInt32(rendererInfo.originalFlag)
832 && parcel.WriteInt32(rendererInfo.samplingRate)
833 && parcel.WriteInt32(rendererInfo.format)
834 && parcel.WriteInt32(static_cast<int32_t>(rendererState))
835 && outputDeviceInfo.Marshalling(parcel);
836 }
Marshalling(Parcel & parcel,bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion)837 bool Marshalling(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion) const
838 {
839 return parcel.WriteInt32(createrUID)
840 && parcel.WriteInt32(hasSystemPermission ? clientUID : EMPTY_UID)
841 && parcel.WriteInt32(sessionId)
842 && parcel.WriteInt32(callerPid)
843 && parcel.WriteInt32(clientPid)
844 && parcel.WriteInt32(tokenId)
845 && parcel.WriteInt32(channelCount)
846 && rendererInfo.Marshalling(parcel)
847 && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.contentType))
848 && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.streamUsage))
849 && parcel.WriteInt32(rendererInfo.rendererFlags)
850 && parcel.WriteInt32(rendererInfo.originalFlag)
851 && parcel.WriteInt32(rendererInfo.samplingRate)
852 && parcel.WriteInt32(rendererInfo.format)
853 && parcel.WriteInt32(hasSystemPermission ? static_cast<int32_t>(rendererState) :
854 RENDERER_INVALID)
855 && outputDeviceInfo.Marshalling(parcel, hasBTPermission, hasSystemPermission, apiVersion);
856 }
Unmarshalling(Parcel & parcel)857 void Unmarshalling(Parcel &parcel)
858 {
859 createrUID = parcel.ReadInt32();
860 clientUID = parcel.ReadInt32();
861 sessionId = parcel.ReadInt32();
862 callerPid = parcel.ReadInt32();
863 clientPid = parcel.ReadInt32();
864 tokenId = parcel.ReadInt32();
865 channelCount = parcel.ReadInt32();
866
867 rendererInfo.Unmarshalling(parcel);
868
869 rendererInfo.contentType = static_cast<ContentType>(parcel.ReadInt32());
870 rendererInfo.streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
871 rendererInfo.rendererFlags = parcel.ReadInt32();
872 rendererInfo.originalFlag = parcel.ReadInt32();
873 rendererInfo.samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
874 rendererInfo.format = static_cast<AudioSampleFormat>(parcel.ReadInt32());
875
876 rendererState = static_cast<RendererState>(parcel.ReadInt32());
877 outputDeviceInfo.Unmarshalling(parcel);
878 }
879 };
880
881 class AudioCapturerChangeInfo {
882 public:
883 int32_t createrUID;
884 int32_t clientUID;
885 int32_t sessionId;
886 int32_t callerPid;
887 int32_t clientPid;
888 AudioCapturerInfo capturerInfo;
889 CapturerState capturerState;
890 DeviceInfo inputDeviceInfo;
891 bool muted;
892 uint32_t appTokenId;
893
AudioCapturerChangeInfo(const AudioCapturerChangeInfo & audioCapturerChangeInfo)894 AudioCapturerChangeInfo(const AudioCapturerChangeInfo &audioCapturerChangeInfo)
895 {
896 *this = audioCapturerChangeInfo;
897 }
898 AudioCapturerChangeInfo() = default;
899 ~AudioCapturerChangeInfo() = default;
Marshalling(Parcel & parcel)900 bool Marshalling(Parcel &parcel) const
901 {
902 return parcel.WriteInt32(createrUID)
903 && parcel.WriteInt32(clientUID)
904 && parcel.WriteInt32(sessionId)
905 && parcel.WriteInt32(callerPid)
906 && parcel.WriteInt32(clientPid)
907 && capturerInfo.Marshalling(parcel)
908 && parcel.WriteInt32(static_cast<int32_t>(capturerState))
909 && inputDeviceInfo.Marshalling(parcel)
910 && parcel.WriteBool(muted)
911 && parcel.WriteUint32(appTokenId);
912 }
913
Marshalling(Parcel & parcel,bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion)914 bool Marshalling(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion) const
915 {
916 return parcel.WriteInt32(createrUID)
917 && parcel.WriteInt32(hasSystemPermission ? clientUID : EMPTY_UID)
918 && parcel.WriteInt32(sessionId)
919 && parcel.WriteInt32(callerPid)
920 && parcel.WriteInt32(clientPid)
921 && capturerInfo.Marshalling(parcel)
922 && parcel.WriteInt32(hasSystemPermission ? static_cast<int32_t>(capturerState) : CAPTURER_INVALID)
923 && inputDeviceInfo.Marshalling(parcel, hasBTPermission, hasSystemPermission, apiVersion)
924 && parcel.WriteBool(muted)
925 && parcel.WriteUint32(appTokenId);
926 }
927
Unmarshalling(Parcel & parcel)928 void Unmarshalling(Parcel &parcel)
929 {
930 createrUID = parcel.ReadInt32();
931 clientUID = parcel.ReadInt32();
932 sessionId = parcel.ReadInt32();
933 callerPid = parcel.ReadInt32();
934 clientPid = parcel.ReadInt32();
935 capturerInfo.Unmarshalling(parcel);
936 capturerState = static_cast<CapturerState>(parcel.ReadInt32());
937 inputDeviceInfo.Unmarshalling(parcel);
938 muted = parcel.ReadBool();
939 appTokenId = parcel.ReadUint32();
940 }
941 };
942
943 struct AudioStreamChangeInfo {
944 AudioRendererChangeInfo audioRendererChangeInfo;
945 AudioCapturerChangeInfo audioCapturerChangeInfo;
946 };
947
948 enum AudioPin {
949 AUDIO_PIN_NONE = 0, // Invalid pin
950 AUDIO_PIN_OUT_SPEAKER = 1 << 0, // Speaker output pin
951 AUDIO_PIN_OUT_HEADSET = 1 << 1, // Wired headset pin for output
952 AUDIO_PIN_OUT_LINEOUT = 1 << 2, // Line-out pin
953 AUDIO_PIN_OUT_HDMI = 1 << 3, // HDMI output pin
954 AUDIO_PIN_OUT_USB = 1 << 4, // USB output pin
955 AUDIO_PIN_OUT_USB_EXT = 1 << 5, // Extended USB output pin
956 AUDIO_PIN_OUT_BLUETOOTH_SCO = 1 << 6, // Bluetooth SCO output pin
957 AUDIO_PIN_OUT_DAUDIO_DEFAULT = 1 << 7, // Daudio default output pin
958 AUDIO_PIN_OUT_HEADPHONE = 1 << 8, // Wired headphone output pin
959 AUDIO_PIN_OUT_USB_HEADSET = 1 << 9, // Arm usb output pin
960 AUDIO_PIN_OUT_DP = 1 << 11,
961 AUDIO_PIN_IN_MIC = 1 << 27 | 1 << 0, // Microphone input pin
962 AUDIO_PIN_IN_HS_MIC = 1 << 27 | 1 << 1, // Wired headset microphone pin for input
963 AUDIO_PIN_IN_LINEIN = 1 << 27 | 1 << 2, // Line-in pin
964 AUDIO_PIN_IN_USB_EXT = 1 << 27 | 1 << 3, // Extended USB input pin
965 AUDIO_PIN_IN_BLUETOOTH_SCO_HEADSET = 1 << 27 | 1 << 4, // Bluetooth SCO headset input pin
966 AUDIO_PIN_IN_DAUDIO_DEFAULT = 1 << 27 | 1 << 5, // Daudio default input pin
967 AUDIO_PIN_IN_USB_HEADSET = 1 << 27 | 1 << 6, // Arm usb input pin
968 };
969
970 enum AudioParamKey {
971 NONE = 0,
972 VOLUME = 1,
973 INTERRUPT = 2,
974 PARAM_KEY_STATE = 5,
975 A2DP_SUSPEND_STATE = 6, // for bluetooth sink
976 BT_HEADSET_NREC = 7,
977 BT_WBS = 8,
978 A2DP_OFFLOAD_STATE = 9, // for a2dp offload
979 GET_DP_DEVICE_INFO = 10, // for dp sink
980 USB_DEVICE = 101, // Check USB device type ARM or HIFI
981 PERF_INFO = 201,
982 MMI = 301,
983 PARAM_KEY_LOWPOWER = 1000,
984 };
985
986 struct DStatusInfo {
987 char networkId[NETWORK_ID_SIZE];
988 AudioPin hdiPin = AUDIO_PIN_NONE;
989 int32_t mappingVolumeId = 0;
990 int32_t mappingInterruptId = 0;
991 int32_t deviceId;
992 int32_t channelMasks;
993 std::string deviceName = "";
994 bool isConnected = false;
995 std::string macAddress;
996 DeviceStreamInfo streamInfo = {};
997 ConnectType connectType = CONNECT_TYPE_LOCAL;
998 };
999
1000 struct AudioRendererDataInfo {
1001 uint8_t *buffer;
1002 size_t flag;
1003 };
1004
1005 enum AudioPermissionState {
1006 AUDIO_PERMISSION_START = 0,
1007 AUDIO_PERMISSION_STOP = 1,
1008 };
1009
1010 class AudioRendererPolicyServiceDiedCallback {
1011 public:
1012 virtual ~AudioRendererPolicyServiceDiedCallback() = default;
1013
1014 /**
1015 * Called when audio policy service died.
1016 * @since 10
1017 */
1018 virtual void OnAudioPolicyServiceDied() = 0;
1019 };
1020
1021 class AudioStreamPolicyServiceDiedCallback {
1022 public:
1023 virtual ~AudioStreamPolicyServiceDiedCallback() = default;
1024
1025 /**
1026 * Called when audio policy service died.
1027 * @since 11
1028 */
1029 virtual void OnAudioPolicyServiceDied() = 0;
1030 };
1031
1032 /**
1033 * Describes three-dimensional value.
1034 * @since 11
1035 */
1036 struct Vector3D {
1037 /**
1038 * X-axis value.
1039 * @since 11
1040 */
1041 float x;
1042 /**
1043 * Y-axis value.
1044 * @since 11
1045 */
1046 float y;
1047 /**
1048 * Z-axis value.
1049 * @since 11
1050 */
1051 float z;
1052 };
1053
1054 struct SessionInfo {
1055 SourceType sourceType;
1056 uint32_t rate;
1057 uint32_t channels;
1058 };
1059
1060 enum CastType {
1061 CAST_TYPE_NULL = 0,
1062 CAST_TYPE_ALL,
1063 CAST_TYPE_PROJECTION,
1064 CAST_TYPE_COOPERATION,
1065 };
1066
1067 class AudioPnpDeviceChangeCallback {
1068 public:
1069 virtual ~AudioPnpDeviceChangeCallback() = default;
1070 virtual void OnPnpDeviceStatusChanged(const std::string &info) = 0;
1071 virtual void OnMicrophoneBlocked(const std::string &info) = 0;
1072 };
1073
1074 struct SourceInfo {
1075 SourceType sourceType_;
1076 uint32_t rate_;
1077 uint32_t channels_;
1078 };
1079
1080 /**
1081 * @brief Device group used by set/get volume.
1082 */
1083 enum DeviceGroup {
1084 /** Invalid device group */
1085 DEVICE_GROUP_INVALID = -1,
1086 /** Built in device */
1087 DEVICE_GROUP_BUILT_IN,
1088 /** Wired device */
1089 DEVICE_GROUP_WIRED,
1090 /** Wireless device */
1091 DEVICE_GROUP_WIRELESS,
1092 /** Remote cast device */
1093 DEVICE_GROUP_REMOTE_CAST,
1094 /* earpiece device*/
1095 DEVICE_GROUP_EARPIECE,
1096 };
1097
1098 static const std::map<DeviceType, DeviceGroup> DEVICE_GROUP_FOR_VOLUME = {
1099 {DEVICE_TYPE_EARPIECE, DEVICE_GROUP_EARPIECE},
1100 {DEVICE_TYPE_SPEAKER, DEVICE_GROUP_BUILT_IN},
1101 {DEVICE_TYPE_WIRED_HEADSET, DEVICE_GROUP_WIRED},
1102 {DEVICE_TYPE_USB_HEADSET, DEVICE_GROUP_WIRED},
1103 {DEVICE_TYPE_USB_ARM_HEADSET, DEVICE_GROUP_WIRED},
1104 {DEVICE_TYPE_DP, DEVICE_GROUP_WIRED},
1105 {DEVICE_TYPE_BLUETOOTH_A2DP, DEVICE_GROUP_WIRELESS},
1106 {DEVICE_TYPE_BLUETOOTH_SCO, DEVICE_GROUP_WIRELESS},
1107 {DEVICE_TYPE_REMOTE_CAST, DEVICE_GROUP_REMOTE_CAST},
1108 };
1109
GetVolumeGroupForDevice(DeviceType deviceType)1110 static inline DeviceGroup GetVolumeGroupForDevice(DeviceType deviceType)
1111 {
1112 auto it = DEVICE_GROUP_FOR_VOLUME.find(deviceType);
1113 if (it == DEVICE_GROUP_FOR_VOLUME.end()) {
1114 return DEVICE_GROUP_INVALID;
1115 }
1116 return it->second;
1117 }
1118
1119 enum RouterType {
1120 /**
1121 * None router.
1122 * @since 12
1123 */
1124 ROUTER_TYPE_NONE = 0,
1125 /**
1126 * Default router.
1127 * @since 12
1128 */
1129 ROUTER_TYPE_DEFAULT,
1130 /**
1131 * Stream filter router.
1132 * @since 12
1133 */
1134 ROUTER_TYPE_STREAM_FILTER,
1135 /**
1136 * Package filter router.
1137 * @since 12
1138 */
1139 ROUTER_TYPE_PACKAGE_FILTER,
1140 /**
1141 * Cockpit phone router.
1142 * @since 12
1143 */
1144 ROUTER_TYPE_COCKPIT_PHONE,
1145 /**
1146 * Privacy priority router.
1147 * @since 12
1148 */
1149 ROUTER_TYPE_PRIVACY_PRIORITY,
1150 /**
1151 * Public priority router.
1152 * @since 12
1153 */
1154 ROUTER_TYPE_PUBLIC_PRIORITY,
1155 /**
1156 * Pair device router.
1157 * @since 12
1158 */
1159 ROUTER_TYPE_PAIR_DEVICE,
1160 /**
1161 * User select router.
1162 * @since 12
1163 */
1164 ROUTER_TYPE_USER_SELECT,
1165 };
1166
1167 enum RenderMode {
1168 /**
1169 * Primary render mode.
1170 * @since 12
1171 */
1172 PRIMARY,
1173 /**
1174 * VOIP render mode.
1175 * @since 12
1176 */
1177 VOIP,
1178 /**
1179 * Offload render mode.
1180 * @since 12
1181 */
1182 OFFLOAD,
1183 /**
1184 * Low latency render mode.
1185 * @since 12
1186 */
1187 LOW_LATENCY,
1188 };
1189
1190 enum WriteDataCallbackType {
1191 /**
1192 * Use OH_AudioRenderer_Callbacks.OH_AudioRenderer_OnWriteData
1193 * @since 12
1194 */
1195 WRITE_DATA_CALLBACK_WITHOUT_RESULT = 0,
1196 /**
1197 * Use OH_AudioRenderer_OnWriteDataCallback.
1198 * @since 12
1199 */
1200 WRITE_DATA_CALLBACK_WITH_RESULT = 1
1201 };
1202
1203 enum PolicyType {
1204 EDM_POLICY_TYPE = 0,
1205 PRIVACY_POLCIY_TYPE = 1,
1206 TEMPORARY_POLCIY_TYPE = 2,
1207 };
1208
1209 static inline const std::unordered_set<SourceType> specialSourceTypeSet_ = {
1210 SOURCE_TYPE_PLAYBACK_CAPTURE,
1211 SOURCE_TYPE_WAKEUP,
1212 SOURCE_TYPE_VIRTUAL_CAPTURE,
1213 SOURCE_TYPE_REMOTE_CAST
1214 };
1215 } // namespace AudioStandard
1216 } // namespace OHOS
1217 #endif // AUDIO_INFO_H
1218