• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 STREAM_H
16 #define STREAM_H
17 
18 #include <deque>
19 #include "audio_renderer.h"
20 #include "audio_info.h"
21 #include "audio_stream_info.h"
22 #include "audio_errors.h"
23 #include "isoundpool.h"
24 #include "media_description.h"
25 #include "cpp/mutex.h"
26 #include "media_dfx.h"
27 #include "thread_pool.h"
28 #include "audio_system_manager.h"
29 #include "soundpool_xcollie.h"
30 #include "parallel_stream_manager.h"
31 #include "cache_buffer.h"
32 
33 namespace OHOS {
34 namespace Media {
35 using namespace MediaAVCodec;
36 struct AudioBufferEntry;
37 class ParallelStreamManager;
38 
39 class Stream :
40     public AudioStandard::AudioRendererWriteCallback,
41     public AudioStandard::AudioRendererFirstFrameWritingCallback,
42     public AudioStandard::AudioRendererCallback,
43     public std::enable_shared_from_this<Stream> {
44 public:
45     Stream(const Format &trackFormat, const int32_t &soundID, const int32_t &streamID,
46         std::shared_ptr<ThreadPool> streamStopThreadPool);
47     ~Stream();
48     void SetSoundData(const std::shared_ptr<AudioBufferEntry> &cacheData, const size_t &cacheDataTotalSize);
49     void SetPlayParamAndRendererInfo(PlayParams &playParameters, AudioStandard::AudioRendererInfo &audioRenderInfo);
50     void SetManager(std::weak_ptr<OHOS::Media::ParallelStreamManager> parallelStreamManager);
51     void PreparePlay();
52     int32_t DoPlay();
53     int32_t Stop();
54     void OnWriteData(size_t length) override;
55     void OnFirstFrameWriting(uint64_t latency) override;
56     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
57     void OnStateChange(const AudioStandard::RendererState state,
58         const AudioStandard::StateChangeCmdType cmdType) override;
59     int32_t SetVolume(const float leftVolume, const float rightVolume);
60     int32_t SetRate(const AudioStandard::AudioRendererRate renderRate);
61     int32_t SetPriority(const int32_t priority);
62     int32_t SetLoop(const int32_t loop);
63     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
64     int32_t SetStreamCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
65     int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback);
66     int32_t GetSoundID();
67     int32_t GetStreamID();
68     int32_t GetPriority();
69 
70 private:
71     std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(
72         const AudioStandard::AudioRendererInfo &audioRendererInfo, const PlayParams &playParams);
73     void DealAudioRendererParams(AudioStandard::AudioRendererOptions &rendererOptions,
74         const AudioStandard::AudioRendererInfo &audioRendererInfo);
75     bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo);
76     void PrepareAudioRenderer(std::unique_ptr<AudioStandard::AudioRenderer> &audioRenderer);
77     void DealPlayParamsBeforePlay(const PlayParams &playParams);
78     static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate);
79     void DealWriteData(size_t length);
80     void AddStopTask();
81     int32_t GetGlobalId(int32_t soundID);
82     void DelGlobalId(int32_t globalId);
83     void SetGlobalId(int32_t soundID, int32_t globalId);
84 
85     Format trackFormat_;
86     int32_t soundID_ = 0;
87     int32_t streamID_ = 0;
88     PlayParams playParameters_;
89     AudioStandard::AudioRendererInfo audioRendererInfo_;
90     size_t cacheDataTotalSize_ = 0;
91     std::shared_ptr<AudioBufferEntry> fullCacheData_;
92 
93     std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_;
94     std::atomic<bool> isRunning_ = false;
95     std::atomic<bool> startStopFlag_ = false;
96     AudioStandard::AudioSampleFormat sampleFormat_ = AudioStandard::AudioSampleFormat::INVALID_WIDTH;
97     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
98     std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr;
99     std::shared_ptr<ISoundPoolCallback> streamCallback_ = nullptr;
100     ffrt::mutex streamLock_;
101     std::weak_ptr<ThreadPool> streamStopThreadPool_;
102     std::weak_ptr<OHOS::Media::ParallelStreamManager> manager_;
103 
104     int32_t loop_ = 0;
105     int32_t priority_ = 0;
106     int32_t rendererFlags_ = 0;
107     size_t cacheDataFrameIndex_ = 0;
108     int32_t havePlayedCount_ = 0;
109 };
110 } // namespace Media
111 } // namespace OHOS
112 #endif // STREAM_H
113