vlcwindowless_mac.cpp 14.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlcwindowless_mac.cpp: VLC NPAPI windowless plugin for Mac
 *****************************************************************************
4
 * Copyright (C) 2012-2014 VLC Authors and VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan # org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <npapi.h>
#include "vlcwindowless_mac.h"

27 28
#define SHOW_BRANDING 1

29
CGImageRef createImageNamed(CFStringRef);
30

31
CGImageRef createImageNamed(CFStringRef name)
32 33 34 35 36 37 38
{
    CFURLRef url = CFBundleCopyResourceURL(CFBundleGetBundleWithIdentifier(CFSTR("org.videolan.vlc-npapi-plugin")), name, CFSTR("png"), NULL);

    if (!url)
        return NULL;

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(url, NULL);
39 40
    if (!imageSource) {
        CFRelease(url);
41
        return NULL;
42
    }
43 44 45

    CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    CFRelease(imageSource);
46
    CFRelease(url);
47 48 49 50

    return image;
}

51 52 53
VlcWindowlessMac::VlcWindowlessMac(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode)
{
54 55 56 57 58
    const char *userAgent = NPN_UserAgent(this->getBrowser());
    if (strstr(userAgent, "Safari") && strstr(userAgent, "Version/5")) {
        legacy_drawing_mode = true;
        fprintf(stderr, "Safari 5 detected, using legacy drawing mode\n");
    }
59 60 61 62
}

VlcWindowlessMac::~VlcWindowlessMac()
{
63 64
    if (lastFrame)
        CGImageRelease(lastFrame);
65 66
    if (colorspace != nil)
        CGColorSpaceRelease(colorspace);
67 68
}

