vlcwindowless_mac.cpp 13.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 30 31 32 33 34 35 36 37 38
static CGImageRef createImageNamed(CFStringRef);

static CGImageRef createImageNamed(CFStringRef name)
{
    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
    colorspace = CGColorSpaceCreateDeviceRGB();
55 56 57 58 59 60

    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");
    }
61 62 63 64
}

VlcWindowlessMac::~VlcWindowlessMac()
{
65 66
    if (lastFrame)
        CGImageRelease(lastFrame);
67
    CGColorSpaceRelease(colorspace);
68 69
}

70 71 72 73 74 75 76 77 78 79 80
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.);

81 82 83 84 85
    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.);

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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];
105
        values[0] = CTFontCreateWithName(CFSTR("HelveticaNeue-Light"),18,NULL);
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 133
        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

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

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

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

    CGContextRestoreGState(cgContext);
}

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

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 249
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;
        }

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

252
        if (m_media_width == 0 || m_media_height == 0 || (!lastFrame && !VlcPluginBase::playlist_isplaying()) || !get_player().is_open()) {
253
            drawNoPlayback(cgContext);
254
            return true;
255
        }
256 257 258 259 260 261 262 263

        CGContextSaveGState(cgContext);

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

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

267 268
        static const size_t kComponentsPerPixel = 4;
        static const size_t kBitsPerComponent = sizeof(unsigned char) * 8;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        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.;

            /* 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);
294 295

            CGDataProviderRelease(dataProvider);
296
            CFRelease(dataRef);
297 298 299 300 301 302 303 304 305 306 307 308 309 310

            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);
311 312
        }

313 314 315 316
        if(lastFrame) {
            CGContextDrawImage(cgContext, rect, lastFrame);
            CGImageRelease(lastFrame);
        }
317 318 319

        CGContextRestoreGState(cgContext);

320 321 322 323 324
        return true;
    }

    return VlcPluginBase::handle_event(event);
}
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

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

void VlcWindowlessMac::set_player_window() {
    libvlc_video_set_format_callbacks(getMD(),
                                      video_format_proxy,
                                      video_cleanup_proxy);
    libvlc_video_set_callbacks(getMD(),
                               video_lock_proxy,
                               video_unlock_proxy,
                               video_display_proxy,
                               this);
}