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
16#ifndef AVSOURCE_MOCK_H
17#define AVSOURCE_MOCK_H
18
19#include <string>
20#include <memory>
21#include "nocopyable.h"
22#include "avformat_mock.h"
23#include "av_common.h"
24#include "avcodec_common.h"
25#include "avsource.h"
26#include "native_avsource.h"
27#include "common/native_mfmagic.h"
28
29namespace OHOS {
30namespace MediaAVCodec {
31class AVSourceMock : public NoCopyable {
32public:
33    virtual ~AVSourceMock() = default;
34    virtual int32_t Destroy() =0;
35    virtual std::shared_ptr<FormatMock> GetSourceFormat() = 0;
36    virtual std::shared_ptr<FormatMock> GetTrackFormat(uint32_t trackIndex) = 0;
37    virtual std::shared_ptr<FormatMock> GetUserData() = 0;
38};
39
40class __attribute__((visibility("default"))) AVSourceMockFactory {
41public:
42    static std::shared_ptr<AVSourceMock> CreateSourceWithURI(char *uri);
43    static std::shared_ptr<AVSourceMock> CreateSourceWithFD(int32_t fd, int64_t offset, int64_t size);
44    static std::shared_ptr<AVSourceMock> CreateWithDataSource(
45        const std::shared_ptr<Media::IMediaDataSource> &dataSource);
46    static std::shared_ptr<AVSourceMock> CreateWithDataSource(OH_AVDataSource *dataSource);
47private:
48    AVSourceMockFactory() = delete;
49    ~AVSourceMockFactory() = delete;
50};
51
52class NativeAVDataSource : public OHOS::Media::IMediaDataSource {
53public:
54    explicit NativeAVDataSource(OH_AVDataSource *dataSource)
55        : dataSource_(dataSource)
56    {
57    }
58    virtual ~NativeAVDataSource() = default;
59
60    int32_t ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos = -1)
61    {
62        std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(
63            mem->GetBase(), mem->GetSize(), mem->GetSize()
64        );
65        OH_AVBuffer* avBuffer = new OH_AVBuffer(buffer);
66        return dataSource_->readAt(avBuffer, length, pos);
67    }
68
69    int32_t GetSize(int64_t &size)
70    {
71        size = dataSource_->size;
72        return 0;
73    }
74
75    int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
76    {
77        return ReadAt(mem, length, pos);
78    }
79
80    int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
81    {
82        return ReadAt(mem, length);
83    }
84
85private:
86    OH_AVDataSource* dataSource_;
87};
88
89}  // namespace MediaAVCodec
90}  // namespace OHOS
91#endif // AVSOURCE_MOCK_H