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