| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // Implementation of GtkVideoRenderer | |
| 12 | |
| 13 #include "webrtc/api/video/i420_buffer.h" | |
| 14 #include "webrtc/api/video/video_frame.h" | |
| 15 #include "webrtc/media/devices/gtkvideorenderer.h" | |
| 16 | |
| 17 #include <gdk/gdk.h> | |
| 18 #include <glib.h> | |
| 19 #include <gtk/gtk.h> | |
| 20 | |
| 21 #include "libyuv/convert_argb.h" | |
| 22 | |
| 23 namespace cricket { | |
| 24 | |
| 25 class ScopedGdkLock { | |
| 26 public: | |
| 27 ScopedGdkLock() { | |
| 28 gdk_threads_enter(); | |
| 29 } | |
| 30 | |
| 31 ~ScopedGdkLock() { | |
| 32 gdk_threads_leave(); | |
| 33 } | |
| 34 }; | |
| 35 | |
| 36 GtkVideoRenderer::GtkVideoRenderer(int x, int y) | |
| 37 : window_(NULL), | |
| 38 draw_area_(NULL), | |
| 39 initial_x_(x), | |
| 40 initial_y_(y), | |
| 41 width_(0), | |
| 42 height_(0) { | |
| 43 g_type_init(); | |
| 44 // g_thread_init API is deprecated since glib 2.31.0, see release note: | |
| 45 // http://mail.gnome.org/archives/gnome-announce-list/2011-October/msg00041.ht
ml | |
| 46 #if !GLIB_CHECK_VERSION(2, 31, 0) | |
| 47 g_thread_init(NULL); | |
| 48 #endif | |
| 49 gdk_threads_init(); | |
| 50 } | |
| 51 | |
| 52 GtkVideoRenderer::~GtkVideoRenderer() { | |
| 53 if (window_) { | |
| 54 ScopedGdkLock lock; | |
| 55 gtk_widget_destroy(window_); | |
| 56 // Run the Gtk main loop to tear down the window. | |
| 57 Pump(); | |
| 58 } | |
| 59 // Don't need to destroy draw_area_ because it is not top-level, so it is | |
| 60 // implicitly destroyed by the above. | |
| 61 } | |
| 62 | |
| 63 bool GtkVideoRenderer::SetSize(int width, int height) { | |
| 64 ScopedGdkLock lock; | |
| 65 | |
| 66 // If the dimension is the same, no-op. | |
| 67 if (width_ == width && height_ == height) { | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 // For the first frame, initialize the GTK window | |
| 72 if ((!window_ && !Initialize(width, height)) || IsClosed()) { | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 image_.reset(new uint8_t[width * height * 4]); | |
| 77 gtk_widget_set_size_request(draw_area_, width, height); | |
| 78 | |
| 79 width_ = width; | |
| 80 height_ = height; | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 void GtkVideoRenderer::OnFrame(const webrtc::VideoFrame& video_frame) { | |
| 85 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( | |
| 86 video_frame.video_frame_buffer()); | |
| 87 if (video_frame.rotation() != webrtc::kVideoRotation_0) { | |
| 88 buffer = webrtc::I420Buffer::Rotate(*buffer, video_frame.rotation()); | |
| 89 } | |
| 90 | |
| 91 // Need to set size as the frame might be rotated. | |
| 92 if (!SetSize(buffer->width(), buffer->height())) { | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 // convert I420 frame to ABGR format, which is accepted by GTK | |
| 97 libyuv::I420ToARGB(buffer->DataY(), buffer->StrideY(), | |
| 98 buffer->DataU(), buffer->StrideU(), | |
| 99 buffer->DataV(), buffer->StrideV(), | |
| 100 image_.get(), buffer->width() * 4, | |
| 101 buffer->width(), buffer->height()); | |
| 102 | |
| 103 ScopedGdkLock lock; | |
| 104 | |
| 105 if (IsClosed()) { | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 // draw the ABGR image | |
| 110 gdk_draw_rgb_32_image(draw_area_->window, | |
| 111 draw_area_->style->fg_gc[GTK_STATE_NORMAL], | |
| 112 0, | |
| 113 0, | |
| 114 buffer->width(), | |
| 115 buffer->height(), | |
| 116 GDK_RGB_DITHER_MAX, | |
| 117 image_.get(), | |
| 118 buffer->width() * 4); | |
| 119 | |
| 120 // Run the Gtk main loop to refresh the window. | |
| 121 Pump(); | |
| 122 } | |
| 123 | |
| 124 bool GtkVideoRenderer::Initialize(int width, int height) { | |
| 125 gtk_init(NULL, NULL); | |
| 126 window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 127 draw_area_ = gtk_drawing_area_new(); | |
| 128 if (!window_ || !draw_area_) { | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 gtk_window_set_position(GTK_WINDOW(window_), GTK_WIN_POS_CENTER); | |
| 133 gtk_window_set_title(GTK_WINDOW(window_), "Video Renderer"); | |
| 134 gtk_window_set_resizable(GTK_WINDOW(window_), FALSE); | |
| 135 gtk_widget_set_size_request(draw_area_, width, height); | |
| 136 gtk_container_add(GTK_CONTAINER(window_), draw_area_); | |
| 137 gtk_widget_show_all(window_); | |
| 138 gtk_window_move(GTK_WINDOW(window_), initial_x_, initial_y_); | |
| 139 | |
| 140 image_.reset(new uint8_t[width * height * 4]); | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 void GtkVideoRenderer::Pump() { | |
| 145 while (gtk_events_pending()) { | |
| 146 gtk_main_iteration(); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 bool GtkVideoRenderer::IsClosed() const { | |
| 151 if (!window_) { | |
| 152 // Not initialized yet, so hasn't been closed. | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 if (!GTK_IS_WINDOW(window_) || !GTK_IS_DRAWING_AREA(draw_area_)) { | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 } // namespace cricket | |
| OLD | NEW |