69 70 71 72 73 74 75 76 77 78 79
void VlcWindowlessMac::drawNoPlayback(CGContextRef cgContext)
{
    float windowWidth = npwindow.width;
    float windowHeight = npwindow.height;

    CGContextSaveGState(cgContext);

    // this context is flipped..
    CGContextTranslateCTM(cgContext, 0.0, windowHeight);
    CGContextScaleCTM(cgContext, 1., -1.);

80 81 82 83 84
    CGColorRef backgroundColor;
    unsigned r = 0, g = 0, b = 0;
    HTMLColor2RGB(get_options().get_bg_color().c_str(), &r, &g, &b);
    backgroundColor = CGColorCreateGenericRGB(r, g, b, 1.);

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if (get_options().get_enable_branding()) {
        // draw background
        CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
        CGContextSetFillColorWithColor(cgContext, backgroundColor);
        CGContextDrawPath(cgContext, kCGPathFill);

        // draw gradient
        CGImageRef gradient = createImageNamed(CFSTR("gradient"));
        CGContextDrawImage(cgContext, CGRectMake(0., 0., windowWidth, 150.), gradient);
        CGImageRelease(gradient);

        // draw info text
        CGContextSetGrayStrokeColor(cgContext, .95, 1.);
        CGContextSetTextDrawingMode(cgContext, kCGTextFill);
        CGContextSetGrayFillColor(cgContext, 1., 1.);
        CFStringRef keys[2];
        keys[0] = kCTFontAttributeName;
        keys[1] = kCTForegroundColorFromContextAttributeName;
        CFTypeRef values[2];
104
        values[0] = CTFontCreateWithName(CFSTR("HelveticaNeue-Light"),18,NULL);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        values[1] = kCFBooleanTrue;
        CFDictionaryRef stylesDict = CFDictionaryCreate(kCFAllocatorDefault,
                                                        (const void **)&keys,
                                                        (const void **)&values,
                                                        2, NULL, NULL);
        CFAttributedStringRef attRef = CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("VLC Web Plugin"), stylesDict);
        CTLineRef textLine = CTLineCreateWithAttributedString(attRef);
        CGContextSetTextPosition(cgContext, 25., 45.);
        CTLineDraw(textLine, cgContext);
        CFRelease(textLine);
        CFRelease(attRef);

        // print smaller text from here
        CFRelease(stylesDict);
        values[0] = CTFontCreateWithName(CFSTR("Helvetica"),12,NULL);
        stylesDict = CFDictionaryCreate(kCFAllocatorDefault,
                                        (const void **)&keys,
                                        (const void **)&values,
                                        2, NULL, NULL);

        // draw version string
        CFStringRef arch;
    #ifdef __x86_64__
        arch = CFSTR("64-bit");
    #else
        arch = CFSTR("32-bit");
    #endif

133 134 135
        CFStringRef versionText = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s — windowless mode — %@"), libvlc_get_version(), arch);
        attRef = CFAttributedStringCreate(kCFAllocatorDefault, versionText, stylesDict);
        CFRelease(versionText);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        textLine = CTLineCreateWithAttributedString(attRef);
        CGContextSetTextPosition(cgContext, 25., 25.);
        CTLineDraw(textLine, cgContext);
        CFRelease(textLine);
        CFRelease(attRef);
        CFRelease(stylesDict);

        // draw cone
        CGImageRef cone = createImageNamed(CFSTR("cone"));
        CGFloat coneWidth = CGImageGetWidth(cone);
        CGFloat coneHeight = CGImageGetHeight(cone);
        if (windowHeight <= 320.) {
            coneWidth = coneWidth / 2.;
            coneHeight = coneHeight / 2.;
        }
        CGContextDrawImage(cgContext, CGRectMake((windowWidth - coneWidth) / 2., (windowHeight - coneHeight) / 2., coneWidth, coneHeight), cone);
        CGImageRelease(cone);
153 154 155 156 157 158 159 160 161

        // draw custom text
        values[0] = CTFontCreateWithName(CFSTR("Helvetica"),14,NULL);
        stylesDict = CFDictionaryCreate(kCFAllocatorDefault,
                                        (const void **)&keys,
                                        (const void **)&values,
                                        2, NULL, NULL);
        const char *text = get_options().get_bg_text().c_str();
        if (text != NULL) {
162 163 164
            CFStringRef customText = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
            attRef = CFAttributedStringCreate(kCFAllocatorDefault, customText, stylesDict);
            CFRelease(customText);
165 166 167 168 169 170 171 172
            textLine = CTLineCreateWithAttributedString(attRef);
            CGRect textRect = CTLineGetImageBounds(textLine, cgContext);
            CGContextSetTextPosition(cgContext, ((windowWidth - textRect.size.width) / 2.), (windowHeight / 2.) + (coneHeight / 2.) + textRect.size.height + 50.);
            CTLineDraw(textLine, cgContext);
            CFRelease(textLine);
            CFRelease(attRef);
        }
        CFRelease(stylesDict);
173 174 175 176
    } else {
        CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
        CGContextSetFillColorWithColor(cgContext, backgroundColor);
        CGContextDrawPath(cgContext, kCGPathFill);
177 178 179 180 181 182 183 184 185 186

        const char *text = get_options().get_bg_text().c_str();
        if (text != NULL) {
            CGContextSetGrayStrokeColor(cgContext, .95, 1.);
            CGContextSetTextDrawingMode(cgContext, kCGTextFill);
            CGContextSetGrayFillColor(cgContext, 1., 1.);
            CFStringRef keys[2];
            keys[0] = kCTFontAttributeName;
            keys[1] = kCTForegroundColorFromContextAttributeName;
            CFTypeRef values[2];
187
            values[0] = CTFontCreateWithName(CFSTR("HelveticaNeue-Light"),18,NULL);
188 189 190 191 192
            values[1] = kCFBooleanTrue;
            CFDictionaryRef stylesDict = CFDictionaryCreate(kCFAllocatorDefault,
                                                            (const void **)&keys,
                                                            (const void **)&values,
                                                            2, NULL, NULL);
193 194 195
            CFStringRef customText = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
            CFAttributedStringRef attRef = CFAttributedStringCreate(kCFAllocatorDefault, customText, stylesDict);
            CFRelease(customText);
196 197 198 199 200 201 202 203
            CTLineRef textLine = CTLineCreateWithAttributedString(attRef);
            CGRect textRect = CTLineGetImageBounds(textLine, cgContext);
            CGContextSetTextPosition(cgContext, ((windowWidth - textRect.size.width) / 2.), (windowHeight / 2.));
            CTLineDraw(textLine, cgContext);
            CFRelease(textLine);
            CFRelease(attRef);
            CFRelease(stylesDict);
        }
204
    }
205
    CGColorRelease(backgroundColor);
206 207 208 209

    CGContextRestoreGState(cgContext);
}

