• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef ST_AUDIO_OFFLOAD_STREAM_H
16 #define ST_AUDIO_OFFLOAD_STREAM_H
17 
18 #include <bitset>
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <mutex>
24 #include "singleton.h"
25 #include "audio_group_handle.h"
26 #include "power_mgr_client.h"
27 #include "audio_manager_base.h"
28 #include "audio_module_info.h"
29 #include "audio_volume_config.h"
30 #include "audio_system_manager.h"
31 #include "audio_router_center.h"
32 #include "audio_errors.h"
33 #include "audio_policy_manager_factory.h"
34 #include "audio_stream_collector.h"
35 #include "ipc_skeleton.h"
36 
37 #include "audio_router_map.h"
38 #include "audio_iohandle_map.h"
39 #include "audio_active_device.h"
40 #include "audio_stream_collector.h"
41 
42 namespace OHOS {
43 namespace AudioStandard {
44 
45 class AudioOffloadStream {
46 public:
GetInstance()47     static AudioOffloadStream& GetInstance()
48     {
49         static AudioOffloadStream instance;
50         return instance;
51     }
52     void OffloadStreamSetCheck(uint32_t sessionId);
53     void OffloadStreamReleaseCheck(uint32_t sessionId);
54     void HandlePowerStateChanged(PowerMgr::PowerState state);
55     void ResetOffloadMode(int32_t sessionId);
56     int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType);
57     void RemoteOffloadStreamRelease(uint32_t sessionId);
58     void CheckStreamMode(const int64_t activateSessionId);
59     bool CheckStreamMultichannelMode(const int64_t activateSessionId);
60     int32_t MoveToOutputDevice(uint32_t sessionId, std::string portName);
61     std::vector<SinkInput> FilterSinkInputs(int32_t sessionId, std::vector<SinkInput> sinkInputs);
62     void SetOffloadAvailableFromXML(AudioModuleInfo &moduleInfo);
63     int32_t DynamicUnloadOffloadModule();
64     int32_t UnloadMchModule();
65     int32_t LoadMchModule();
66     bool GetOffloadAvailableFromXml() const;
67     void ResetOffloadModeOnSpatializationChanged(std::vector<int32_t> &allSessions);
68     int32_t ActivateConcurrencyFromServer(AudioPipeType incomingPipe);
69 private:
AudioOffloadStream()70     AudioOffloadStream() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
71         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
72         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
73         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
74         audioRouteMap_(AudioRouteMap::GetInstance()),
75         audioActiveDevice_(AudioActiveDevice::GetInstance()) {}
~AudioOffloadStream()76     ~AudioOffloadStream() {}
77     bool CheckStreamOffloadMode(int64_t activateSessionId, AudioStreamType streamType);
78     bool CheckSpatializationAndEffectState();
79     void SetOffloadMode();
80     int32_t MoveToNewPipeInner(const uint32_t sessionId, const AudioPipeType pipeType);
81     int32_t LoadOffloadModule();
82     int32_t UnloadOffloadModule();
83     AudioModuleInfo ConstructMchAudioModuleInfo(DeviceType deviceType);
84     AudioModuleInfo ConstructOffloadAudioModuleInfo(DeviceType deviceType);
85 private:
86     std::optional<uint32_t> offloadSessionID_;
87     PowerMgr::PowerState currentPowerState_ = PowerMgr::PowerState::AWAKE;
88     std::mutex offloadMutex_;
89     bool currentOffloadSessionIsBackground_ = false;
90     bool isOffloadAvailable_ = false;
91 
92     std::mutex offloadCloseMutex_;
93     std::mutex offloadOpenMutex_;
94     std::atomic<bool> isOffloadOpened_ = false;
95     std::condition_variable offloadCloseCondition_;
96 
97     IAudioPolicyInterface& audioPolicyManager_;
98     AudioRouterCenter& audioRouterCenter_;
99     AudioStreamCollector& streamCollector_;
100     AudioIOHandleMap& audioIOHandleMap_;
101     AudioRouteMap& audioRouteMap_;
102     AudioActiveDevice& audioActiveDevice_;
103 };
104 
105 }
106 }
107 
108 #endif