blob: d5edfc6e43ce7ce1f1f7952c478dc32499a8a50d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001// libjingle
2// Copyright 2004 Google Inc.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
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.
14//
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.
25//
26// Implementation of GdiVideoRenderer on Windows
27
28#ifdef WIN32
29
30#include "talk/media/devices/gdivideorenderer.h"
31
32#include "talk/base/scoped_ptr.h"
33#include "talk/base/thread.h"
34#include "talk/base/win32window.h"
35#include "talk/media/base/videocommon.h"
36#include "talk/media/base/videoframe.h"
37
38namespace cricket {
39
40/////////////////////////////////////////////////////////////////////////////
41// Definition of private class VideoWindow. We use a worker thread to manage
42// the window.
43/////////////////////////////////////////////////////////////////////////////
44class GdiVideoRenderer::VideoWindow : public talk_base::Win32Window {
45 public:
46 VideoWindow(int x, int y, int width, int height);
47 virtual ~VideoWindow();
48
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);
53
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);
59
60 protected:
61 // Override virtual method of talk_base::Win32Window. Context: worker Thread.
62 virtual bool OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
63 LRESULT& result);
64
65 private:
66 enum { kSetSizeMsg = WM_USER, kRenderFrameMsg};
67
68 class WindowThread : public talk_base::Thread {
69 public:
70 explicit WindowThread(VideoWindow* window) : window_(window) {}
71
72 // Override virtual method of talk_base::Thread. Context: worker Thread.
73 virtual void Run() {
74 // Initialize the window
75 if (!window_ || !window_->Initialize()) {
76 return;
77 }
78 // Run the message loop
79 MSG msg;
80 while (GetMessage(&msg, NULL, 0, 0) > 0) {
81 TranslateMessage(&msg);
82 DispatchMessage(&msg);
83 }
84 }
85
86 private:
87 VideoWindow* window_;
88 };
89
90 // Context: worker Thread.
91 bool Initialize();
92 void OnPaint();
93 void OnSize(int width, int height, bool frame_changed);
94 void OnRenderFrame(const VideoFrame* frame);
95
96 BITMAPINFO bmi_;
97 talk_base::scoped_array<uint8> image_;
98 talk_base::scoped_ptr<WindowThread> window_thread_;
99 // The initial position of the window.
100 int initial_x_;
101 int initial_y_;
102};
103
104/////////////////////////////////////////////////////////////////////////////
105// Implementation of class VideoWindow
106/////////////////////////////////////////////////////////////////////////////
107GdiVideoRenderer::VideoWindow::VideoWindow(
108 int x, int y, int width, int height)
109 : initial_x_(x),
110 initial_y_(y) {
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;
119
120 image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
121}
122
123GdiVideoRenderer::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.
126 if (handle()) {
127 SendMessage(handle(), WM_CLOSE, 0, 0);
128 }
129}
130
131bool 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));
139 }
140 return true;
141}
142
143bool GdiVideoRenderer::VideoWindow::RenderFrame(const VideoFrame* frame) {
144 if (!handle()) {
145 return false;
146 }
147
148 SendMessage(handle(), kRenderFrameMsg, reinterpret_cast<WPARAM>(frame), 0);
149 return true;
150}
151
152bool GdiVideoRenderer::VideoWindow::OnMessage(UINT uMsg, WPARAM wParam,
153 LPARAM lParam, LRESULT& result) {
154 switch (uMsg) {
155 case WM_PAINT:
156 OnPaint();
157 return true;
158
159 case WM_DESTROY:
160 PostQuitMessage(0); // post WM_QUIT to end the message loop in Run()
161 return false;
162
163 case WM_SIZE: // The window UI was resized.
164 OnSize(LOWORD(lParam), HIWORD(lParam), false);
165 return true;
166
167 case kSetSizeMsg: // The video resolution changed.
168 OnSize(LOWORD(lParam), HIWORD(lParam), true);
169 return true;
170
171 case kRenderFrameMsg:
172 OnRenderFrame(reinterpret_cast<const VideoFrame*>(wParam));
173 return true;
174 }
175 return false;
176}
177
178bool GdiVideoRenderer::VideoWindow::Initialize() {
179 if (!talk_base::Win32Window::Create(
180 NULL, L"Video Renderer",
181 WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
182 WS_EX_APPWINDOW,
183 initial_x_, initial_y_,
184 bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight)) {
185 return false;
186 }
187 OnSize(bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight, false);
188 return true;
189}
190
191void GdiVideoRenderer::VideoWindow::OnPaint() {
192 RECT rcClient;
193 GetClientRect(handle(), &rcClient);
194 PAINTSTRUCT ps;
195 HDC hdc = BeginPaint(handle(), &ps);
196 StretchDIBits(hdc,
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);
201}
202
203void 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);
209
210 // Find offset between window size and client size
211 POINT ptDiff;
212 ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
213 ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
214
215 // Resize client
216 MoveWindow(handle(), rcWindow.left, rcWindow.top,
217 width + ptDiff.x, height + ptDiff.y, false);
218 UpdateWindow(handle());
219 ShowWindow(handle(), SW_SHOW);
220
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]);
228 }
229}
230
231void GdiVideoRenderer::VideoWindow::OnRenderFrame(const VideoFrame* frame) {
232 if (!frame) {
233 return;
234 }
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);
240}
241
242/////////////////////////////////////////////////////////////////////////////
243// Implementation of class GdiVideoRenderer
244/////////////////////////////////////////////////////////////////////////////
245GdiVideoRenderer::GdiVideoRenderer(int x, int y)
246 : initial_x_(x),
247 initial_y_(y) {
248}
249GdiVideoRenderer::~GdiVideoRenderer() {}
250
251bool 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));
254 }
255 return window_->SetSize(width, height);
256}
257
258bool GdiVideoRenderer::RenderFrame(const VideoFrame* frame) {
259 if (!frame || !window_.get()) {
260 return false;
261 }
262 return window_->RenderFrame(frame);
263}
264
265} // namespace cricket
266#endif // WIN32