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