VideoPage.xaml.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com>          *
*                                                                         *
* 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, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/
Nicolas Jager's avatar
Nicolas Jager committed
19 20 21 22
#include "pch.h"

#include "VideoPage.xaml.h"

atraczyk's avatar
atraczyk committed
23 24
#include <MemoryBuffer.h>   // IMemoryBufferByteAccess

Nicolas Jager's avatar
Nicolas Jager committed
25
using namespace RingClientUWP::Views;
Nicolas Jager's avatar
Nicolas Jager committed
26
using namespace ViewModel;
atraczyk's avatar
atraczyk committed
27
using namespace Video;
Nicolas Jager's avatar
Nicolas Jager committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41

using namespace Concurrency;
using namespace Platform;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Media::Capture;
42 43
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
Nicolas Jager's avatar
Nicolas Jager committed
44

atraczyk's avatar
atraczyk committed
45 46 47 48 49 50 51
using namespace Windows::Graphics::Display;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Media;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::Media::Capture;
using namespace Windows::Devices::Sensors;

Nicolas Jager's avatar
Nicolas Jager committed
52 53 54 55
VideoPage::VideoPage()
{
    InitializeComponent();

atraczyk's avatar
atraczyk committed
56 57 58 59 60 61 62 63
    VideoManager::instance->captureManager()->displayInformation = DisplayInformation::GetForCurrentView();
    VideoManager::instance->captureManager()->EnumerateWebcamsAsync();

    Page::NavigationCacheMode = Navigation::NavigationCacheMode::Required;

    VideoManager::instance->rendererManager()->writeVideoFrame +=
        ref new WriteVideoFrame([this](String^ id, uint8_t* buf, int width, int height)
    {
64
        CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
atraczyk's avatar
atraczyk committed
65 66 67 68 69 70
        ref new DispatchedHandler([=]() {
            try {
                if (!VideoManager::instance->rendererManager()->renderers->Size)
                    return;
                VideoManager::instance->rendererManager()->renderer(id)->isRendering = true;
                create_task(WriteFrameAsSoftwareBitmapAsync(id, buf, width, height))
71
                .then([=](task<void> previousTask) {
atraczyk's avatar
atraczyk committed
72 73 74 75
                    try {
                        previousTask.get();
                    }
                    catch (Platform::Exception^ e) {
76
                        RingDebug::instance->WriteLine( "Caught exception from previous task.\n" );
atraczyk's avatar
atraczyk committed
77 78 79 80
                    }
                });
            }
            catch(Platform::COMException^ e) {
81
                RingDebug::instance->WriteLine(e->ToString());
atraczyk's avatar
atraczyk committed
82 83 84 85 86 87 88 89 90
            }
        }));
    });

    VideoManager::instance->captureManager()->startPreviewing +=
        ref new StartPreviewing([this]()
    {
        PreviewImage->Visibility = Windows::UI::Xaml::Visibility::Visible;
        PreviewImage->FlowDirection = VideoManager::instance->captureManager()->mirroringPreview ?
91 92
                                      Windows::UI::Xaml::FlowDirection::RightToLeft :
                                      Windows::UI::Xaml::FlowDirection::LeftToRight;
atraczyk's avatar
atraczyk committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    });

    VideoManager::instance->captureManager()->stopPreviewing +=
        ref new StopPreviewing([this]()
    {
        PreviewImage->Source = nullptr;
        PreviewImage->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    });

    VideoManager::instance->captureManager()->getSink +=
        ref new GetSink([this]()
    {
        return PreviewImage;
    });

    VideoManager::instance->rendererManager()->clearRenderTarget +=
        ref new ClearRenderTarget([this]()
    {
        IncomingVideoImage->Source = nullptr;
    });

    RingD::instance->incomingAccountMessage +=
        ref new IncomingAccountMessage([&](String^ accountId, String^ from, String^ payload)
    {
        scrollDown();
    });

    RingD::instance->stateChange +=
        ref new StateChange([&](String^ callId, CallStatus state, int code)
    {
123
        if (state == CallStatus::NONE) {
atraczyk's avatar
atraczyk committed
124 125 126 127
            Video::VideoManager::instance->rendererManager()->raiseClearRenderTarget();
        }
    });

128
    RingD::instance->incomingMessage += ref new RingClientUWP::IncomingMessage(this, &RingClientUWP::Views::VideoPage::OnincomingMessage);
atraczyk's avatar
atraczyk committed
129
}
130

atraczyk's avatar
atraczyk committed
131 132 133
void
RingClientUWP::Views::VideoPage::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e)
{
134 135
    updatePageContent();
}
Nicolas Jager's avatar
Nicolas Jager committed
136

137 138
void RingClientUWP::Views::VideoPage::updatePageContent()
{
Nicolas Jager's avatar
Nicolas Jager committed
139 140 141
    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
    auto contact = (item) ? item->_contact : nullptr;

142 143 144 145
    if (!contact)
        return;

    _callee_->Text = contact->name_;
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

    _messagesList_->ItemsSource = contact->_conversation->_messages;

    scrollDown();
}

void RingClientUWP::Views::VideoPage::scrollDown()
{
    _scrollView_->UpdateLayout();
    _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
}

