2
// Copyright 2004 Google Inc.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
7
// 1. Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// 2. Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// 3. The name of the author may not be used to endorse or promote products
13
// derived from this software without specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
// Implementation of GdiVideoRenderer on Windows
30
#include "talk/session/phone/gdivideorenderer.h"
32
#include "talk/base/scoped_ptr.h"
33
#include "talk/base/thread.h"
34
#include "talk/base/win32window.h"
35
#include "talk/session/phone/videocommon.h"
36
#include "talk/session/phone/videoframe.h"
40
/////////////////////////////////////////////////////////////////////////////
41
// Definition of private class VideoWindow. We use a worker thread to manage
43
/////////////////////////////////////////////////////////////////////////////
44
class GdiVideoRenderer::VideoWindow : public talk_base::Win32Window {
46
VideoWindow(int x, int y, int width, int height);
47
virtual ~VideoWindow();
49
// Called when the video size changes. If it is called the first time, we
50
// create and start the thread. Otherwise, we send kSetSizeMsg to the thread.
51
// Context: non-worker thread.
52
bool SetSize(int width, int height);
54
// Called when a new frame is available. Upon this call, we send
55
// kRenderFrameMsg to the window thread. Context: non-worker thread. It may be
56
// better to pass RGB bytes to VideoWindow. However, we pass VideoFrame to put
57
// all the thread synchronization within VideoWindow.
58
bool RenderFrame(const VideoFrame* frame);
61
// Override virtual method of talk_base::Win32Window. Context: worker Thread.
62
virtual bool OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
66
enum { kSetSizeMsg = WM_USER, kRenderFrameMsg};
68
class WindowThread : public talk_base::Thread {
70
explicit WindowThread(VideoWindow* window) : window_(window) {}
72
// Override virtual method of talk_base::Thread. Context: worker Thread.
74
// Initialize the window
75
if (!window_ || !window_->Initialize()) {
78
// Run the message loop
80
while (GetMessage(&msg, NULL, 0, 0) > 0) {
81
TranslateMessage(&msg);
82
DispatchMessage(&msg);
90
// Context: worker Thread.
93
void OnSize(int width, int height, bool frame_changed);
94
void OnRenderFrame(const VideoFrame* frame);
97
talk_base::scoped_array<uint8> image_;
98
talk_base::scoped_ptr<WindowThread> window_thread_;
99
// The initial position of the window.
104
/////////////////////////////////////////////////////////////////////////////
105
// Implementation of class VideoWindow
106
/////////////////////////////////////////////////////////////////////////////
107
GdiVideoRenderer::VideoWindow::VideoWindow(
108
int x, int y, int width, int height)
111
memset(&bmi_.bmiHeader, 0, sizeof(bmi_.bmiHeader));
112
bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
113
bmi_.bmiHeader.biPlanes = 1;
114
bmi_.bmiHeader.biBitCount = 32;
115
bmi_.bmiHeader.biCompression = BI_RGB;
116
bmi_.bmiHeader.biWidth = width;
117
bmi_.bmiHeader.biHeight = -height;
118
bmi_.bmiHeader.biSizeImage = width * height * 4;
120
image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
123
GdiVideoRenderer::VideoWindow::~VideoWindow() {
124
// Context: caller Thread. We cannot call Destroy() since the window was
125
// created by another thread. Instead, we send WM_CLOSE message.
127
SendMessage(handle(), WM_CLOSE, 0, 0);
131
bool GdiVideoRenderer::VideoWindow::SetSize(int width, int height) {
132
if (!window_thread_.get()) {
133
// Create and start the window thread.
134
window_thread_.reset(new WindowThread(this));
135
return window_thread_->Start();
136
} else if (width != bmi_.bmiHeader.biWidth ||
137
height != -bmi_.bmiHeader.biHeight) {
138
SendMessage(handle(), kSetSizeMsg, 0, MAKELPARAM(width, height));
143
bool GdiVideoRenderer::VideoWindow::RenderFrame(const VideoFrame* frame) {
148
SendMessage(handle(), kRenderFrameMsg, reinterpret_cast<WPARAM>(frame), 0);
152
bool GdiVideoRenderer::VideoWindow::OnMessage(UINT uMsg, WPARAM wParam,
153
LPARAM lParam, LRESULT& result) {
160
PostQuitMessage(0); // post WM_QUIT to end the message loop in Run()
163
case WM_SIZE: // The window UI was resized.
164
OnSize(LOWORD(lParam), HIWORD(lParam), false);
167
case kSetSizeMsg: // The video resolution changed.
168
OnSize(LOWORD(lParam), HIWORD(lParam), true);
171
case kRenderFrameMsg:
172
OnRenderFrame(reinterpret_cast<const VideoFrame*>(wParam));
178
bool GdiVideoRenderer::VideoWindow::Initialize() {
179
if (!talk_base::Win32Window::Create(
180
NULL, L"Video Renderer",
181
WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
183
initial_x_, initial_y_,
184
bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight)) {
187
OnSize(bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight, false);
191
void GdiVideoRenderer::VideoWindow::OnPaint() {
193
GetClientRect(handle(), &rcClient);
195
HDC hdc = BeginPaint(handle(), &ps);
197
0, 0, rcClient.right, rcClient.bottom, // destination rect
198
0, 0, bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight, // source rect
199
image_.get(), &bmi_, DIB_RGB_COLORS, SRCCOPY);
200
EndPaint(handle(), &ps);
203
void GdiVideoRenderer::VideoWindow::OnSize(int width, int height,
204
bool frame_changed) {
205
// Get window and client sizes
206
RECT rcClient, rcWindow;
207
GetClientRect(handle(), &rcClient);
208
GetWindowRect(handle(), &rcWindow);
210
// Find offset between window size and client size
212
ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
213
ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
216
MoveWindow(handle(), rcWindow.left, rcWindow.top,
217
width + ptDiff.x, height + ptDiff.y, false);
218
UpdateWindow(handle());
219
ShowWindow(handle(), SW_SHOW);
221
if (frame_changed && (width != bmi_.bmiHeader.biWidth ||
222
height != -bmi_.bmiHeader.biHeight)) {
223
// Update the bmi and image buffer
224
bmi_.bmiHeader.biWidth = width;
225
bmi_.bmiHeader.biHeight = -height;
226
bmi_.bmiHeader.biSizeImage = width * height * 4;
227
image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
231
void GdiVideoRenderer::VideoWindow::OnRenderFrame(const VideoFrame* frame) {
235
// Convert frame to ARGB format, which is accepted by GDI
236
frame->ConvertToRgbBuffer(cricket::FOURCC_ARGB, image_.get(),
237
bmi_.bmiHeader.biSizeImage,
238
bmi_.bmiHeader.biWidth * 4);
239
InvalidateRect(handle(), 0, 0);
242
/////////////////////////////////////////////////////////////////////////////
243
// Implementation of class GdiVideoRenderer
244
/////////////////////////////////////////////////////////////////////////////
245
GdiVideoRenderer::GdiVideoRenderer(int x, int y)
249
GdiVideoRenderer::~GdiVideoRenderer() {}
251
bool GdiVideoRenderer::SetSize(int width, int height, int reserved) {
252
if (!window_.get()) { // Create the window for the first frame
253
window_.reset(new VideoWindow(initial_x_, initial_y_, width, height));
255
return window_->SetSize(width, height);
258
bool GdiVideoRenderer::RenderFrame(const VideoFrame* frame) {
259
if (!frame || !window_.get()) {
262
return window_->RenderFrame(frame);
265
} // namespace cricket