2 * Copyright (C) 2007 Haakon Sporsheim <hakon.sporsheim@tandberg.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-dx9screencapsrc
22 * @title: dx9screencapsrc
24 * This element uses DirectX to capture the desktop or a portion of it.
25 * The default is capturing the whole desktop, but #GstDX9ScreenCapSrc:x,
26 * #GstDX9ScreenCapSrc:y, #GstDX9ScreenCapSrc:width and
27 * #GstDX9ScreenCapSrc:height can be used to select a particular region.
28 * Use #GstDX9ScreenCapSrc:monitor for changing which monitor to capture
31 * ## Example pipelines
33 * gst-launch-1.0 dx9screencapsrc ! videoconvert ! dshowvideosink
34 * ]| Capture the desktop and display it.
36 * gst-launch-1.0 dx9screencapsrc x=100 y=100 width=320 height=240 !
37 * videoconvert ! dshowvideosink
38 * ]| Capture a portion of the desktop and display it.
46 #include "gstdx9screencapsrc.h"
47 #include <gst/video/video.h>
49 GST_DEBUG_CATEGORY_STATIC (dx9screencapsrc_debug);
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGR"))
57 #define gst_dx9screencapsrc_parent_class parent_class
58 G_DEFINE_TYPE (GstDX9ScreenCapSrc, gst_dx9screencapsrc, GST_TYPE_PUSH_SRC);
71 static IDirect3D9 *g_d3d9 = NULL;
74 static void gst_dx9screencapsrc_dispose (GObject * object);
75 static void gst_dx9screencapsrc_set_property (GObject * object,
76 guint prop_id, const GValue * value, GParamSpec * pspec);
77 static void gst_dx9screencapsrc_get_property (GObject * object,
78 guint prop_id, GValue * value, GParamSpec * pspec);
80 static GstCaps *gst_dx9screencapsrc_fixate (GstBaseSrc * bsrc, GstCaps * caps);
81 static gboolean gst_dx9screencapsrc_set_caps (GstBaseSrc * bsrc,
83 static GstCaps *gst_dx9screencapsrc_get_caps (GstBaseSrc * bsrc,
85 static gboolean gst_dx9screencapsrc_start (GstBaseSrc * bsrc);
86 static gboolean gst_dx9screencapsrc_stop (GstBaseSrc * bsrc);
88 static gboolean gst_dx9screencapsrc_unlock (GstBaseSrc * bsrc);
90 static GstFlowReturn gst_dx9screencapsrc_create (GstPushSrc * src,
95 gst_dx9screencapsrc_class_init (GstDX9ScreenCapSrcClass * klass)
97 GObjectClass *go_class;
98 GstElementClass *e_class;
99 GstBaseSrcClass *bs_class;
100 GstPushSrcClass *ps_class;
102 go_class = (GObjectClass *) klass;
103 e_class = (GstElementClass *) klass;
104 bs_class = (GstBaseSrcClass *) klass;
105 ps_class = (GstPushSrcClass *) klass;
107 go_class->dispose = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_dispose);
108 go_class->set_property = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_set_property);
109 go_class->get_property = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_get_property);
111 bs_class->get_caps = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_get_caps);
112 bs_class->set_caps = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_set_caps);
113 bs_class->start = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_start);
114 bs_class->stop = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_stop);
115 bs_class->unlock = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_unlock);
116 bs_class->fixate = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_fixate);
118 ps_class->create = GST_DEBUG_FUNCPTR (gst_dx9screencapsrc_create);
120 g_object_class_install_property (go_class, PROP_MONITOR,
121 g_param_spec_int ("monitor", "Monitor",
122 "Which monitor to use (0 = 1st monitor and default)",
123 0, G_MAXINT, 0, G_PARAM_READWRITE));
125 g_object_class_install_property (go_class, PROP_SHOW_CURSOR,
126 g_param_spec_boolean ("cursor", "Show mouse cursor",
127 "Whether to show mouse cursor (default off)",
128 FALSE, G_PARAM_READWRITE));
130 g_object_class_install_property (go_class, PROP_X_POS,
131 g_param_spec_int ("x", "X",
132 "Horizontal coordinate of top left corner for the screen capture "
133 "area", 0, G_MAXINT, 0, G_PARAM_READWRITE));
134 g_object_class_install_property (go_class, PROP_Y_POS,
135 g_param_spec_int ("y", "Y",
136 "Vertical coordinate of top left corner for the screen capture "
137 "area", 0, G_MAXINT, 0, G_PARAM_READWRITE));
139 g_object_class_install_property (go_class, PROP_WIDTH,
140 g_param_spec_int ("width", "Width",
141 "Width of screen capture area (0 = maximum)",
142 0, G_MAXINT, 0, G_PARAM_READWRITE));
143 g_object_class_install_property (go_class, PROP_HEIGHT,
144 g_param_spec_int ("height", "Height",
145 "Height of screen capture area (0 = maximum)",
146 0, G_MAXINT, 0, G_PARAM_READWRITE));
148 gst_element_class_add_static_pad_template (e_class, &src_template);
150 gst_element_class_set_static_metadata (e_class,
151 "DirectX 9 screen capture source", "Source/Video", "Captures screen",
152 "Haakon Sporsheim <hakon.sporsheim@tandberg.com>");
154 GST_DEBUG_CATEGORY_INIT (dx9screencapsrc_debug, "dx9screencapsrc", 0,
155 "DirectX 9 screen capture source");
159 gst_dx9screencapsrc_init (GstDX9ScreenCapSrc * src)
161 /* Set src element initial values... */
163 src->d3d9_device = NULL;
170 src->show_cursor = FALSE;
171 src->monitor_info.cbSize = sizeof (MONITORINFO);
173 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
174 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
177 g_d3d9 = Direct3DCreate9 (D3D_SDK_VERSION);
179 IDirect3D9_AddRef (g_d3d9);
183 gst_dx9screencapsrc_dispose (GObject * object)
185 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (object);
188 GST_ERROR_OBJECT (object,
189 "DX9 surface was not freed in _stop, freeing in _dispose!");
190 IDirect3DSurface9_Release (src->surface);
194 if (src->d3d9_device) {
195 IDirect3DDevice9_Release (src->d3d9_device);
196 src->d3d9_device = NULL;
199 if (!IDirect3D9_Release (g_d3d9))
202 G_OBJECT_CLASS (parent_class)->dispose (object);
206 gst_dx9screencapsrc_set_property (GObject * object,
207 guint prop_id, const GValue * value, GParamSpec * pspec)
209 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (object);
213 src->monitor = g_value_get_int (value);
215 case PROP_SHOW_CURSOR:
216 src->show_cursor = g_value_get_boolean (value);
219 src->capture_x = g_value_get_int (value);
222 src->capture_y = g_value_get_int (value);
225 src->capture_w = g_value_get_int (value);
228 src->capture_h = g_value_get_int (value);
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 gst_dx9screencapsrc_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec)
240 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (object);
244 g_value_set_int (value, src->monitor);
246 case PROP_SHOW_CURSOR:
247 g_value_set_boolean (value, src->show_cursor);
250 g_value_set_int (value, src->capture_x);
253 g_value_set_int (value, src->capture_y);
256 g_value_set_int (value, src->capture_w);
259 g_value_set_int (value, src->capture_h);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 gst_dx9screencapsrc_fixate (GstBaseSrc * bsrc, GstCaps * caps)
270 GstStructure *structure;
272 caps = gst_caps_make_writable (caps);
274 structure = gst_caps_get_structure (caps, 0);
276 gst_structure_fixate_field_nearest_int (structure, "width", 640);
277 gst_structure_fixate_field_nearest_int (structure, "height", 480);
278 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
280 caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
286 gst_dx9screencapsrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps)
288 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (bsrc);
289 GstStructure *structure;
290 const GValue *framerate;
292 structure = gst_caps_get_structure (caps, 0);
294 src->src_rect = src->screen_rect;
295 if (src->capture_w && src->capture_h) {
296 src->src_rect.left += src->capture_x;
297 src->src_rect.top += src->capture_y;
298 src->src_rect.right = src->src_rect.left + src->capture_w;
299 src->src_rect.bottom = src->src_rect.top + src->capture_h;
302 framerate = gst_structure_get_value (structure, "framerate");
304 src->rate_numerator = gst_value_get_fraction_numerator (framerate);
305 src->rate_denominator = gst_value_get_fraction_denominator (framerate);
308 GST_DEBUG_OBJECT (src, "size %dx%d, %d/%d fps",
309 (gint) (src->src_rect.right - src->src_rect.left),
310 (gint) (src->src_rect.bottom - src->src_rect.top),
311 src->rate_numerator, src->rate_denominator);
317 gst_dx9screencapsrc_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
319 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (bsrc);
323 if (src->monitor >= IDirect3D9_GetAdapterCount (g_d3d9)) {
324 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
325 ("Specified monitor with index %d not found", src->monitor), (NULL));
329 if (FAILED (IDirect3D9_GetAdapterDisplayMode (g_d3d9, src->monitor,
334 SetRect (&rect_dst, 0, 0, src->disp_mode.Width, src->disp_mode.Height);
335 src->screen_rect = rect_dst;
337 if (src->capture_w && src->capture_h &&
338 src->capture_x + src->capture_w < rect_dst.right - rect_dst.left &&
339 src->capture_y + src->capture_h < rect_dst.bottom - rect_dst.top) {
340 rect_dst.left = src->capture_x;
341 rect_dst.top = src->capture_y;
342 rect_dst.right = src->capture_x + src->capture_w;
343 rect_dst.bottom = src->capture_y + src->capture_h;
346 src->capture_x = src->capture_y = 0;
347 src->capture_w = src->capture_h = 0;
351 * Expose as xRGB even though the Surface is allocated as ARGB!
352 * This is due to IDirect3DDevice9_GetFrontBufferData which only takes
353 * ARGB surface, but the A channel is in reality never used.
354 * I should add that I had problems specifying ARGB. It might be a bug
355 * in ffmpegcolorspace which I used for testing.
356 * Another interesting thing is that directdrawsink did not support ARGB,
357 * but only xRGB. (On my system, using 32b color depth) And according to
358 * the DirectX documentation ARGB is NOT a valid display buffer format,
361 caps = gst_caps_new_simple ("video/x-raw",
362 "format", G_TYPE_STRING, "BGRx",
363 "width", G_TYPE_INT, rect_dst.right - rect_dst.left,
364 "height", G_TYPE_INT, rect_dst.bottom - rect_dst.top,
365 "framerate", GST_TYPE_FRACTION_RANGE, 1, 1, G_MAXINT, 1,
366 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL);
370 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
371 gst_caps_unref (caps);
379 gst_dx9screencapsrc_start (GstBaseSrc * bsrc)
381 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (bsrc);
382 D3DPRESENT_PARAMETERS d3dpp;
386 src->frame_number = -1;
388 ZeroMemory (&d3dpp, sizeof (D3DPRESENT_PARAMETERS));
389 d3dpp.Windowed = TRUE;
390 d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
391 d3dpp.BackBufferFormat = src->disp_mode.Format;
392 d3dpp.BackBufferHeight = src->disp_mode.Height;
393 d3dpp.BackBufferWidth = src->disp_mode.Width;
394 d3dpp.BackBufferCount = 1;
395 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
396 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
397 d3dpp.hDeviceWindow = GetDesktopWindow ();
398 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
399 d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
401 if (src->monitor >= IDirect3D9_GetAdapterCount (g_d3d9)) {
402 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
403 ("Specified monitor with index %d not found", src->monitor), (NULL));
407 res = IDirect3D9_CreateDevice (g_d3d9, src->monitor, D3DDEVTYPE_HAL,
408 GetDesktopWindow (), D3DCREATE_SOFTWARE_VERTEXPROCESSING,
409 &d3dpp, &src->d3d9_device);
413 monitor = IDirect3D9_GetAdapterMonitor (g_d3d9, src->monitor);
414 GetMonitorInfo (monitor, &src->monitor_info);
417 SUCCEEDED (IDirect3DDevice9_CreateOffscreenPlainSurface (src->d3d9_device,
418 src->disp_mode.Width, src->disp_mode.Height, D3DFMT_A8R8G8B8,
419 D3DPOOL_SYSTEMMEM, &src->surface, NULL));
423 gst_dx9screencapsrc_stop (GstBaseSrc * bsrc)
425 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (bsrc);
428 IDirect3DSurface9_Release (src->surface);
436 gst_dx9screencapsrc_unlock (GstBaseSrc * bsrc)
438 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (bsrc);
440 GST_OBJECT_LOCK (src);
442 GST_DEBUG_OBJECT (src, "Waking up waiting clock");
443 gst_clock_id_unschedule (src->clock_id);
445 GST_OBJECT_UNLOCK (src);
451 gst_dx9screencapsrc_create (GstPushSrc * push_src, GstBuffer ** buf)
453 GstDX9ScreenCapSrc *src = GST_DX9SCREENCAPSRC (push_src);
455 gint new_buf_size, i;
456 gint width, height, stride;
458 GstClockTime buf_time, buf_dur;
459 D3DLOCKED_RECT locked_rect;
463 guint64 frame_number;
465 if (G_UNLIKELY (!src->d3d9_device)) {
466 GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
467 ("format wasn't negotiated before create function"));
468 return GST_FLOW_NOT_NEGOTIATED;
471 clock = gst_element_get_clock (GST_ELEMENT (src));
473 GstClockTime time, base_time;
475 /* Calculate sync time. */
477 time = gst_clock_get_time (clock);
478 base_time = gst_element_get_base_time (GST_ELEMENT (src));
479 buf_time = time - base_time;
481 if (src->rate_numerator) {
482 frame_number = gst_util_uint64_scale (buf_time,
483 src->rate_numerator, GST_SECOND * src->rate_denominator);
488 buf_time = GST_CLOCK_TIME_NONE;
492 if (frame_number != -1 && frame_number == src->frame_number) {
496 /* Need to wait for the next frame */
499 /* Figure out what the next frame time is */
500 buf_time = gst_util_uint64_scale (frame_number,
501 src->rate_denominator * GST_SECOND, src->rate_numerator);
503 id = gst_clock_new_single_shot_id (clock,
504 buf_time + gst_element_get_base_time (GST_ELEMENT (src)));
505 GST_OBJECT_LOCK (src);
507 GST_OBJECT_UNLOCK (src);
509 GST_DEBUG_OBJECT (src, "Waiting for next frame time %" G_GUINT64_FORMAT,
511 ret = gst_clock_id_wait (id, NULL);
513 GST_OBJECT_LOCK (src);
514 gst_clock_id_unref (id);
515 src->clock_id = NULL;
516 GST_OBJECT_UNLOCK (src);
518 if (ret == GST_CLOCK_UNSCHEDULED) {
519 /* Got woken up by the unlock function */
520 gst_object_unref (clock);
521 return GST_FLOW_FLUSHING;
524 /* Duration is a complete 1/fps frame duration */
526 gst_util_uint64_scale_int (GST_SECOND, src->rate_denominator,
527 src->rate_numerator);
528 } else if (frame_number != -1) {
529 GstClockTime next_buf_time;
531 GST_DEBUG_OBJECT (src, "No need to wait for next frame time %"
532 G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
533 G_GINT64_FORMAT, buf_time, frame_number, src->frame_number);
534 next_buf_time = gst_util_uint64_scale (frame_number + 1,
535 src->rate_denominator * GST_SECOND, src->rate_numerator);
536 /* Frame duration is from now until the next expected capture time */
537 buf_dur = next_buf_time - buf_time;
539 buf_dur = GST_CLOCK_TIME_NONE;
541 src->frame_number = frame_number;
543 height = (src->src_rect.bottom - src->src_rect.top);
544 width = (src->src_rect.right - src->src_rect.left);
545 new_buf_size = width * 4 * height;
548 "creating buffer of %d bytes with %dx%d image",
549 new_buf_size, width, height);
551 /* Do screen capture and put it into buffer...
552 * Acquire front buffer, and lock it
555 IDirect3DDevice9_GetFrontBufferData (src->d3d9_device, 0, src->surface);
557 GST_DEBUG_OBJECT (src, "DirectX::GetBackBuffer failed.");
559 gst_object_unref (clock);
560 return GST_FLOW_ERROR;
563 if (src->show_cursor) {
566 ci.cbSize = sizeof (CURSORINFO);
568 if (ci.flags & CURSOR_SHOWING) {
572 GetIconInfo (ci.hCursor, &ii);
574 if (SUCCEEDED (IDirect3DSurface9_GetDC (src->surface, &memDC))) {
575 HCURSOR cursor = CopyImage (ci.hCursor, IMAGE_CURSOR, 0, 0,
576 LR_MONOCHROME | LR_DEFAULTSIZE);
579 ci.ptScreenPos.x - ii.xHotspot - src->monitor_info.rcMonitor.left,
580 ci.ptScreenPos.y - ii.yHotspot - src->monitor_info.rcMonitor.top,
583 DestroyCursor (cursor);
584 IDirect3DSurface9_ReleaseDC (src->surface, memDC);
587 DeleteObject (ii.hbmColor);
588 DeleteObject (ii.hbmMask);
593 IDirect3DSurface9_LockRect (src->surface, &locked_rect, &(src->src_rect),
594 D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
596 GST_DEBUG_OBJECT (src, "DirectX::LockRect failed.");
598 gst_object_unref (clock);
599 return GST_FLOW_ERROR;
602 new_buf = gst_buffer_new_and_alloc (new_buf_size);
603 gst_buffer_map (new_buf, &map, GST_MAP_WRITE);
604 p_dst = (LPBYTE) map.data;
605 p_src = (LPBYTE) locked_rect.pBits;
607 for (i = 0; i < height; ++i) {
608 memcpy (p_dst, p_src, stride);
610 p_src += locked_rect.Pitch;
612 gst_buffer_unmap (new_buf, &map);
614 /* Unlock copy of front buffer */
615 IDirect3DSurface9_UnlockRect (src->surface);
617 GST_BUFFER_TIMESTAMP (new_buf) = buf_time;
618 GST_BUFFER_DURATION (new_buf) = buf_dur;
621 gst_object_unref (clock);