/* * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AVSOURCE_MOCK_H #define AVSOURCE_MOCK_H #include #include #include "nocopyable.h" #include "avformat_mock.h" #include "av_common.h" #include "avcodec_common.h" #include "avsource.h" #include "native_avsource.h" #include "common/native_mfmagic.h" namespace OHOS { namespace MediaAVCodec { class AVSourceMock : public NoCopyable { public: virtual ~AVSourceMock() = default; virtual int32_t Destroy() =0; virtual std::shared_ptr GetSourceFormat() = 0; virtual std::shared_ptr GetTrackFormat(uint32_t trackIndex) = 0; virtual std::shared_ptr GetUserData() = 0; }; class __attribute__((visibility("default"))) AVSourceMockFactory { public: static std::shared_ptr CreateSourceWithURI(char *uri); static std::shared_ptr CreateSourceWithFD(int32_t fd, int64_t offset, int64_t size); static std::shared_ptr CreateWithDataSource( const std::shared_ptr &dataSource); static std::shared_ptr CreateWithDataSource(OH_AVDataSource *dataSource); private: AVSourceMockFactory() = delete; ~AVSourceMockFactory() = delete; }; class NativeAVDataSource : public OHOS::Media::IMediaDataSource { public: explicit NativeAVDataSource(OH_AVDataSource *dataSource) : isExt_(false), dataSource_(dataSource), dataSourceExt_(nullptr), userData_(nullptr) { } NativeAVDataSource(OH_AVDataSourceExt* dataSourceExt, void* userData) : isExt_(true), dataSource_(nullptr), dataSourceExt_(dataSourceExt), userData_(userData) { } virtual ~NativeAVDataSource() = default; int32_t ReadAt(const std::shared_ptr &mem, uint32_t length, int64_t pos = -1) { std::shared_ptr buffer = AVBuffer::CreateAVBuffer( mem->GetBase(), mem->GetSize(), mem->GetSize() ); OH_AVBuffer avBuffer(buffer); if (isExt_) { return dataSourceExt_->readAt(&avBuffer, length, pos, userData_); } else { return dataSource_->readAt(&avBuffer, length, pos); } } int32_t GetSize(int64_t &size) { if (isExt_) { size = dataSourceExt_->size; } else { size = dataSource_->size; } return 0; } int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr &mem) { return ReadAt(mem, length, pos); } int32_t ReadAt(uint32_t length, const std::shared_ptr &mem) { return ReadAt(mem, length); } private: bool isExt_ = false; OH_AVDataSource* dataSource_; OH_AVDataSourceExt* dataSourceExt_ = nullptr; void* userData_ = nullptr; }; } // namespace MediaAVCodec } // namespace OHOS #endif // AVSOURCE_MOCK_H