210 211 212 213 214
NPError VlcWindowlessMac::get_root_layer(void *value)
{
    return NPERR_GENERIC_ERROR;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
bool VlcWindowlessMac::handle_event(void *event)
{
    NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;

    if (!event)
        return false;

    NPCocoaEventType eventType = cocoaEvent->type;

    switch (eventType) {
        case NPCocoaEventMouseDown:
        {
            if (cocoaEvent->data.mouse.clickCount >= 2)
                VlcWindowlessBase::toggle_fullscreen();

            return true;
        }
        case NPCocoaEventMouseUp:
        case NPCocoaEventKeyUp:
        case NPCocoaEventKeyDown:
        case NPCocoaEventFocusChanged:
        case NPCocoaEventScrollWheel:
            return true;

        default:
            break;
    }

    if (eventType == NPCocoaEventDrawRect) {
        CGContextRef cgContext = cocoaEvent->data.draw.context;
        if (!cgContext) {
            return false;
        }

249
        CGContextClearRect(cgContext, CGRectMake(0, 0, npwindow.width, npwindow.height) );
250

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
251
        if (m_media_width == 0 || m_media_height == 0 || (!lastFrame && !player().mlp().isPlaying())) {
252
            drawNoPlayback(cgContext);
253
            return true;
254
        }
255 256 257 258 259 260 261 262

        CGContextSaveGState(cgContext);

        /* context is flipped */
        CGContextTranslateCTM(cgContext, 0.0, npwindow.height);
        CGContextScaleCTM(cgContext, 1., -1.);

        /* Compute the position of the video */
263 264 265
        float left = 0;
        float top  = 0;

266 267
        const size_t kComponentsPerPixel = 4;
        const size_t kBitsPerComponent = sizeof(unsigned char) * 8;
268 269 270 271 272 273 274 275
        CGRect rect;

        if (m_media_width != 0 && m_media_height != 0) {
            cached_width = m_media_width;
            cached_height = m_media_height;
            left = (npwindow.width  - m_media_width) / 2.;
            top = (npwindow.height - m_media_height) / 2.;

276 277 278
            if (colorspace == nil) {
               /* support for BT.709 and BT.2020 color spaces was introduced with OS X 10.11
                * on older OS versions, we can't show correct colors, so we fallback on linear RGB */
279 280 281
               SInt32 minorVersion;
               Gestalt(gestaltSystemVersionMinor, &minorVersion);
               if (minorVersion >= 11) {
282
                   fprintf(stderr, "Guessing color space based on video dimensions (%ix%i)\n", m_media_source_width, m_media_source_height);
283

284 285 286 287 288
                   if (m_media_source_height >= 2000 || m_media_source_width >= 3800) {
                       fprintf(stderr, "Using BT.2020 color space\n");
                       colorspace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
                   } else if (m_media_source_height > 576) {
                       fprintf(stderr, "Using BT.709 color space\n");
289 290
                       colorspace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
                   } else {
291
                       fprintf(stderr, "SD content, using linear RGB color space\n");
292 293 294
                       colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
                   }
               } else {
295
                   fprintf(stderr, "WARNING: OS does not support BT.709 or BT.2020 color spaces, output may vary\n");
296 297 298 299
                   colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
               }
            }

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
            /* fetch frame */
            CFDataRef dataRef = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
                                                            (const uint8_t *)&m_frame_buf[0],
                                                            sizeof(m_frame_buf[0]),
                                                            kCFAllocatorNull);
            CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(dataRef);
            lastFrame = CGImageCreate(m_media_width,
                                      m_media_height,
                                      kBitsPerComponent,
                                      kBitsPerComponent * kComponentsPerPixel,
                                      kComponentsPerPixel * m_media_width,
                                      colorspace,
                                      kCGBitmapByteOrder16Big,
                                      dataProvider,
                                      NULL,
                                      true,
                                      kCGRenderingIntentPerceptual);
317 318

            CGDataProviderRelease(dataProvider);
319
            CFRelease(dataRef);
320 321 322 323 324 325 326 327 328 329 330 331 332 333

            if (!lastFrame) {
                fprintf(stderr, "image creation failed\n");
                CGImageRelease(lastFrame);
                CGContextRestoreGState(cgContext);
                return true;
            }

            rect = CGRectMake(left, top, m_media_width, m_media_height);
        } else {
            fprintf(stderr, "drawing old frame again\n");
            left = (npwindow.width - cached_width) / 2.;
            top = (npwindow.height - cached_height) / 2.;
            rect = CGRectMake(left, top, cached_width, cached_width);
334 335
        }

336 337 338 339
        if(lastFrame) {
            CGContextDrawImage(cgContext, rect, lastFrame);
            CGImageRelease(lastFrame);
        }
340 341 342

        CGContextRestoreGState(cgContext);

343 344 345 346 347
        return true;
    }

    return VlcPluginBase::handle_event(event);
}
348 349 350 351 352 353 354 355 356 357 358 359

void VlcWindowlessMac::video_display_cb(void * /*picture*/)
{
    if (p_browser) {
        if (!legacy_drawing_mode)
            NPN_PluginThreadAsyncCall(p_browser,
                                      VlcWindowlessBase::invalidate_window_proxy,
                                      this);
        else
            invalidate_window();
    }
}