VLCExtensionsManager.m 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
//
//  VLCExtensionsManager.m
//  VLCKit
//
//  Created by Pierre d'Herbemont on 1/26/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "VLCExtensionsManager.h"
#import "VLCExtension.h"
#import "VLCLibrary.h"
#import "VLCLibVLCBridging.h"
#import <vlc_extensions.h>
14
#import <vlc_input.h>
15
#include <vlc_modules.h>
16

17
// Here comes the nasty hack.
18
#define MODULE_STRING "VLCKit"
19 20
#import "../../../../lib/media_player_internal.h"
#import "../../../../lib/libvlc_internal.h"
21 22 23

static input_thread_t *libvlc_media_player_get_input_thread(libvlc_media_player_t *player)
{
24 25
    vlc_mutex_lock(&player->input.lock);
    input_thread_t *input = player->input.p_thread;
26 27
    if(input)
        vlc_object_hold(input);
28
    vlc_mutex_unlock(&player->input.lock);
29 30 31 32 33 34 35 36 37 38 39 40 41 42
    return input;
}

static vlc_object_t *libvlc_get_vlc_instance(libvlc_instance_t *instance)
{
    vlc_mutex_lock(&instance->instance_lock);
    libvlc_int_t *libvlc = instance->p_libvlc_int;
    if(libvlc)
        vlc_object_hold(libvlc);
    vlc_mutex_unlock(&instance->instance_lock);
    return VLC_OBJECT(libvlc);
}


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#define _instance ((extensions_manager_t *)instance)

@implementation VLCExtensionsManager
static VLCExtensionsManager *sharedManager = nil;

+ (VLCExtensionsManager *)sharedManager
{
    if (!sharedManager)
    {
        /* Initialize a shared instance */
        sharedManager = [[self alloc] init];
    }
    return sharedManager;
}

- (void)dealloc
{
    module_unneed(_instance, _instance->p_module);
    vlc_object_release(_instance);

63 64 65
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [_extensions release];
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    [super dealloc];
}

- (NSArray *)extensions
{
    if (!instance)
    {
        vlc_object_t *libvlc = libvlc_get_vlc_instance([VLCLibrary sharedInstance]);
        instance = vlc_object_create(libvlc, sizeof(extensions_manager_t));
        if (!_instance)
        {
            vlc_object_release(libvlc);
            return nil;
        }

        _instance->p_module = module_need(_instance, "extension", NULL, false);
        NSAssert(_instance->p_module, @"Unable to load extensions module");

        vlc_object_release(libvlc);
    }

87 88 89
    if (_extensions)
        return _extensions;
    _extensions = [[NSMutableArray alloc] init];
90
    extension_t *ext;
91
    vlc_mutex_lock(&_instance->lock);
92 93
    FOREACH_ARRAY(ext, _instance->extensions)
        VLCExtension *extension = [[VLCExtension alloc] initWithInstance:ext];
94
        [_extensions addObject:extension];
95 96
        [extension release];
    FOREACH_END()
97
    vlc_mutex_unlock(&_instance->lock);
98
    return _extensions;
99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

- (void)runExtension:(VLCExtension *)extension
{
    extension_t *ext = [extension instance];

    if(extension_TriggerOnly(_instance, ext))
        extension_Trigger(_instance, ext);
    else
    {
        if(!extension_IsActivated(_instance, ext))
            extension_Activate(_instance, ext);
    }
}
113 114 115 116

- (void)mediaPlayerLikelyChangedInput
{
    input_thread_t *input = _player ? libvlc_media_player_get_input_thread([_player libVLCMediaPlayer]) : NULL;
117 118 119 120 121 122

    // Don't send more than appropriate
    if (_previousInput == input)
        return;
    _previousInput = input;

123 124 125 126 127 128 129 130 131 132
    for(VLCExtension *extension in _extensions)
        extension_SetInput(_instance, [extension instance], input);
    if (input)
        vlc_object_release(input);
}

- (void)setMediaPlayer:(VLCMediaPlayer *)player
{
    if (_player == player)
        return;
133 134 135 136 137 138

    // Don't set a NULL mediaPlayer.
    // so that we always have an input around.
    if (!player)
        return;

139 140 141 142 143 144 145 146 147 148 149 150
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:VLCMediaPlayerStateChanged object:_player];

    [_player release];
    _player = [player retain];

    [self mediaPlayerLikelyChangedInput];


    if (player)
        [center addObserver:self selector:@selector(mediaPlayerLikelyChangedInput) name:VLCMediaPlayerStateChanged object:_player];
}
151 152 153 154 155

- (VLCMediaPlayer *)mediaPlayer
{
    return _player;
}
156
@end