static gboolean gst_gdiscreencapsrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); HWND capture; HDC device; GstStructure *structure; const GValue *framerate; structure = gst_caps_get_structure (caps, 0); src->src_rect = src->screen_rect; if (src->capture_w && src->capture_h) { src->src_rect.left += src->capture_x; src->src_rect.top += src->capture_y; src->src_rect.right = src->src_rect.left + src->capture_w; src->src_rect.bottom = src->src_rect.top + src->capture_h; } framerate = gst_structure_get_value (structure, "framerate"); if (framerate) { src->rate_numerator = gst_value_get_fraction_numerator (framerate); src->rate_denominator = gst_value_get_fraction_denominator (framerate); } src->info.bmiHeader.biSize = sizeof (BITMAPINFOHEADER); src->info.bmiHeader.biWidth = src->src_rect.right - src->src_rect.left; src->info.bmiHeader.biHeight = src->src_rect.top - src->src_rect.bottom; src->info.bmiHeader.biPlanes = 1; src->info.bmiHeader.biBitCount = 24; src->info.bmiHeader.biCompression = BI_RGB; src->info.bmiHeader.biSizeImage = 0; src->info.bmiHeader.biXPelsPerMeter = 0; src->info.bmiHeader.biYPelsPerMeter = 0; src->info.bmiHeader.biClrUsed = 0; src->info.bmiHeader.biClrImportant = 0; /* Cleanup first */ if (src->hBitmap != INVALID_HANDLE_VALUE) DeleteObject (src->hBitmap); if (src->memDC != INVALID_HANDLE_VALUE) DeleteDC (src->memDC); /* Allocate */ capture = GetDesktopWindow (); device = GetDC (capture); src->hBitmap = CreateDIBSection (device, &(src->info), DIB_RGB_COLORS, (void **) &(src->dibMem), 0, 0); src->memDC = CreateCompatibleDC (device); SelectObject (src->memDC, src->hBitmap); ReleaseDC (capture, device); GST_DEBUG_OBJECT (src, "size %dx%d, %d/%d fps", (gint) src->info.bmiHeader.biWidth, (gint) (-src->info.bmiHeader.biHeight), src->rate_numerator, src->rate_denominator); return TRUE; }
static gboolean gst_gdiscreencapsrc_stop (GstBaseSrc * bsrc) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); return TRUE; }
static void gst_gdiscreencapsrc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (object); switch (prop_id) { case PROP_MONITOR: if (g_value_get_int (value) >= GetSystemMetrics (SM_CMONITORS)) { G_OBJECT_WARN_INVALID_PSPEC (object, "Monitor", prop_id, pspec); break; } src->monitor = g_value_get_int (value); break; case PROP_SHOW_CURSOR: src->show_cursor = g_value_get_boolean (value); break; case PROP_X_POS: src->capture_x = g_value_get_int (value); break; case PROP_Y_POS: src->capture_y = g_value_get_int (value); break; case PROP_WIDTH: src->capture_w = g_value_get_int (value); break; case PROP_HEIGHT: src->capture_h = g_value_get_int (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; }; }
static void gst_gdiscreencapsrc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (object); switch (prop_id) { case PROP_MONITOR: g_value_set_int (value, src->monitor); break; case PROP_SHOW_CURSOR: g_value_set_boolean (value, src->show_cursor); break; case PROP_X_POS: g_value_set_int (value, src->capture_x); break; case PROP_Y_POS: g_value_set_int (value, src->capture_y); break; case PROP_WIDTH: g_value_set_int (value, src->capture_w); break; case PROP_HEIGHT: g_value_set_int (value, src->capture_h); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; }; }
static GstCaps * gst_gdiscreencapsrc_get_caps (GstBaseSrc * bsrc) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); RECT rect_dst; src->screen_rect = rect_dst = gst_win32_get_monitor_rect (src->monitor); if (src->capture_w && src->capture_h && src->capture_x + src->capture_w < rect_dst.right - rect_dst.left && src->capture_y + src->capture_h < rect_dst.bottom - rect_dst.top) { rect_dst.left = src->capture_x; rect_dst.top = src->capture_y; rect_dst.right = src->capture_x + src->capture_w; rect_dst.bottom = src->capture_y + src->capture_h; } else { /* Default values. */ src->capture_x = src->capture_y = 0; src->capture_w = src->capture_h = 0; } GST_DEBUG ("width = %d, height=%d", rect_dst.right - rect_dst.left, rect_dst.bottom - rect_dst.top); return gst_caps_new_simple ("video/x-raw-rgb", "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24, "endianness", G_TYPE_INT, G_BIG_ENDIAN, "red_mask", G_TYPE_INT, GST_VIDEO_BYTE3_MASK_24_INT, "green_mask", G_TYPE_INT, GST_VIDEO_BYTE2_MASK_24_INT, "blue_mask", G_TYPE_INT, GST_VIDEO_BYTE1_MASK_24_INT, "width", G_TYPE_INT, rect_dst.right - rect_dst.left, "height", G_TYPE_INT, rect_dst.bottom - rect_dst.top, "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, NULL); }
static gboolean gst_gdiscreencapsrc_start (GstBaseSrc * bsrc) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); src->frame_number = -1; return TRUE; }
static gboolean gst_gdiscreencapsrc_unlock (GstBaseSrc * bsrc) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); GST_OBJECT_LOCK (src); if (src->clock_id) { GST_DEBUG_OBJECT (src, "Waking up waiting clock"); gst_clock_id_unschedule (src->clock_id); } GST_OBJECT_UNLOCK (src); return TRUE; }
static void gst_gdiscreencapsrc_dispose (GObject * object) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (object); if (src->hBitmap != INVALID_HANDLE_VALUE) DeleteObject (src->hBitmap); if (src->memDC != INVALID_HANDLE_VALUE) DeleteDC (src->memDC); src->hBitmap = (HBITMAP) INVALID_HANDLE_VALUE; src->memDC = (HDC) INVALID_HANDLE_VALUE; src->dibMem = NULL; }
static void gst_gdiscreencapsrc_get_times (GstBaseSrc * basesrc, GstBuffer * buffer, GstClockTime * start, GstClockTime * end) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (basesrc); GstClockTime timestamp; timestamp = GST_BUFFER_TIMESTAMP (buffer); if (GST_CLOCK_TIME_IS_VALID (timestamp)) { GstClockTime duration = GST_BUFFER_DURATION (buffer); if (GST_CLOCK_TIME_IS_VALID (duration)) *end = timestamp + duration; *start = timestamp; } }
static GstCaps * gst_gdiscreencapsrc_get_caps (GstBaseSrc * bsrc, GstCaps * filter) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); RECT rect_dst; GstCaps *caps; src->screen_rect = rect_dst = gst_win32_get_monitor_rect (src->monitor); if (src->capture_w && src->capture_h && src->capture_x + src->capture_w < rect_dst.right - rect_dst.left && src->capture_y + src->capture_h < rect_dst.bottom - rect_dst.top) { rect_dst.left = src->capture_x; rect_dst.top = src->capture_y; rect_dst.right = src->capture_x + src->capture_w; rect_dst.bottom = src->capture_y + src->capture_h; } else { /* Default values. */ src->capture_x = src->capture_y = 0; src->capture_w = src->capture_h = 0; } GST_DEBUG ("width = %d, height=%d", (gint) (rect_dst.right - rect_dst.left), (gint) (rect_dst.bottom - rect_dst.top)); caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "BGR", "width", G_TYPE_INT, rect_dst.right - rect_dst.left, "height", G_TYPE_INT, rect_dst.bottom - rect_dst.top, "framerate", GST_TYPE_FRACTION_RANGE, 1, G_MAXINT, G_MAXINT, 1, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL); if (filter) { GstCaps *tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST); gst_caps_unref (caps); caps = tmp; } return caps; }
static GstFlowReturn gst_gdiscreencapsrc_create (GstPushSrc * push_src, GstBuffer ** buf) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (push_src); GstBuffer *new_buf; gint new_buf_size; GstClock *clock; GstClockTime buf_time, buf_dur; guint64 frame_number; if (G_UNLIKELY (!src->info.bmiHeader.biWidth || !src->info.bmiHeader.biHeight)) { GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL), ("format wasn't negotiated before create function")); return GST_FLOW_NOT_NEGOTIATED; } new_buf_size = GST_ROUND_UP_4 (src->info.bmiHeader.biWidth * 3) * (-src->info.bmiHeader.biHeight); GST_LOG_OBJECT (src, "creating buffer of %d bytes with %dx%d image", new_buf_size, (gint) src->info.bmiHeader.biWidth, (gint) (-src->info.bmiHeader.biHeight)); new_buf = gst_buffer_new_and_alloc (new_buf_size); clock = gst_element_get_clock (GST_ELEMENT (src)); if (clock != NULL) { GstClockTime time, base_time; /* Calculate sync time. */ time = gst_clock_get_time (clock); base_time = gst_element_get_base_time (GST_ELEMENT (src)); buf_time = time - base_time; if (src->rate_numerator) { frame_number = gst_util_uint64_scale (buf_time, src->rate_numerator, GST_SECOND * src->rate_denominator); } else { frame_number = -1; } } else { buf_time = GST_CLOCK_TIME_NONE; frame_number = -1; } if (frame_number != -1 && frame_number == src->frame_number) { GstClockID id; GstClockReturn ret; /* Need to wait for the next frame */ frame_number += 1; /* Figure out what the next frame time is */ buf_time = gst_util_uint64_scale (frame_number, src->rate_denominator * GST_SECOND, src->rate_numerator); id = gst_clock_new_single_shot_id (clock, buf_time + gst_element_get_base_time (GST_ELEMENT (src))); GST_OBJECT_LOCK (src); src->clock_id = id; GST_OBJECT_UNLOCK (src); GST_DEBUG_OBJECT (src, "Waiting for next frame time %" G_GUINT64_FORMAT, buf_time); ret = gst_clock_id_wait (id, NULL); GST_OBJECT_LOCK (src); gst_clock_id_unref (id); src->clock_id = NULL; if (ret == GST_CLOCK_UNSCHEDULED) { /* Got woken up by the unlock function */ GST_OBJECT_UNLOCK (src); return GST_FLOW_FLUSHING; } GST_OBJECT_UNLOCK (src); /* Duration is a complete 1/fps frame duration */ buf_dur = gst_util_uint64_scale_int (GST_SECOND, src->rate_denominator, src->rate_numerator); } else if (frame_number != -1) { GstClockTime next_buf_time; GST_DEBUG_OBJECT (src, "No need to wait for next frame time %" G_GUINT64_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %" G_GINT64_FORMAT, buf_time, frame_number, src->frame_number); next_buf_time = gst_util_uint64_scale (frame_number + 1, src->rate_denominator * GST_SECOND, src->rate_numerator); /* Frame duration is from now until the next expected capture time */ buf_dur = next_buf_time - buf_time; } else { buf_dur = GST_CLOCK_TIME_NONE; } src->frame_number = frame_number; GST_BUFFER_TIMESTAMP (new_buf) = buf_time; GST_BUFFER_DURATION (new_buf) = buf_dur; /* Do screen capture and put it into buffer... */ gst_gdiscreencapsrc_screen_capture (src, new_buf); gst_object_unref (clock); *buf = new_buf; return GST_FLOW_OK; }
static GstFlowReturn gst_gdiscreencapsrc_create (GstPushSrc * push_src, GstBuffer ** buf) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (push_src); GstBuffer *new_buf; GstFlowReturn res; gint new_buf_size; GstClock *clock; GstClockTime time; GstClockTime base_time; if (G_UNLIKELY (!src->info.bmiHeader.biWidth || !src->info.bmiHeader.biHeight)) { GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL), ("format wasn't negotiated before create function")); return GST_FLOW_NOT_NEGOTIATED; } else if (G_UNLIKELY (src->rate_numerator == 0 && src->frames == 1)) { GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->frames); return GST_FLOW_UNEXPECTED; } new_buf_size = GST_ROUND_UP_4 (src->info.bmiHeader.biWidth * 3) * (-src->info.bmiHeader.biHeight); GST_LOG_OBJECT (src, "creating buffer of %lu bytes with %dx%d image for frame %d", new_buf_size, src->info.bmiHeader.biWidth, -src->info.bmiHeader.biHeight, (gint) src->frames); res = gst_pad_alloc_buffer_and_set_caps (GST_BASE_SRC_PAD (src), GST_BUFFER_OFFSET_NONE, new_buf_size, GST_PAD_CAPS (GST_BASE_SRC_PAD (push_src)), &new_buf); if (res != GST_FLOW_OK) { GST_DEBUG_OBJECT (src, "could not allocate buffer, reason %s", gst_flow_get_name (res)); return res; } clock = gst_element_get_clock (GST_ELEMENT (src)); if (clock) { /* Calculate sync time. */ GstClockTime frame_time = gst_util_uint64_scale_int (src->frames * GST_SECOND, src->rate_denominator, src->rate_numerator); time = gst_clock_get_time (clock); base_time = gst_element_get_base_time (GST_ELEMENT (src)); GST_BUFFER_TIMESTAMP (new_buf) = MAX (time - base_time, frame_time); } else { GST_BUFFER_TIMESTAMP (new_buf) = GST_CLOCK_TIME_NONE; } /* Do screen capture and put it into buffer... */ gst_gdiscreencapsrc_screen_capture (src, new_buf); if (src->rate_numerator) { GST_BUFFER_DURATION (new_buf) = gst_util_uint64_scale_int (GST_SECOND, src->rate_denominator, src->rate_numerator); if (clock) { GST_BUFFER_DURATION (new_buf) = MAX (GST_BUFFER_DURATION (new_buf), gst_clock_get_time (clock) - time); } } else { /* NONE means forever */ GST_BUFFER_DURATION (new_buf) = GST_CLOCK_TIME_NONE; } GST_BUFFER_OFFSET (new_buf) = src->frames; src->frames++; GST_BUFFER_OFFSET_END (new_buf) = src->frames; gst_object_unref (clock); *buf = new_buf; return GST_FLOW_OK; }
static gboolean gst_gdiscreencapsrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps) { GstGDIScreenCapSrc *src = GST_GDISCREENCAPSRC (bsrc); HWND capture; HDC device; GstStructure *structure; const GValue *framerate; gint red_mask, green_mask, blue_mask; gint bpp; structure = gst_caps_get_structure (caps, 0); gst_structure_get_int (structure, "red_mask", &red_mask); gst_structure_get_int (structure, "green_mask", &green_mask); gst_structure_get_int (structure, "blue_mask", &blue_mask); if (blue_mask != GST_VIDEO_BYTE1_MASK_24_INT || green_mask != GST_VIDEO_BYTE2_MASK_24_INT || red_mask != GST_VIDEO_BYTE3_MASK_24_INT) { GST_ERROR ("Wrong red_,green_,blue_ mask provided. " "We only support RGB and BGR"); return FALSE; } gst_structure_get_int (structure, "bpp", &bpp); if (bpp != 24) { GST_ERROR ("Wrong bpp provided %d. We only support 24 bpp", bpp); return FALSE; } src->src_rect = src->screen_rect; if (src->capture_w && src->capture_h) { src->src_rect.left += src->capture_x; src->src_rect.top += src->capture_y; src->src_rect.right = src->src_rect.left + src->capture_w; src->src_rect.bottom = src->src_rect.top + src->capture_h; } if (framerate = gst_structure_get_value (structure, "framerate")) { src->rate_numerator = gst_value_get_fraction_numerator (framerate); src->rate_denominator = gst_value_get_fraction_denominator (framerate); } src->info.bmiHeader.biSize = sizeof (BITMAPINFOHEADER); src->info.bmiHeader.biWidth = src->src_rect.right - src->src_rect.left; src->info.bmiHeader.biHeight = src->src_rect.top - src->src_rect.bottom; src->info.bmiHeader.biPlanes = 1; src->info.bmiHeader.biBitCount = 24; src->info.bmiHeader.biCompression = BI_RGB; src->info.bmiHeader.biSizeImage = 0; src->info.bmiHeader.biXPelsPerMeter = 0; src->info.bmiHeader.biYPelsPerMeter = 0; src->info.bmiHeader.biClrUsed = 0; src->info.bmiHeader.biClrImportant = 0; /* Cleanup first */ if (src->hBitmap != INVALID_HANDLE_VALUE) DeleteObject (src->hBitmap); if (src->memDC != INVALID_HANDLE_VALUE) DeleteDC (src->memDC); /* Allocate */ capture = GetDesktopWindow (); device = GetDC (capture); src->hBitmap = CreateDIBSection (device, &(src->info), DIB_RGB_COLORS, (void **) &(src->dibMem), 0, 0); src->memDC = CreateCompatibleDC (device); SelectObject (src->memDC, src->hBitmap); ReleaseDC (capture, device); GST_DEBUG_OBJECT (src, "size %dx%d, %d/%d fps", src->info.bmiHeader.biWidth, -src->info.bmiHeader.biHeight, src->rate_numerator, src->rate_denominator); return TRUE; }