mediasettingsview.cpp 22.8 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4
 *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

21
#include "mediasettingsview.h"
22 23

#include <gtk/gtk.h>
24 25 26 27 28 29
#include <string>

// LRC
#include <api/avmodel.h>
#include <api/newvideo.h>

30
#include "video/video_widget.h"
31 32 33 34 35

namespace { namespace details
{
class CppImpl;
}}
36

37
struct _MediaSettingsView
38
{
39
    GtkScrolledWindow parent;
40 41
};

42
struct _MediaSettingsViewClass
43
{
44
    GtkScrolledWindowClass parent_class;
45 46
};

47
typedef struct _MediaSettingsViewPrivate MediaSettingsViewPrivate;
48

49
struct _MediaSettingsViewPrivate
50
{
51 52
    GtkWidget *vbox_main;

53 54 55 56 57
    /* audio settings */
    GtkWidget *combobox_manager;
    GtkWidget *combobox_ringtone;
    GtkWidget *combobox_output;
    GtkWidget *combobox_input;
58
    GtkWidget *levelbar_input;
59 60

    /* camera settings */
61 62 63 64 65 66
    GtkWidget *combobox_device;
    GtkWidget *combobox_channel;
    GtkWidget *combobox_resolution;
    GtkWidget *combobox_framerate;
    GtkWidget *video_widget;

67 68 69 70 71 72
    /* this is used to keep track of the state of the preview when the settings
     * are opened; if a call is in progress, then the preview should already be
     * started and we don't want to stop it when the settings are closed, in this
     * case */
    gboolean video_started_by_settings;

73
    QMetaObject::Connection local_renderer_connection;
74
    QMetaObject::Connection device_event_connection;
75
    QMetaObject::Connection audio_meter_connection;
76 77

    /* hardware accel settings */
78
    GtkWidget *checkbutton_hardware_acceleration;
79 80

    details::CppImpl* cpp; ///< Non-UI and C++ only code
81 82
};

83
G_DEFINE_TYPE_WITH_PRIVATE(MediaSettingsView, media_settings_view, GTK_TYPE_SCROLLED_WINDOW);
84

85
#define MEDIA_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MEDIA_SETTINGS_VIEW_TYPE, MediaSettingsViewPrivate))
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
namespace { namespace details
{

class CppImpl
{
public:
    explicit CppImpl(MediaSettingsView& widget, lrc::api::AVModel& avModel);

    void drawAudioDevices();
    void drawFramerates();
    void drawResolutions();
    void drawChannels();
    void drawVideoDevices();

    lrc::api::AVModel* avModel_ = nullptr;
    MediaSettingsView* self = nullptr; // The GTK widget itself
    MediaSettingsViewPrivate* widgets = nullptr;
};

CppImpl::CppImpl(MediaSettingsView& widget, lrc::api::AVModel& avModel)
: self {&widget}
, widgets {MEDIA_SETTINGS_VIEW_GET_PRIVATE(&widget)}
, avModel_(&avModel)
{
    gtk_toggle_button_set_active(
112 113
        GTK_TOGGLE_BUTTON(widgets->checkbutton_hardware_acceleration),
        avModel_->getHardwareAcceleration());
114 115 116 117 118 119 120 121 122 123 124 125 126 127

    auto activeIdx = 0;
    auto currentManager = avModel_->getAudioManager();
    auto i = 0;
    for (const auto& manager : avModel_->getSupportedAudioManagers()) {
        if (manager == currentManager) {
            activeIdx = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_manager), nullptr, manager.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_manager), activeIdx);
    drawAudioDevices();
    drawVideoDevices();
128 129

