Overte C++ Documentation
MediaTypeLibrary.h
1 //
2 // MediaTypeLibrary.h
3 // libraries/shared/src/shared
4 //
5 // Created by Sabrina Shanman on 2018/11/28.
6 // Copyright 2018 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_MediaTypeLibrary_h
13 #define hifi_MediaTypeLibrary_h
14 
15 #include <vector>
16 #include <string>
17 #include <functional>
18 #include <mutex>
19 
20 #include "HifiTypes.h"
21 
22 // A short sequence of bytes, typically at the beginning of the file, which identifies the file format
23 class FileSignature {
24 public:
25  FileSignature(const std::string& bytes, int byteOffset) :
26  bytes(bytes),
27  byteOffset(byteOffset) {
28  }
29  FileSignature(const FileSignature& fileSignature) :
30  bytes(fileSignature.bytes),
31  byteOffset(fileSignature.byteOffset) {
32  }
33 
34  std::string bytes;
35  int byteOffset;
36 };
37 
38 // A named file extension with a list of known ways to positively identify the file type
39 class MediaType {
40 public:
41  MediaType(const std::string& name) :
42  name(name) {
43  }
44  MediaType() {};
45  MediaType(const MediaType& mediaType) :
46  name(mediaType.name),
47  extensions(mediaType.extensions),
48  webMediaTypes(mediaType.webMediaTypes),
49  fileSignatures(mediaType.fileSignatures) {
50  }
51  MediaType& operator=(const MediaType&) = default;
52 
53  static MediaType NONE;
54 
55  std::string name;
56  std::vector<std::string> extensions;
57  std::vector<std::string> webMediaTypes;
58  std::vector<FileSignature> fileSignatures;
59 };
60 
61 class MediaTypeLibrary {
62 public:
63  using ID = unsigned int;
64  static const ID INVALID_ID { 0 };
65 
66  ID registerMediaType(const MediaType& mediaType);
67  void unregisterMediaType(const ID& id);
68 
69  MediaType getMediaType(const ID& id) const;
70 
71  ID findMediaTypeForData(const hifi::ByteArray& data) const;
72  ID findMediaTypeForURL(const hifi::URL& url) const;
73  ID findMediaTypeForWebID(const std::string& webMediaType) const;
74 
75 protected:
76  ID nextID { 1 };
77 
78  class Entry {
79  public:
80  Entry(const ID& id, const MediaType& mediaType) :
81  id(id),
82  mediaType(mediaType) {
83  }
84  ID id;
85  MediaType mediaType;
86  };
87 
88  std::vector<Entry> _mediaTypes;
89 };
90 
91 #endif // hifi_MeidaTypeLibrary_h