]> git.sesse.net Git - casparcg/blob - core/producer/media_info/in_memory_media_info_repository.cpp
[streaming_consumer] Implemented support for separating audio channels into separate...
[casparcg] / core / producer / media_info / in_memory_media_info_repository.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "in_memory_media_info_repository.h"
25
26 #include <map>
27 #include <vector>
28
29 #include <boost/thread/mutex.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/filesystem.hpp>
32 #include <boost/algorithm/string/case_conv.hpp>
33
34 #include "media_info.h"
35 #include "media_info_repository.h"
36
37 namespace caspar { namespace core {
38
39 class in_memory_media_info_repository : public media_info_repository
40 {
41         boost::mutex mutex_;
42         std::map<std::wstring, boost::optional<media_info>> info_by_file_;
43         std::vector<media_info_extractor> extractors_;
44 public:
45         virtual void register_extractor(media_info_extractor extractor) override
46         {
47                 boost::lock_guard<boost::mutex> lock(mutex_);
48
49                 extractors_.push_back(extractor);
50         }
51
52         virtual boost::optional<media_info> get(const std::wstring& file) override
53         {
54                 boost::lock_guard<boost::mutex> lock(mutex_);
55
56                 auto iter = info_by_file_.find(file);
57
58                 if (iter == info_by_file_.end())
59                 {
60                         media_info info;
61                         auto extension = boost::to_upper_copy(boost::filesystem::path(file).extension().wstring());
62                         bool success = false;
63
64                         BOOST_FOREACH(auto& extractor, extractors_)
65                         {
66                                 success = extractor(file, extension, info);
67
68                                 if (success)
69                                         break;
70                         }
71
72                         boost::optional<media_info> result;
73
74                         if (success)
75                                 result = info;
76
77                         info_by_file_.insert(std::make_pair(file, result));
78
79                         return result;
80                 }
81
82                 return iter->second;
83         }
84
85         virtual void remove(const std::wstring& file) override
86         {
87                 boost::lock_guard<boost::mutex> lock(mutex_);
88
89                 info_by_file_.erase(file);
90         }
91 };
92
93 spl::shared_ptr<struct media_info_repository> create_in_memory_media_info_repository()
94 {
95         return spl::make_shared<in_memory_media_info_repository>();
96 }
97
98 }}