    gtk_level_bar_set_value(GTK_LEVEL_BAR(widgets->levelbar_input), 0.0);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

void
CppImpl::drawAudioDevices()
{
    if (!avModel_) {
        g_warning("AVModel not initialized yet");
        return;
    }
    auto activeOutput = 0, activeRingtone = 0;
    auto currentOutput = avModel_->getOutputDevice();
    auto currentRingtone = avModel_->getRingtoneDevice();
    auto i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_ringtone));
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_output));
    for (const auto& output : avModel_->getAudioOutputDevices()) {
        if (output == currentOutput) {
            activeOutput = i;
        }
        if (output == currentRingtone) {
            activeRingtone = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_ringtone), nullptr, output.c_str());
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_output), nullptr, output.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_ringtone), activeRingtone);
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_output), activeOutput);

    auto activeInput = 0;
    auto currentInput = avModel_->getInputDevice();
    i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_input));
    for (const auto& input : avModel_->getAudioInputDevices()) {
        if (input == currentInput) {
            activeInput = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_input), nullptr, input.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_input), activeInput);
}

void
CppImpl::drawFramerates()
{
    if (!avModel_) {
        g_warning("AVModel not initialized yet");
        return;
    }
180
    using namespace lrc::api;
181
    auto active = 0;
182
    auto currentDevice = avModel_->getDefaultDevice();
183
    auto deviceCaps = avModel_->getDeviceCapabilities(currentDevice);
184
    std::string currentChannel = "", currentRes = "", currentRate = "";
185
    int currentResIndex;
186
    try {
187 188 189
        auto deviceSettings = avModel_->getDeviceSettings(currentDevice);
        currentChannel = deviceSettings.channel;
        currentRes = deviceSettings.size;
190
        currentRate = std::to_string(static_cast<uint8_t>(deviceSettings.rate));
191
        if (deviceCaps.find(currentChannel) == deviceCaps.end()) return;
192 193 194 195 196 197 198 199 200
        auto resRates = deviceCaps.at(currentChannel);
        auto it = std::find_if(resRates.begin(), resRates.end(),
            [&currentRes](const std::pair<video::Resolution, video::FrameratesList>& element) {
                return element.first == currentRes;
            });
        if (it == resRates.end()) {
            throw std::out_of_range("Can't find resolution");
        }
        currentResIndex = std::distance(resRates.begin(), it);
201 202 203 204
    } catch (const std::out_of_range&) {
        g_warning("drawFramerates out_of_range exception");
        return;
    }
205 206 207
    if (deviceCaps.find(currentChannel) == deviceCaps.end()
        || deviceCaps.at(currentChannel).size() < currentResIndex)
        return;
208
    auto rates = deviceCaps.at(currentChannel).at(currentResIndex).second;
209 210 211
    auto i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_framerate));
    for (const auto& rate : rates) {
212
        auto rateStr = std::to_string(static_cast<uint8_t>(rate));
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        if (rateStr == currentRate) {
            active = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_framerate), nullptr, rateStr.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_framerate), active);
}

void
CppImpl::drawResolutions()
{
    if (!avModel_) {
        g_warning("AVModel not initialized yet");
        return;
    }
    auto active = 0;
230
    auto currentDevice = avModel_->getDefaultDevice();
231 232 233 234 235 236 237 238
    std::string currentChannel = "", currentRes = "";
    try {
        currentChannel = avModel_->getDeviceSettings(currentDevice).channel;
        currentRes = avModel_->getDeviceSettings(currentDevice).size;
    } catch (const std::out_of_range&) {
        g_warning("drawResolutions out_of_range exception");
        return;
    }
239 240
    auto capabilities = avModel_->getDeviceCapabilities(currentDevice);
    if (capabilities.find(currentChannel) == capabilities.end()) return;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    auto resToRates = avModel_->getDeviceCapabilities(currentDevice).at(currentChannel);
    auto i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_resolution));
    for (const auto& item : resToRates) {
        if (item.first == currentRes) {
            active = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_resolution), nullptr, item.first.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_resolution), active);
    drawFramerates();
}

void
CppImpl::drawChannels()
{
    if (!avModel_) {
        g_warning("AVModel not initialized yet");
        return;
    }
    auto active = 0;
263
    auto currentDevice = avModel_->getDefaultDevice();
264 265 266 267 268 269 270
    std::string currentChannel = "";
    try {
        currentChannel = avModel_->getDeviceSettings(currentDevice).channel;
    } catch (const std::out_of_range&) {
        g_warning("drawChannels out_of_range exception");
        return;
    }
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    auto i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_channel));
    for (const auto& capabilites : avModel_->getDeviceCapabilities(currentDevice)) {
        if (capabilites.first == currentChannel) {
            active = i;
        }
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_channel), nullptr, capabilites.first.c_str());
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_channel), active);
    drawResolutions();
}

