]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/reroute_producer.cpp
* Merged layer_producer and channel_producer from 2.0 to the reroute module (replacin...
[casparcg] / modules / reroute / producer / reroute_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "reroute_producer.h"
25 #include "layer_producer.h"
26 #include "channel_producer.h"
27
28 #include <core/producer/frame_producer.h>
29 #include <core/video_channel.h>
30
31 #include <boost/algorithm/string.hpp>
32 #include <boost/range/algorithm/find_if.hpp>
33
34 namespace caspar { namespace reroute {
35                 
36 spl::shared_ptr<core::frame_producer> create_producer(
37                 const core::frame_producer_dependencies& dependencies,
38                 const std::vector<std::wstring>& params)
39 {
40         static const std::wstring PREFIX = L"route://";
41
42         if (params.empty() ||
43                 !boost::starts_with(params.at(0), PREFIX) ||
44                 boost::ends_with(params.at(0), L"_A") ||
45                 boost::ends_with(params.at(0), L"_ALPHA"))
46         {
47                 return core::frame_producer::empty();
48         }
49
50         auto& url = params.at(0);
51         auto channel_layer_spec = url.substr(PREFIX.length());
52         auto dash = channel_layer_spec.find(L"-");
53         bool has_layer_spec = dash != std::wstring::npos;
54         int channel_id;
55
56         if (has_layer_spec)
57                 channel_id = boost::lexical_cast<int>(channel_layer_spec.substr(0, dash));
58         else
59                 channel_id = boost::lexical_cast<int>(channel_layer_spec);
60
61         auto found_channel = boost::find_if(dependencies.channels, [=](spl::shared_ptr<core::video_channel> ch) { return ch->index() == channel_id; });
62
63         if (found_channel == dependencies.channels.end())
64                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(L"No channel with id " + channel_id));
65
66         if (has_layer_spec)
67         {
68                 auto layer = boost::lexical_cast<int>(channel_layer_spec.substr(dash + 1));
69
70                 return create_layer_producer(*found_channel, layer);
71         }
72         else
73         {
74                 return create_channel_producer(dependencies, *found_channel);
75         }
76 }
77
78 }}