已开启
improve audio capture #9348
yxc2创建于 7 天前
improve audio capture #9348
已开启
共 4 个文件变更+161-111
| @@ -15,22 +15,25 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | 18 | ||
| 19 | + | ||
| 20 | + | ||
| 22 | 21 | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + | ||
| 23 | 25 | ||
| 24 | namespace OHOS { | 26 | namespace OHOS { |
| 25 | namespace Media { | 27 | namespace Media { |
| 26 | namespace Pipeline { | 28 | namespace Pipeline { |
| 27 | 29 | ||
| 28 | -class AudioDataSourceFilter : public Filter, public std::enable_shared_from_this<AudioDataSourceFilter> { | 30 | +class AudioDataSourceFilter : public Filter, |
| 31 | + public IAudioDataSourceListener, | ||
| 32 | + public std::enable_shared_from_this<AudioDataSourceFilter> { | ||
| 29 | public: | 33 | public: |
| 30 | explicit AudioDataSourceFilter(std::string name, FilterType type); | 34 | explicit AudioDataSourceFilter(std::string name, FilterType type); |
| 31 | ~AudioDataSourceFilter() override; | 35 | ~AudioDataSourceFilter() override; |
| 32 | - void Init(const std::shared_ptr<EventReceiver>& receiver, | 36 | + void Init(const std::shared_ptr<EventReceiver> &receiver, const std::shared_ptr<FilterCallback> &callback) override; |
| 33 | - const std::shared_ptr<FilterCallback>& callback) override; | ||
| 34 | Status DoPrepare() override; | 37 | Status DoPrepare() override; |
| 35 | Status DoStart() override; | 38 | Status DoStart() override; |
| 36 | Status DoPause() override; | 39 | Status DoPause() override; |
| @@ -38,39 +41,44 @@ public: | |||
| 38 | Status DoStop() override; | 41 | Status DoStop() override; |
| 39 | Status DoFlush() override; | 42 | Status DoFlush() override; |
| 40 | Status DoRelease() override; | 43 | Status DoRelease() override; |
| 41 | - void SetParameter(const std::shared_ptr<Meta>& meta) override; | 44 | + void SetParameter(const std::shared_ptr<Meta> &meta) override; |
| 42 | - void GetParameter(std::shared_ptr<Meta>& meta) override; | 45 | + void GetParameter(std::shared_ptr<Meta> &meta) override; |
| 43 | - Status LinkNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType) override; | 46 | + Status LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override; |
| 44 | - Status UpdateNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType) override; | 47 | + Status UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override; |
| 45 | - Status UnLinkNext(const std::shared_ptr<Filter>& nextFilter, StreamType outType) override; | 48 | + Status UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override; |
| 46 | Status SendEos(); | 49 | Status SendEos(); |
| 47 | FilterType GetFilterType(); | 50 | FilterType GetFilterType(); |
| 48 | - void SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource); | 51 | + void SetAudioDataSource(const std::shared_ptr<IAudioDataSource> &audioSource); |
| 49 | - void OnLinkedResult(const sptr<AVBufferQueueProducer>& queue, std::shared_ptr<Meta>& meta); | 52 | + void OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta); |
| 50 | - Status OnLinked(StreamType inType, const std::shared_ptr<Meta>& meta, | 53 | + Status OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta, |
| 51 | - const std::shared_ptr<FilterLinkCallback>& callback) override; | 54 | + const std::shared_ptr<FilterLinkCallback> &callback) override; |
| 52 | - Status OnUpdated(StreamType inType, const std::shared_ptr<Meta>& meta, | 55 | + Status OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta, |
| 53 | - const std::shared_ptr<FilterLinkCallback>& callback) override; | 56 | + const std::shared_ptr<FilterLinkCallback> &callback) override; |
| 54 | - Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback) override; | 57 | + Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback> &callback) override; |
| 55 | - void OnUnlinkedResult(const std::shared_ptr<Meta>& meta); | 58 | + void OnUnlinkedResult(const std::shared_ptr<Meta> &meta); |
| 56 | - void OnUpdatedResult(const std::shared_ptr<Meta>& meta); | 59 | + void OnUpdatedResult(const std::shared_ptr<Meta> &meta); |
| 57 | void SetVideoFirstFramePts(int64_t firstFramePts); | 60 | void SetVideoFirstFramePts(int64_t firstFramePts); |
| 61 | + void OnAudioDataReady() override; | ||
| 62 | + | ||
| 58 | private: | 63 | private: |
| 59 | - void ReadLoop(); | 64 | + void ProcessLoop(); |
| 60 | - int32_t RelativeSleep(int64_t nanoTime); | 65 | + AudioDataSourceReadAtActionState DoReadAndPush(); |
| 61 | - std::shared_ptr<Task> taskPtr_{ nullptr }; | 66 | + std::shared_ptr<Task> taskPtr_{nullptr}; |
| 62 | sptr<AVBufferQueueProducer> outputBufferQueue_; | 67 | sptr<AVBufferQueueProducer> outputBufferQueue_; |
| 63 | - std::shared_ptr<IAudioDataSource> audioDataSource_{ nullptr }; | 68 | + std::shared_ptr<IAudioDataSource> audioDataSource_{nullptr}; |
| 64 | 69 | ||
| 65 | std::shared_ptr<EventReceiver> receiver_; | 70 | std::shared_ptr<EventReceiver> receiver_; |
| 66 | std::shared_ptr<FilterCallback> callback_; | 71 | std::shared_ptr<FilterCallback> callback_; |
| 67 | 72 | ||
| 68 | std::shared_ptr<Filter> nextFilter_; | 73 | std::shared_ptr<Filter> nextFilter_; |
| 69 | - std::atomic<bool> eos_{ false }; | 74 | + std::atomic<bool> eos_{false}; |
| 70 | 75 | ||
| 71 | - Mutex captureMutex_{}; | 76 | + std::mutex dataMutex_; |
| 77 | + std::condition_variable dataCond_; | ||
| 78 | + bool dataReady_ = false; | ||
| 79 | + std::atomic<bool> stopped_{false}; | ||
| 72 | }; | 80 | }; |
| 73 | } // namespace Pipeline | 81 | } // namespace Pipeline |
| 74 | } // namespace Media | 82 | } // namespace Media |
| 75 | } // namespace OHOS | 83 | } // namespace OHOS |
| 76 | -#endif // FILTERS_AUDIO_CAPTURE_FILTER_H | 84 | +#endif // FILTERS_AUDIO_CAPTURE_FILTER_H |
| @@ -12,7 +12,7 @@ | |||
| 12 | * See the License for the specific language governing permissions and | 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. | 13 | * limitations under the License. |
| 14 | */ | 14 | */ |
| 15 | - | 15 | + |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| @@ -20,11 +20,8 @@ | |||
| 20 | namespace { | 20 | namespace { |
| 21 | constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "AudioDataSourceFilter" }; | 21 | constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "AudioDataSourceFilter" }; |
| 22 | static constexpr uint8_t LOG_LIMIT_HUNDRED = 100; | 22 | static constexpr uint8_t LOG_LIMIT_HUNDRED = 100; |
| 23 | -static constexpr uint64_t AUDIO_NS_PER_SECOND = 1000000000; // ns 1s | ||
| 24 | -static constexpr int64_t AUDIO_DATASOURCE_FILTER_READ_FAILED_WAIT_TIME = 21333333; // 20000000 us 20ms | ||
| 25 | -static constexpr int64_t AUDIO_DATASOURCE_FILTER_READ_SUCCESS_WAIT_TIME = 4000000; // us 4ms | ||
| 26 | } | 23 | } |
| 27 | - | 24 | + |
| 28 | namespace OHOS { | 25 | namespace OHOS { |
| 29 | namespace Media { | 26 | namespace Media { |
| 30 | namespace Pipeline { | 27 | namespace Pipeline { |
| @@ -34,7 +31,7 @@ static AutoRegisterFilter<AudioDataSourceFilter> g_registerAudioDataSourceFilter | |||
| 34 | [](const std::string& name, const FilterType type) { | 31 | [](const std::string& name, const FilterType type) { |
| 35 | return std::make_shared<AudioDataSourceFilter>(name, FilterType::AUDIO_DATA_SOURCE); | 32 | return std::make_shared<AudioDataSourceFilter>(name, FilterType::AUDIO_DATA_SOURCE); |
| 36 | }); | 33 | }); |
| 37 | - | 34 | + |
| 38 | /// End of Stream Buffer Flag | 35 | /// End of Stream Buffer Flag |
| 39 | constexpr uint32_t BUFFER_FLAG_EOS = 0x00000001; | 36 | constexpr uint32_t BUFFER_FLAG_EOS = 0x00000001; |
| 40 | class AudioDataSourceFilterLinkCallback : public FilterLinkCallback { | 37 | class AudioDataSourceFilterLinkCallback : public FilterLinkCallback { |
| @@ -43,7 +40,7 @@ public: | |||
| 43 | : audioDataSourceFilter_(std::move(audioDataSourceFilter)) | 40 | : audioDataSourceFilter_(std::move(audioDataSourceFilter)) |
| 44 | { | 41 | { |
| 45 | } | 42 | } |
| 46 | - | 43 | + |
| 47 | void OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta) override | 44 | void OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta) override |
| 48 | { | 45 | { |
| 49 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { | 46 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { |
| @@ -52,7 +49,7 @@ public: | |||
| 52 | MEDIA_LOG_I("OnLinkedResult invalid dataSourceFilter"); | 49 | MEDIA_LOG_I("OnLinkedResult invalid dataSourceFilter"); |
| 53 | } | 50 | } |
| 54 | } | 51 | } |
| 55 | - | 52 | + |
| 56 | void OnUnlinkedResult(std::shared_ptr<Meta> &meta) override | 53 | void OnUnlinkedResult(std::shared_ptr<Meta> &meta) override |
| 57 | { | 54 | { |
| 58 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { | 55 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { |
| @@ -61,7 +58,7 @@ public: | |||
| 61 | MEDIA_LOG_I("OnUnlinkedResult invalid dataSourceFilter"); | 58 | MEDIA_LOG_I("OnUnlinkedResult invalid dataSourceFilter"); |
| 62 | } | 59 | } |
| 63 | } | 60 | } |
| 64 | - | 61 | + |
| 65 | void OnUpdatedResult(std::shared_ptr<Meta> &meta) override | 62 | void OnUpdatedResult(std::shared_ptr<Meta> &meta) override |
| 66 | { | 63 | { |
| 67 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { | 64 | if (auto dataSourceFilter = audioDataSourceFilter_.lock()) { |
| @@ -70,21 +67,21 @@ public: | |||
| 70 | MEDIA_LOG_I("OnUpdatedResult invalid dataSourceFilter"); | 67 | MEDIA_LOG_I("OnUpdatedResult invalid dataSourceFilter"); |
| 71 | } | 68 | } |
| 72 | } | 69 | } |
| 73 | - | 70 | + |
| 74 | private: | 71 | private: |
| 75 | std::weak_ptr<AudioDataSourceFilter> audioDataSourceFilter_; | 72 | std::weak_ptr<AudioDataSourceFilter> audioDataSourceFilter_; |
| 76 | }; | 73 | }; |
| 77 | - | 74 | + |
| 78 | AudioDataSourceFilter::AudioDataSourceFilter(std::string name, FilterType type): Filter(name, type) | 75 | AudioDataSourceFilter::AudioDataSourceFilter(std::string name, FilterType type): Filter(name, type) |
| 79 | { | 76 | { |
| 80 | MEDIA_LOG_I("audio data source filter create"); | 77 | MEDIA_LOG_I("audio data source filter create"); |
| 81 | } | 78 | } |
| 82 | - | 79 | + |
| 83 | AudioDataSourceFilter::~AudioDataSourceFilter() | 80 | AudioDataSourceFilter::~AudioDataSourceFilter() |
| 84 | { | 81 | { |
| 85 | MEDIA_LOG_I("audio data source filter destroy"); | 82 | MEDIA_LOG_I("audio data source filter destroy"); |
| 86 | } | 83 | } |
| 87 | - | 84 | + |
| 88 | void AudioDataSourceFilter::Init(const std::shared_ptr<EventReceiver> &receiver, | 85 | void AudioDataSourceFilter::Init(const std::shared_ptr<EventReceiver> &receiver, |
| 89 | const std::shared_ptr<FilterCallback> &callback) | 86 | const std::shared_ptr<FilterCallback> &callback) |
| 90 | { | 87 | { |
| @@ -93,10 +90,10 @@ void AudioDataSourceFilter::Init(const std::shared_ptr<EventReceiver> &receiver, | |||
| 93 | callback_ = callback; | 90 | callback_ = callback; |
| 94 | if (!taskPtr_) { | 91 | if (!taskPtr_) { |
| 95 | taskPtr_ = std::make_shared<Task>("DataReader"); | 92 | taskPtr_ = std::make_shared<Task>("DataReader"); |
| 96 | - taskPtr_->RegisterJob([this] { ReadLoop(); return 0;}); | 93 | + taskPtr_->RegisterJob([this] { ProcessLoop(); return 0;}); |
| 97 | } | 94 | } |
| 98 | } | 95 | } |
| 99 | - | 96 | + |
| 100 | Status AudioDataSourceFilter::DoPrepare() | 97 | Status AudioDataSourceFilter::DoPrepare() |
| 101 | { | 98 | { |
| 102 | MEDIA_LOG_I("AudioDataSourceFilter DoPrepare"); | 99 | MEDIA_LOG_I("AudioDataSourceFilter DoPrepare"); |
| @@ -108,72 +105,100 @@ Status AudioDataSourceFilter::DoPrepare() | |||
| 108 | StreamType::STREAMTYPE_RAW_AUDIO); | 105 | StreamType::STREAMTYPE_RAW_AUDIO); |
| 109 | return Status::OK; | 106 | return Status::OK; |
| 110 | } | 107 | } |
| 111 | - | 108 | + |
| 112 | Status AudioDataSourceFilter::DoStart() | 109 | Status AudioDataSourceFilter::DoStart() |
| 113 | { | 110 | { |
| 114 | MEDIA_LOG_I("AudioDataSourceFilter DoStart"); | 111 | MEDIA_LOG_I("AudioDataSourceFilter DoStart"); |
| 115 | eos_ = false; | 112 | eos_ = false; |
| 113 | + { | ||
| 114 | + std::lock_guard<std::mutex> lock(dataMutex_); | ||
| 115 | + stopped_.store(false); | ||
| 116 | + dataReady_ = false; | ||
| 117 | + } | ||
| 116 | if (taskPtr_) { | 118 | if (taskPtr_) { |
| 117 | taskPtr_->Start(); | 119 | taskPtr_->Start(); |
| 118 | } | 120 | } |
| 119 | return Status::OK; | 121 | return Status::OK; |
| 120 | } | 122 | } |
| 121 | - | 123 | + |
| 122 | Status AudioDataSourceFilter::DoPause() | 124 | Status AudioDataSourceFilter::DoPause() |
| 123 | { | 125 | { |
| 124 | MEDIA_LOG_I("AudioDataSourceFilter DoPause"); | 126 | MEDIA_LOG_I("AudioDataSourceFilter DoPause"); |
| 127 | + { | ||
| 128 | + std::lock_guard<std::mutex> lock(dataMutex_); | ||
| 129 | + stopped_.store(true); | ||
| 130 | + dataCond_.notify_all(); | ||
| 131 | + } | ||
| 125 | if (taskPtr_) { | 132 | if (taskPtr_) { |
| 126 | taskPtr_->Pause(); | 133 | taskPtr_->Pause(); |
| 127 | } | 134 | } |
| 128 | return Status::OK; | 135 | return Status::OK; |
| 129 | } | 136 | } |
| 130 | - | 137 | + |
| 131 | Status AudioDataSourceFilter::DoResume() | 138 | Status AudioDataSourceFilter::DoResume() |
| 132 | { | 139 | { |
| 133 | MEDIA_LOG_I("AudioDataSourceFilter DoResume"); | 140 | MEDIA_LOG_I("AudioDataSourceFilter DoResume"); |
| 141 | + { | ||
| 142 | + std::lock_guard<std::mutex> lock(dataMutex_); | ||
| 143 | + stopped_.store(false); | ||
| 144 | + dataReady_ = false; | ||
| 145 | + } | ||
| 134 | if (taskPtr_) { | 146 | if (taskPtr_) { |
| 135 | taskPtr_->Start(); | 147 | taskPtr_->Start(); |
| 136 | } | 148 | } |
| 137 | return Status::OK; | 149 | return Status::OK; |
| 138 | } | 150 | } |
| 139 | - | 151 | + |
| 140 | Status AudioDataSourceFilter::DoStop() | 152 | Status AudioDataSourceFilter::DoStop() |
| 141 | { | 153 | { |
| 142 | MEDIA_LOG_I("AudioDataSourceFilter DoStop"); | 154 | MEDIA_LOG_I("AudioDataSourceFilter DoStop"); |
| 143 | - // stop task firstly | 155 | + { |
| 156 | + std::lock_guard<std::mutex> lock(dataMutex_); | ||
| 157 | + stopped_.store(true); | ||
| 158 | + dataReady_ = false; | ||
| 159 | + dataCond_.notify_all(); | ||
| 160 | + } | ||
| 144 | if (taskPtr_) { | 161 | if (taskPtr_) { |
| 145 | taskPtr_->Stop(); | 162 | taskPtr_->Stop(); |
| 146 | } | 163 | } |
| 147 | return Status::OK; | 164 | return Status::OK; |
| 148 | } | 165 | } |
| 149 | - | 166 | + |
| 150 | Status AudioDataSourceFilter::DoFlush() | 167 | Status AudioDataSourceFilter::DoFlush() |
| 151 | { | 168 | { |
| 152 | MEDIA_LOG_I("AudioDataSourceFilter DoFlush"); | 169 | MEDIA_LOG_I("AudioDataSourceFilter DoFlush"); |
| 153 | return Status::OK; | 170 | return Status::OK; |
| 154 | } | 171 | } |
| 155 | - | 172 | + |
| 156 | Status AudioDataSourceFilter::DoRelease() | 173 | Status AudioDataSourceFilter::DoRelease() |
| 157 | { | 174 | { |
| 158 | MEDIA_LOG_I("AudioDataSourceFilter DoRelease"); | 175 | MEDIA_LOG_I("AudioDataSourceFilter DoRelease"); |
| 176 | + { | ||
| 177 | + std::lock_guard<std::mutex> lock(dataMutex_); | ||
| 178 | + stopped_.store(true); | ||
| 179 | + dataCond_.notify_all(); | ||
| 180 | + } | ||
| 159 | if (taskPtr_) { | 181 | if (taskPtr_) { |
| 160 | taskPtr_->Stop(); | 182 | taskPtr_->Stop(); |
| 161 | } | 183 | } |
| 162 | taskPtr_ = nullptr; | 184 | taskPtr_ = nullptr; |
| 185 | + if (audioDataSource_) { | ||
| 186 | + audioDataSource_->SetListener(nullptr); | ||
| 187 | + } | ||
| 163 | audioDataSource_ = nullptr; | 188 | audioDataSource_ = nullptr; |
| 164 | return Status::OK; | 189 | return Status::OK; |
| 165 | } | 190 | } |
| 166 | - | 191 | + |
| 167 | void AudioDataSourceFilter::SetParameter(const std::shared_ptr<Meta> &meta) | 192 | void AudioDataSourceFilter::SetParameter(const std::shared_ptr<Meta> &meta) |
| 168 | { | 193 | { |
| 169 | MEDIA_LOG_I("AudioDataSourceFilter SetParameter"); | 194 | MEDIA_LOG_I("AudioDataSourceFilter SetParameter"); |
| 170 | } | 195 | } |
| 171 | - | 196 | + |
| 172 | void AudioDataSourceFilter::GetParameter(std::shared_ptr<Meta> &meta) | 197 | void AudioDataSourceFilter::GetParameter(std::shared_ptr<Meta> &meta) |
| 173 | { | 198 | { |
| 174 | MEDIA_LOG_I("AudioDataSourceFilter GetParameter"); | 199 | MEDIA_LOG_I("AudioDataSourceFilter GetParameter"); |
| 175 | } | 200 | } |
| 176 | - | 201 | + |
| 177 | Status AudioDataSourceFilter::LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) | 202 | Status AudioDataSourceFilter::LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) |
| 178 | { | 203 | { |
| 179 | MEDIA_LOG_I("AudioDataSourceFilter LinkNext"); | 204 | MEDIA_LOG_I("AudioDataSourceFilter LinkNext"); |
| @@ -186,16 +211,19 @@ Status AudioDataSourceFilter::LinkNext(const std::shared_ptr<Filter> &nextFilter | |||
| 186 | nextFilter->OnLinked(outType, meta, filterLinkCallback); | 211 | nextFilter->OnLinked(outType, meta, filterLinkCallback); |
| 187 | return Status::OK; | 212 | return Status::OK; |
| 188 | } | 213 | } |
| 189 | - | 214 | + |
| 190 | FilterType AudioDataSourceFilter::GetFilterType() | 215 | FilterType AudioDataSourceFilter::GetFilterType() |
| 191 | { | 216 | { |
| 192 | MEDIA_LOG_I("AudioDataSourceFilter GetFilterType"); | 217 | MEDIA_LOG_I("AudioDataSourceFilter GetFilterType"); |
| 193 | return FilterType::AUDIO_CAPTURE; | 218 | return FilterType::AUDIO_CAPTURE; |
| 194 | } | 219 | } |
| 195 | - | 220 | + |
| 196 | void AudioDataSourceFilter::SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource) | 221 | void AudioDataSourceFilter::SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource) |
| 197 | { | 222 | { |
| 198 | audioDataSource_ = audioSource; | 223 | audioDataSource_ = audioSource; |
| 224 | + if (audioDataSource_) { | ||
| 225 | + audioDataSource_->SetListener(shared_from_this()); | ||
| 226 | + } | ||
| 199 | } | 227 | } |
| 200 | 228 | ||
| 201 | void AudioDataSourceFilter::SetVideoFirstFramePts(int64_t firstFramePts) | 229 | void AudioDataSourceFilter::SetVideoFirstFramePts(int64_t firstFramePts) |
| @@ -224,121 +252,124 @@ Status AudioDataSourceFilter::SendEos() | |||
| 224 | eos_ = true; | 252 | eos_ = true; |
| 225 | return ret; | 253 | return ret; |
| 226 | } | 254 | } |
| 227 | - | 255 | + |
| 228 | -void AudioDataSourceFilter::ReadLoop() | 256 | +void AudioDataSourceFilter::OnAudioDataReady() |
| 229 | { | 257 | { |
| 230 | - MEDIA_LOG_D("AudioDataSourceFilter ReadLoop In"); | 258 | + std::lock_guard<std::mutex> lock(dataMutex_); |
| 231 | - if (eos_.load() || audioDataSource_ == nullptr) { | 259 | + dataReady_ = true; |
| 232 | - return; | 260 | + dataCond_.notify_one(); |
| 261 | +} | ||
| 262 | + | ||
| 263 | +void AudioDataSourceFilter::ProcessLoop() | ||
| 264 | +{ | ||
| 265 | + { | ||
| 266 | + std::unique_lock<std::mutex> lock(dataMutex_); | ||
| 267 | + dataCond_.wait(lock, [this] { return dataReady_ || stopped_.load(); }); | ||
| 268 | + if (stopped_.load()) { | ||
| 269 | + return; | ||
| 270 | + } | ||
| 271 | + dataReady_ = false; | ||
| 272 | + } | ||
| 273 | + while (!stopped_.load() && !eos_.load()) { | ||
| 274 | + if (DoReadAndPush() != AudioDataSourceReadAtActionState::OK) { | ||
| 275 | + break; | ||
| 276 | + } | ||
| 277 | + } | ||
| 278 | +} | ||
| 279 | + | ||
| 280 | +AudioDataSourceReadAtActionState AudioDataSourceFilter::DoReadAndPush() | ||
| 281 | +{ | ||
| 282 | + if (audioDataSource_ == nullptr) { | ||
| 283 | + return AudioDataSourceReadAtActionState::INVALID; | ||
| 233 | } | 284 | } |
| 234 | int64_t bufferSize = 0; | 285 | int64_t bufferSize = 0; |
| 235 | if (audioDataSource_->GetSize(bufferSize) != 0) { | 286 | if (audioDataSource_->GetSize(bufferSize) != 0) { |
| 236 | - MEDIA_LOGE_LIMIT(LOG_LIMIT_HUNDRED, "Get audioCaptureModule buffer size fail"); | 287 | + return AudioDataSourceReadAtActionState::RETRY_SKIP; |
| 237 | - return; | 288 | + } |
| 289 | + if (outputBufferQueue_ == nullptr) { | ||
| 290 | + MEDIA_LOG_I("AudioDataSourceFilter outputBufferQueue_ is nullptr"); | ||
| 291 | + return AudioDataSourceReadAtActionState::INVALID; | ||
| 238 | } | 292 | } |
| 239 | - MEDIA_LOG_D("AudioDataSourceFilter GetSize : " PUBLIC_LOG_D64, bufferSize); | ||
| 240 | std::shared_ptr<AVBuffer> buffer; | 293 | std::shared_ptr<AVBuffer> buffer; |
| 241 | AVBufferConfig avBufferConfig; | 294 | AVBufferConfig avBufferConfig; |
| 242 | avBufferConfig.size = bufferSize; | 295 | avBufferConfig.size = bufferSize; |
| 243 | avBufferConfig.memoryFlag = MemoryFlag::MEMORY_READ_WRITE; | 296 | avBufferConfig.memoryFlag = MemoryFlag::MEMORY_READ_WRITE; |
| 244 | - if (outputBufferQueue_ == nullptr) { | ||
| 245 | - MEDIA_LOG_I("AudioDataSourceFilter outputBufferQueue_ is nullptr"); | ||
| 246 | - return; | ||
| 247 | - } | ||
| 248 | Status status = outputBufferQueue_->RequestBuffer(buffer, avBufferConfig, TIME_OUT_MS); | 297 | Status status = outputBufferQueue_->RequestBuffer(buffer, avBufferConfig, TIME_OUT_MS); |
| 249 | if (status != Status::OK) { | 298 | if (status != Status::OK) { |
| 250 | MEDIA_LOGE_LIMIT(LOG_LIMIT_HUNDRED, "AudioDataSourceFilter RequestBuffer fail"); | 299 | MEDIA_LOGE_LIMIT(LOG_LIMIT_HUNDRED, "AudioDataSourceFilter RequestBuffer fail"); |
| 251 | - return; | 300 | + audioDataSource_->ReadAt(nullptr, bufferSize); |
| 301 | + return AudioDataSourceReadAtActionState::RETRY_SKIP; | ||
| 252 | } | 302 | } |
| 253 | AudioDataSourceReadAtActionState readAtRet = audioDataSource_->ReadAt(buffer, bufferSize); | 303 | AudioDataSourceReadAtActionState readAtRet = audioDataSource_->ReadAt(buffer, bufferSize); |
| 254 | if (readAtRet != AudioDataSourceReadAtActionState::OK) { | 304 | if (readAtRet != AudioDataSourceReadAtActionState::OK) { |
| 255 | - if (readAtRet != AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG) { // log after Started | 305 | + if (readAtRet != AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG) { |
| 256 | MEDIA_LOGE_LIMIT(LOG_LIMIT_HUNDRED, "AudioDataSourceFilter ReadAt fail ret: %{public}d", | 306 | MEDIA_LOGE_LIMIT(LOG_LIMIT_HUNDRED, "AudioDataSourceFilter ReadAt fail ret: %{public}d", |
| 257 | static_cast<int32_t>(readAtRet)); | 307 | static_cast<int32_t>(readAtRet)); |
| 258 | } | 308 | } |
| 259 | outputBufferQueue_->PushBuffer(buffer, false); | 309 | outputBufferQueue_->PushBuffer(buffer, false); |
| 260 | - if (readAtRet == AudioDataSourceReadAtActionState::RETRY_IN_INTERVAL) { // retry after 20ms | 310 | + return readAtRet; |
| 261 | - RelativeSleep(AUDIO_DATASOURCE_FILTER_READ_FAILED_WAIT_TIME); | 311 | + } |
| 262 | - } | 312 | + if (buffer == nullptr || buffer->memory_ == nullptr) { |
| 263 | - return; | 313 | + MEDIA_LOG_E("buffer->memory_ is nullptr"); |
| 314 | + return AudioDataSourceReadAtActionState::RETRY_SKIP; | ||
| 264 | } | 315 | } |
| 265 | - FALSE_RETURN_MSG(buffer != nullptr && buffer->memory_ != nullptr, "buffer->memory_ is nullptr"); | ||
| 266 | buffer->memory_->SetSize(bufferSize); | 316 | buffer->memory_->SetSize(bufferSize); |
| 267 | status = outputBufferQueue_->PushBuffer(buffer, true); | 317 | status = outputBufferQueue_->PushBuffer(buffer, true); |
| 268 | if (status != Status::OK) { | 318 | if (status != Status::OK) { |
| 269 | MEDIA_LOG_E("AudioDataSourceFilter PushBuffer fail"); | 319 | MEDIA_LOG_E("AudioDataSourceFilter PushBuffer fail"); |
| 270 | } | 320 | } |
| 271 | - RelativeSleep(AUDIO_DATASOURCE_FILTER_READ_SUCCESS_WAIT_TIME); | 321 | + return AudioDataSourceReadAtActionState::OK; |
| 272 | } | 322 | } |
| 273 | - | 323 | + |
| 274 | void AudioDataSourceFilter::OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta) | 324 | void AudioDataSourceFilter::OnLinkedResult(const sptr<AVBufferQueueProducer> &queue, std::shared_ptr<Meta> &meta) |
| 275 | { | 325 | { |
| 276 | MEDIA_LOG_I("AudioDataSourceFilter OnLinkedResult"); | 326 | MEDIA_LOG_I("AudioDataSourceFilter OnLinkedResult"); |
| 277 | outputBufferQueue_ = queue; | 327 | outputBufferQueue_ = queue; |
| 278 | } | 328 | } |
| 279 | - | 329 | + |
| 280 | Status AudioDataSourceFilter::UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) | 330 | Status AudioDataSourceFilter::UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) |
| 281 | { | 331 | { |
| 282 | MEDIA_LOG_I("AudioDataSourceFilter UpdateNext"); | 332 | MEDIA_LOG_I("AudioDataSourceFilter UpdateNext"); |
| 283 | return Status::OK; | 333 | return Status::OK; |
| 284 | } | 334 | } |
| 285 | - | 335 | + |
| 286 | Status AudioDataSourceFilter::UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) | 336 | Status AudioDataSourceFilter::UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) |
| 287 | { | 337 | { |
| 288 | MEDIA_LOG_I("AudioDataSourceFilter UnLinkNext"); | 338 | MEDIA_LOG_I("AudioDataSourceFilter UnLinkNext"); |
| 289 | return Status::OK; | 339 | return Status::OK; |
| 290 | } | 340 | } |
| 291 | - | 341 | + |
| 292 | Status AudioDataSourceFilter::OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta, | 342 | Status AudioDataSourceFilter::OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta, |
| 293 | const std::shared_ptr<FilterLinkCallback> &callback) | 343 | const std::shared_ptr<FilterLinkCallback> &callback) |
| 294 | { | 344 | { |
| 295 | MEDIA_LOG_I("AudioDataSourceFilter OnLinked"); | 345 | MEDIA_LOG_I("AudioDataSourceFilter OnLinked"); |
| 296 | return Status::OK; | 346 | return Status::OK; |
| 297 | } | 347 | } |
| 298 | - | 348 | + |
| 299 | Status AudioDataSourceFilter::OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta, | 349 | Status AudioDataSourceFilter::OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta, |
| 300 | const std::shared_ptr<FilterLinkCallback> &callback) | 350 | const std::shared_ptr<FilterLinkCallback> &callback) |
| 301 | { | 351 | { |
| 302 | MEDIA_LOG_I("AudioDataSourceFilter OnUpdated"); | 352 | MEDIA_LOG_I("AudioDataSourceFilter OnUpdated"); |
| 303 | return Status::OK; | 353 | return Status::OK; |
| 304 | } | 354 | } |
| 305 | - | 355 | + |
| 306 | Status AudioDataSourceFilter::OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback> &callback) | 356 | Status AudioDataSourceFilter::OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback> &callback) |
| 307 | { | 357 | { |
| 308 | MEDIA_LOG_I("AudioDataSourceFilter OnUnLinked"); | 358 | MEDIA_LOG_I("AudioDataSourceFilter OnUnLinked"); |
| 309 | return Status::OK; | 359 | return Status::OK; |
| 310 | } | 360 | } |
| 311 | - | 361 | + |
| 312 | void AudioDataSourceFilter::OnUnlinkedResult(const std::shared_ptr<Meta> &meta) | 362 | void AudioDataSourceFilter::OnUnlinkedResult(const std::shared_ptr<Meta> &meta) |
| 313 | { | 363 | { |
| 314 | MEDIA_LOG_I("AudioDataSourceFilter OnUnlinkedResult"); | 364 | MEDIA_LOG_I("AudioDataSourceFilter OnUnlinkedResult"); |
| 315 | (void) meta; | 365 | (void) meta; |
| 316 | } | 366 | } |
| 317 | - | 367 | + |
| 318 | void AudioDataSourceFilter::OnUpdatedResult(const std::shared_ptr<Meta> &meta) | 368 | void AudioDataSourceFilter::OnUpdatedResult(const std::shared_ptr<Meta> &meta) |
| 319 | { | 369 | { |
| 320 | MEDIA_LOG_I("AudioDataSourceFilter OnUpdatedResult"); | 370 | MEDIA_LOG_I("AudioDataSourceFilter OnUpdatedResult"); |
| 321 | (void) meta; | 371 | (void) meta; |
| 322 | } | 372 | } |
| 323 | - | ||
| 324 | -int32_t AudioDataSourceFilter::RelativeSleep(int64_t nanoTime) | ||
| 325 | -{ | ||
| 326 | - int32_t ret = -1; // -1 for bad result. | ||
| 327 | - if (nanoTime <= 0) { | ||
| 328 | - MEDIA_LOG_E("RelativeSleep nanoTime <= 0"); | ||
| 329 | - return ret; | ||
| 330 | - } | ||
| 331 | - struct timespec time; | ||
| 332 | - time.tv_sec = nanoTime / AUDIO_NS_PER_SECOND; | ||
| 333 | - time.tv_nsec = nanoTime - (time.tv_sec * AUDIO_NS_PER_SECOND); // Avoids % operation. | ||
| 334 | - clockid_t clockId = CLOCK_MONOTONIC; | ||
| 335 | - const int relativeFlag = 0; // flag of relative sleep. | ||
| 336 | - ret = clock_nanosleep(clockId, relativeFlag, &time, nullptr); | ||
| 337 | - if (ret != 0) { | ||
| 338 | - MEDIA_LOG_I("RelativeSleep may failed, ret is :%{public}d", ret); | ||
| 339 | - } | ||
| 340 | - return ret; | ||
| 341 | -} | ||
| 342 | } // namespace Pipeline | 373 | } // namespace Pipeline |
| 343 | } // namespace Media | 374 | } // namespace Media |
| 344 | } // namespace OHOS | 375 | } // namespace OHOS |
| @@ -13,6 +13,7 @@ | |||
| 13 | * limitations under the License. | 13 | * limitations under the License. |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | + | ||
| 16 | 17 | ||
| 17 | 18 | ||
| 18 | 19 | ||
| @@ -198,7 +199,8 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_001, Test | |||
| 198 | std::shared_ptr<MockAudioDataSource> mockAudioDataSource = std::make_shared<MockAudioDataSource>(); | 199 | std::shared_ptr<MockAudioDataSource> mockAudioDataSource = std::make_shared<MockAudioDataSource>(); |
| 199 | audioDataSourceFilter_->audioDataSource_ = mockAudioDataSource; | 200 | audioDataSourceFilter_->audioDataSource_ = mockAudioDataSource; |
| 200 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).Times(0); | 201 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).Times(0); |
| 201 | - audioDataSourceFilter_->ReadLoop(); | 202 | + audioDataSourceFilter_->dataReady_ = true; |
| 203 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 202 | } | 204 | } |
| 203 | 205 | ||
| 204 | /** | 206 | /** |
| @@ -216,7 +218,8 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_002, Test | |||
| 216 | 218 | ||
| 217 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 1)); | 219 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 1)); |
| 218 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)).Times(0); | 220 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)).Times(0); |
| 219 | - audioDataSourceFilter_->ReadLoop(); | 221 | + audioDataSourceFilter_->dataReady_ = true; |
| 222 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 220 | } | 223 | } |
| 221 | 224 | ||
| 222 | /** | 225 | /** |
| @@ -233,7 +236,8 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_003, Test | |||
| 233 | 236 | ||
| 234 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 0)); | 237 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 0)); |
| 235 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)).Times(0); | 238 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)).Times(0); |
| 236 | - audioDataSourceFilter_->ReadLoop(); | 239 | + audioDataSourceFilter_->dataReady_ = true; |
| 240 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 237 | } | 241 | } |
| 238 | 242 | ||
| 239 | /** | 243 | /** |
| @@ -252,8 +256,10 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_004, Test | |||
| 252 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 0)); | 256 | EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 0)); |
| 253 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)) | 257 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)) |
| 254 | .WillOnce(Return(Status::ERROR_UNKNOWN)); | 258 | .WillOnce(Return(Status::ERROR_UNKNOWN)); |
| 259 | + EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)).Times(1); | ||
| 255 | EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(0); | 260 | EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(0); |
| 256 | - audioDataSourceFilter_->ReadLoop(); | 261 | + audioDataSourceFilter_->dataReady_ = true; |
| 262 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 257 | } | 263 | } |
| 258 | 264 | ||
| 259 | /** | 265 | /** |
| @@ -275,7 +281,8 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_005, Test | |||
| 275 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)) | 281 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)) |
| 276 | .WillOnce(Return(OHOS::Media::AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG)); | 282 | .WillOnce(Return(OHOS::Media::AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG)); |
| 277 | EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(1); | 283 | EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(1); |
| 278 | - audioDataSourceFilter_->ReadLoop(); | 284 | + audioDataSourceFilter_->dataReady_ = true; |
| 285 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 279 | } | 286 | } |
| 280 | 287 | ||
| 281 | /** | 288 | /** |
| @@ -293,14 +300,17 @@ HWTEST_F(AudioDataSourceFilterUnitTest, AudioDataSourceFilter_ReadLoop_006, Test | |||
| 293 | std::shared_ptr<AVBuffer> mockEmptyOutputBuffer = std::make_shared<AVBuffer>(); | 300 | std::shared_ptr<AVBuffer> mockEmptyOutputBuffer = std::make_shared<AVBuffer>(); |
| 294 | mockEmptyOutputBuffer->memory_ = std::make_shared<AVMemory>(); | 301 | mockEmptyOutputBuffer->memory_ = std::make_shared<AVMemory>(); |
| 295 | 302 | ||
| 296 | - EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)).WillOnce(Return((int32_t) 0)); | 303 | + EXPECT_CALL(*mockAudioDataSource, GetSize(testing::_)) |
| 304 | + .WillOnce(Return((int32_t) 0)).WillRepeatedly(Return((int32_t) 0)); | ||
| 297 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)) | 305 | EXPECT_CALL(*mockAVBufferQueueProducer, RequestBuffer(testing::_, testing::_, testing::_)) |
| 298 | .WillOnce(DoAll(SetArgReferee<0>(mockEmptyOutputBuffer), Return(Status::OK))); | 306 | .WillOnce(DoAll(SetArgReferee<0>(mockEmptyOutputBuffer), Return(Status::OK))); |
| 299 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)) | 307 | EXPECT_CALL(*mockAudioDataSource, ReadAt(testing::_, testing::_)) |
| 300 | - .WillOnce(Return(OHOS::Media::AudioDataSourceReadAtActionState::OK)); | 308 | + .WillOnce(Return(OHOS::Media::AudioDataSourceReadAtActionState::OK)) |
| 309 | + .WillRepeatedly(Return(OHOS::Media::AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG)); | ||
| 301 | EXPECT_CALL(*mockAudioDataSource, SetVideoFirstFramePts(testing::_)).Times(0); | 310 | EXPECT_CALL(*mockAudioDataSource, SetVideoFirstFramePts(testing::_)).Times(0); |
| 302 | - EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(1); | 311 | + EXPECT_CALL(*mockAVBufferQueueProducer, PushBuffer(testing::_, testing::_)).Times(testing::AtLeast(1)); |
| 303 | - audioDataSourceFilter_->ReadLoop(); | 312 | + audioDataSourceFilter_->dataReady_ = true; |
| 313 | + audioDataSourceFilter_->ProcessLoop(); | ||
| 304 | } | 314 | } |
| 305 | 315 | ||
| 306 | /** | 316 | /** |
| @@ -84,6 +84,7 @@ public: | |||
| 84 | (std::shared_ptr<AVBuffer> buffer, uint32_t length), (override)); | 84 | (std::shared_ptr<AVBuffer> buffer, uint32_t length), (override)); |
| 85 | MOCK_METHOD(int32_t, GetSize, (int64_t &size), (override)); | 85 | MOCK_METHOD(int32_t, GetSize, (int64_t &size), (override)); |
| 86 | MOCK_METHOD(void, SetVideoFirstFramePts, (int64_t firstFramePts), (override)); | 86 | MOCK_METHOD(void, SetVideoFirstFramePts, (int64_t firstFramePts), (override)); |
| 87 | + MOCK_METHOD(void, SetListener, (std::shared_ptr<IAudioDataSourceListener> listener), (override)); | ||
| 87 | }; | 88 | }; |
| 88 | 89 | ||
| 89 | class TestEventReceiver : public Pipeline::EventReceiver { | 90 | class TestEventReceiver : public Pipeline::EventReceiver { |
| @@ -153,4 +154,4 @@ protected: | |||
| 153 | } // namespace Pipeline | 154 | } // namespace Pipeline |
| 154 | } // namespace Media | 155 | } // namespace Media |
| 155 | } // namespace OHOS | 156 | } // namespace OHOS |
| 156 | -#endif // HISTREAMER_AUDIO_DATA_SOURCE_FILTER_UNIT_TEST_H | 157 | +#endif // HISTREAMER_AUDIO_DATA_SOURCE_FILTER_UNIT_TEST_H |