]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/flv.cpp
2.1: Refactored UTF string handling.
[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/exception/exceptions.h>\r
27 \r
28 #include <boost/filesystem.hpp>\r
29 \r
30 #include <iostream>\r
31 \r
32 #include <unordered_map>\r
33 \r
34 namespace caspar { namespace ffmpeg {\r
35         \r
36 std::map<std::string, std::string> read_flv_meta_info(const std::string& filename)\r
37 {\r
38         std::map<std::string, std::string>  values;\r
39 \r
40         if(boost::filesystem2::path(filename).extension() != ".flv")\r
41                 return values;\r
42         \r
43         try\r
44         {\r
45                 if(!boost::filesystem2::exists(filename))\r
46                         BOOST_THROW_EXCEPTION(caspar_exception());\r
47         \r
48                 std::fstream fileStream = std::fstream(filename, std::fstream::in);\r
49                 \r
50                 std::vector<char> bytes2(256);\r
51                 fileStream.read(bytes2.data(), bytes2.size());\r
52 \r
53                 auto ptr = bytes2.data();\r
54                 \r
55                 ptr += 27;\r
56                                                 \r
57                 if(std::string(ptr, ptr+10) == "onMetaData")\r
58                 {\r
59                         ptr += 16;\r
60 \r
61                         for(int n = 0; n < 16; ++n)\r
62                         {\r
63                                 char name_size = *ptr++;\r
64 \r
65                                 if(name_size == 0)\r
66                                         break;\r
67 \r
68                                 auto name = std::string(ptr, ptr + name_size);\r
69                                 ptr += name_size;\r
70 \r
71                                 char data_type = *ptr++;\r
72                                 switch(data_type)\r
73                                 {\r
74                                 case 0: // double\r
75                                         {\r
76                                                 static_assert(sizeof(double) == 8, "");\r
77                                                 std::reverse(ptr, ptr+8);\r
78                                                 values[name] = boost::lexical_cast<std::string>(*(double*)(ptr));\r
79                                                 ptr += 9;\r
80 \r
81                                                 break;\r
82                                         }\r
83                                 case 1: // bool\r
84                                         {\r
85                                                 values[name] = boost::lexical_cast<std::string>(*ptr != 0);\r
86                                                 ptr += 2;\r
87 \r
88                                                 break;\r
89                                         }\r
90                                 }\r
91                         }\r
92                 }\r
93         }\r
94         catch(...)\r
95         {\r
96                 CASPAR_LOG_CURRENT_EXCEPTION();\r
97         }\r
98 \r
99     return values;\r
100 }\r
101 \r
102 }}