Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Side by Side Diff: talk/examples/peerconnection/client/linux/main_wnd.cc

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

Powered by Google App Engine
This is Rietveld 408576698