• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2012 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 #include "examples/peerconnection/client/linux/main_wnd.h"
12 
13 #include <cairo.h>
14 #include <gdk/gdk.h>
15 #include <gdk/gdkkeysyms.h>
16 #include <glib-object.h>
17 #include <glib.h>
18 #include <gobject/gclosure.h>
19 #include <gtk/gtk.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <cstdint>
26 #include <map>
27 #include <utility>
28 
29 #include "api/video/i420_buffer.h"
30 #include "api/video/video_frame_buffer.h"
31 #include "api/video/video_rotation.h"
32 #include "api/video/video_source_interface.h"
33 #include "rtc_base/checks.h"
34 #include "rtc_base/logging.h"
35 #include "third_party/libyuv/include/libyuv/convert.h"
36 #include "third_party/libyuv/include/libyuv/convert_from.h"
37 
38 namespace {
39 
40 //
41 // Simple static functions that simply forward the callback to the
42 // GtkMainWnd instance.
43 //
44 
OnDestroyedCallback(GtkWidget * widget,GdkEvent * event,gpointer data)45 gboolean OnDestroyedCallback(GtkWidget* widget,
46                              GdkEvent* event,
47                              gpointer data) {
48   reinterpret_cast<GtkMainWnd*>(data)->OnDestroyed(widget, event);
49   return FALSE;
50 }
51 
OnClickedCallback(GtkWidget * widget,gpointer data)52 void OnClickedCallback(GtkWidget* widget, gpointer data) {
53   reinterpret_cast<GtkMainWnd*>(data)->OnClicked(widget);
54 }
55 
SimulateButtonClick(gpointer button)56 gboolean SimulateButtonClick(gpointer button) {
57   g_signal_emit_by_name(button, "clicked");
58   return false;
59 }
60 
OnKeyPressCallback(GtkWidget * widget,GdkEventKey * key,gpointer data)61 gboolean OnKeyPressCallback(GtkWidget* widget,
62                             GdkEventKey* key,
63                             gpointer data) {
64   reinterpret_cast<GtkMainWnd*>(data)->OnKeyPress(widget, key);
65   return false;
66 }
67 
OnRowActivatedCallback(GtkTreeView * tree_view,GtkTreePath * path,GtkTreeViewColumn * column,gpointer data)68 void OnRowActivatedCallback(GtkTreeView* tree_view,
69                             GtkTreePath* path,
70                             GtkTreeViewColumn* column,
71                             gpointer data) {
72   reinterpret_cast<GtkMainWnd*>(data)->OnRowActivated(tree_view, path, column);
73 }
74 
SimulateLastRowActivated(gpointer data)75 gboolean SimulateLastRowActivated(gpointer data) {
76   GtkTreeView* tree_view = reinterpret_cast<GtkTreeView*>(data);
77   GtkTreeModel* model = gtk_tree_view_get_model(tree_view);
78 
79   // "if iter is NULL, then the number of toplevel nodes is returned."
80   int rows = gtk_tree_model_iter_n_children(model, NULL);
81   GtkTreePath* lastpath = gtk_tree_path_new_from_indices(rows - 1, -1);
82 
83   // Select the last item in the list
84   GtkTreeSelection* selection = gtk_tree_view_get_selection(tree_view);
85   gtk_tree_selection_select_path(selection, lastpath);
86 
87   // Our TreeView only has one column, so it is column 0.
88   GtkTreeViewColumn* column = gtk_tree_view_get_column(tree_view, 0);
89 
90   gtk_tree_view_row_activated(tree_view, lastpath, column);
91 
92   gtk_tree_path_free(lastpath);
93   return false;
94 }
95 
96 // Creates a tree view, that we use to display the list of peers.
InitializeList(GtkWidget * list)97 void InitializeList(GtkWidget* list) {
98   GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
99   GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(
100       "List Items", renderer, "text", 0, NULL);
101   gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
102   GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
103   gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
104   g_object_unref(store);
105 }
106 
107 // Adds an entry to a tree view.
AddToList(GtkWidget * list,const gchar * str,int value)108 void AddToList(GtkWidget* list, const gchar* str, int value) {
109   GtkListStore* store =
110       GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
111 
112   GtkTreeIter iter;
113   gtk_list_store_append(store, &iter);
114   gtk_list_store_set(store, &iter, 0, str, 1, value, -1);
115 }
116 
117 struct UIThreadCallbackData {
UIThreadCallbackData__anon3f069bf30111::UIThreadCallbackData118   explicit UIThreadCallbackData(MainWndCallback* cb, int id, void* d)
119       : callback(cb), msg_id(id), data(d) {}
120   MainWndCallback* callback;
121   int msg_id;
122   void* data;
123 };
124 
HandleUIThreadCallback(gpointer data)125 gboolean HandleUIThreadCallback(gpointer data) {
126   UIThreadCallbackData* cb_data = reinterpret_cast<UIThreadCallbackData*>(data);
127   cb_data->callback->UIThreadCallback(cb_data->msg_id, cb_data->data);
128   delete cb_data;
129   return false;
130 }
131 
Redraw(gpointer data)132 gboolean Redraw(gpointer data) {
133   GtkMainWnd* wnd = reinterpret_cast<GtkMainWnd*>(data);
134   wnd->OnRedraw();
135   return false;
136 }
137 
Draw(GtkWidget * widget,cairo_t * cr,gpointer data)138 gboolean Draw(GtkWidget* widget, cairo_t* cr, gpointer data) {
139   GtkMainWnd* wnd = reinterpret_cast<GtkMainWnd*>(data);
140   wnd->Draw(widget, cr);
141   return false;
142 }
143 
144 }  // namespace
145 
146 //
147 // GtkMainWnd implementation.
148 //
149 
GtkMainWnd(const char * server,int port,bool autoconnect,bool autocall)150 GtkMainWnd::GtkMainWnd(const char* server,
151                        int port,
152                        bool autoconnect,
153                        bool autocall)
154     : window_(NULL),
155       draw_area_(NULL),
156       vbox_(NULL),
157       server_edit_(NULL),
158       port_edit_(NULL),
159       peer_list_(NULL),
160       callback_(NULL),
161       server_(server),
162       autoconnect_(autoconnect),
163       autocall_(autocall) {
164   char buffer[10];
165   snprintf(buffer, sizeof(buffer), "%i", port);
166   port_ = buffer;
167 }
168 
~GtkMainWnd()169 GtkMainWnd::~GtkMainWnd() {
170   RTC_DCHECK(!IsWindow());
171 }
172 
RegisterObserver(MainWndCallback * callback)173 void GtkMainWnd::RegisterObserver(MainWndCallback* callback) {
174   callback_ = callback;
175 }
176 
IsWindow()177 bool GtkMainWnd::IsWindow() {
178   return window_ != NULL && GTK_IS_WINDOW(window_);
179 }
180 
MessageBox(const char * caption,const char * text,bool is_error)181 void GtkMainWnd::MessageBox(const char* caption,
182                             const char* text,
183                             bool is_error) {
184   GtkWidget* dialog = gtk_message_dialog_new(
185       GTK_WINDOW(window_), GTK_DIALOG_DESTROY_WITH_PARENT,
186       is_error ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s",
187       text);
188   gtk_window_set_title(GTK_WINDOW(dialog), caption);
189   gtk_dialog_run(GTK_DIALOG(dialog));
190   gtk_widget_destroy(dialog);
191 }
192 
current_ui()193 MainWindow::UI GtkMainWnd::current_ui() {
194   if (vbox_)
195     return CONNECT_TO_SERVER;
196 
197   if (peer_list_)
198     return LIST_PEERS;
199 
200   return STREAMING;
201 }
202 
StartLocalRenderer(webrtc::VideoTrackInterface * local_video)203 void GtkMainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
204   local_renderer_.reset(new VideoRenderer(this, local_video));
205 }
206 
StopLocalRenderer()207 void GtkMainWnd::StopLocalRenderer() {
208   local_renderer_.reset();
209 }
210 
StartRemoteRenderer(webrtc::VideoTrackInterface * remote_video)211 void GtkMainWnd::StartRemoteRenderer(
212     webrtc::VideoTrackInterface* remote_video) {
213   remote_renderer_.reset(new VideoRenderer(this, remote_video));
214 }
215 
StopRemoteRenderer()216 void GtkMainWnd::StopRemoteRenderer() {
217   remote_renderer_.reset();
218 }
219 
QueueUIThreadCallback(int msg_id,void * data)220 void GtkMainWnd::QueueUIThreadCallback(int msg_id, void* data) {
221   g_idle_add(HandleUIThreadCallback,
222              new UIThreadCallbackData(callback_, msg_id, data));
223 }
224 
Create()225 bool GtkMainWnd::Create() {
226   RTC_DCHECK(window_ == NULL);
227 
228   window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
229   if (window_) {
230     gtk_window_set_position(GTK_WINDOW(window_), GTK_WIN_POS_CENTER);
231     gtk_window_set_default_size(GTK_WINDOW(window_), 640, 480);
232     gtk_window_set_title(GTK_WINDOW(window_), "PeerConnection client");
233     g_signal_connect(G_OBJECT(window_), "delete-event",
234                      G_CALLBACK(&OnDestroyedCallback), this);
235     g_signal_connect(window_, "key-press-event", G_CALLBACK(OnKeyPressCallback),
236                      this);
237 
238     SwitchToConnectUI();
239   }
240 
241   return window_ != NULL;
242 }
243 
Destroy()244 bool GtkMainWnd::Destroy() {
245   if (!IsWindow())
246     return false;
247 
248   gtk_widget_destroy(window_);
249   window_ = NULL;
250 
251   return true;
252 }
253 
SwitchToConnectUI()254 void GtkMainWnd::SwitchToConnectUI() {
255   RTC_LOG(LS_INFO) << __FUNCTION__;
256 
257   RTC_DCHECK(IsWindow());
258   RTC_DCHECK(vbox_ == NULL);
259 
260   gtk_container_set_border_width(GTK_CONTAINER(window_), 10);
261 
262   if (peer_list_) {
263     gtk_widget_destroy(peer_list_);
264     peer_list_ = NULL;
265   }
266 
267   vbox_ = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
268   GtkWidget* valign = gtk_alignment_new(0, 1, 0, 0);
269   gtk_container_add(GTK_CONTAINER(vbox_), valign);
270   gtk_container_add(GTK_CONTAINER(window_), vbox_);
271 
272   GtkWidget* hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
273 
274   GtkWidget* label = gtk_label_new("Server");
275   gtk_container_add(GTK_CONTAINER(hbox), label);
276 
277   server_edit_ = gtk_entry_new();
278   gtk_entry_set_text(GTK_ENTRY(server_edit_), server_.c_str());
279   gtk_widget_set_size_request(server_edit_, 400, 30);
280   gtk_container_add(GTK_CONTAINER(hbox), server_edit_);
281 
282   port_edit_ = gtk_entry_new();
283   gtk_entry_set_text(GTK_ENTRY(port_edit_), port_.c_str());
284   gtk_widget_set_size_request(port_edit_, 70, 30);
285   gtk_container_add(GTK_CONTAINER(hbox), port_edit_);
286 
287   GtkWidget* button = gtk_button_new_with_label("Connect");
288   gtk_widget_set_size_request(button, 70, 30);
289   g_signal_connect(button, "clicked", G_CALLBACK(OnClickedCallback), this);
290   gtk_container_add(GTK_CONTAINER(hbox), button);
291 
292   GtkWidget* halign = gtk_alignment_new(1, 0, 0, 0);
293   gtk_container_add(GTK_CONTAINER(halign), hbox);
294   gtk_box_pack_start(GTK_BOX(vbox_), halign, FALSE, FALSE, 0);
295 
296   gtk_widget_show_all(window_);
297 
298   if (autoconnect_)
299     g_idle_add(SimulateButtonClick, button);
300 }
301 
SwitchToPeerList(const Peers & peers)302 void GtkMainWnd::SwitchToPeerList(const Peers& peers) {
303   RTC_LOG(LS_INFO) << __FUNCTION__;
304 
305   if (!peer_list_) {
306     gtk_container_set_border_width(GTK_CONTAINER(window_), 0);
307     if (vbox_) {
308       gtk_widget_destroy(vbox_);
309       vbox_ = NULL;
310       server_edit_ = NULL;
311       port_edit_ = NULL;
312     } else if (draw_area_) {
313       gtk_widget_destroy(draw_area_);
314       draw_area_ = NULL;
315       draw_buffer_.reset();
316     }
317 
318     peer_list_ = gtk_tree_view_new();
319     g_signal_connect(peer_list_, "row-activated",
320                      G_CALLBACK(OnRowActivatedCallback), this);
321     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(peer_list_), FALSE);
322     InitializeList(peer_list_);
323     gtk_container_add(GTK_CONTAINER(window_), peer_list_);
324     gtk_widget_show_all(window_);
325   } else {
326     GtkListStore* store =
327         GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(peer_list_)));
328     gtk_list_store_clear(store);
329   }
330 
331   AddToList(peer_list_, "List of currently connected peers:", -1);
332   for (Peers::const_iterator i = peers.begin(); i != peers.end(); ++i)
333     AddToList(peer_list_, i->second.c_str(), i->first);
334 
335   if (autocall_ && peers.begin() != peers.end())
336     g_idle_add(SimulateLastRowActivated, peer_list_);
337 }
338 
SwitchToStreamingUI()339 void GtkMainWnd::SwitchToStreamingUI() {
340   RTC_LOG(LS_INFO) << __FUNCTION__;
341 
342   RTC_DCHECK(draw_area_ == NULL);
343 
344   gtk_container_set_border_width(GTK_CONTAINER(window_), 0);
345   if (peer_list_) {
346     gtk_widget_destroy(peer_list_);
347     peer_list_ = NULL;
348   }
349 
350   draw_area_ = gtk_drawing_area_new();
351   gtk_container_add(GTK_CONTAINER(window_), draw_area_);
352   g_signal_connect(G_OBJECT(draw_area_), "draw", G_CALLBACK(&::Draw), this);
353 
354   gtk_widget_show_all(window_);
355 }
356 
OnDestroyed(GtkWidget * widget,GdkEvent * event)357 void GtkMainWnd::OnDestroyed(GtkWidget* widget, GdkEvent* event) {
358   callback_->Close();
359   window_ = NULL;
360   draw_area_ = NULL;
361   vbox_ = NULL;
362   server_edit_ = NULL;
363   port_edit_ = NULL;
364   peer_list_ = NULL;
365 }
366 
OnClicked(GtkWidget * widget)367 void GtkMainWnd::OnClicked(GtkWidget* widget) {
368   // Make the connect button insensitive, so that it cannot be clicked more than
369   // once.  Now that the connection includes auto-retry, it should not be
370   // necessary to click it more than once.
371   gtk_widget_set_sensitive(widget, false);
372   server_ = gtk_entry_get_text(GTK_ENTRY(server_edit_));
373   port_ = gtk_entry_get_text(GTK_ENTRY(port_edit_));
374   int port = port_.length() ? atoi(port_.c_str()) : 0;
375   callback_->StartLogin(server_, port);
376 }
377 
OnKeyPress(GtkWidget * widget,GdkEventKey * key)378 void GtkMainWnd::OnKeyPress(GtkWidget* widget, GdkEventKey* key) {
379   if (key->type == GDK_KEY_PRESS) {
380     switch (key->keyval) {
381       case GDK_KEY_Escape:
382         if (draw_area_) {
383           callback_->DisconnectFromCurrentPeer();
384         } else if (peer_list_) {
385           callback_->DisconnectFromServer();
386         }
387         break;
388 
389       case GDK_KEY_KP_Enter:
390       case GDK_KEY_Return:
391         if (vbox_) {
392           OnClicked(NULL);
393         } else if (peer_list_) {
394           // OnRowActivated will be called automatically when the user
395           // presses enter.
396         }
397         break;
398 
399       default:
400         break;
401     }
402   }
403 }
404 
OnRowActivated(GtkTreeView * tree_view,GtkTreePath * path,GtkTreeViewColumn * column)405 void GtkMainWnd::OnRowActivated(GtkTreeView* tree_view,
406                                 GtkTreePath* path,
407                                 GtkTreeViewColumn* column) {
408   RTC_DCHECK(peer_list_ != NULL);
409   GtkTreeIter iter;
410   GtkTreeModel* model;
411   GtkTreeSelection* selection =
412       gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
413   if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
414     char* text;
415     int id = -1;
416     gtk_tree_model_get(model, &iter, 0, &text, 1, &id, -1);
417     if (id != -1)
418       callback_->ConnectToPeer(id);
419     g_free(text);
420   }
421 }
422 
OnRedraw()423 void GtkMainWnd::OnRedraw() {
424   gdk_threads_enter();
425 
426   VideoRenderer* remote_renderer = remote_renderer_.get();
427   if (remote_renderer && remote_renderer->image() != NULL &&
428       draw_area_ != NULL) {
429     width_ = remote_renderer->width();
430     height_ = remote_renderer->height();
431 
432     if (!draw_buffer_.get()) {
433       draw_buffer_size_ = (width_ * height_ * 4) * 4;
434       draw_buffer_.reset(new uint8_t[draw_buffer_size_]);
435       gtk_widget_set_size_request(draw_area_, width_ * 2, height_ * 2);
436     }
437 
438     const uint32_t* image =
439         reinterpret_cast<const uint32_t*>(remote_renderer->image());
440     uint32_t* scaled = reinterpret_cast<uint32_t*>(draw_buffer_.get());
441     for (int r = 0; r < height_; ++r) {
442       for (int c = 0; c < width_; ++c) {
443         int x = c * 2;
444         scaled[x] = scaled[x + 1] = image[c];
445       }
446 
447       uint32_t* prev_line = scaled;
448       scaled += width_ * 2;
449       memcpy(scaled, prev_line, (width_ * 2) * 4);
450 
451       image += width_;
452       scaled += width_ * 2;
453     }
454 
455     VideoRenderer* local_renderer = local_renderer_.get();
456     if (local_renderer && local_renderer->image()) {
457       image = reinterpret_cast<const uint32_t*>(local_renderer->image());
458       scaled = reinterpret_cast<uint32_t*>(draw_buffer_.get());
459       // Position the local preview on the right side.
460       scaled += (width_ * 2) - (local_renderer->width() / 2);
461       // right margin...
462       scaled -= 10;
463       // ... towards the bottom.
464       scaled += (height_ * width_ * 4) - ((local_renderer->height() / 2) *
465                                           (local_renderer->width() / 2) * 4);
466       // bottom margin...
467       scaled -= (width_ * 2) * 5;
468       for (int r = 0; r < local_renderer->height(); r += 2) {
469         for (int c = 0; c < local_renderer->width(); c += 2) {
470           scaled[c / 2] = image[c + r * local_renderer->width()];
471         }
472         scaled += width_ * 2;
473       }
474     }
475 
476     gtk_widget_queue_draw(draw_area_);
477   }
478 
479   gdk_threads_leave();
480 }
481 
Draw(GtkWidget * widget,cairo_t * cr)482 void GtkMainWnd::Draw(GtkWidget* widget, cairo_t* cr) {
483   cairo_format_t format = CAIRO_FORMAT_ARGB32;
484   cairo_surface_t* surface = cairo_image_surface_create_for_data(
485       draw_buffer_.get(), format, width_ * 2, height_ * 2,
486       cairo_format_stride_for_width(format, width_ * 2));
487   cairo_set_source_surface(cr, surface, 0, 0);
488   cairo_rectangle(cr, 0, 0, width_ * 2, height_ * 2);
489   cairo_fill(cr);
490   cairo_surface_destroy(surface);
491 }
492 
VideoRenderer(GtkMainWnd * main_wnd,webrtc::VideoTrackInterface * track_to_render)493 GtkMainWnd::VideoRenderer::VideoRenderer(
494     GtkMainWnd* main_wnd,
495     webrtc::VideoTrackInterface* track_to_render)
496     : width_(0),
497       height_(0),
498       main_wnd_(main_wnd),
499       rendered_track_(track_to_render) {
500   rendered_track_->AddOrUpdateSink(this, rtc::VideoSinkWants());
501 }
502 
~VideoRenderer()503 GtkMainWnd::VideoRenderer::~VideoRenderer() {
504   rendered_track_->RemoveSink(this);
505 }
506 
SetSize(int width,int height)507 void GtkMainWnd::VideoRenderer::SetSize(int width, int height) {
508   gdk_threads_enter();
509 
510   if (width_ == width && height_ == height) {
511     return;
512   }
513 
514   width_ = width;
515   height_ = height;
516   image_.reset(new uint8_t[width * height * 4]);
517   gdk_threads_leave();
518 }
519 
OnFrame(const webrtc::VideoFrame & video_frame)520 void GtkMainWnd::VideoRenderer::OnFrame(const webrtc::VideoFrame& video_frame) {
521   gdk_threads_enter();
522 
523   rtc::scoped_refptr<webrtc::I420BufferInterface> buffer(
524       video_frame.video_frame_buffer()->ToI420());
525   if (video_frame.rotation() != webrtc::kVideoRotation_0) {
526     buffer = webrtc::I420Buffer::Rotate(*buffer, video_frame.rotation());
527   }
528   SetSize(buffer->width(), buffer->height());
529 
530   // TODO(bugs.webrtc.org/6857): This conversion is correct for little-endian
531   // only. Cairo ARGB32 treats pixels as 32-bit values in *native* byte order,
532   // with B in the least significant byte of the 32-bit value. Which on
533   // little-endian means that memory layout is BGRA, with the B byte stored at
534   // lowest address. Libyuv's ARGB format (surprisingly?) uses the same
535   // little-endian format, with B in the first byte in memory, regardless of
536   // native endianness.
537   libyuv::I420ToARGB(buffer->DataY(), buffer->StrideY(), buffer->DataU(),
538                      buffer->StrideU(), buffer->DataV(), buffer->StrideV(),
539                      image_.get(), width_ * 4, buffer->width(),
540                      buffer->height());
541 
542   gdk_threads_leave();
543 
544   g_idle_add(Redraw, main_wnd_);
545 }
546