• 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);
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     static std::map<pid_t, std::map<AudioStreamType, AudioInterrupt>> sharedInterrupts_;
77     std::shared_ptr<AudioContainerRenderStream> audioStream_;
78     std::shared_ptr<AudioInterruptCallback> audioInterruptCallback_ = nullptr;
79     std::shared_ptr<AudioStreamCallback> audioStreamCallback_ = nullptr;
80     AudioInterrupt audioInterrupt_ = {
81         STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, AudioStreamType::STREAM_DEFAULT, 0, false};
82     AudioInterrupt sharedInterrupt_ = {
83         STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, AudioStreamType::STREAM_DEFAULT, 0, false};
84     uint32_t sessionID_ = INVALID_SESSION;
85     AudioStandard::InterruptMode mode_ = AudioStandard::InterruptMode::SHARE_MODE;
86 };
87 
88 class AudioInterruptCallbackGateway : public AudioInterruptCallback {
89 public:
90     explicit AudioInterruptCallbackGateway(const std::shared_ptr<AudioContainerRenderStream> &audioStream,
91         const AudioInterrupt &audioInterrupt);
92     virtual ~AudioInterruptCallbackGateway();
93 
94     void OnInterrupt(const InterruptEventInternal &interruptEvent) override;
95     void SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback);
96 private:
97     void NotifyEvent(const InterruptEvent &interruptEvent);
98     void HandleAndNotifyForcedEvent(const InterruptEventInternal &interruptEvent);
99     void NotifyForcePausedToResume(const InterruptEventInternal &interruptEvent);
100     bool HandleForceDucking(const InterruptEventInternal &interruptEvent);
101     std::shared_ptr<AudioContainerRenderStream> audioStream_;
102     std::weak_ptr<AudioRendererCallback> callback_;
103     std::shared_ptr<AudioRendererCallback> cb_;
104     AudioInterrupt audioInterrupt_ {};
105     bool isForcePaused_ = false;
106     bool isForceDucked_ = false;
107     float instanceVolBeforeDucking_ = 0.2f;
108 };
109 
110 class AudioStreamRenderCallback : public AudioStreamCallback {
111 public:
112     virtual ~AudioStreamRenderCallback() = default;
113 
114     void OnStateChange(const State state, StateChangeCmdType cmdType = CMD_FROM_CLIENT) override;
115     void SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback);
116 private:
117     std::weak_ptr<AudioRendererCallback> callback_;
118 };
119 
120 }  // namespace AudioStandard
121 }  // namespace OHOS
122 #endif // AUDIO_RENDERER_GATEWAY_H
123