void
CppImpl::drawVideoDevices()
{
    if (!avModel_) {
        g_warning("AVModel not initialized yet");
        return;
    }
    auto active = 0;
292
    auto current = avModel_->getDefaultDevice();
293 294 295 296
    if (current == "") {
        // Avoid to draw devices if no camera is selected
        return;
    }
297 298 299 300 301 302
    auto i = 0;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->combobox_device));
    for (const auto& device : avModel_->getDevices()) {
        if (device == current) {
            active = i;
        }
303 304 305 306
        try {
            auto name = avModel_->getDeviceSettings(device).name;
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->combobox_device), nullptr, name.c_str());
        } catch (...) {}
307 308 309 310 311 312 313 314 315 316
        i++;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_device), active);
    drawChannels();
}

}} // namespace details



317
static void
318
media_settings_view_dispose(GObject *object)
319
{
320 321
    MediaSettingsView *view = MEDIA_SETTINGS_VIEW(object);
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(view);
322

323 324
    /* make sure to stop the preview if this view is getting destroyed */
    if (priv->video_started_by_settings) {
325
        priv->cpp->avModel_->stopPreview();
326 327 328
        priv->video_started_by_settings = FALSE;
    }

329
    QObject::disconnect(priv->local_renderer_connection);
330
    QObject::disconnect(priv->device_event_connection);
331
    QObject::disconnect(priv->audio_meter_connection);
332

333
    G_OBJECT_CLASS(media_settings_view_parent_class)->dispose(object);
334 335
}

336
static void
337
hardware_acceleration_toggled(GtkToggleButton *toggle_button, MediaSettingsView *self)
338
{
339 340
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
341 342
    gboolean hardware_acceleration = gtk_toggle_button_get_active(toggle_button);
    priv->cpp->avModel_->setHardwareAcceleration(hardware_acceleration);
343 344 345 346 347 348 349 350 351 352 353
}

static void
set_audio_manager(MediaSettingsView* self)
{
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* audio_manager = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_manager));
    if (audio_manager) {
        priv->cpp->avModel_->setAudioManager(audio_manager);
        priv->cpp->drawAudioDevices();
354 355 356 357
    }
}

static void
358
set_ringtone_device(MediaSettingsView* self)
359
{
360 361 362 363 364 365
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* ringtone_device = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_ringtone));
    if (ringtone_device) {
        priv->cpp->avModel_->setRingtoneDevice(ringtone_device);
    }
366 367
}

368 369
static void
set_output_device(MediaSettingsView* self)
370
{
371 372 373 374 375
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* output_device = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_output));
    if (output_device) {
        priv->cpp->avModel_->setOutputDevice(output_device);
376
    }
377
}
378

379 380 381 382 383 384 385 386 387
static void
set_input_device(MediaSettingsView* self)
{
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* input_device = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_input));
    if (input_device) {
        priv->cpp->avModel_->setInputDevice(input_device);
    }
388 389
}

390
static void
391
set_video_device(MediaSettingsView* self)
392
{
393 394
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
395 396
    auto* device_name = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_device));
    if (device_name) {
397 398
        auto device_id = priv->cpp->avModel_->getDeviceIdFromName(device_name);
        if (device_id.empty()) {
399 400 401 402 403 404
            g_warning("set_video_device couldn't find device: %s", device_name);
            return;
        }
        auto currentDevice = priv->cpp->avModel_->getDefaultDevice();
        if (currentDevice == device_id) return;
        priv->cpp->avModel_->setDefaultDevice(device_id);
405 406 407 408
        try {
            auto settings = priv->cpp->avModel_->getDeviceSettings(currentDevice);
            priv->cpp->avModel_->setDeviceSettings(settings);
        } catch (const std::out_of_range&) {
409
            g_warning("set_video_device out_of_range exception");
410
        }
411
        priv->cpp->drawVideoDevices();
412
    }
413 414
}

