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 TYPE_FINDER_H
17 #define TYPE_FINDER_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include "osal/task/task.h"
24 #include "buffer/avbuffer.h"
25 #include "plugin/plugin_buffer.h"
26 #include "plugin/plugin_info.h"
27 
28 namespace OHOS {
29 namespace Media {
30 using namespace Plugins;
31 class TypeFinder : public std::enable_shared_from_this<TypeFinder>, public Plugins::DataSource {
32 public:
33     TypeFinder();
34 
35     ~TypeFinder() override;
36 
37     void Init(std::string uri, uint64_t mediaDataSize, std::function<Status(int32_t, uint64_t, size_t)> checkRange,
38         std::function<Status(int32_t, uint64_t, size_t, std::shared_ptr<Buffer>&)> peekRange, int32_t streamId);
39 
40     std::string FindMediaType();
41 
42     void FindMediaTypeAsync(std::function<void(std::string)> typeFound);
43 
44     Status ReadAt(int64_t offset, std::shared_ptr<Buffer>& buffer, size_t expectedLen) override;
45 
46     Status GetSize(uint64_t& size) override;
47 
48     Plugins::Seekable GetSeekable() override;
49 
50     int32_t GetStreamID() override;
51 
IsDash()52     bool IsDash() override { return false; }
53 private:
54     void DoTask();
55 
56     std::string SniffMediaType();
57 
58     std::string GuessMediaType() const;
59 
60     bool IsOffsetValid(int64_t offset) const;
61 
62     bool IsSniffNeeded(std::string uri);
63 
64     void SortPlugins(const std::string& uriSuffix);
65 
66     bool sniffNeeded_;
67     std::string uri_;
68     uint64_t mediaDataSize_;
69     std::string pluginName_;
70     std::vector<std::shared_ptr<Plugins::PluginInfo>> plugins_;
71     std::atomic<bool> pluginRegistryChanged_;
72     std::shared_ptr<Task> task_;
73     std::function<Status(int32_t, uint64_t, size_t)> checkRange_;
74     std::function<Status(int32_t, uint64_t, size_t, std::shared_ptr<Buffer>&)> peekRange_;
75     std::function<void(std::string)> typeFound_;
76     int32_t streamID_ = -1;
77 };
78 } // namespace Media
79 } // namespace OHOS
80 #endif // TYPE_FINDER_H
81