2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
26 #include <common/except.h>
27 #include <common/array.h>
28 #include <common/future.h>
29 #include <common/timer.h>
31 #include <core/frame/frame_visitor.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/frame/geometry.h>
38 #include <boost/lexical_cast.hpp>
39 #include <boost/thread/future.hpp>
41 namespace caspar { namespace core {
43 struct mutable_frame::impl : boost::noncopyable
45 std::vector<array<std::uint8_t>> buffers_;
46 core::audio_buffer audio_data_;
47 const core::pixel_format_desc desc_;
49 core::frame_geometry geometry_ = frame_geometry::get_default();
50 caspar::timer since_created_timer_;
52 impl(std::vector<array<std::uint8_t>> buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc)
53 : buffers_(std::move(buffers))
54 , audio_data_(std::move(audio_buffer))
58 for (auto& buffer : buffers_)
60 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
64 mutable_frame::mutable_frame(std::vector<array<std::uint8_t>> image_buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc)
65 : impl_(new impl(std::move(image_buffers), std::move(audio_buffer), tag, desc)){}
66 mutable_frame::~mutable_frame(){}
67 mutable_frame::mutable_frame(mutable_frame&& other) : impl_(std::move(other.impl_)){}
68 mutable_frame& mutable_frame::operator=(mutable_frame&& other)
70 impl_ = std::move(other.impl_);
73 void mutable_frame::swap(mutable_frame& other){impl_.swap(other.impl_);}
74 const core::pixel_format_desc& mutable_frame::pixel_format_desc() const{return impl_->desc_;}
75 const array<std::uint8_t>& mutable_frame::image_data(std::size_t index) const{return impl_->buffers_.at(index);}
76 const core::audio_buffer& mutable_frame::audio_data() const{return impl_->audio_data_;}
77 array<std::uint8_t>& mutable_frame::image_data(std::size_t index){return impl_->buffers_.at(index);}
78 core::audio_buffer& mutable_frame::audio_data(){return impl_->audio_data_;}
79 std::size_t mutable_frame::width() const{return impl_->desc_.planes.at(0).width;}
80 std::size_t mutable_frame::height() const{return impl_->desc_.planes.at(0).height;}
81 const void* mutable_frame::stream_tag()const{return impl_->tag_;}
82 const void* mutable_frame::data_tag()const{return impl_.get();}
83 const frame_geometry& mutable_frame::geometry() const { return impl_->geometry_; }
84 void mutable_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
85 caspar::timer mutable_frame::since_created() const { return impl_->since_created_timer_; }
87 const const_frame& const_frame::empty()
90 static const_frame empty(&dummy);
94 struct const_frame::impl : boost::noncopyable
96 mutable std::vector<std::shared_future<array<const std::uint8_t>>> future_buffers_;
97 core::audio_buffer audio_data_;
98 const core::pixel_format_desc desc_;
100 core::frame_geometry geometry_;
101 caspar::timer since_created_timer_;
102 bool should_record_age_;
103 mutable tbb::atomic<int64_t> recorded_age_;
105 impl(const void* tag)
106 : desc_(core::pixel_format::invalid)
108 , geometry_(frame_geometry::get_default())
109 , should_record_age_(true)
114 impl(std::shared_future<array<const std::uint8_t>> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc)
115 : audio_data_(std::move(audio_buffer))
118 , geometry_(frame_geometry::get_default())
119 , should_record_age_(false)
121 if (desc.format != core::pixel_format::bgra)
122 CASPAR_THROW_EXCEPTION(not_implemented());
124 future_buffers_.push_back(std::move(image));
127 impl(mutable_frame&& other)
128 : audio_data_(other.audio_data())
129 , desc_(other.pixel_format_desc())
130 , tag_(other.stream_tag())
131 , geometry_(other.geometry())
132 , since_created_timer_(other.since_created())
133 , should_record_age_(true)
135 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
137 future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
143 array<const std::uint8_t> image_data(int index) const
145 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
148 std::size_t width() const
150 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
153 std::size_t height() const
155 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
158 std::size_t size() const
160 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
163 int64_t get_age_millis() const
165 if (should_record_age_)
167 if (recorded_age_ == -1)
168 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
170 return recorded_age_;
173 return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
177 const_frame::const_frame(const void* tag) : impl_(new impl(tag)){}
178 const_frame::const_frame(std::shared_future<array<const std::uint8_t>> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc)
179 : impl_(new impl(std::move(image), std::move(audio_buffer), tag, desc)){}
180 const_frame::const_frame(mutable_frame&& other) : impl_(new impl(std::move(other))){}
181 const_frame::~const_frame(){}
182 const_frame::const_frame(const_frame&& other) : impl_(std::move(other.impl_)){}
183 const_frame& const_frame::operator=(const_frame&& other)
185 impl_ = std::move(other.impl_);
188 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
189 const_frame& const_frame::operator=(const const_frame& other)
194 bool const_frame::operator==(const const_frame& other){return impl_ == other.impl_;}
195 bool const_frame::operator!=(const const_frame& other){return !(*this == other);}
196 bool const_frame::operator<(const const_frame& other){return impl_ < other.impl_;}
197 bool const_frame::operator>(const const_frame& other){return impl_ > other.impl_;}
198 const core::pixel_format_desc& const_frame::pixel_format_desc()const{return impl_->desc_;}
199 array<const std::uint8_t> const_frame::image_data(int index)const{return impl_->image_data(index);}
200 const core::audio_buffer& const_frame::audio_data()const{return impl_->audio_data_;}
201 std::size_t const_frame::width()const{return impl_->width();}
202 std::size_t const_frame::height()const{return impl_->height();}
203 std::size_t const_frame::size()const{return impl_->size();}
204 const void* const_frame::stream_tag()const{return impl_->tag_;}
205 const void* const_frame::data_tag()const{return impl_.get();}
206 const frame_geometry& const_frame::geometry() const { return impl_->geometry_; }
207 void const_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
208 int64_t const_frame::get_age_millis() const { return impl_->get_age_millis(); }