1 /*
2  * Copyright (c) 2021-2021 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 MEDIA_PIPELINE_FILE_SOURCE_PLUGIN_H
17 #define MEDIA_PIPELINE_FILE_SOURCE_PLUGIN_H
18 
19 #include <cstdio>
20 #include "plugin/common/plugin_types.h"
21 #include "plugin/interface/source_plugin.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugin {
26 namespace FileSource {
27 class FileSourceAllocator : public Allocator {
28 public:
29     FileSourceAllocator() = default;
30     ~FileSourceAllocator() override = default;
31 
32     void* Alloc(size_t size) override;
33     void Free(void* ptr) override; // NOLINT: void*
34 };
35 
36 class FileSourcePlugin : public SourcePlugin {
37 public:
38     explicit FileSourcePlugin(std::string name);
39     ~FileSourcePlugin() override;
40 
41     Status Init() override;
42     Status Deinit() override;
43     Status Prepare() override;
44     Status Reset() override;
45     Status GetParameter(Tag tag, ValueType& value) override;
46     Status SetParameter(Tag tag, const ValueType& value) override;
47     std::shared_ptr<Allocator> GetAllocator() override;
48     Status SetCallback(Callback* cb) override;
49     Status SetSource(std::shared_ptr<MediaSource> source) override;
50     Status Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen) override;
51     Status GetSize(uint64_t& size) override;
52     Seekable GetSeekable() override;
53     Status SeekTo(uint64_t offset) override;
54 
55 private:
56     std::string fileName_ {};
57     std::FILE* fp_;
58     uint64_t fileSize_;
59     Seekable seekable_;
60     uint64_t position_;
61     std::shared_ptr<FileSourceAllocator> mAllocator_ {nullptr};
62 
63     Status ParseFileName(const std::string& uri);
64     Status CheckFileStat();
65     Status OpenFile();
66     void CloseFile();
67 };
68 } // namespace FileSource
69 } // namespace Plugin
70 } // namespace Media
71 } // namespace OHOS
72 
73 #endif // MEDIA_PIPELINE_FILE_SOURCE_PLUGIN_H
74