]> git.sesse.net Git - casparcg/blob - core/frame/frame.cpp
* Fixed bug where a new audio stream was created in the audio mixer for each frame...
[casparcg] / core / frame / frame.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 "frame.h"
25
26 #include <common/except.h>
27 #include <common/array.h>
28 #include <common/future.h>
29 #include <common/timer.h>
30
31 #include <core/frame/frame_visitor.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/frame/geometry.h>
34 #include <core/frame/audio_channel_layout.h>
35
36 #include <cstdint>
37 #include <vector>
38
39 #include <boost/lexical_cast.hpp>
40 #include <boost/thread/future.hpp>
41
42 namespace caspar { namespace core {
43                 
44 struct mutable_frame::impl : boost::noncopyable
45 {                       
46         std::vector<array<std::uint8_t>>                        buffers_;
47         core::mutable_audio_buffer                                      audio_data_;
48         const core::pixel_format_desc                           desc_;
49         const core::audio_channel_layout                        channel_layout_;
50         const void*                                                                     tag_;
51         core::frame_geometry                                            geometry_                               = frame_geometry::get_default();
52         caspar::timer                                                           since_created_timer_;
53         
54         impl(
55                         std::vector<array<std::uint8_t>> buffers,
56                         mutable_audio_buffer audio_data,
57                         const void* tag,
58                         const core::pixel_format_desc& desc,
59                         const core::audio_channel_layout& channel_layout)
60                 : buffers_(std::move(buffers))
61                 , audio_data_(std::move(audio_data))
62                 , desc_(desc)
63                 , channel_layout_(channel_layout)
64                 , tag_(tag)
65         {
66                 for (auto& buffer : buffers_)
67                         if(!buffer.data())
68                                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
69         }
70 };
71         
72 mutable_frame::mutable_frame(
73                 std::vector<array<std::uint8_t>> image_buffers,
74                 mutable_audio_buffer audio_data,
75                 const void* tag,
76                 const core::pixel_format_desc& desc,
77                 const core::audio_channel_layout& channel_layout)
78         : impl_(new impl(std::move(image_buffers), std::move(audio_data), tag, desc, channel_layout)){}
79 mutable_frame::~mutable_frame(){}
80 mutable_frame::mutable_frame(mutable_frame&& other) : impl_(std::move(other.impl_)){}
81 mutable_frame& mutable_frame::operator=(mutable_frame&& other)
82 {
83         impl_ = std::move(other.impl_);
84         return *this;
85 }
86 void mutable_frame::swap(mutable_frame& other){impl_.swap(other.impl_);}
87 const core::pixel_format_desc& mutable_frame::pixel_format_desc() const{return impl_->desc_;}
88 const core::audio_channel_layout& mutable_frame::audio_channel_layout() const { return impl_->channel_layout_; }
89 const array<std::uint8_t>& mutable_frame::image_data(std::size_t index) const{return impl_->buffers_.at(index);}
90 const core::mutable_audio_buffer& mutable_frame::audio_data() const{return impl_->audio_data_;}
91 array<std::uint8_t>& mutable_frame::image_data(std::size_t index){return impl_->buffers_.at(index);}
92 core::mutable_audio_buffer& mutable_frame::audio_data(){return impl_->audio_data_;}
93 std::size_t mutable_frame::width() const{return impl_->desc_.planes.at(0).width;}
94 std::size_t mutable_frame::height() const{return impl_->desc_.planes.at(0).height;}                                             
95 const void* mutable_frame::stream_tag()const{return impl_->tag_;}                               
96 const frame_geometry& mutable_frame::geometry() const { return impl_->geometry_; }
97 void mutable_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
98 caspar::timer mutable_frame::since_created() const { return impl_->since_created_timer_; }
99
100 const const_frame& const_frame::empty()
101 {
102         static int dummy;
103         static const_frame empty(&dummy);
104         return empty;
105 }
106
107 struct const_frame::impl : boost::noncopyable
108 {                       
109         mutable std::vector<std::shared_future<array<const std::uint8_t>>>      future_buffers_;
110         mutable core::audio_buffer                                                                                      audio_data_;
111         const core::pixel_format_desc                                                                           desc_;
112         const core::audio_channel_layout                                                                        channel_layout_;
113         const void*                                                                                                                     tag_;
114         core::frame_geometry                                                                                            geometry_;
115         caspar::timer                                                                                                           since_created_timer_;
116         bool                                                                                                                            should_record_age_;
117         mutable tbb::atomic<int64_t>                                                                            recorded_age_;
118
119         impl(const void* tag)
120                 : audio_data_(0, 0, true, 0)
121                 , desc_(core::pixel_format::invalid)
122                 , channel_layout_(audio_channel_layout::invalid())
123                 , tag_(tag)     
124                 , geometry_(frame_geometry::get_default())
125                 , should_record_age_(true)
126         {
127                 recorded_age_ = 0;
128         }
129         
130         impl(
131                         std::shared_future<array<const std::uint8_t>> image,
132                         audio_buffer audio_data,
133                         const void* tag,
134                         const core::pixel_format_desc& desc,
135                         const core::audio_channel_layout& channel_layout)
136                 : audio_data_(std::move(audio_data))
137                 , desc_(desc)
138                 , channel_layout_(channel_layout)
139                 , tag_(tag)
140                 , geometry_(frame_geometry::get_default())
141                 , should_record_age_(false)
142         {
143                 if (desc.format != core::pixel_format::bgra)
144                         CASPAR_THROW_EXCEPTION(not_implemented());
145                 
146                 future_buffers_.push_back(std::move(image));
147         }
148
149         impl(mutable_frame&& other)
150                 : audio_data_(0, 0, true, 0) // Complex init done in body instead.
151                 , desc_(other.pixel_format_desc())
152                 , channel_layout_(other.audio_channel_layout())
153                 , tag_(other.stream_tag())
154                 , geometry_(other.geometry())
155                 , since_created_timer_(other.since_created())
156                 , should_record_age_(true)
157         {
158                 spl::shared_ptr<mutable_audio_buffer> shared_audio_data(new mutable_audio_buffer(std::move(other.audio_data())));
159                 // pointer returned by vector::data() should be the same after move, but just to be safe.
160                 audio_data_ = audio_buffer(shared_audio_data->data(), shared_audio_data->size(), true, std::move(shared_audio_data));
161
162                 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
163                 {
164                         future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
165                 }
166
167                 recorded_age_ = -1;
168         }
169
170         array<const std::uint8_t> image_data(int index) const
171         {
172                 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
173         }
174
175         std::size_t width() const
176         {
177                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
178         }
179
180         std::size_t height() const
181         {
182                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
183         }
184
185         std::size_t size() const
186         {
187                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
188         }
189
190         int64_t get_age_millis() const
191         {
192                 if (should_record_age_)
193                 {
194                         if (recorded_age_ == -1)
195                                 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
196
197                         return recorded_age_;
198                 }
199                 else
200                         return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
201         }
202 };
203         
204 const_frame::const_frame(const void* tag) : impl_(new impl(tag)){}
205 const_frame::const_frame(
206                 std::shared_future<array<const std::uint8_t>> image,
207                 audio_buffer audio_data,
208                 const void* tag,
209                 const core::pixel_format_desc& desc,
210                 const core::audio_channel_layout& channel_layout)
211         : impl_(new impl(std::move(image), std::move(audio_data), tag, desc, channel_layout)){}
212 const_frame::const_frame(mutable_frame&& other) : impl_(new impl(std::move(other))){}
213 const_frame::~const_frame(){}
214 const_frame::const_frame(const_frame&& other) : impl_(std::move(other.impl_)){}
215 const_frame& const_frame::operator=(const_frame&& other)
216 {
217         impl_ = std::move(other.impl_);
218         return *this;
219 }
220 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
221 const_frame& const_frame::operator=(const const_frame& other)
222 {
223         impl_ = other.impl_;
224         return *this;
225 }
226 bool const_frame::operator==(const const_frame& other){return impl_ == other.impl_;}
227 bool const_frame::operator!=(const const_frame& other){return !(*this == other);}
228 bool const_frame::operator<(const const_frame& other){return impl_ < other.impl_;}
229 bool const_frame::operator>(const const_frame& other){return impl_ > other.impl_;}
230 const core::pixel_format_desc& const_frame::pixel_format_desc()const{return impl_->desc_;}
231 const core::audio_channel_layout& const_frame::audio_channel_layout()const { return impl_->channel_layout_; }
232 array<const std::uint8_t> const_frame::image_data(int index)const{return impl_->image_data(index);}
233 const core::audio_buffer& const_frame::audio_data()const{return impl_->audio_data_;}
234 std::size_t const_frame::width()const{return impl_->width();}
235 std::size_t const_frame::height()const{return impl_->height();} 
236 std::size_t const_frame::size()const{return impl_->size();}                                             
237 const void* const_frame::stream_tag()const{return impl_->tag_;}                         
238 const frame_geometry& const_frame::geometry() const { return impl_->geometry_; }
239 void const_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
240 int64_t const_frame::get_age_millis() const { return impl_->get_age_millis(); }
241
242 }}