| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // Implementation of GdiVideoRenderer on Windows | |
| 12 | |
| 13 #ifdef WIN32 | |
| 14 | |
| 15 #include "webrtc/media/devices/gdivideorenderer.h" | |
| 16 | |
| 17 #include "libyuv/convert_argb.h" | |
| 18 #include "webrtc/base/thread.h" | |
| 19 #include "webrtc/base/win32window.h" | |
| 20 #include "webrtc/media/engine/webrtcvideoframe.h" | |
| 21 | |
| 22 namespace cricket { | |
| 23 | |
| 24 ///////////////////////////////////////////////////////////////////////////// | |
| 25 // Definition of private class VideoWindow. We use a worker thread to manage | |
| 26 // the window. | |
| 27 ///////////////////////////////////////////////////////////////////////////// | |
| 28 class GdiVideoRenderer::VideoWindow : public rtc::Win32Window { | |
| 29 public: | |
| 30 VideoWindow(int x, int y, int width, int height); | |
| 31 virtual ~VideoWindow(); | |
| 32 | |
| 33 // Called when a new frame is available. Upon this call, we send | |
| 34 // kRenderFrameMsg to the window thread. Context: non-worker thread. It may be | |
| 35 // better to pass RGB bytes to VideoWindow. However, we pass VideoFrame to put | |
| 36 // all the thread synchronization within VideoWindow. | |
| 37 void OnFrame(const VideoFrame& frame); | |
| 38 | |
| 39 protected: | |
| 40 // Override virtual method of rtc::Win32Window. Context: worker Thread. | |
| 41 bool OnMessage(UINT uMsg, | |
| 42 WPARAM wParam, | |
| 43 LPARAM lParam, | |
| 44 LRESULT& result) override; | |
| 45 | |
| 46 private: | |
| 47 enum { kSetSizeMsg = WM_USER, kRenderFrameMsg}; | |
| 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 class WindowThread : public rtc::Thread { | |
| 55 public: | |
| 56 explicit WindowThread(VideoWindow* window) : window_(window) {} | |
| 57 | |
| 58 virtual ~WindowThread() { | |
| 59 Stop(); | |
| 60 } | |
| 61 | |
| 62 // Override virtual method of rtc::Thread. Context: worker Thread. | |
| 63 virtual void Run() { | |
| 64 // Initialize the window | |
| 65 if (!window_ || !window_->Initialize()) { | |
| 66 return; | |
| 67 } | |
| 68 // Run the message loop | |
| 69 MSG msg; | |
| 70 while (GetMessage(&msg, NULL, 0, 0) > 0) { | |
| 71 TranslateMessage(&msg); | |
| 72 DispatchMessage(&msg); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 VideoWindow* window_; | |
| 78 }; | |
| 79 | |
| 80 // Context: worker Thread. | |
| 81 bool Initialize(); | |
| 82 void OnPaint(); | |
| 83 void OnSize(int width, int height, bool frame_changed); | |
| 84 void OnRenderFrame(const VideoFrame* frame); | |
| 85 | |
| 86 BITMAPINFO bmi_; | |
| 87 std::unique_ptr<uint8_t[]> image_; | |
| 88 std::unique_ptr<WindowThread> window_thread_; | |
| 89 // The initial position of the window. | |
| 90 int initial_x_; | |
| 91 int initial_y_; | |
| 92 }; | |
| 93 | |
| 94 ///////////////////////////////////////////////////////////////////////////// | |
| 95 // Implementation of class VideoWindow | |
| 96 ///////////////////////////////////////////////////////////////////////////// | |
| 97 GdiVideoRenderer::VideoWindow::VideoWindow( | |
| 98 int x, int y, int width, int height) | |
| 99 : initial_x_(x), | |
| 100 initial_y_(y) { | |
| 101 memset(&bmi_.bmiHeader, 0, sizeof(bmi_.bmiHeader)); | |
| 102 bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
| 103 bmi_.bmiHeader.biPlanes = 1; | |
| 104 bmi_.bmiHeader.biBitCount = 32; | |
| 105 bmi_.bmiHeader.biCompression = BI_RGB; | |
| 106 bmi_.bmiHeader.biWidth = width; | |
| 107 bmi_.bmiHeader.biHeight = -height; | |
| 108 bmi_.bmiHeader.biSizeImage = width * height * 4; | |
| 109 | |
| 110 image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]); | |
| 111 } | |
| 112 | |
| 113 GdiVideoRenderer::VideoWindow::~VideoWindow() { | |
| 114 // Context: caller Thread. We cannot call Destroy() since the window was | |
| 115 // created by another thread. Instead, we send WM_CLOSE message. | |
| 116 if (handle()) { | |
| 117 SendMessage(handle(), WM_CLOSE, 0, 0); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 bool GdiVideoRenderer::VideoWindow::SetSize(int width, int height) { | |
| 122 if (!window_thread_.get()) { | |
| 123 // Create and start the window thread. | |
| 124 window_thread_.reset(new WindowThread(this)); | |
| 125 return window_thread_->Start(); | |
| 126 } else if (width != bmi_.bmiHeader.biWidth || | |
| 127 height != -bmi_.bmiHeader.biHeight) { | |
| 128 SendMessage(handle(), kSetSizeMsg, 0, MAKELPARAM(width, height)); | |
| 129 } | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 void GdiVideoRenderer::VideoWindow::OnFrame(const VideoFrame& video_frame) { | |
| 134 if (!handle()) { | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 const cricket::WebRtcVideoFrame frame( | |
| 139 webrtc::I420Buffer::Rotate(video_frame.video_frame_buffer(), | |
| 140 video_frame.rotation()), | |
| 141 webrtc::kVideoRotation_0, video_frame.timestamp_us()); | |
| 142 | |
| 143 if (SetSize(frame.width(), frame.height())) { | |
| 144 SendMessage(handle(), kRenderFrameMsg, reinterpret_cast<WPARAM>(&frame), 0); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 bool GdiVideoRenderer::VideoWindow::OnMessage(UINT uMsg, WPARAM wParam, | |
| 149 LPARAM lParam, LRESULT& result) { | |
| 150 switch (uMsg) { | |
| 151 case WM_PAINT: | |
| 152 OnPaint(); | |
| 153 return true; | |
| 154 | |
| 155 case WM_DESTROY: | |
| 156 PostQuitMessage(0); // post WM_QUIT to end the message loop in Run() | |
| 157 return false; | |
| 158 | |
| 159 case WM_SIZE: // The window UI was resized. | |
| 160 OnSize(LOWORD(lParam), HIWORD(lParam), false); | |
| 161 return true; | |
| 162 | |
| 163 case kSetSizeMsg: // The video resolution changed. | |
| 164 OnSize(LOWORD(lParam), HIWORD(lParam), true); | |
| 165 return true; | |
| 166 | |
| 167 case kRenderFrameMsg: | |
| 168 OnRenderFrame(reinterpret_cast<const VideoFrame*>(wParam)); | |
| 169 return true; | |
| 170 } | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 bool GdiVideoRenderer::VideoWindow::Initialize() { | |
| 175 if (!rtc::Win32Window::Create( | |
| 176 NULL, L"Video Renderer", | |
| 177 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, | |
| 178 WS_EX_APPWINDOW, | |
| 179 initial_x_, initial_y_, | |
| 180 bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight)) { | |
| 181 return false; | |
| 182 } | |
| 183 OnSize(bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight, false); | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 void GdiVideoRenderer::VideoWindow::OnPaint() { | |
| 188 RECT rcClient; | |
| 189 GetClientRect(handle(), &rcClient); | |
| 190 PAINTSTRUCT ps; | |
| 191 HDC hdc = BeginPaint(handle(), &ps); | |
| 192 StretchDIBits(hdc, | |
| 193 0, 0, rcClient.right, rcClient.bottom, // destination rect | |
| 194 0, 0, bmi_.bmiHeader.biWidth, -bmi_.bmiHeader.biHeight, // source rect | |
| 195 image_.get(), &bmi_, DIB_RGB_COLORS, SRCCOPY); | |
| 196 EndPaint(handle(), &ps); | |
| 197 } | |
| 198 | |
| 199 void GdiVideoRenderer::VideoWindow::OnSize(int width, int height, | |
| 200 bool frame_changed) { | |
| 201 // Get window and client sizes | |
| 202 RECT rcClient, rcWindow; | |
| 203 GetClientRect(handle(), &rcClient); | |
| 204 GetWindowRect(handle(), &rcWindow); | |
| 205 | |
| 206 // Find offset between window size and client size | |
| 207 POINT ptDiff; | |
| 208 ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right; | |
| 209 ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom; | |
| 210 | |
| 211 // Resize client | |
| 212 MoveWindow(handle(), rcWindow.left, rcWindow.top, | |
| 213 width + ptDiff.x, height + ptDiff.y, false); | |
| 214 UpdateWindow(handle()); | |
| 215 ShowWindow(handle(), SW_SHOW); | |
| 216 | |
| 217 if (frame_changed && (width != bmi_.bmiHeader.biWidth || | |
| 218 height != -bmi_.bmiHeader.biHeight)) { | |
| 219 // Update the bmi and image buffer | |
| 220 bmi_.bmiHeader.biWidth = width; | |
| 221 bmi_.bmiHeader.biHeight = -height; | |
| 222 bmi_.bmiHeader.biSizeImage = width * height * 4; | |
| 223 image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void GdiVideoRenderer::VideoWindow::OnRenderFrame(const VideoFrame* frame) { | |
| 228 if (!frame) { | |
| 229 return; | |
| 230 } | |
| 231 // Convert frame to ARGB format, which is accepted by GDI | |
| 232 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( | |
| 233 frame->video_frame_buffer()); | |
| 234 libyuv::I420ToARGB(buffer->DataY(), buffer->StrideY(), | |
| 235 buffer->DataU(), buffer->StrideU(), | |
| 236 buffer->DataV(), buffer->StrideV(), | |
| 237 image_.get(), bmi_.bmiHeader.biWidth * 4, | |
| 238 buffer->width(), buffer->height()); | |
| 239 InvalidateRect(handle(), 0, 0); | |
| 240 } | |
| 241 | |
| 242 ///////////////////////////////////////////////////////////////////////////// | |
| 243 // Implementation of class GdiVideoRenderer | |
| 244 ///////////////////////////////////////////////////////////////////////////// | |
| 245 GdiVideoRenderer::GdiVideoRenderer(int x, int y) | |
| 246 : initial_x_(x), | |
| 247 initial_y_(y) { | |
| 248 } | |
| 249 GdiVideoRenderer::~GdiVideoRenderer() {} | |
| 250 | |
| 251 void GdiVideoRenderer::OnFrame(const VideoFrame& frame) { | |
| 252 if (!window_.get()) { // Create the window for the first frame | |
| 253 window_.reset( | |
| 254 new VideoWindow(initial_x_, initial_y_, frame.width(), frame.height())); | |
| 255 } | |
| 256 window_->OnFrame(frame); | |
| 257 } | |
| 258 | |
| 259 } // namespace cricket | |
| 260 #endif // WIN32 | |
| OLD | NEW |