1 /* 2 * libjingle 3 * Copyright 2012, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 29 #include "talk/examples/peerconnection/client/linux/main_wnd.h" 30 31 #include <gdk/gdkkeysyms.h> 32 #include <gtk/gtk.h> 33 #include <stddef.h> 34 35 #include "talk/examples/peerconnection/client/defaults.h" 36 #include "talk/base/common.h" 37 #include "talk/base/logging.h" 38 #include "talk/base/stringutils.h" 39 40 using talk_base::sprintfn; 41 42 namespace { 43 44 // 45 // Simple static functions that simply forward the callback to the 46 // GtkMainWnd instance. 47 // 48 49 gboolean OnDestroyedCallback(GtkWidget* widget, GdkEvent* event, 50 gpointer data) { 51 reinterpret_cast<GtkMainWnd*>(data)->OnDestroyed(widget, event); 52 return FALSE; 53 } 54 55 void OnClickedCallback(GtkWidget* widget, gpointer data) { 56 reinterpret_cast<GtkMainWnd*>(data)->OnClicked(widget); 57 } 58 59 gboolean SimulateButtonClick(gpointer button) { 60 g_signal_emit_by_name(button, "clicked"); 61 return false; 62 } 63 64 gboolean OnKeyPressCallback(GtkWidget* widget, GdkEventKey* key, 65 gpointer data) { 66 reinterpret_cast<GtkMainWnd*>(data)->OnKeyPress(widget, key); 67 return false; 68 } 69 70 void OnRowActivatedCallback(GtkTreeView* tree_view, GtkTreePath* path, 71 GtkTreeViewColumn* column, gpointer data) { 72 reinterpret_cast<GtkMainWnd*>(data)->OnRowActivated(tree_view, path, column); 73 } 74 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. 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. 108 void AddToList(GtkWidget* list, const gchar* str, int value) { 109 GtkListStore* store = GTK_LIST_STORE( 110 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 { 118 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 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 132 gboolean Redraw(gpointer data) { 133 GtkMainWnd* wnd = reinterpret_cast<GtkMainWnd*>(data); 134 wnd->OnRedraw(); 135 return false; 136 } 137 } // end anonymous 138 139 // 140 // GtkMainWnd implementation. 141 // 142 143 GtkMainWnd::GtkMainWnd(const char* server, int port, bool autoconnect, 144 bool autocall) 145 : window_(NULL), draw_area_(NULL), vbox_(NULL), server_edit_(NULL), 146 port_edit_(NULL), peer_list_(NULL), callback_(NULL), 147 server_(server), autoconnect_(autoconnect), autocall_(autocall) { 148 char buffer[10]; 149 sprintfn(buffer, sizeof(buffer), "%i", port); 150 port_ = buffer; 151 } 152 153 GtkMainWnd::~GtkMainWnd() { 154 ASSERT(!IsWindow()); 155 } 156 157 void GtkMainWnd::RegisterObserver(MainWndCallback* callback) { 158 callback_ = callback; 159 } 160 161 bool GtkMainWnd::IsWindow() { 162 return window_ != NULL && GTK_IS_WINDOW(window_); 163 } 164 165 void GtkMainWnd::MessageBox(const char* caption, const char* text, 166 bool is_error) { 167 GtkWidget* dialog = gtk_message_dialog_new(GTK_WINDOW(window_), 168 GTK_DIALOG_DESTROY_WITH_PARENT, 169 is_error ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, 170 GTK_BUTTONS_CLOSE, "%s", text); 171 gtk_window_set_title(GTK_WINDOW(dialog), caption); 172 gtk_dialog_run(GTK_DIALOG(dialog)); 173 gtk_widget_destroy(dialog); 174 } 175 176 MainWindow::UI GtkMainWnd::current_ui() { 177 if (vbox_) 178 return CONNECT_TO_SERVER; 179 180 if (peer_list_) 181 return LIST_PEERS; 182 183 return STREAMING; 184 } 185 186 187 void GtkMainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) { 188 local_renderer_.reset(new VideoRenderer(this, local_video)); 189 } 190 191 void GtkMainWnd::StopLocalRenderer() { 192 local_renderer_.reset(); 193 } 194 195 void GtkMainWnd::StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) { 196 remote_renderer_.reset(new VideoRenderer(this, remote_video)); 197 } 198 199 void GtkMainWnd::StopRemoteRenderer() { 200 remote_renderer_.reset(); 201 } 202 203 void GtkMainWnd::QueueUIThreadCallback(int msg_id, void* data) { 204 g_idle_add(HandleUIThreadCallback, 205 new UIThreadCallbackData(callback_, msg_id, data)); 206 } 207 208 bool GtkMainWnd::Create() { 209 ASSERT(window_ == NULL); 210 211 window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); 212 if (window_) { 213 gtk_window_set_position(GTK_WINDOW(window_), GTK_WIN_POS_CENTER); 214 gtk_window_set_default_size(GTK_WINDOW(window_), 640, 480); 215 gtk_window_set_title(GTK_WINDOW(window_), "PeerConnection client"); 216 g_signal_connect(G_OBJECT(window_), "delete-event", 217 G_CALLBACK(&OnDestroyedCallback), this); 218 g_signal_connect(window_, "key-press-event", G_CALLBACK(OnKeyPressCallback), 219 this); 220 221 SwitchToConnectUI(); 222 } 223 224 return window_ != NULL; 225 } 226 227 bool GtkMainWnd::Destroy() { 228 if (!IsWindow()) 229 return false; 230 231 gtk_widget_destroy(window_); 232 window_ = NULL; 233 234 return true; 235 } 236 237 void GtkMainWnd::SwitchToConnectUI() { 238 LOG(INFO) << __FUNCTION__; 239 240 ASSERT(IsWindow()); 241 ASSERT(vbox_ == NULL); 242 243 gtk_container_set_border_width(GTK_CONTAINER(window_), 10); 244 245 if (peer_list_) { 246 gtk_widget_destroy(peer_list_); 247 peer_list_ = NULL; 248 } 249 250 vbox_ = gtk_vbox_new(FALSE, 5); 251 GtkWidget* valign = gtk_alignment_new(0, 1, 0, 0); 252 gtk_container_add(GTK_CONTAINER(vbox_), valign); 253 gtk_container_add(GTK_CONTAINER(window_), vbox_); 254 255 GtkWidget* hbox = gtk_hbox_new(FALSE, 5); 256 257 GtkWidget* label = gtk_label_new("Server"); 258 gtk_container_add(GTK_CONTAINER(hbox), label); 259 260 server_edit_ = gtk_entry_new(); 261 gtk_entry_set_text(GTK_ENTRY(server_edit_), server_.c_str()); 262 gtk_widget_set_size_request(server_edit_, 400, 30); 263 gtk_container_add(GTK_CONTAINER(hbox), server_edit_); 264 265 port_edit_ = gtk_entry_new(); 266 gtk_entry_set_text(GTK_ENTRY(port_edit_), port_.c_str()); 267 gtk_widget_set_size_request(port_edit_, 70, 30); 268 gtk_container_add(GTK_CONTAINER(hbox), port_edit_); 269 270 GtkWidget* button = gtk_button_new_with_label("Connect"); 271 gtk_widget_set_size_request(button, 70, 30); 272 g_signal_connect(button, "clicked", G_CALLBACK(OnClickedCallback), this); 273 gtk_container_add(GTK_CONTAINER(hbox), button); 274 275 GtkWidget* halign = gtk_alignment_new(1, 0, 0, 0); 276 gtk_container_add(GTK_CONTAINER(halign), hbox); 277 gtk_box_pack_start(GTK_BOX(vbox_), halign, FALSE, FALSE, 0); 278 279 gtk_widget_show_all(window_); 280 281 if (autoconnect_) 282 g_idle_add(SimulateButtonClick, button); 283 } 284 285 void GtkMainWnd::SwitchToPeerList(const Peers& peers) { 286 LOG(INFO) << __FUNCTION__; 287 288 if (!peer_list_) { 289 gtk_container_set_border_width(GTK_CONTAINER(window_), 0); 290 if (vbox_) { 291 gtk_widget_destroy(vbox_); 292 vbox_ = NULL; 293 server_edit_ = NULL; 294 port_edit_ = NULL; 295 } else if (draw_area_) { 296 gtk_widget_destroy(draw_area_); 297 draw_area_ = NULL; 298 draw_buffer_.reset(); 299 } 300 301 peer_list_ = gtk_tree_view_new(); 302 g_signal_connect(peer_list_, "row-activated", 303 G_CALLBACK(OnRowActivatedCallback), this); 304 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(peer_list_), FALSE); 305 InitializeList(peer_list_); 306 gtk_container_add(GTK_CONTAINER(window_), peer_list_); 307 gtk_widget_show_all(window_); 308 } else { 309 GtkListStore* store = 310 GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(peer_list_))); 311 gtk_list_store_clear(store); 312 } 313 314 AddToList(peer_list_, "List of currently connected peers:", -1); 315 for (Peers::const_iterator i = peers.begin(); i != peers.end(); ++i) 316 AddToList(peer_list_, i->second.c_str(), i->first); 317 318 if (autocall_ && peers.begin() != peers.end()) 319 g_idle_add(SimulateLastRowActivated, peer_list_); 320 } 321 322 void GtkMainWnd::SwitchToStreamingUI() { 323 LOG(INFO) << __FUNCTION__; 324 325 ASSERT(draw_area_ == NULL); 326 327 gtk_container_set_border_width(GTK_CONTAINER(window_), 0); 328 if (peer_list_) { 329 gtk_widget_destroy(peer_list_); 330 peer_list_ = NULL; 331 } 332 333 draw_area_ = gtk_drawing_area_new(); 334 gtk_container_add(GTK_CONTAINER(window_), draw_area_); 335 336 gtk_widget_show_all(window_); 337 } 338 339 void GtkMainWnd::OnDestroyed(GtkWidget* widget, GdkEvent* event) { 340 callback_->Close(); 341 window_ = NULL; 342 draw_area_ = NULL; 343 vbox_ = NULL; 344 server_edit_ = NULL; 345 port_edit_ = NULL; 346 peer_list_ = NULL; 347 } 348 349 void GtkMainWnd::OnClicked(GtkWidget* widget) { 350 // Make the connect button insensitive, so that it cannot be clicked more than 351 // once. Now that the connection includes auto-retry, it should not be 352 // necessary to click it more than once. 353 gtk_widget_set_sensitive(widget, false); 354 server_ = gtk_entry_get_text(GTK_ENTRY(server_edit_)); 355 port_ = gtk_entry_get_text(GTK_ENTRY(port_edit_)); 356 int port = port_.length() ? atoi(port_.c_str()) : 0; 357 callback_->StartLogin(server_, port); 358 } 359 360 void GtkMainWnd::OnKeyPress(GtkWidget* widget, GdkEventKey* key) { 361 if (key->type == GDK_KEY_PRESS) { 362 switch (key->keyval) { 363 case GDK_Escape: 364 if (draw_area_) { 365 callback_->DisconnectFromCurrentPeer(); 366 } else if (peer_list_) { 367 callback_->DisconnectFromServer(); 368 } 369 break; 370 371 case GDK_KP_Enter: 372 case GDK_Return: 373 if (vbox_) { 374 OnClicked(NULL); 375 } else if (peer_list_) { 376 // OnRowActivated will be called automatically when the user 377 // presses enter. 378 } 379 break; 380 381 default: 382 break; 383 } 384 } 385 } 386 387 void GtkMainWnd::OnRowActivated(GtkTreeView* tree_view, GtkTreePath* path, 388 GtkTreeViewColumn* column) { 389 ASSERT(peer_list_ != NULL); 390 GtkTreeIter iter; 391 GtkTreeModel* model; 392 GtkTreeSelection* selection = 393 gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)); 394 if (gtk_tree_selection_get_selected(selection, &model, &iter)) { 395 char* text; 396 int id = -1; 397 gtk_tree_model_get(model, &iter, 0, &text, 1, &id, -1); 398 if (id != -1) 399 callback_->ConnectToPeer(id); 400 g_free(text); 401 } 402 } 403 404 void GtkMainWnd::OnRedraw() { 405 gdk_threads_enter(); 406 407 VideoRenderer* remote_renderer = remote_renderer_.get(); 408 if (remote_renderer && remote_renderer->image() != NULL && 409 draw_area_ != NULL) { 410 int width = remote_renderer->width(); 411 int height = remote_renderer->height(); 412 413 if (!draw_buffer_.get()) { 414 draw_buffer_size_ = (width * height * 4) * 4; 415 draw_buffer_.reset(new uint8[draw_buffer_size_]); 416 gtk_widget_set_size_request(draw_area_, width * 2, height * 2); 417 } 418 419 const uint32* image = reinterpret_cast<const uint32*>( 420 remote_renderer->image()); 421 uint32* scaled = reinterpret_cast<uint32*>(draw_buffer_.get()); 422 for (int r = 0; r < height; ++r) { 423 for (int c = 0; c < width; ++c) { 424 int x = c * 2; 425 scaled[x] = scaled[x + 1] = image[c]; 426 } 427 428 uint32* prev_line = scaled; 429 scaled += width * 2; 430 memcpy(scaled, prev_line, (width * 2) * 4); 431 432 image += width; 433 scaled += width * 2; 434 } 435 436 VideoRenderer* local_renderer = local_renderer_.get(); 437 if (local_renderer && local_renderer->image()) { 438 image = reinterpret_cast<const uint32*>(local_renderer->image()); 439 scaled = reinterpret_cast<uint32*>(draw_buffer_.get()); 440 // Position the local preview on the right side. 441 scaled += (width * 2) - (local_renderer->width() / 2); 442 // right margin... 443 scaled -= 10; 444 // ... towards the bottom. 445 scaled += (height * width * 4) - 446 ((local_renderer->height() / 2) * 447 (local_renderer->width() / 2) * 4); 448 // bottom margin... 449 scaled -= (width * 2) * 5; 450 for (int r = 0; r < local_renderer->height(); r += 2) { 451 for (int c = 0; c < local_renderer->width(); c += 2) { 452 scaled[c / 2] = image[c + r * local_renderer->width()]; 453 } 454 scaled += width * 2; 455 } 456 } 457 458 gdk_draw_rgb_32_image(draw_area_->window, 459 draw_area_->style->fg_gc[GTK_STATE_NORMAL], 460 0, 461 0, 462 width * 2, 463 height * 2, 464 GDK_RGB_DITHER_MAX, 465 draw_buffer_.get(), 466 (width * 2) * 4); 467 } 468 469 gdk_threads_leave(); 470 } 471 472 GtkMainWnd::VideoRenderer::VideoRenderer( 473 GtkMainWnd* main_wnd, 474 webrtc::VideoTrackInterface* track_to_render) 475 : width_(0), 476 height_(0), 477 main_wnd_(main_wnd), 478 rendered_track_(track_to_render) { 479 rendered_track_->AddRenderer(this); 480 } 481 482 GtkMainWnd::VideoRenderer::~VideoRenderer() { 483 rendered_track_->RemoveRenderer(this); 484 } 485 486 void GtkMainWnd::VideoRenderer::SetSize(int width, int height) { 487 gdk_threads_enter(); 488 width_ = width; 489 height_ = height; 490 image_.reset(new uint8[width * height * 4]); 491 gdk_threads_leave(); 492 } 493 494 void GtkMainWnd::VideoRenderer::RenderFrame(const cricket::VideoFrame* frame) { 495 gdk_threads_enter(); 496 497 int size = width_ * height_ * 4; 498 // TODO: Convert directly to RGBA 499 frame->ConvertToRgbBuffer(cricket::FOURCC_ARGB, 500 image_.get(), 501 size, 502 width_ * 4); 503 // Convert the B,G,R,A frame to R,G,B,A, which is accepted by GTK. 504 // The 'A' is just padding for GTK, so we can use it as temp. 505 uint8* pix = image_.get(); 506 uint8* end = image_.get() + size; 507 while (pix < end) { 508 pix[3] = pix[0]; // Save B to A. 509 pix[0] = pix[2]; // Set Red. 510 pix[2] = pix[3]; // Set Blue. 511 pix[3] = 0xFF; // Fixed Alpha. 512 pix += 4; 513 } 514 515 gdk_threads_leave(); 516 517 g_idle_add(Redraw, main_wnd_); 518 } 519 520 521