]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.0. Implemented frame-accurate auto play functionality which also takes into account...
[casparcg] / core / producer / layer.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 \r
21 #include "../stdafx.h"\r
22 \r
23 #include "layer.h"\r
24 \r
25 #include "frame_producer.h"\r
26 \r
27 #include "frame/basic_frame.h"\r
28 \r
29 \r
30 namespace caspar { namespace core {\r
31         \r
32 struct layer::implementation\r
33 {                               \r
34         safe_ptr<frame_producer>        foreground_;\r
35         safe_ptr<frame_producer>        background_;\r
36         bool                                            is_paused_;\r
37         int                                                     auto_play_delta_;\r
38         int64_t                                         frame_number_;\r
39 public:\r
40         implementation() \r
41                 : foreground_(frame_producer::empty())\r
42                 , background_(frame_producer::empty())\r
43                 , is_paused_(false)\r
44                 , auto_play_delta_(-1){}\r
45         \r
46         void pause(){is_paused_ = true;}\r
47         void resume(){is_paused_ = false;}\r
48 \r
49         void load(const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
50         {               \r
51                 auto_play_delta_ = auto_play_delta;\r
52 \r
53                 background_ = producer;\r
54 \r
55                 if(preview) // Play the first frame and pause.\r
56                 {                       \r
57                         play();\r
58                         receive();\r
59                         pause();\r
60                 }\r
61         }\r
62         \r
63         void play()\r
64         {                       \r
65                 if(background_ != frame_producer::empty())\r
66                 {\r
67                         background_->set_leading_producer(foreground_);\r
68                         foreground_ = background_;\r
69                         frame_number_ = 0;\r
70                         background_ = frame_producer::empty();\r
71                 }\r
72                 resume();\r
73         }\r
74         \r
75         void stop()\r
76         {\r
77                 foreground_ = frame_producer::empty();\r
78         }\r
79                 \r
80         safe_ptr<basic_frame> receive()\r
81         {               \r
82                 if(is_paused_)\r
83                         return foreground_->last_frame();\r
84                 \r
85                 auto frame = receive_and_follow(foreground_);\r
86                 if(frame == core::basic_frame::late())\r
87                         return foreground_->last_frame();\r
88                         \r
89                 ++frame_number_;\r
90 \r
91                 if(auto_play_delta_ >= 0)\r
92                 {\r
93                         const auto frames_left = foreground_->nb_frames() - frame_number_ - auto_play_delta_;\r
94 \r
95                         if(frames_left <= 0 || frame == core::basic_frame::eof())\r
96                         {\r
97                                 CASPAR_VERIFY(auto_play_delta_ != 0 || frame == core::basic_frame::eof())\r
98 \r
99                                 CASPAR_LOG(info) << L"Automatically playing next clip with " << auto_play_delta_ << " frames offset.";\r
100                                 \r
101                                 auto_play_delta_ = -1;\r
102                                 play();\r
103                                 frame = receive();\r
104                         }\r
105 \r
106                 }\r
107                                 \r
108                 return frame;\r
109         }\r
110 };\r
111 \r
112 layer::layer() : impl_(new implementation()){}\r
113 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
114 layer& layer::operator=(layer&& other)\r
115 {\r
116         impl_ = std::move(other.impl_);\r
117         return *this;\r
118 }\r
119 layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){}\r
120 layer& layer::operator=(const layer& other)\r
121 {\r
122         layer temp(other);\r
123         temp.swap(*this);\r
124         return *this;\r
125 }\r
126 void layer::swap(layer& other)\r
127 {       \r
128         impl_.swap(other.impl_);\r
129 }\r
130 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool preview, int auto_play_delta){return impl_->load(frame_producer, preview, auto_play_delta);}      \r
131 void layer::play(){impl_->play();}\r
132 void layer::pause(){impl_->pause();}\r
133 void layer::stop(){impl_->stop();}\r
134 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
135 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
136 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
137 }}