• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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_CONCURRENCY_SERVICE_H
16 #define ST_AUDIO_CONCURRENCY_SERVICE_H
17 #include <mutex>
18 
19 #include "iremote_object.h"
20 
21 #include "audio_policy_log.h"
22 #include "audio_concurrency_callback.h"
23 #include "i_audio_concurrency_event_dispatcher.h"
24 #include "audio_concurrency_parser.h"
25 #include "audio_policy_server_handler.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 
30 class AudioConcurrencyService : public std::enable_shared_from_this<AudioConcurrencyService>,
31                                 public IAudioConcurrencyEventDispatcher {
32 public:
AudioConcurrencyService()33     AudioConcurrencyService()
34     {
35         AUDIO_INFO_LOG("ctor");
36     }
~AudioConcurrencyService()37     virtual ~AudioConcurrencyService()
38     {
39         AUDIO_ERR_LOG("dtor");
40     }
41     void Init();
42     void DispatchConcurrencyEventWithSessionId(uint32_t sessionID) override;
43     int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object);
44     int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID);
45     void SetCallbackHandler(std::shared_ptr<AudioPolicyServerHandler> handler);
46     int32_t ActivateAudioConcurrency(AudioPipeType incomingPipeType,
47         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos,
48         const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos);
49 private:
50     // Inner class for death handler
51     class AudioConcurrencyDeathRecipient : public IRemoteObject::DeathRecipient {
52     public:
53         explicit AudioConcurrencyDeathRecipient(
54             const std::shared_ptr<AudioConcurrencyService> &service, uint32_t sessionID);
55         virtual ~AudioConcurrencyDeathRecipient() = default;
56 
57         DISALLOW_COPY_AND_MOVE(AudioConcurrencyDeathRecipient);
58 
59         void OnRemoteDied(const wptr<IRemoteObject> &remote);
60 
61     private:
62         const std::weak_ptr<AudioConcurrencyService> service_;
63         const uint32_t sessionID_;
64     };
65     // Inner class for callback
66     class AudioConcurrencyClient {
67     public:
68         explicit AudioConcurrencyClient(const std::shared_ptr<AudioConcurrencyCallback> &callback,
69             const sptr<IRemoteObject> &object, const sptr<AudioConcurrencyDeathRecipient> &deathRecipient,
70             uint32_t sessionID);
71         virtual ~AudioConcurrencyClient();
72 
73         DISALLOW_COPY_AND_MOVE(AudioConcurrencyClient);
74 
75         void OnConcedeStream();
76 
77     private:
78         const std::shared_ptr<AudioConcurrencyCallback> callback_;
79         const sptr<IRemoteObject> object_;
80         sptr<AudioConcurrencyDeathRecipient> deathRecipient_;
81         const uint32_t sessionID_;
82     };
83     std::map<int32_t /*sessionId*/, std::shared_ptr<AudioConcurrencyClient>> concurrencyClients_ = {};
84     std::map<std::pair<AudioPipeType, AudioPipeType>, ConcurrencyAction> concurrencyCfgMap_ = {};
85     std::shared_ptr<AudioPolicyServerHandler> handler_;
86     std::mutex cbMapMutex_;
87 };
88 } // namespace AudioStandard
89 } // namespace OHOS
90 #endif