Skip to content

Commit

Permalink
Enable Windowless plusgin by default on Windows
Browse files Browse the repository at this point in the history
Issue #46: Begin adding the function (memory crash)
Issue #44: Fix
Issue #45: Fix for IE only (to be done for Safari)
  • Loading branch information
sarandogou committed Oct 23, 2015
1 parent a8612d4 commit a0e34eb
Show file tree
Hide file tree
Showing 12 changed files with 981 additions and 651 deletions.
16 changes: 16 additions & 0 deletions common/_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ typedef enum _ArrayType
}
_ArrayType;

typedef enum __TrackType {
_TrackTypeNone = 0x00,
_TrackTypeAudio = (0x01 << 0),
_TrackTypeVideo = (0x01 << 1),
_TrackTypeAudioVideo = _TrackTypeAudio | _TrackTypeVideo,
_TrackTypeAll = 0xFF
}
_TrackType;

typedef struct __TrustedWebsite {
std::string scheme;
std::string host;
Expand Down Expand Up @@ -354,6 +363,13 @@ struct _Sequence {
void Add(cpp11::shared_ptr<T> v) {
values.push_back(v);
}
void AddSeq(_Sequence<T>* seq) {
if (seq) {
for (size_t i = 0; i < seq->values.size(); ++i) {
values.push_back(seq->values[i]);
}
}
}
~_Sequence() {
Clear();
}
Expand Down
153 changes: 138 additions & 15 deletions common/_RTCDisplay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,109 @@ HWND _VideoRenderer::GetHwnd()
{
return m_Hwnd;
}

static inline LONG Width(const RECT& r)
{
return r.right - r.left;
}

static inline LONG Height(const RECT& r)
{
return r.bottom - r.top;
}

typedef struct _WERatio {
DWORD Numerator;
DWORD Denominator;
} WERatio;

//-----------------------------------------------------------------------------
// CorrectAspectRatio
//
// Converts a rectangle from the source's pixel aspect ratio (PAR) to 1:1 PAR.
// Returns the corrected rectangle.
//
// For example, a 720 x 486 rect with a PAR of 9:10, when converted to 1x1 PAR,
// is stretched to 720 x 540.
// Copyright (C) Microsoft
//-----------------------------------------------------------------------------

static inline RECT CorrectAspectRatio(const RECT& src, const WERatio& srcPAR)
{
// Start with a rectangle the same size as src, but offset to the origin (0,0).
RECT rc = { 0, 0, src.right - src.left, src.bottom - src.top };

if ((srcPAR.Numerator != 1) || (srcPAR.Denominator != 1))
{
// Correct for the source's PAR.

if (srcPAR.Numerator > srcPAR.Denominator)
{
// The source has "wide" pixels, so stretch the width.
rc.right = MulDiv(rc.right, srcPAR.Numerator, srcPAR.Denominator);
}
else if (srcPAR.Numerator < srcPAR.Denominator)
{
// The source has "tall" pixels, so stretch the height.
rc.bottom = MulDiv(rc.bottom, srcPAR.Denominator, srcPAR.Numerator);
}
// else: PAR is 1:1, which is a no-op.
}
return rc;
}

//-------------------------------------------------------------------
// LetterBoxDstRect
//
// Takes a src rectangle and constructs the largest possible
// destination rectangle within the specifed destination rectangle
// such thatthe video maintains its current shape.
//
// This function assumes that pels are the same shape within both the
// source and destination rectangles.
// Copyright (C) Microsoft
//-------------------------------------------------------------------

static inline RECT LetterBoxRect(const RECT& rcSrc, const RECT& rcDst)
{
// figure out src/dest scale ratios
int iSrcWidth = Width(rcSrc);
int iSrcHeight = Height(rcSrc);

int iDstWidth = Width(rcDst);
int iDstHeight = Height(rcDst);

int iDstLBWidth;
int iDstLBHeight;

if (MulDiv(iSrcWidth, iDstHeight, iSrcHeight) <= iDstWidth) {

// Column letter boxing ("pillar box")

iDstLBWidth = MulDiv(iDstHeight, iSrcWidth, iSrcHeight);
iDstLBHeight = iDstHeight;
}
else {

// Row letter boxing.

iDstLBWidth = iDstWidth;
iDstLBHeight = MulDiv(iDstWidth, iSrcHeight, iSrcWidth);
}


// Create a centered rectangle within the current destination rect

RECT rc;

LONG left = rcDst.left + ((iDstWidth - iDstLBWidth) >> 1);
LONG top = rcDst.top + ((iDstHeight - iDstLBHeight) >> 1);

SetRect(&rc, left, top, left + iDstLBWidth, top + iDstLBHeight);

return rc;
}

#elif WE_UNDER_APPLE
void _VideoRenderer::SetLayer(CALayer *layer)
{
Expand Down Expand Up @@ -166,20 +269,12 @@ bool _VideoRenderer::PaintFrame(intptr_t layer /*= 0*/)
}
}
else {
//TODO(dmi): Image not at the right position
rc.left = 0;
rc.right = abs(drawInfo->prcBounds->left) + abs(drawInfo->prcBounds->right);
rc.top = 0;
rc.bottom = abs(drawInfo->prcBounds->top) + abs(drawInfo->prcBounds->bottom);
/* rc.left = drawInfo->prcBounds->left;
rc.right = drawInfo->prcBounds->right;
rc.top = drawInfo->prcBounds->top;
rc.bottom = drawInfo->prcBounds->bottom; */
rc = *(RECT*)drawInfo->prcBounds;
}

#if 1
int height = abs(m_bmi.bmiHeader.biHeight);
#if WE_USE_AUTORESIZE // Flickering when resizing (in windowless mode only: https://github.com/sarandogou/webrtc-everywhere/issues/44)
int width = m_bmi.bmiHeader.biWidth;
int height = abs(m_bmi.bmiHeader.biHeight);

HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
::SetStretchBltMode(dc_mem, HALFTONE);
Expand Down Expand Up @@ -216,10 +311,38 @@ bool _VideoRenderer::PaintFrame(intptr_t layer /*= 0*/)
::SelectObject(dc_mem, bmp_old);
::DeleteObject(bmp_mem);
::DeleteDC(dc_mem);
#else
::StretchDIBits(hdc, 0, 0, (rc.right - rc.left), (rc.bottom - rc.top),
0, 0, m_bmi.bmiHeader.biWidth, abs(m_bmi.bmiHeader.biHeight), image, &m_bmi, DIB_RGB_COLORS, SRCCOPY);
#endif
#else /* !WE_USE_AUTORESIZE */
static const WERatio pixelAR = { 1, 1 };
RECT rcSrc = { 0, 0, m_bmi.bmiHeader.biWidth, abs(m_bmi.bmiHeader.biHeight) };
rcSrc = CorrectAspectRatio(rcSrc, pixelAR);
const RECT rcDest = LetterBoxRect(rcSrc, rc);

HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
::SetStretchBltMode(dc_mem, HALFTONE);

// Create offscreen bmp memory
HBITMAP bmp_mem = ::CreateCompatibleBitmap(ps.hdc, rc.right, rc.bottom);
HGDIOBJ bmp_old = ::SelectObject(dc_mem, bmp_mem);

POINT logical_area = { rc.right, rc.bottom };
DPtoLP(ps.hdc, &logical_area, 1);

HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
RECT logical_rect = { 0, 0, logical_area.x, logical_area.y };
::FillRect(dc_mem, &logical_rect, brush);
::DeleteObject(brush);

StretchDIBits(dc_mem, rcDest.left, rcDest.top, Width(rcDest), Height(rcDest),
rcSrc.left, rcSrc.top, Width(rcSrc), Height(rcSrc), image, &m_bmi, DIB_RGB_COLORS, SRCCOPY);

BitBlt(ps.hdc, 0, 0, logical_area.x, logical_area.y, dc_mem, 0, 0, SRCCOPY);

// Cleanup.
::SelectObject(dc_mem, bmp_old);
::DeleteObject(bmp_mem);
::DeleteDC(dc_mem);
#endif /* WE_USE_AUTORESIZE */

if (m_Hwnd) {
::EndPaint(m_Hwnd, &ps);
}
Expand Down
4 changes: 4 additions & 0 deletions common/_Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ LRESULT CALLBACK _Utils::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
{
return TRUE; // avoid background erasing.
}
case WM_WINDOWPOSCHANGING:
break;
case WM_WINDOWPOSCHANGED:
break;
case WM_PAINT:
{
_RTCDisplay* display = dynamic_cast<_RTCDisplay*>(reinterpret_cast<_RTCDisplay*>(GetWindowLongPtr(hWnd, GWLP_USERDATA)));
Expand Down
Binary file modified ie/Debug/webrtc-everywhere.res
Binary file not shown.
45 changes: 28 additions & 17 deletions ie/MediaStream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ STDMETHODIMP CMediaStream::get_id(BSTR* pVal)

STDMETHODIMP CMediaStream::getAudioTracks(__out VARIANT* Tracks)
{
return getTracks(FALSE/*video*/, Tracks);
return getTracks(_TrackTypeAudio, Tracks);
}

STDMETHODIMP CMediaStream::getVideoTracks(__out VARIANT* Tracks)
{
return getTracks(TRUE/*video*/, Tracks);
return getTracks(_TrackTypeVideo, Tracks);
}

STDMETHODIMP CMediaStream::getTracks(__out VARIANT* Tracks)
{
return getTracks(_TrackTypeAll, Tracks);
}

STDMETHODIMP CMediaStream::getTrackById(__in BSTR trackId, __out VARIANT* MediaStreamTrack)
Expand Down Expand Up @@ -246,7 +251,7 @@ STDMETHODIMP CMediaStream::stop()
return S_OK;
}

HRESULT CMediaStream::getTracks(BOOL video, VARIANT* Tracks)
HRESULT CMediaStream::getTracks(_TrackType type, VARIANT* Tracks)
{
HRESULT hr = S_OK;
if (!m_Stream) {
Expand All @@ -262,20 +267,26 @@ HRESULT CMediaStream::getTracks(BOOL video, VARIANT* Tracks)
CHECK_HR_RETURN(hr = pluginInstance->GetDispatch(spDispatch));

std::vector<CComVariant> vect;
std::shared_ptr<_Sequence<_MediaStreamTrack> > tracks = video ? m_Stream->getVideoTracks() : m_Stream->getAudioTracks();
if (tracks) {
for (size_t i = 0; i < tracks->values.size(); ++i) {
if (!tracks->values[i]) {
continue;
}
CComObject<CMediaStreamTrack>* _track;
hr = Utils::CreateInstanceWithRef(&_track);
if (SUCCEEDED(hr)) {
_track->SetDispatcher(pluginInstance);
_track->SetTrack(tracks->values[i]);
vect.push_back(CComVariant(_track));
SafeRelease(&_track);
}
std::shared_ptr<_Sequence<_MediaStreamTrack> > _tracks;
cpp11::shared_ptr<_Sequence<_MediaStreamTrack> > tracks(new _Sequence<_MediaStreamTrack>());
if ((type & _TrackTypeAudio) == _TrackTypeAudio && (_tracks = m_Stream->getAudioTracks()) && _tracks.get()) {
tracks->AddSeq(_tracks.get());
}
if ((type & _TrackTypeVideo) == _TrackTypeVideo && (_tracks = m_Stream->getVideoTracks()) && _tracks.get()) {
tracks->AddSeq(_tracks.get());
}

for (size_t i = 0; i < tracks->values.size(); ++i) {
if (!tracks->values[i]) {
continue;
}
CComObject<CMediaStreamTrack>* _track;
hr = Utils::CreateInstanceWithRef(&_track);
if (SUCCEEDED(hr)) {
_track->SetDispatcher(pluginInstance);
_track->SetTrack(tracks->values[i]);
vect.push_back(CComVariant(_track));
SafeRelease(&_track);
}
}

Expand Down
3 changes: 2 additions & 1 deletion ie/MediaStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ END_COM_MAP()
STDMETHOD(get_id)(__out BSTR* pVal);
STDMETHOD(getAudioTracks)(__out VARIANT* Tracks);
STDMETHOD(getVideoTracks)(__out VARIANT* Tracks);
STDMETHOD(getTracks)(__out VARIANT* Tracks);
STDMETHOD(getTrackById)(__in BSTR trackId, __out VARIANT* MediaStreamTrack);
STDMETHOD(addTrack)(__in VARIANT MediaStreamTrack);
STDMETHOD(removeTrack)(__in VARIANT MediaStreamTrack);
Expand All @@ -66,7 +67,7 @@ END_COM_MAP()
STDMETHOD(stop)();

private:
HRESULT getTracks(BOOL video, VARIANT* Tracks);
HRESULT getTracks(_TrackType type, VARIANT* Tracks);

// callbacks
void onended();
Expand Down
Loading

0 comments on commit a0e34eb

Please sign in to comment.