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
 * $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.
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

28 29 30
#include <npapi.h>
#include "vlcwindowless_mac.h"

31 32
#define SHOW_BRANDING 1

33
CGImageRef createImageNamed(CFStringRef);
34

35
CGImageRef createImageNamed(CFStringRef name)
36 37 38 39 40 41 42
{
    CFURLRef url = CFBundleCopyResourceURL(CFBundleGetBundleWithIdentifier(CFSTR("org.videolan.vlc-npapi-plugin")), name, CFSTR("png"), NULL);

    if (!url)
        return NULL;

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(url, NULL);
43 44
    if (!imageSource) {
        CFRelease(url);
45
        return NULL;
46
    }
47 48 49

    CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    CFRelease(imageSource);
50
    CFRelease(url);
51 52 53 54

    return image;
}

55 56 57
VlcWindowlessMac::VlcWindowlessMac(NPP instance, NPuint16_t mode) :
    VlcWindowlessBase(instance, mode)
{
58 59 60 61 62
    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");
    }
63 64 65 66
}

VlcWindowlessMac::~VlcWindowlessMac()
{
67 68
    if (lastFrame)
        CGImageRelease(lastFrame);
69 70
    if (colorspace != nil)
        CGColorSpaceRelease(colorspace);
71 72
}

73 74 75 76 77 78 79 80 81 82 83
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.);

84 85 86 87 88
    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.);

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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];
108
        values[0] = CTFontCreateWithName(CFSTR("HelveticaNeue-Light"),18,NULL);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        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

137 138 139
        CFStringRef versionText = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s — windowless mode — %@"), libvlc_get_version(), arch);
        attRef = CFAttributedStringCreate(kCFAllocatorDefault, versionText, stylesDict);
        CFRelease(versionText);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        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);
157 158 159 160 161 162 163 164 165

        // 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) {
166 167 168
            CFStringRef customText = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
            attRef = CFAttributedStringCreate(kCFAllocatorDefault, customText, stylesDict);
            CFRelease(customText);
169 170 171 172 173 174 175 176
            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);
177 178 179 180
    } else {
        CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
        CGContextSetFillColorWithColor(cgContext, backgroundColor);
        CGContextDrawPath(cgContext, kCGPathFill);
181 182 183 184 185 186 187 188 189 190

        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];
191
            values[0] = CTFontCreateWithName(CFSTR("HelveticaNeue-Light"),18,NULL);
192 193 194 195 196
            values[1] = kCFBooleanTrue;
            CFDictionaryRef stylesDict = CFDictionaryCreate(kCFAllocatorDefault,
                                                            (const void **)&keys,
                                                            (const void **)&values,
                                                            2, NULL, NULL);
197 198 199
            CFStringRef customText = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
            CFAttributedStringRef attRef = CFAttributedStringCreate(kCFAllocatorDefault, customText, stylesDict);
            CFRelease(customText);
200 201 202 203 204 205 206 207
            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);
        }
208
    }
209
    CGColorRelease(backgroundColor);
210 211 212 213

    CGContextRestoreGState(cgContext);
}

214 215 216 217 218
NPError VlcWindowlessMac::get_root_layer(void *value)
{
    return NPERR_GENERIC_ERROR;
}

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 249 250 251 252
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;
        }

253
        CGContextClearRect(cgContext, CGRectMake(0, 0, npwindow.width, npwindow.height) );
254

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

        CGContextSaveGState(cgContext);

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

        /* Compute the position of the video */
267 268 269
        float left = 0;
        float top  = 0;

270 271
        const size_t kComponentsPerPixel = 4;
        const size_t kBitsPerComponent = sizeof(unsigned char) * 8;
272 273 274 275 276 277 278 279
        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.;

280 281 282
            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 */
283 284 285
               SInt32 minorVersion;
               Gestalt(gestaltSystemVersionMinor, &minorVersion);
               if (minorVersion >= 11) {
286
                   fprintf(stderr, "Guessing color space based on video dimensions (%ix%i)\n", m_media_source_width, m_media_source_height);
287

288 289 290 291 292
                   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");
293 294
                       colorspace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
                   } else {
295
                       fprintf(stderr, "SD content, using linear RGB color space\n");
296 297 298
                       colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
                   }
               } else {
299
                   fprintf(stderr, "WARNING: OS does not support BT.709 or BT.2020 color spaces, output may vary\n");
300 301 302 303
                   colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
               }
            }

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            /* 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);
321 322

            CGDataProviderRelease(dataProvider);
323
            CFRelease(dataRef);
324 325 326 327 328 329 330 331 332 333 334 335 336 337

            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);
338 339
        }

340 341 342 343
        if(lastFrame) {
            CGContextDrawImage(cgContext, rect, lastFrame);
            CGImageRelease(lastFrame);
        }
344 345 346

        CGContextRestoreGState(cgContext);

347 348 349 350 351
        return true;
    }

    return VlcPluginBase::handle_event(event);
}
352 353 354 355 356 357 358 359 360 361 362 363

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();
    }
}