415
static void
416
set_channel(MediaSettingsView* self)
417
{
418 419 420 421
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* video_channel = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_channel));
    if (video_channel) {
422
        auto currentDevice = priv->cpp->avModel_->getDefaultDevice();
423 424
        try {
            auto settings = priv->cpp->avModel_->getDeviceSettings(currentDevice);
425
            if (settings.channel == video_channel) return;
426 427 428 429 430 431
            settings.channel = video_channel;
            priv->cpp->avModel_->setDeviceSettings(settings);
        } catch (const std::out_of_range&) {
            g_warning("set_channel out_of_range exception");
            return;
        }
432 433 434
        priv->cpp->drawChannels();
    }
}
435

436 437 438 439 440 441 442
static void
set_resolution(MediaSettingsView* self)
{
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* video_resolution = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_resolution));
    if (video_resolution) {
443
        auto currentDevice = priv->cpp->avModel_->getDefaultDevice();
444 445
        try {
            auto settings = priv->cpp->avModel_->getDeviceSettings(currentDevice);
446
            if (settings.size == video_resolution) return;
447 448 449 450 451 452
            settings.size = video_resolution;
            priv->cpp->avModel_->setDeviceSettings(settings);
        } catch (const std::out_of_range&) {
            g_warning("set_resolution out_of_range exception");
            return;
        }
453 454 455
        priv->cpp->drawFramerates();
    }
}
456

457 458 459 460 461 462 463
static void
set_framerate(MediaSettingsView* self)
{
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* video_framerate = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(priv->combobox_framerate));
    if (video_framerate) {
464
        auto currentDevice = priv->cpp->avModel_->getDefaultDevice();
465
        try {
466
            auto settings = priv->cpp->avModel_->getDeviceSettings(currentDevice);
467
            if (settings.rate == std::stoi(video_framerate)) return;
468
            settings.rate = std::stoi(video_framerate);
469
            priv->cpp->avModel_->setDeviceSettings(settings);
470 471 472 473 474
        } catch (...) {
            g_debug("Cannot convert framerate.");
        }
    }
}
475

476 477 478 479
static void
media_settings_view_init(MediaSettingsView *view)
{
    gtk_widget_init_template(GTK_WIDGET(view));
480 481 482
}

static void
483
media_settings_view_class_init(MediaSettingsViewClass *klass)
484
{
485
    G_OBJECT_CLASS(klass)->dispose = media_settings_view_dispose;
486 487

    gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
488
                                                "/net/jami/JamiGnome/mediasettingsview.ui");
489

490
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, vbox_main);
491 492 493 494 495 496 497 498
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_manager);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_ringtone);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_output);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_input);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_device);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_channel);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_resolution);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, combobox_framerate);
499
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, checkbutton_hardware_acceleration);
500
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), MediaSettingsView, levelbar_input);
501 502 503
}

GtkWidget *
504
media_settings_view_new(lrc::api::AVModel& avModel)
505
{
506 507 508 509 510
    auto self = g_object_new(MEDIA_SETTINGS_VIEW_TYPE, NULL);
    auto* priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
    priv->cpp = new details::CppImpl(
        *(reinterpret_cast<MediaSettingsView*>(self)), avModel
    );
511

512
    // CppImpl ctor
513 514
    g_signal_connect(priv->checkbutton_hardware_acceleration, "toggled",
        G_CALLBACK(hardware_acceleration_toggled), self);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    g_signal_connect_swapped(priv->combobox_manager, "changed",
        G_CALLBACK(set_audio_manager), self);
    g_signal_connect_swapped(priv->combobox_ringtone, "changed",
        G_CALLBACK(set_ringtone_device), self);
    g_signal_connect_swapped(priv->combobox_output, "changed",
        G_CALLBACK(set_output_device), self);
    g_signal_connect_swapped(priv->combobox_input, "changed",
        G_CALLBACK(set_input_device), self);
    g_signal_connect_swapped(priv->combobox_device, "changed",
        G_CALLBACK(set_video_device), self);
    g_signal_connect_swapped(priv->combobox_channel, "changed",
        G_CALLBACK(set_channel), self);
    g_signal_connect_swapped(priv->combobox_resolution, "changed",
        G_CALLBACK(set_resolution), self);
    g_signal_connect_swapped(priv->combobox_framerate, "changed",
        G_CALLBACK(set_framerate), self);
531 532 533 534 535 536 537
    priv->audio_meter_connection = QObject::connect(
        &*priv->cpp->avModel_,
        &lrc::api::AVModel::audioMeter,
        [=](const std::string& id, float level) {
            if (id == "audiolayer_id")
                gtk_level_bar_set_value(GTK_LEVEL_BAR(priv->levelbar_input), level);
        });
538
    return (GtkWidget *)self;
539
}
540 541

