blob: f96dfacad8d8791f0a51ce09752643190c8e9a01 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028// Implementation of CarbonVideoRenderer
29
30#include "talk/media/devices/carbonvideorenderer.h"
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include "talk/media/base/videocommon.h"
33#include "talk/media/base/videoframe.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000034#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36namespace cricket {
37
38CarbonVideoRenderer::CarbonVideoRenderer(int x, int y)
39 : image_width_(0),
40 image_height_(0),
41 x_(x),
42 y_(y),
43 image_ref_(NULL),
44 window_ref_(NULL) {
45}
46
47CarbonVideoRenderer::~CarbonVideoRenderer() {
48 if (window_ref_) {
49 DisposeWindow(window_ref_);
50 }
51}
52
53// Called from the main event loop. All renderering needs to happen on
54// the main thread.
55OSStatus CarbonVideoRenderer::DrawEventHandler(EventHandlerCallRef handler,
56 EventRef event,
57 void* data) {
58 OSStatus status = noErr;
59 CarbonVideoRenderer* renderer = static_cast<CarbonVideoRenderer*>(data);
60 if (renderer != NULL) {
61 if (!renderer->DrawFrame()) {
62 LOG(LS_ERROR) << "Failed to draw frame.";
63 }
64 }
65 return status;
66}
67
68bool CarbonVideoRenderer::DrawFrame() {
69 // Grab the image lock to make sure it is not changed why we'll draw it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 rtc::CritScope cs(&image_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
72 if (image_.get() == NULL) {
73 // Nothing to draw, just return.
74 return true;
75 }
76 int width = image_width_;
77 int height = image_height_;
78 CGDataProviderRef provider =
79 CGDataProviderCreateWithData(NULL, image_.get(), width * height * 4,
80 NULL);
81 CGColorSpaceRef color_space_ref = CGColorSpaceCreateDeviceRGB();
82 CGBitmapInfo bitmap_info = kCGBitmapByteOrderDefault;
83 CGColorRenderingIntent rendering_intent = kCGRenderingIntentDefault;
84 CGImageRef image_ref = CGImageCreate(width, height, 8, 32, width * 4,
85 color_space_ref, bitmap_info, provider,
86 NULL, false, rendering_intent);
87 CGDataProviderRelease(provider);
88
89 if (image_ref == NULL) {
90 return false;
91 }
92 CGContextRef context;
93 SetPortWindowPort(window_ref_);
94 if (QDBeginCGContext(GetWindowPort(window_ref_), &context) != noErr) {
95 CGImageRelease(image_ref);
96 return false;
97 }
98 Rect window_bounds;
99 GetWindowPortBounds(window_ref_, &window_bounds);
100
101 // Anchor the image to the top left corner.
102 int x = 0;
103 int y = window_bounds.bottom - CGImageGetHeight(image_ref);
104 CGRect dst_rect = CGRectMake(x, y, CGImageGetWidth(image_ref),
105 CGImageGetHeight(image_ref));
106 CGContextDrawImage(context, dst_rect, image_ref);
107 CGContextFlush(context);
108 QDEndCGContext(GetWindowPort(window_ref_), &context);
109 CGImageRelease(image_ref);
110 return true;
111}
112
113bool CarbonVideoRenderer::SetSize(int width, int height, int reserved) {
114 if (width != image_width_ || height != image_height_) {
115 // Grab the image lock while changing its size.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 rtc::CritScope cs(&image_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 image_width_ = width;
118 image_height_ = height;
119 image_.reset(new uint8[width * height * 4]);
120 memset(image_.get(), 255, width * height * 4);
121 }
122 return true;
123}
124
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000125bool CarbonVideoRenderer::RenderFrame(const VideoFrame* video_frame) {
126 if (!video_frame) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 return false;
128 }
129 {
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000130 const VideoFrame* frame = video_frame->GetCopyWithRotationApplied();
131
132 if (!SetSize(frame->GetWidth(), frame->GetHeight(), 0)) {
133 return false;
134 }
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 // Grab the image lock so we are not trashing up the image being drawn.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 rtc::CritScope cs(&image_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 frame->ConvertToRgbBuffer(cricket::FOURCC_ABGR,
139 image_.get(),
140 frame->GetWidth() * frame->GetHeight() * 4,
141 frame->GetWidth() * 4);
142 }
143
144 // Trigger a repaint event for the whole window.
145 Rect bounds;
146 InvalWindowRect(window_ref_, GetWindowPortBounds(window_ref_, &bounds));
147 return true;
148}
149
150bool CarbonVideoRenderer::Initialize() {
151 OSStatus err;
152 WindowAttributes attributes =
153 kWindowStandardDocumentAttributes |
154 kWindowLiveResizeAttribute |
155 kWindowFrameworkScaledAttribute |
156 kWindowStandardHandlerAttribute;
157
158 struct Rect bounds;
159 bounds.top = y_;
160 bounds.bottom = 480;
161 bounds.left = x_;
162 bounds.right = 640;
163 err = CreateNewWindow(kDocumentWindowClass, attributes,
164 &bounds, &window_ref_);
165 if (!window_ref_ || err != noErr) {
166 LOG(LS_ERROR) << "CreateNewWindow failed, error code: " << err;
167 return false;
168 }
169 static const EventTypeSpec event_spec = {
170 kEventClassWindow,
171 kEventWindowDrawContent
172 };
173
174 err = InstallWindowEventHandler(
175 window_ref_,
176 NewEventHandlerUPP(CarbonVideoRenderer::DrawEventHandler),
177 GetEventTypeCount(event_spec),
178 &event_spec,
179 this,
180 NULL);
181 if (err != noErr) {
182 LOG(LS_ERROR) << "Failed to install event handler, error code: " << err;
183 return false;
184 }
185 SelectWindow(window_ref_);
186 ShowWindow(window_ref_);
187 return true;
188}
189
190} // namespace cricket