• 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 
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 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 class AVSourceMock : public NoCopyable {
32 public:
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 
40 class __attribute__((visibility("default"))) AVSourceMockFactory {
41 public:
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);
47 private:
48     AVSourceMockFactory() = delete;
49     ~AVSourceMockFactory() = delete;
50 };
51 
52 class NativeAVDataSource : public OHOS::Media::IMediaDataSource {
53 public:
NativeAVDataSource(OH_AVDataSource * dataSource)54     explicit NativeAVDataSource(OH_AVDataSource *dataSource)
55         : isExt_(false), dataSource_(dataSource), dataSourceExt_(nullptr), userData_(nullptr)
56     {
57     }
58 
NativeAVDataSource(OH_AVDataSourceExt * dataSourceExt,void * userData)59     NativeAVDataSource(OH_AVDataSourceExt* dataSourceExt, void* userData)
60         : isExt_(true), dataSource_(nullptr), dataSourceExt_(dataSourceExt), userData_(userData)
61     {
62     }
63 
64     virtual ~NativeAVDataSource() = default;
65 
66     int32_t ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos = -1)
67     {
68         std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(
69             mem->GetBase(), mem->GetSize(), mem->GetSize()
70         );
71         OH_AVBuffer avBuffer(buffer);
72 
73         if (isExt_) {
74             return dataSourceExt_->readAt(&avBuffer, length, pos, userData_);
75         } else {
76             return dataSource_->readAt(&avBuffer, length, pos);
77         }
78     }
79 
GetSize(int64_t & size)80     int32_t GetSize(int64_t &size)
81     {
82         if (isExt_) {
83             size = dataSourceExt_->size;
84         } else {
85             size = dataSource_->size;
86         }
87         return 0;
88     }
89 
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)90     int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
91     {
92         return ReadAt(mem, length, pos);
93     }
94 
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)95     int32_t ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
96     {
97         return ReadAt(mem, length);
98     }
99 
100 private:
101     bool isExt_ = false;
102     OH_AVDataSource* dataSource_;
103     OH_AVDataSourceExt* dataSourceExt_ = nullptr;
104     void* userData_ = nullptr;
105 };
106 
107 }  // namespace MediaAVCodec
108 }  // namespace OHOS
109 #endif // AVSOURCE_MOCK_H