• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 SOUND_PARSER_H
16 #define SOUND_PARSER_H
17 
18 #include <atomic>
19 #include <deque>
20 #include <memory>
21 #include <mutex>
22 #include "cpp/mutex.h"
23 #include "ashmem.h"
24 #include "avcodec_audio_decoder.h"
25 #include "avcodec_errors.h"
26 #include "avdemuxer.h"
27 #include "avsource.h"
28 #include "buffer/avsharedmemory.h"
29 #include "avcodec_codec_name.h"
30 #include "cache_buffer.h"
31 #include "isoundpool.h"
32 #include "media_description.h"
33 #include "media_dfx.h"
34 #include "media_errors.h"
35 #include "media_log.h"
36 #include "securec.h"
37 
38 namespace OHOS {
39 namespace Media {
40 using namespace MediaAVCodec;
41 struct AudioBufferEntry;
42 
43 class SoundDecoderCallback : public AVCodecCallback, public NoCopyable {
44 public:
45     class SoundDecodeListener {
46     public:
SoundDecodeListener()47         SoundDecodeListener()
48         {
49             (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundDecodeListener",
50                 "Construction SoundDecodeListener");
51         }
~SoundDecodeListener()52         virtual ~SoundDecodeListener()
53         {
54             (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundDecodeListener",
55                 "Destruction SoundDecodeListener");
56         }
57         virtual void OnSoundDecodeCompleted(const std::shared_ptr<AudioBufferEntry> &fullCacheData) = 0;
58         virtual void SetSoundBufferTotalSize(const size_t soundBufferTotalSize) = 0;
59     };
60 
61     SoundDecoderCallback(const int32_t soundID, const std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> &audioDec,
62         const std::shared_ptr<MediaAVCodec::AVDemuxer> &demuxer, const bool isRawFile);
63     ~SoundDecoderCallback();
SetDecodeCallback(const std::shared_ptr<SoundDecodeListener> & listener)64     int32_t SetDecodeCallback(const std::shared_ptr<SoundDecodeListener> &listener)
65     {
66         (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundDecoderCallback",
67             "%{public}s:%{public}d", __func__, __LINE__);
68         if (listener == nullptr) {
69             (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundDecodeListener", "Invalid listener");
70             return MSERR_INVALID_VAL;
71         }
72         listener_ = listener;
73         return MSERR_OK;
74     }
75     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
76     void OnOutputFormatChanged(const Format &format) override;
77     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
78     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
79         std::shared_ptr<AVSharedMemory> buffer) override;
80 
81     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
82     int32_t ReCombineCacheData();
83     int32_t Release();
84 
85 private:
86     void DealBufferRawFile(MediaAVCodec::AVCodecBufferFlag bufferFlag, MediaAVCodec::AVCodecBufferInfo sampleInfo,
87         uint32_t index, std::shared_ptr<AVSharedMemory> buffer);
88 
89     int32_t soundID_ = 0;
90     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDec_;
91     std::shared_ptr<MediaAVCodec::AVDemuxer> demuxer_;
92     std::shared_ptr<SoundDecodeListener> listener_;
93     std::string trackMimeTypeInfo_;
94     bool isRawFile_ = false;
95     bool eosFlag_ = false;
96     std::deque<std::shared_ptr<AudioBufferEntry>> availableAudioBuffers_;
97     std::shared_ptr<AudioBufferEntry> fullCacheData_;
98     bool decodeShouldCompleted_;
99     int32_t currentSoundBufferSize_;
100     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
101     std::mutex amutex_;
102 };
103 
104 class SoundParser : public std::enable_shared_from_this<SoundParser> {
105 public:
106     SoundParser(int32_t soundID, std::string url);
107     SoundParser(int32_t soundID, int32_t fd, int64_t offset, int64_t length);
108     ~SoundParser();
109     int32_t DoParser();
GetSoundID()110     int32_t GetSoundID() const
111     {
112         return soundID_;
113     }
114     int32_t GetSoundData(std::shared_ptr<AudioBufferEntry> &soundData) const;
115     size_t GetSoundDataTotalSize() const;
GetSoundTrackFormat()116     MediaAVCodec::Format GetSoundTrackFormat() const
117     {
118         return trackFormat_;
119     }
120     bool IsSoundParserCompleted() const;
121 
122     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
123     int32_t Release();
124     int64_t GetSourceDuration();
125 
126 private:
127     class SoundParserListener : public SoundDecoderCallback::SoundDecodeListener {
128     public:
SoundParserListener(const std::weak_ptr<SoundParser> soundParser)129         explicit SoundParserListener(const std::weak_ptr<SoundParser> soundParser) : soundParserInner_(soundParser) {}
130         void OnSoundDecodeCompleted(
131             const std::shared_ptr<AudioBufferEntry> &fullCacheData) override;
132         void SetSoundBufferTotalSize(const size_t soundBufferTotalSize) override;
133         int32_t GetSoundData(std::shared_ptr<AudioBufferEntry> &soundData) const;
134         size_t GetSoundDataTotalSize() const;
135         bool IsSoundParserCompleted() const;
136 
137     private:
138         std::weak_ptr<SoundParser> soundParserInner_;
139         std::shared_ptr<AudioBufferEntry> soundData_;
140         size_t soundBufferTotalSize_ = 0;
141         std::atomic<bool> isSoundParserCompleted_ = false;
142     };
143 
144     int32_t DoDemuxer(MediaAVCodec::Format *trackFormat);
145     int32_t DoDecode(MediaAVCodec::Format &trackFormat);
146     int32_t soundID_ = 0;
147     std::shared_ptr<MediaAVCodec::AVDemuxer> demuxer_;
148     std::shared_ptr<MediaAVCodec::AVSource> source_;
149     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDec_;
150     std::shared_ptr<SoundDecoderCallback> audioDecCb_;
151     ffrt::mutex soundParserLock_;
152     std::shared_ptr<SoundParserListener> soundParserListener_;
153     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
154     bool isRawFile_ = false;
155     int32_t fdSource_ = -1;
156 
157     MediaAVCodec::Format trackFormat_;
158     int64_t sourceDurationInfo_{};
159 
160     static constexpr int32_t AUDIO_SOURCE_TRACK_COUNT = 1;
161     static constexpr int32_t AUDIO_SOURCE_TRACK_INDEX = 0;
162     static constexpr int64_t MIN_FD = 3;
163 };
164 } // namespace Media
165 } // namespace OHOS
166 #endif // SOUND_PARSER_H
167