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>
34 #include <core/frame/audio_channel_layout.h>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/thread/future.hpp>
42 namespace caspar { namespace core {
44 struct mutable_frame::impl : boost::noncopyable
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_;
51 core::frame_geometry geometry_ = frame_geometry::get_default();
52 caspar::timer since_created_timer_;
55 std::vector<array<std::uint8_t>> buffers,
56 mutable_audio_buffer audio_data,
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))
63 , channel_layout_(channel_layout)
66 for (auto& buffer : buffers_)
68 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
72 mutable_frame::mutable_frame(
73 std::vector<array<std::uint8_t>> image_buffers,
74 mutable_audio_buffer audio_data,
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)
83 impl_ = std::move(other.impl_);
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_; }
100 const const_frame& const_frame::empty()
103 static const_frame empty(&dummy);
107 struct const_frame::impl : boost::noncopyable
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_;
114 core::frame_geometry geometry_;
115 caspar::timer since_created_timer_;
116 bool should_record_age_;
117 mutable tbb::atomic<int64_t> recorded_age_;
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())
124 , geometry_(frame_geometry::get_default())
125 , should_record_age_(true)
131 std::shared_future<array<const std::uint8_t>> image,
132 audio_buffer audio_data,
134 const core::pixel_format_desc& desc,
135 const core::audio_channel_layout& channel_layout)
136 : audio_data_(std::move(audio_data))
138 , channel_layout_(channel_layout)
140 , geometry_(frame_geometry::get_default())
141 , should_record_age_(false)
143 if (desc.format != core::pixel_format::bgra)
144 CASPAR_THROW_EXCEPTION(not_implemented());
146 future_buffers_.push_back(std::move(image));
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)
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));
162 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
164 future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
170 array<const std::uint8_t> image_data(int index) const
172 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
175 std::size_t width() const
177 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
180 std::size_t height() const
182 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
185 std::size_t size() const
187 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
190 int64_t get_age_millis() const
192 if (should_record_age_)
194 if (recorded_age_ == -1)
195 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
197 return recorded_age_;
200 return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
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,
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)
217 impl_ = std::move(other.impl_);
220 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
221 const_frame& const_frame::operator=(const const_frame& other)
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(); }