2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
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"
24 XBMCCOMMONS_STANDARD_EXCEPTION(RenderCaptureException
);
27 /// \defgroup python_xbmc_RenderCapture RenderCapture
28 /// \ingroup python_xbmc
30 /// @brief **Kodi's render capture.**
32 /// \python_class{ RenderCapture() }
35 ///--------------------------------------------------------------------------
38 class RenderCapture
: public AddonClass
40 unsigned int m_captureId
;
42 unsigned int m_height
;
46 inline RenderCapture()
48 m_captureId
= UINT_MAX
;
53 inline ~RenderCapture() override
55 auto& components
= CServiceBroker::GetAppComponents();
56 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
57 appPlayer
->RenderCaptureRelease(m_captureId
);
61 #ifdef DOXYGEN_SHOULD_USE_THIS
63 /// \ingroup python_xbmc_RenderCapture
64 /// @brief \python_func{ getWidth() }
67 /// To get width of captured image as set during RenderCapture.capture().
68 /// Returns 0 prior to calling capture.
70 /// @return Width or 0 prior to calling capture
74 inline int getWidth() { return m_width
; }
77 #ifdef DOXYGEN_SHOULD_USE_THIS
79 /// \ingroup python_xbmc_RenderCapture
80 /// @brief \python_func{ getHeight() }
83 /// To get height of captured image as set during RenderCapture.capture().
84 /// Returns 0 prior to calling capture.
86 /// @return height or 0 prior to calling capture
89 inline int getHeight() { return m_height
; }
92 #ifdef DOXYGEN_SHOULD_USE_THIS
94 /// \ingroup python_xbmc_RenderCapture
95 /// @brief \python_func{ getAspectRatio() }
96 /// Get aspect ratio of currently displayed video.
98 /// @return Aspect ratio
99 /// @warning This may be called prior to calling RenderCapture.capture().
103 inline float getAspectRatio()
105 const auto& components
= CServiceBroker::GetAppComponents();
106 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
107 return appPlayer
->GetRenderAspectRatio();
111 #ifdef DOXYGEN_SHOULD_USE_THIS
113 /// \ingroup python_xbmc_RenderCapture
114 /// @brief \python_func{ getImageFormat() }
117 /// @return Format of captured image: 'BGRA'
120 ///-----------------------------------------------------------------------
121 /// @python_v17 Image will now always be returned in BGRA
125 inline const char* getImageFormat()
131 #ifdef DOXYGEN_SHOULD_USE_THIS
133 /// \ingroup python_xbmc_RenderCapture
134 /// @brief \python_func{ getImage([msecs]) }
135 /// Returns captured image as a bytearray.
137 /// @param msecs [opt] Milliseconds to wait. Waits
138 /// 1000ms if not specified
139 /// @return Captured image as a bytearray
141 /// @note The size of the image is m_width * m_height * 4
144 ///-----------------------------------------------------------------------
145 /// @python_v17 Added the option to specify wait time in msec.
149 inline XbmcCommons::Buffer
getImage(unsigned int msecs
= 0)
152 if (!GetPixels(msecs
))
153 return XbmcCommons::Buffer(0);
155 size_t size
= m_width
* m_height
* 4;
156 return XbmcCommons::Buffer(m_buffer
, size
);
159 #ifdef DOXYGEN_SHOULD_USE_THIS
161 /// \ingroup python_xbmc_RenderCapture
162 /// @brief \python_func{ capture(width, height) }
163 /// Issue capture request.
165 /// @param width Width capture image should be rendered to
166 /// @param height Height capture image should should be rendered to
169 ///-----------------------------------------------------------------------
170 /// @python_v17 Removed the option to pass **flags**
174 inline void capture(int width
, int height
)
177 auto& components
= CServiceBroker::GetAppComponents();
178 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
182 appPlayer
->RenderCaptureRelease(m_captureId
);
185 m_captureId
= appPlayer
->RenderCaptureAlloc();
188 m_buffer
= new uint8_t[m_width
*m_height
*4];
189 appPlayer
->RenderCapture(m_captureId
, m_width
, m_height
, CAPTUREFLAG_CONTINUOUS
);
192 // hide these from swig
194 inline bool GetPixels(unsigned int msec
)
196 auto& components
= CServiceBroker::GetAppComponents();
197 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
198 return appPlayer
->RenderCaptureGetPixels(m_captureId
, msec
, m_buffer
,
199 m_width
* m_height
* 4);