]> git.sesse.net Git - casparcg/blob - core/producer/playlist/playlist_producer.cpp
2.0.2: - stage: PARAM command is now synchronized with rendering thread.
[casparcg] / core / producer / playlist / playlist_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */ \r
20 #include "../../stdafx.h"\r
21 \r
22 #include "playlist_producer.h"\r
23 \r
24 #include <core/producer/frame_producer.h>\r
25 #include <core/producer/frame/basic_frame.h>\r
26 \r
27 #include <boost/regex.hpp>\r
28 \r
29 #include <deque>\r
30 \r
31 namespace caspar { namespace core {     \r
32 \r
33 struct playlist_producer : public frame_producer\r
34 {                               \r
35         safe_ptr<frame_factory>                         factory_;\r
36         safe_ptr<basic_frame>                           last_frame_;\r
37         safe_ptr<frame_producer>                        current_;\r
38         bool                                                            loop_;\r
39 \r
40         std::deque<safe_ptr<frame_producer>> producers_;\r
41 \r
42         playlist_producer(const safe_ptr<frame_factory>& factory, bool loop) \r
43                 : factory_(factory)\r
44                 , last_frame_(basic_frame::empty())\r
45                 , current_(frame_producer::empty())\r
46                 , loop_(loop)\r
47         {\r
48         }\r
49         \r
50         virtual safe_ptr<basic_frame> receive(int hints)\r
51         {\r
52                 if(current_ == frame_producer::empty() && !producers_.empty())\r
53                 {\r
54                         current_ = producers_.front();\r
55                         producers_.pop_front();\r
56                         if(loop_)\r
57                                 producers_.push_back(current_);\r
58                 }\r
59 \r
60                 auto frame = current_->receive(hints);\r
61                 if(frame == basic_frame::eof())\r
62                 {\r
63                         current_ = frame_producer::empty();\r
64                         return receive(hints);\r
65                 }\r
66 \r
67                 return last_frame_ = frame;\r
68         }\r
69 \r
70         virtual safe_ptr<core::basic_frame> last_frame() const\r
71         {\r
72                 return disable_audio(last_frame_);\r
73         }\r
74 \r
75         virtual std::wstring print() const\r
76         {\r
77                 return L"playlist[]";\r
78         }       \r
79 \r
80         virtual int64_t nb_frames() const \r
81         {\r
82                 return std::numeric_limits<int>::max();\r
83         }\r
84 \r
85         virtual std::wstring param(const std::wstring& param)\r
86         {               \r
87                 static const boost::wregex push_front_exp       (L"PUSH_FRONT (?<PARAM>.+)");           \r
88                 static const boost::wregex push_back_exp        (L"PUSH_BACK (?<PARAM>.+)");\r
89                 static const boost::wregex pop_front_exp        (L"POP_FRONT");         \r
90                 static const boost::wregex pop_back_exp         (L"POP_BACK");                  \r
91                 static const boost::wregex insert_exp           (L"INSERT (?<POS>\\d+) (?<PARAM>.+)");  \r
92                 static const boost::wregex remove_exp           (L"REMOVE (?<POS>\\d+) (?<PARAM>.+)");  \r
93                 static const boost::wregex list_exp                     (L"LIST");                      \r
94                 static const boost::wregex loop_exp                     (L"LOOP\\s*(?<VALUE>\\d?)");\r
95                 \r
96                 boost::wsmatch what;\r
97 \r
98                 if(boost::regex_match(param, what, push_front_exp))\r
99                         return push_front(what["PARAM"].str()); \r
100                 else if(boost::regex_match(param, what, push_back_exp))\r
101                         return push_back(what["PARAM"].str()); \r
102                 if(boost::regex_match(param, what, pop_front_exp))\r
103                         return pop_front(); \r
104                 else if(boost::regex_match(param, what, pop_back_exp))\r
105                         return pop_back(); \r
106                 else if(boost::regex_match(param, what, insert_exp))\r
107                         return insert(boost::lexical_cast<size_t>(what["POS"].str()), what["PARAM"].str());\r
108                 else if(boost::regex_match(param, what, remove_exp))\r
109                         return erase(boost::lexical_cast<size_t>(what["POS"].str()));\r
110                 else if(boost::regex_match(param, what, list_exp))\r
111                         return list();\r
112                 else if(boost::regex_match(param, what, loop_exp))\r
113                 {\r
114                         if(!what["VALUE"].str().empty())\r
115                                 loop_ = boost::lexical_cast<bool>(what["VALUE"].str());\r
116                         return boost::lexical_cast<std::wstring>(loop_);\r
117                 }\r
118 \r
119                 BOOST_THROW_EXCEPTION(invalid_argument());\r
120         }\r
121         \r
122         std::wstring push_front(const std::wstring& str)\r
123         {\r
124                 producers_.push_front(create_producer(factory_, str)); \r
125                 return L"";\r
126         }\r
127 \r
128         std::wstring  push_back(const std::wstring& str)\r
129         {\r
130                 producers_.push_back(create_producer(factory_, str)); \r
131                 return L"";\r
132         }\r
133 \r
134         std::wstring pop_front()\r
135         {\r
136                 producers_.pop_front();\r
137                 return L"";\r
138         }\r
139 \r
140         std::wstring pop_back()\r
141         {\r
142                 producers_.pop_back();\r
143                 return L"";\r
144         }\r
145 \r
146 \r
147         std::wstring  insert(size_t pos, const std::wstring& str)\r
148         {\r
149                 if(pos >= producers_.size())\r
150                         BOOST_THROW_EXCEPTION(out_of_range());\r
151                 producers_.insert(std::begin(producers_) + pos, create_producer(factory_, str));\r
152                 return L"";\r
153         }\r
154 \r
155         std::wstring  erase(size_t pos)\r
156         {\r
157                 if(pos >= producers_.size())\r
158                         BOOST_THROW_EXCEPTION(out_of_range());\r
159                 producers_.erase(std::begin(producers_) + pos);\r
160                 return L"";\r
161         }\r
162 \r
163         std::wstring list() const\r
164         {\r
165                 std::wstring result = L"<array>";\r
166                 BOOST_FOREACH(auto& producer, producers_)               \r
167                         result += L"<string>" + producer->print() + L"</string>\n";\r
168                 return result + L"</array>";\r
169         }\r
170 };\r
171 \r
172 safe_ptr<frame_producer> create_playlist_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
173 {\r
174         if(boost::range::find(params, L"PLAYLIST") == params.end())\r
175                 return core::frame_producer::empty();\r
176 \r
177         bool loop = boost::range::find(params, L"LOOP") != params.end();\r
178 \r
179         return make_safe<playlist_producer>(frame_factory, loop);\r
180 }\r
181 \r
182 }}\r
183 \r