Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
RenderCapture.h
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "AddonClass.h"
12#include "Exception.h"
13#include "ServiceBroker.h"
14#include "application/ApplicationComponents.h"
15#include "application/ApplicationPlayer.h"
16#include "commons/Buffer.h"
17#include "rendering/capture/CaptureConvert.h"
18#include "rendering/capture/CaptureHandle.h"
19#include "rendering/capture/CaptureService.h"
20
21#include <chrono>
22#include <cstdint>
23#include <memory>
24
25namespace XBMCAddon
26{
27 namespace xbmc
28 {
29 XBMCCOMMONS_STANDARD_EXCEPTION(RenderCaptureException);
30
31 //
42 //
43 class RenderCapture : public AddonClass
44 {
46 std::shared_ptr<KODI::RENDERING::CAPTURE::CCaptureService> m_service;
47 std::unique_ptr<KODI::RENDERING::CAPTURE::CCaptureHandle> m_handle;
48 unsigned int m_width{0};
49 unsigned int m_height{0};
50 std::unique_ptr<uint8_t[]> m_buffer;
51
52 public:
53 RenderCapture() = default;
54 ~RenderCapture() override = default;
55
56#ifdef DOXYGEN_SHOULD_USE_THIS
68#else
69 inline int getWidth() { return m_width; }
70#endif
71
72#ifdef DOXYGEN_SHOULD_USE_THIS
83#else
84 inline int getHeight() { return m_height; }
85#endif
86
87#ifdef DOXYGEN_SHOULD_USE_THIS
97#else
98 inline float getAspectRatio()
99 {
100 const auto& components = CServiceBroker::GetAppComponents();
101 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
102 return appPlayer->GetRenderAspectRatio();
103 }
104#endif
105
106#ifdef DOXYGEN_SHOULD_USE_THIS
119#else
120 inline const char* getImageFormat()
121#endif
122 {
123 return "BGRA";
124 }
125
126#ifdef DOXYGEN_SHOULD_USE_THIS
143#else
144 inline XbmcCommons::Buffer getImage(unsigned int msecs = 0)
145#endif
146 {
147 if (!GetPixels(msecs))
148 return XbmcCommons::Buffer(0);
149
150 size_t size = m_width * m_height * 4;
151 return XbmcCommons::Buffer(m_buffer.get(), size);
152 }
153
154#ifdef DOXYGEN_SHOULD_USE_THIS
168#else
169 inline void capture(int width, int height)
170#endif
171 {
172 // cancel any previous request before its buffer goes away
173 m_handle.reset();
174
175 m_width = width;
176 m_height = height;
177 m_buffer = std::make_unique<uint8_t[]>(static_cast<size_t>(m_width) * m_height * 4);
178
179 m_service = CServiceBroker::GetCaptureService();
180 if (!m_service)
181 return;
182
183 KODI::RENDERING::CAPTURE::CaptureSpec spec;
184 spec.content = KODI::RENDERING::CAPTURE::CaptureContent::VIDEO;
185 spec.cadence = KODI::RENDERING::CAPTURE::CaptureCadence::CONTINUOUS;
186 spec.width = m_width;
187 spec.height = m_height;
188 m_handle = m_service->Submit(spec);
189 }
190
191// hide these from swig
192#ifndef SWIG
193 inline bool GetPixels(unsigned int msec)
194 {
195 if (!m_handle)
196 return false;
197 if (!m_handle->WaitNext(std::chrono::milliseconds(msec ? msec : 1000)))
198 return false;
199 const KODI::RENDERING::CAPTURE::CaptureResult result = m_handle->CopyResult();
200 // legacy contract: raw output-coded 8-bit BGRA, never tonemapped; an
201 // HDR session delivers PQ/HLG-coded bytes exactly as glReadPixels did
202 return KODI::RENDERING::CAPTURE::CaptureCopyBGRA8(result, m_width, m_height,
203 m_buffer.get());
204 }
205#endif
206
207 };
209 }
210}
Definition RenderCapture.h:44
getImage(...)
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...
Definition RenderCapture.h:142
getAspectRatio()
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...
getHeight()
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...
getImageFormat()
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...
Definition RenderCapture.h:118
capture(...)
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...
Definition RenderCapture.h:167
getWidth()
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...