]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/flv.cpp
15a6fa3525da9083f8b0b7f7b9897739b79da0a2
[casparcg] / modules / ffmpeg / producer / util / flv.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "flv.h"\r
25 \r
26 #include <common/except.h>\r
27 #include <common/log.h>\r
28 \r
29 #include <boost/filesystem.hpp>\r
30 \r
31 #include <iostream>\r
32 \r
33 #include <unordered_map>\r
34 \r
35 namespace caspar { namespace ffmpeg {\r
36         \r
37 std::map<std::string, std::string> read_flv_meta_info(const std::string& filename)\r
38 {\r
39         std::map<std::string, std::string>  values;\r
40 \r
41         if(boost::filesystem::path(filename).extension().string() != ".flv")\r
42                 return values;\r
43         \r
44         try\r
45         {\r
46                 if(!boost::filesystem::exists(filename))\r
47                         CASPAR_THROW_EXCEPTION(caspar_exception());\r
48         \r
49                 std::fstream fileStream = std::fstream(filename, std::fstream::in);\r
50                 \r
51                 std::vector<char> bytes2(256);\r
52                 fileStream.read(bytes2.data(), bytes2.size());\r
53 \r
54                 auto ptr = bytes2.data();\r
55                 \r
56                 ptr += 27;\r
57                                                 \r
58                 if(std::string(ptr, ptr+10) == "onMetaData")\r
59                 {\r
60                         ptr += 16;\r
61 \r
62                         for(int n = 0; n < 16; ++n)\r
63                         {\r
64                                 char name_size = *ptr++;\r
65 \r
66                                 if(name_size == 0)\r
67                                         break;\r
68 \r
69                                 auto name = std::string(ptr, ptr + name_size);\r
70                                 ptr += name_size;\r
71 \r
72                                 char data_type = *ptr++;\r
73                                 switch(data_type)\r
74                                 {\r
75                                 case 0: // double\r
76                                         {\r
77                                                 static_assert(sizeof(double) == 8, "");\r
78                                                 std::reverse(ptr, ptr+8);\r
79                                                 values[name] = boost::lexical_cast<std::string>(*(double*)(ptr));\r
80                                                 ptr += 9;\r
81 \r
82                                                 break;\r
83                                         }\r
84                                 case 1: // bool\r
85                                         {\r
86                                                 values[name] = boost::lexical_cast<std::string>(*ptr != 0);\r
87                                                 ptr += 2;\r
88 \r
89                                                 break;\r
90                                         }\r
91                                 }\r
92                         }\r
93                 }\r
94         }\r
95         catch(...)\r
96         {\r
97                 CASPAR_LOG_CURRENT_EXCEPTION();\r
98         }\r
99 \r
100     return values;\r
101 }\r
102 \r
103 }}