void
542
media_settings_view_show_preview(MediaSettingsView *self, gboolean show_preview)
543
{
544 545
    g_return_if_fail(IS_MEDIA_SETTINGS_VIEW(self));
    MediaSettingsViewPrivate *priv = MEDIA_SETTINGS_VIEW_GET_PRIVATE(self);
546 547 548 549 550 551 552 553 554 555 556

    /* if TRUE, create a VideoWidget, then check if the preview has already been
     * started (because a call was in progress); if not, then start it.
     * if FALSE, check if the preview was started by this function, if so
     * then stop the preview; then destroy the VideoWidget to make sure we don't
     * get useless frame updates */

    if (show_preview) {
        /* put video widget in */
        priv->video_widget = video_widget_new();
        gtk_widget_show_all(priv->video_widget);
557 558
        gtk_box_pack_start(GTK_BOX(priv->vbox_main), priv->video_widget, TRUE, TRUE, 0);
        // set minimum size for video so it doesn't shrink too much
559
        gtk_widget_set_size_request(priv->video_widget, 400, -1);
560

561 562 563 564 565 566 567 568 569 570
        try {
            const lrc::api::video::Renderer* previewRenderer =
                &priv->cpp->avModel_->getRenderer(
                lrc::api::video::PREVIEW_RENDERER_ID);
            priv->video_started_by_settings = previewRenderer->isRendering();
            if (priv->video_started_by_settings) {
                video_widget_add_new_renderer(VIDEO_WIDGET(priv->video_widget),
                    priv->cpp->avModel_, previewRenderer, VIDEO_RENDERER_REMOTE);
            } else {
                priv->video_started_by_settings = true;
571 572 573 574 575 576
                priv->device_event_connection = QObject::connect(
                    &*priv->cpp->avModel_,
                    &lrc::api::AVModel::deviceEvent,
                    [=]() {
                        priv->cpp->drawVideoDevices();
                    });
577 578 579 580 581 582 583 584 585 586 587 588 589 590
                priv->local_renderer_connection = QObject::connect(
                    &*priv->cpp->avModel_,
                    &lrc::api::AVModel::rendererStarted,
                    [=](const std::string& id) {
                        if (id != lrc::api::video::PREVIEW_RENDERER_ID)
                            return;
                        video_widget_add_new_renderer(
                            VIDEO_WIDGET(priv->video_widget),
                            priv->cpp->avModel_,
                            previewRenderer, VIDEO_RENDERER_REMOTE);
                    });
                priv->cpp->avModel_->startPreview();
            }
        } catch (const std::out_of_range& e) {
591
            g_warning("Cannot start preview");
592
        }
593 594
        priv->cpp->avModel_->startAudioDevice();
        priv->cpp->avModel_->setAudioMeterState(true);
595 596
    } else {
        if (priv->video_started_by_settings) {
597
            priv->cpp->avModel_->stopPreview();
598
            QObject::disconnect(priv->local_renderer_connection);
599
            QObject::disconnect(priv->device_event_connection);
600 601 602 603
            priv->video_started_by_settings = FALSE;
        }

        if (priv->video_widget && IS_VIDEO_WIDGET(priv->video_widget))
604
            gtk_container_remove(GTK_CONTAINER(priv->vbox_main), priv->video_widget);
605
        priv->video_widget = NULL;
606 607
        priv->cpp->avModel_->setAudioMeterState(false);
        priv->cpp->avModel_->stopAudioDevice();
608 609
    }
}