void
RingClientUWP::Views::VideoPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    sendMessage();
}

void
RingClientUWP::Views::VideoPage::_messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    if (e->Key == Windows::System::VirtualKey::Enter) {
        sendMessage();
    }
}

void
RingClientUWP::Views::VideoPage::sendMessage()
{
Nicolas Jager's avatar
Nicolas Jager committed
175 176 177
    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
    auto contact = item->_contact;

178 179 180 181 182 183 184 185
    auto txt = _messageTextBox_->Text;

    /* empty the textbox */
    _messageTextBox_->Text = "";

    if (!contact || txt->IsEmpty())
        return;

186
    RingD::instance->sendSIPTextMessage(txt);
187
    scrollDown();
188
}
Nicolas Jager's avatar
Nicolas Jager committed
189 190 191 192 193 194 195 196 197 198 199 200 201

void RingClientUWP::Views::VideoPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
}


void RingClientUWP::Views::VideoPage::_btnCancel__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{

}

void RingClientUWP::Views::VideoPage::_btnHangUp__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
202
    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
203
    RingD::instance->hangUpCall2(item->_callId);
204

Nicolas Jager's avatar
Nicolas Jager committed
205 206 207 208 209 210
    pressHangUpCall();
}


void RingClientUWP::Views::VideoPage::_btnPause__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
211 212 213 214 215 216
    /*auto item = SmartPanelItemsViewModel::instance->_selectedItem;
    if (item->_callStatus == CallStatus::IN_PROGRESS)
        RingD::instance->pauseCall(item->_callId);
    else if (item->_callStatus == CallStatus::PAUSED)
        RingD::instance->unPauseCall(item->_callId);*/

Nicolas Jager's avatar
Nicolas Jager committed
217 218 219 220 221 222
    pauseCall();
}


void RingClientUWP::Views::VideoPage::_btnChat__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
223 224 225 226 227 228 229 230
    chatOpen = !chatOpen;
    if (chatOpen) {
        _rowChatBx_->Height = 200;
        chatPanelCall();
    }
    else {
        _rowChatBx_->Height = 0;
    }
Nicolas Jager's avatar
Nicolas Jager committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
}


void RingClientUWP::Views::VideoPage::_btnAddFriend__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    addContactCall();
}


void RingClientUWP::Views::VideoPage::_btnSwitch__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    transferCall();
}


void RingClientUWP::Views::VideoPage::_btnMicrophone__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    switchMicrophoneStateCall();
}


void RingClientUWP::Views::VideoPage::_btnMemo__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    reccordVideoCall();
}


void RingClientUWP::Views::VideoPage::_btnHQ__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    qualityVideoLevelCall();
}


void RingClientUWP::Views::VideoPage::_btnVideo__Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    switchVideoStateCall();
}


void RingClientUWP::Views::VideoPage::_videoControl__PointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    if (barFading)
        myStoryboard->Begin();
    barFading_ = true;
}


void RingClientUWP::Views::VideoPage::btnAny_entered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    barFading_ = false;
    myStoryboard->Stop();
}


void RingClientUWP::Views::VideoPage::btnAny_exited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    barFading_ = true;
atraczyk's avatar
atraczyk committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}

task<void>
VideoPage::WriteFrameAsSoftwareBitmapAsync(String^ id, uint8_t* buf, int width, int height)
{
    auto vframe = ref new VideoFrame(BitmapPixelFormat::Bgra8, width, height);
    auto frame = vframe->SoftwareBitmap;

    const int BYTES_PER_PIXEL = 4;

    BitmapBuffer^ buffer = frame->LockBuffer(BitmapBufferAccessMode::ReadWrite);
    IMemoryBufferReference^ reference = buffer->CreateReference();

    Microsoft::WRL::ComPtr<IMemoryBufferByteAccess> byteAccess;
    if (SUCCEEDED(reinterpret_cast<IUnknown*>(reference)->QueryInterface(IID_PPV_ARGS(&byteAccess))))
    {
        byte* data;
        unsigned capacity;
        byteAccess->GetBuffer(&data, &capacity);

        auto desc = buffer->GetPlaneDescription(0);

        for (int row = 0; row < desc.Height; row++)
        {
            for (int col = 0; col < desc.Width; col++)
            {
                auto currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;

                data[currPixel + 0] = buf[currPixel + 0];
                data[currPixel + 1] = buf[currPixel + 1];
                data[currPixel + 2] = buf[currPixel + 2];
            }
        }
    }
    delete reference;
    delete buffer;

    VideoManager::instance->rendererManager()->renderer(id)->isRendering = false;

    auto sbSource = ref new Media::Imaging::SoftwareBitmapSource();
    return create_task(sbSource->SetBitmapAsync(frame))
329
           .then([this, sbSource]()
atraczyk's avatar
atraczyk committed
330 331 332 333 334 335 336 337 338
    {
        try {
            IncomingVideoImage->Source = sbSource;
        }
        catch (Exception^ e) {
            WriteException(e);
        }
    });
}
339 340


341
void RingClientUWP::Views::VideoPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
342 343 344
{
    scrollDown();
}