• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2023 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef SOUND_PARSER_H
17 #define SOUND_PARSER_H
18 
19 #include <atomic>
20 #include <deque>
21 #include <memory>
22 #include <mutex>
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 "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_errors.h"
34 #include "media_log.h"
35 #include "securec.h"
36 
37 namespace OHOS {
38 namespace Media {
39 using namespace MediaAVCodec;
40 
41 class SoundDecoderCallback : public AVCodecCallback, public NoCopyable {
42 public:
43     class SoundDecodeListener {
44     public:
SoundDecodeListener()45         SoundDecodeListener()
46         {
47             MEDIA_INFO_LOG("Construction SoundDecodeListener");
48         }
~SoundDecodeListener()49         virtual ~SoundDecodeListener()
50         {
51             MEDIA_INFO_LOG("Destruction SoundDecodeListener");
52         }
53         virtual void OnSoundDecodeCompleted(const std::deque<std::shared_ptr<AudioBufferEntry>>
54             availableAudioBuffers) = 0;
55         virtual void SetSoundBufferTotalSize(const size_t soundBufferTotalSize) = 0;
56     };
57 
58     SoundDecoderCallback(const int32_t soundID, const std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> &audioDec,
59         const std::shared_ptr<MediaAVCodec::AVDemuxer> &demuxer, const bool isRawFile);
60     ~SoundDecoderCallback();
SetDecodeCallback(const std::shared_ptr<SoundDecodeListener> & listener)61     int32_t SetDecodeCallback(const std::shared_ptr<SoundDecodeListener> &listener)
62     {
63         MEDIA_INFO_LOG("%{public}s:%{public}d", __func__, __LINE__);
64         CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_INVALID_VAL, "Invalid listener.");
65         listener_ = listener;
66         return MSERR_OK;
67     }
68     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
69     void OnOutputFormatChanged(const Format &format) override;
70     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
71     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
72         std::shared_ptr<AVSharedMemory> buffer) override;
73 
74     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
75     int32_t Release();
76 
77 private:
78     const int32_t soundID_;
79     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDec_;
80     std::shared_ptr<MediaAVCodec::AVDemuxer> demuxer_;
81     std::shared_ptr<SoundDecodeListener> listener_;
82     std::string trackMimeTypeInfo_;
83     bool isRawFile_ = false;
84     bool eosFlag_;
85     std::deque<std::shared_ptr<AudioBufferEntry>> availableAudioBuffers_;
86     bool decodeShouldCompleted_;
87     int32_t currentSoundBufferSize_;
88     std::condition_variable bufferCond_;
89     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
90     std::mutex amutex_;
91 };
92 
93 class SoundParser : public std::enable_shared_from_this<SoundParser> {
94 public:
95     SoundParser(int32_t soundID, std::string url);
96     SoundParser(int32_t soundID, int32_t fd, int64_t offset, int64_t length);
97     ~SoundParser();
98     int32_t DoParser();
GetSoundID()99     int32_t GetSoundID() const
100     {
101         return soundID_;
102     }
103     int32_t GetSoundData(std::deque<std::shared_ptr<AudioBufferEntry>> &soundData) const;
104     size_t GetSoundDataTotalSize() const;
GetSoundTrackFormat()105     MediaAVCodec::Format GetSoundTrackFormat() const
106     {
107         return trackFormat_;
108     }
109     bool IsSoundParserCompleted() const;
110 
111     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
112     int32_t Release();
113 
114 private:
115     class SoundParserListener : public SoundDecoderCallback::SoundDecodeListener {
116     public:
SoundParserListener(const std::weak_ptr<SoundParser> soundParser)117         explicit SoundParserListener(const std::weak_ptr<SoundParser> soundParser) : soundParserInner_(soundParser) {}
118 
OnSoundDecodeCompleted(const std::deque<std::shared_ptr<AudioBufferEntry>> availableAudioBuffers)119         void OnSoundDecodeCompleted(const std::deque<std::shared_ptr<AudioBufferEntry>> availableAudioBuffers) override
120         {
121             std::unique_lock<std::mutex> lock(soundParserInner_.lock()->soundParserLock_);
122             if (!soundParserInner_.expired()) {
123                 soundData_ = availableAudioBuffers;
124                 isSoundParserCompleted_.store(true);
125             }
126         }
SetSoundBufferTotalSize(const size_t soundBufferTotalSize)127         void SetSoundBufferTotalSize(const size_t soundBufferTotalSize) override
128         {
129             std::unique_lock<std::mutex> lock(soundParserInner_.lock()->soundParserLock_);
130             if (!soundParserInner_.expired()) {
131                 soundBufferTotalSize_ = soundBufferTotalSize;
132             }
133         }
GetSoundData(std::deque<std::shared_ptr<AudioBufferEntry>> & soundData)134         int32_t GetSoundData(std::deque<std::shared_ptr<AudioBufferEntry>> &soundData) const
135         {
136             std::unique_lock<std::mutex> lock(soundParserInner_.lock()->soundParserLock_);
137             soundData = soundData_;
138             return MSERR_OK;
139         }
140 
GetSoundDataTotalSize()141         size_t GetSoundDataTotalSize() const
142         {
143             std::unique_lock<std::mutex> lock(soundParserInner_.lock()->soundParserLock_);
144             return soundBufferTotalSize_;
145         }
IsSoundParserCompleted()146         bool IsSoundParserCompleted() const
147         {
148             std::unique_lock<std::mutex> lock(soundParserInner_.lock()->soundParserLock_);
149             return isSoundParserCompleted_.load();
150         }
151 
152     private:
153         std::weak_ptr<SoundParser> soundParserInner_;
154         std::deque<std::shared_ptr<AudioBufferEntry>> soundData_;
155         size_t soundBufferTotalSize_ = 0;
156         std::atomic<bool> isSoundParserCompleted_ = false;
157     };
158 
159     int32_t DoDemuxer(MediaAVCodec::Format *trackFormat);
160     int32_t DoDecode(MediaAVCodec::Format trackFormat);
161     int32_t soundID_;
162     std::shared_ptr<MediaAVCodec::AVDemuxer> demuxer_;
163     std::shared_ptr<MediaAVCodec::AVSource> source_;
164     std::shared_ptr<MediaAVCodec::AVCodecAudioDecoder> audioDec_;
165     std::shared_ptr<SoundDecoderCallback> audioDecCb_;
166     std::mutex soundParserLock_;
167     std::shared_ptr<SoundParserListener> soundParserListener_;
168     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
169     bool isRawFile_ = false;
170 
171     MediaAVCodec::Format trackFormat_;
172 
173     static constexpr int32_t AUDIO_SOURCE_TRACK_COUNT = 1;
174     static constexpr int32_t AUDIO_SOURCE_TRACK_INDEX = 0;
175     static constexpr int64_t MIN_FD = 0;
176 };
177 } // namespace Media
178 } // namespace OHOS
179 #endif // SOUND_PARSER_H
180