• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef AUDIO_RENDERER_GATEWAY_H
17 #define AUDIO_RENDERER_GATEWAY_H
18 
19 #include "audio_interrupt_callback.h"
20 #include "audio_container_stream_base.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 constexpr uint32_t INVALID_SESSION = static_cast<uint32_t>(-1);
25 
26 class AudioRendererGateway : public AudioRenderer {
27 public:
28     explicit AudioRendererGateway(AudioStreamType audioStreamType, const AppInfo &appInfo);
29     ~AudioRendererGateway();
30     int32_t GetFrameCount(uint32_t &frameCount) const override;
31     int32_t GetLatency(uint64_t &latency) const override;
32     int32_t SetParams(const AudioRendererParams params) override;
33     int32_t GetParams(AudioRendererParams &params) const override;
34     int32_t GetRendererInfo(AudioRendererInfo &rendererInfo) const override;
35     int32_t GetStreamInfo(AudioStreamInfo &streamInfo) const override;
36     bool Start(StateChangeCmdType cmdType = CMD_FROM_CLIENT) const override;
37     int32_t Write(uint8_t *buffer, size_t bufferSize) override;
38     RendererState GetStatus() const override;
39     bool GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base) const override;
40     bool Drain() const override;
41     bool Pause(StateChangeCmdType cmdType = CMD_FROM_CLIENT) const override;
42     bool Stop() const override;
43     bool Flush() const override;
44     bool Release() const override;
45     int32_t GetBufferSize(size_t &bufferSize) const override;
46     int32_t GetAudioStreamId(uint32_t &sessionID) const override;
47     int32_t SetAudioRendererDesc(AudioRendererDesc audioRendererDesc) const override;
48     int32_t SetStreamType(AudioStreamType audioStreamType) const override;
49     int32_t SetVolume(float volume) const override;
50     float GetVolume() const override;
51     int32_t SetRenderRate(AudioRendererRate renderRate) const override;
52     AudioRendererRate GetRenderRate() const override;
53     int32_t SetRendererCallback(const std::shared_ptr<AudioRendererCallback> &callback) override;
54     int32_t SetRendererPositionCallback(int64_t markPosition,
55         const std::shared_ptr<RendererPositionCallback> &callback) override;
56     void UnsetRendererPositionCallback() override;
57     int32_t SetRendererPeriodPositionCallback(int64_t frameNumber,
58         const std::shared_ptr<RendererPeriodPositionCallback> &callback) override;
59     void UnsetRendererPeriodPositionCallback() override;
60     int32_t SetBufferDuration(uint64_t bufferDuration) const override;
61     int32_t SetRenderMode(AudioRenderMode renderMode) const override;
62     AudioRenderMode GetRenderMode() const override;
63     int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback) override;
64     int32_t GetBufferDesc(BufferDesc &bufDesc) const override;
65     int32_t Enqueue(const BufferDesc &bufDesc) const override;
66     int32_t Clear() const override;
67     int32_t GetBufQueueState(BufferQueueState &bufState) const override;
68     void SetApplicationCachePath(const std::string cachePath) override;
69     void SetInterruptMode(InterruptMode mode) override;
70     int32_t SetLowPowerVolume(float volume) const override;
71     float GetLowPowerVolume() const override;
72     float GetSingleStreamVolume() const override;
73     AudioRendererInfo rendererInfo_ = {};
74 
75 private:
76     int32_t InitAudioInterruptCallback();
77     int32_t InitSharedInterrupt();
78     static std::map<pid_t, std::map<AudioStreamType, AudioInterrupt>> sharedInterrupts_;
79     std::shared_ptr<AudioContainerRenderStream> audioStream_;
80     std::shared_ptr<AudioInterruptCallback> audioInterruptCallback_ = nullptr;
81     std::shared_ptr<AudioStreamCallback> audioStreamCallback_ = nullptr;
82     AppInfo appInfo_ = {};
83     AudioInterrupt audioInterrupt_ = {
84         STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, AudioStreamType::STREAM_DEFAULT, 0};
85     AudioInterrupt sharedInterrupt_ = {
86         STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, AudioStreamType::STREAM_DEFAULT, 0};
87     uint32_t sessionID_ = INVALID_SESSION;
88     AudioStandard::InterruptMode mode_ = AudioStandard::InterruptMode::SHARE_MODE;
89 };
90 
91 class AudioInterruptCallbackGateway : public AudioInterruptCallback {
92 public:
93     explicit AudioInterruptCallbackGateway(const std::shared_ptr<AudioContainerRenderStream> &audioStream,
94         const AudioInterrupt &audioInterrupt);
95     virtual ~AudioInterruptCallbackGateway();
96 
97     void OnInterrupt(const InterruptEventInternal &interruptEvent) override;
98     void SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback);
99 private:
100     void NotifyEvent(const InterruptEvent &interruptEvent);
101     void HandleAndNotifyForcedEvent(const InterruptEventInternal &interruptEvent);
102     void NotifyForcePausedToResume(const InterruptEventInternal &interruptEvent);
103     bool HandleForceDucking(const InterruptEventInternal &interruptEvent);
104     std::shared_ptr<AudioContainerRenderStream> audioStream_;
105     std::weak_ptr<AudioRendererCallback> callback_;
106     std::shared_ptr<AudioRendererCallback> cb_;
107     AudioInterrupt audioInterrupt_ {};
108     bool isForcePaused_ = false;
109     bool isForceDucked_ = false;
110     float instanceVolBeforeDucking_ = 0.2f;
111 };
112 
113 class AudioStreamRenderCallback : public AudioStreamCallback {
114 public:
115     virtual ~AudioStreamRenderCallback() = default;
116 
117     void OnStateChange(const State state, StateChangeCmdType cmdType = CMD_FROM_CLIENT) override;
118     void SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback);
119 private:
120     std::weak_ptr<AudioRendererCallback> callback_;
121 };
122 
123 }  // namespace AudioStandard
124 }  // namespace OHOS
125 #endif // AUDIO_RENDERER_GATEWAY_H
126