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