2020-08-06 16:55:58 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "calls/calls_video_bubble.h"
|
|
|
|
|
|
|
|
#include "webrtc/webrtc_video_track.h"
|
|
|
|
#include "ui/image/image_prepare.h"
|
2020-08-07 14:33:34 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2020-08-06 16:55:58 +00:00
|
|
|
#include "styles/style_calls.h"
|
2020-08-07 14:33:34 +00:00
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
#include "styles/style_layers.h"
|
2020-08-06 16:55:58 +00:00
|
|
|
|
|
|
|
namespace Calls {
|
|
|
|
|
|
|
|
VideoBubble::VideoBubble(
|
|
|
|
not_null<QWidget*> parent,
|
2020-08-11 09:59:48 +00:00
|
|
|
not_null<Webrtc::VideoTrack*> track)
|
2020-08-06 16:55:58 +00:00
|
|
|
: _content(parent)
|
|
|
|
, _track(track)
|
2020-08-11 09:59:48 +00:00
|
|
|
, _state(Webrtc::VideoState::Inactive) {
|
2020-08-06 16:55:58 +00:00
|
|
|
setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::setup() {
|
|
|
|
_content.show();
|
|
|
|
applyDragMode(_dragMode);
|
|
|
|
|
|
|
|
_content.paintRequest(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
paint();
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_track->stateValue(
|
2020-08-11 09:59:48 +00:00
|
|
|
) | rpl::start_with_next([=](Webrtc::VideoState state) {
|
2020-08-06 16:55:58 +00:00
|
|
|
setState(state);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_track->renderNextFrame(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (_track->frameSize().isEmpty()) {
|
|
|
|
_track->markFrameShown();
|
|
|
|
} else {
|
|
|
|
updateVisibility();
|
2020-08-12 16:58:24 +00:00
|
|
|
// We update whole parent widget in this case.
|
|
|
|
// In case we update only bubble without the parent incoming
|
|
|
|
// video frame we may get full parent of old frame with a
|
|
|
|
// rectangular piece of a new frame rendered with that update().
|
|
|
|
//_content.update();
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
2020-08-07 13:28:41 +00:00
|
|
|
void VideoBubble::updateGeometry(
|
|
|
|
DragMode mode,
|
|
|
|
QRect boundingRect,
|
|
|
|
QSize sizeMin,
|
|
|
|
QSize sizeMax) {
|
|
|
|
Expects(!boundingRect.isEmpty());
|
|
|
|
Expects(sizeMax.isEmpty() || !sizeMin.isEmpty());
|
|
|
|
Expects(sizeMax.isEmpty() || sizeMin.width() <= sizeMax.width());
|
|
|
|
Expects(sizeMax.isEmpty() || sizeMin.height() <= sizeMax.height());
|
|
|
|
|
|
|
|
if (sizeMin.isEmpty()) {
|
|
|
|
sizeMin = boundingRect.size();
|
|
|
|
}
|
|
|
|
if (sizeMax.isEmpty()) {
|
|
|
|
sizeMax = sizeMin;
|
|
|
|
}
|
2020-08-06 16:55:58 +00:00
|
|
|
if (_dragMode != mode) {
|
|
|
|
applyDragMode(mode);
|
|
|
|
}
|
2020-08-07 13:28:41 +00:00
|
|
|
if (_boundingRect != boundingRect) {
|
|
|
|
applyBoundingRect(boundingRect);
|
|
|
|
}
|
|
|
|
if (_min != sizeMin || _max != sizeMax) {
|
|
|
|
applySizeConstraints(sizeMin, sizeMax);
|
|
|
|
}
|
|
|
|
if (_geometryDirty && !_lastFrameSize.isEmpty()) {
|
|
|
|
updateSizeToFrame(base::take(_lastFrameSize));
|
|
|
|
}
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:35:31 +00:00
|
|
|
QRect VideoBubble::geometry() const {
|
|
|
|
return _content.isHidden() ? QRect() : _content.geometry();
|
|
|
|
}
|
|
|
|
|
2020-08-07 13:28:41 +00:00
|
|
|
void VideoBubble::applyBoundingRect(QRect rect) {
|
2020-08-06 16:55:58 +00:00
|
|
|
_boundingRect = rect;
|
2020-08-07 13:28:41 +00:00
|
|
|
_geometryDirty = true;
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::applyDragMode(DragMode mode) {
|
|
|
|
_dragMode = mode;
|
|
|
|
if (_dragMode == DragMode::None) {
|
|
|
|
_dragging = false;
|
|
|
|
_content.setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
_content.setAttribute(
|
|
|
|
Qt::WA_TransparentForMouseEvents,
|
2020-08-07 13:28:41 +00:00
|
|
|
true/*(_dragMode == DragMode::None)*/);
|
|
|
|
if (_dragMode == DragMode::SnapToCorners) {
|
|
|
|
_corner = RectPart::BottomRight;
|
|
|
|
} else {
|
|
|
|
_corner = RectPart::None;
|
|
|
|
_lastDraggableSize = _size;
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
2020-08-07 13:28:41 +00:00
|
|
|
_size = QSize();
|
|
|
|
_geometryDirty = true;
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::applySizeConstraints(QSize min, QSize max) {
|
|
|
|
_min = min;
|
|
|
|
_max = max;
|
2020-08-07 13:28:41 +00:00
|
|
|
_geometryDirty = true;
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::paint() {
|
2022-09-16 20:23:27 +00:00
|
|
|
auto p = QPainter(&_content);
|
2020-08-06 16:55:58 +00:00
|
|
|
|
2020-08-07 14:33:34 +00:00
|
|
|
prepareFrame();
|
|
|
|
if (!_frame.isNull()) {
|
|
|
|
const auto padding = st::boxRoundShadow.extend;
|
|
|
|
const auto inner = _content.rect().marginsRemoved(padding);
|
|
|
|
Ui::Shadow::paint(p, inner, _content.width(), st::boxRoundShadow);
|
2020-08-12 08:47:07 +00:00
|
|
|
const auto factor = cIntRetinaFactor();
|
2020-08-07 14:33:34 +00:00
|
|
|
p.drawImage(
|
|
|
|
inner,
|
|
|
|
_frame,
|
2020-08-12 08:47:07 +00:00
|
|
|
QRect(
|
|
|
|
QPoint(_frame.width() - (inner.width() * factor), 0),
|
|
|
|
inner.size() * factor));
|
2020-08-07 14:33:34 +00:00
|
|
|
}
|
2020-08-06 16:55:58 +00:00
|
|
|
_track->markFrameShown();
|
|
|
|
}
|
|
|
|
|
2020-08-07 14:33:34 +00:00
|
|
|
void VideoBubble::prepareFrame() {
|
|
|
|
const auto original = _track->frameSize();
|
|
|
|
if (original.isEmpty()) {
|
|
|
|
_frame = QImage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto padding = st::boxRoundShadow.extend;
|
|
|
|
const auto size = _content.rect().marginsRemoved(padding).size()
|
|
|
|
* cIntRetinaFactor();
|
|
|
|
|
|
|
|
// Should we check 'original' and 'size' aspect ratios?..
|
2020-08-11 09:59:48 +00:00
|
|
|
const auto request = Webrtc::FrameRequest{
|
2020-08-07 14:33:34 +00:00
|
|
|
.resize = size,
|
|
|
|
.outer = size,
|
|
|
|
};
|
|
|
|
const auto frame = _track->frame(request);
|
|
|
|
if (_frame.width() < size.width() || _frame.height() < size.height()) {
|
|
|
|
_frame = QImage(
|
2020-08-12 08:47:07 +00:00
|
|
|
size * cIntRetinaFactor(),
|
2020-08-07 14:33:34 +00:00
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
}
|
|
|
|
Assert(_frame.width() >= frame.width()
|
|
|
|
&& _frame.height() >= frame.height());
|
2022-06-29 12:20:12 +00:00
|
|
|
const auto dstPerLine = _frame.bytesPerLine();
|
|
|
|
const auto srcPerLine = frame.bytesPerLine();
|
2020-08-07 14:33:34 +00:00
|
|
|
const auto lineSize = frame.width() * 4;
|
2022-06-29 12:20:12 +00:00
|
|
|
auto dst = _frame.bits();
|
|
|
|
auto src = frame.bits();
|
|
|
|
const auto till = src + frame.height() * srcPerLine;
|
|
|
|
for (; src != till; src += srcPerLine, dst += dstPerLine) {
|
|
|
|
memcpy(dst, src, lineSize);
|
2020-08-07 14:33:34 +00:00
|
|
|
}
|
2022-01-21 12:31:39 +00:00
|
|
|
_frame = Images::Round(
|
|
|
|
std::move(_frame),
|
2020-08-07 14:33:34 +00:00
|
|
|
ImageRoundRadius::Large,
|
|
|
|
RectPart::AllCorners,
|
2022-01-21 12:31:39 +00:00
|
|
|
QRect(QPoint(), size)
|
|
|
|
).mirrored(true, false);
|
2020-08-07 14:33:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 09:59:48 +00:00
|
|
|
void VideoBubble::setState(Webrtc::VideoState state) {
|
|
|
|
if (state == Webrtc::VideoState::Paused) {
|
2020-08-06 16:55:58 +00:00
|
|
|
using namespace Images;
|
|
|
|
static constexpr auto kRadius = 24;
|
|
|
|
_pausedFrame = Images::BlurLargeImage(_track->frame({}), kRadius);
|
|
|
|
if (_pausedFrame.isNull()) {
|
2020-08-11 09:59:48 +00:00
|
|
|
state = Webrtc::VideoState::Inactive;
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_state = state;
|
|
|
|
updateVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::updateSizeToFrame(QSize frame) {
|
|
|
|
Expects(!frame.isEmpty());
|
|
|
|
|
|
|
|
if (_lastFrameSize == frame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastFrameSize = frame;
|
|
|
|
|
2020-08-07 13:28:41 +00:00
|
|
|
auto size = !_size.isEmpty()
|
2020-08-13 11:16:39 +00:00
|
|
|
? QSize(
|
|
|
|
std::clamp(_size.width(), _min.width(), _max.width()),
|
|
|
|
std::clamp(_size.height(), _min.height(), _max.height()))
|
2020-08-07 13:28:41 +00:00
|
|
|
: (_dragMode == DragMode::None || _lastDraggableSize.isEmpty())
|
|
|
|
? QSize()
|
|
|
|
: _lastDraggableSize;
|
2020-08-06 16:55:58 +00:00
|
|
|
if (size.isEmpty()) {
|
|
|
|
size = frame.scaled((_min + _max) / 2, Qt::KeepAspectRatio);
|
|
|
|
} else {
|
|
|
|
const auto area = size.width() * size.height();
|
2021-09-27 08:13:57 +00:00
|
|
|
const auto w = int(base::SafeRound(std::max(
|
2020-08-13 11:16:39 +00:00
|
|
|
std::sqrt((frame.width() * float64(area)) / (frame.height() * 1.)),
|
2020-08-06 16:55:58 +00:00
|
|
|
1.)));
|
|
|
|
const auto h = area / w;
|
|
|
|
size = QSize(w, h);
|
2020-08-13 11:16:39 +00:00
|
|
|
if (w > _max.width() || h > _max.height()) {
|
|
|
|
size = size.scaled(_max, Qt::KeepAspectRatio);
|
|
|
|
}
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
size = QSize(std::max(1, size.width()), std::max(1, size.height()));
|
|
|
|
setInnerSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::setInnerSize(QSize size) {
|
2020-08-07 13:28:41 +00:00
|
|
|
if (_size == size && !_geometryDirty) {
|
2020-08-06 16:55:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-07 13:28:41 +00:00
|
|
|
_geometryDirty = false;
|
2020-08-06 16:55:58 +00:00
|
|
|
_size = size;
|
2020-08-07 14:33:34 +00:00
|
|
|
const auto topLeft = [&] {
|
2020-08-07 13:28:41 +00:00
|
|
|
switch (_corner) {
|
|
|
|
case RectPart::None:
|
|
|
|
return _boundingRect.topLeft() + QPoint(
|
|
|
|
(_boundingRect.width() - size.width()) / 2,
|
|
|
|
(_boundingRect.height() - size.height()) / 2);
|
|
|
|
case RectPart::TopLeft:
|
|
|
|
return _boundingRect.topLeft();
|
|
|
|
case RectPart::TopRight:
|
|
|
|
return QPoint(
|
|
|
|
_boundingRect.x() + _boundingRect.width() - size.width(),
|
|
|
|
_boundingRect.y());
|
|
|
|
case RectPart::BottomRight:
|
|
|
|
return QPoint(
|
|
|
|
_boundingRect.x() + _boundingRect.width() - size.width(),
|
|
|
|
_boundingRect.y() + _boundingRect.height() - size.height());
|
|
|
|
case RectPart::BottomLeft:
|
|
|
|
return QPoint(
|
|
|
|
_boundingRect.x(),
|
|
|
|
_boundingRect.y() + _boundingRect.height() - size.height());
|
|
|
|
}
|
|
|
|
Unexpected("Corner value in VideoBubble::setInnerSize.");
|
2020-08-07 14:33:34 +00:00
|
|
|
}();
|
|
|
|
const auto inner = QRect(topLeft, size);
|
|
|
|
_content.setGeometry(inner.marginsAdded(st::boxRoundShadow.extend));
|
2020-08-06 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBubble::updateVisibility() {
|
|
|
|
const auto size = _track->frameSize();
|
2020-08-11 09:59:48 +00:00
|
|
|
const auto visible = (_state != Webrtc::VideoState::Inactive)
|
2020-08-06 16:55:58 +00:00
|
|
|
&& !size.isEmpty();
|
|
|
|
if (visible) {
|
|
|
|
updateSizeToFrame(size);
|
|
|
|
}
|
|
|
|
_content.setVisible(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Calls
|