summaryrefslogtreecommitdiffstats
path: root/macosx/HandBrakeXPCService/HandBrakeXPCService.m
blob: facf012681d413e8ae22722fb095285ef10d6fae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*  This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HandBrakeXPCService.h"
#import "HBOutputRedirect.h"

@import HandBrakeKit;

static void *HandBrakeXPCServiceContext = &HandBrakeXPCServiceContext;

@interface HandBrakeXPCService () <HBOutputRedirectListening>

@property (nonatomic, readonly) HBCore *core;

@property (nonatomic, readonly, copy) HBCoreProgressHandler progressHandler;
@property (nonatomic, readonly, copy) HBCoreCompletionHandler completionHandler;
@property (nonatomic, readwrite, copy) void (^reply)(HBCoreResult);

@property (nonatomic, readonly, weak) NSXPCConnection *connection;
@property (nonatomic, readonly) dispatch_queue_t queue;

@property (nonatomic, readwrite) NSArray<NSURL *> *urls;

@end

@implementation HandBrakeXPCService

- (instancetype)initWithConnection:(NSXPCConnection *)connection
{
    self = [super init];
    if (self)
    {
        _connection = connection;

        dispatch_queue_attr_t attr;
        attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0);
        _queue = dispatch_queue_create("fr.handbrake.CoreQueue", attr);

        // Add ourself as stderr/stdout listener
        [HBOutputRedirect.stderrRedirect addListener:self queue:_queue];
        [HBOutputRedirect.stdoutRedirect addListener:self queue:_queue];
    }
    return self;
}

- (void)setDVDNav:(BOOL)enabled
{
    dispatch_sync(_queue, ^{
        [HBCore setDVDNav:enabled];
    });
}

- (void)setUpWithLogLevel:(NSInteger)level name:(NSString *)name
{
    [HBCore initGlobal];

    _core = [[HBCore alloc] initWithLogLevel:level queue:_queue];
    _core.name = name;

    // Completion handler
    void (^completionHandler)(HBCoreResult result) = ^(HBCoreResult result)
    {
        self->_progressHandler = nil;
        self.reply(result);
        self.reply = nil;
    };
    _completionHandler = completionHandler;

    // Set up observers
    [self.core addObserver:self forKeyPath:@"state"
                   options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
                   context:HandBrakeXPCServiceContext];

    [NSProcessInfo.processInfo disableAutomaticTermination:@"Core started"];
}

- (void)tearDown
{
    _core = nil;
    [HBCore closeGlobal];
    [NSProcessInfo.processInfo enableSuddenTermination];
}

- (void)setLogLevel:(NSInteger)logLevel
{
    dispatch_sync(_queue, ^{
        self.core.logLevel = logLevel;
    });
}

- (void)provideResourceAccessWithBookmarks:(NSArray<NSData *> *)bookmarks
{
    dispatch_sync(_queue, ^{
        NSMutableArray<NSURL *> *urls = [NSMutableArray array];
        for (NSData *bookmark in bookmarks)
        {
            NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark options:0 relativeToURL:nil bookmarkDataIsStale:NULL error:NULL];
            if (url)
            {
                [urls addObject:url];
            }
        }
        self.urls = urls;
    });
}

- (void)setAutomaticallyPreventSleep:(BOOL)automaticallyPreventSleep
{
    dispatch_sync(_queue, ^{
        self.core.automaticallyPreventSleep = automaticallyPreventSleep;
    });
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == HandBrakeXPCServiceContext)
    {
        [self.connection.remoteObjectProxy updateState:self.core.state];
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)preventSleep
{
    dispatch_sync(_queue, ^{
        [self.core preventSleep];
    });
}

- (void)allowSleep
{
    dispatch_sync(_queue, ^{
        [self.core allowSleep];
    });
}

- (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)previewsNum minDuration:(NSUInteger)seconds keepPreviews:(BOOL)keepPreviews withReply:(void (^)(HBCoreResult))reply
{
    dispatch_sync(_queue, ^{
        void (^progressHandler)(HBState state, HBProgress progress, NSString *info) = ^(HBState state, HBProgress progress, NSString *info)
        {
            [self.connection.remoteObjectProxy updateProgress:progress.percent hours:progress.hours minutes:progress.minutes seconds:progress.seconds state:state info:info];
        };
        self->_progressHandler = progressHandler;
        self.reply = reply;

        [self.core scanURL:url titleIndex:index previews:previewsNum minDuration:seconds keepPreviews:keepPreviews
           progressHandler:self.progressHandler
         completionHandler:self.completionHandler];
    });
}

- (void)cancelScan
{
    dispatch_sync(_queue, ^{
        [self.core cancelScan];
    });
}

 - (void)encodeJob:(HBJob *)job withReply:(void (^)(HBCoreResult))reply
{
    dispatch_sync(_queue, ^{
        void (^progressHandler)(HBState state, HBProgress progress, NSString *info) = ^(HBState state, HBProgress progress, NSString *info)
        {
            [self.connection.remoteObjectProxy updateProgress:progress.percent hours:progress.hours minutes:progress.minutes seconds:progress.seconds state:state info:info];
        };
        self->_progressHandler = progressHandler;
        self.reply = reply;

        // Reset the title in the job.
        job.title = self.core.titles.firstObject;
        [self.core encodeJob:job
             progressHandler:self.progressHandler
           completionHandler:self.completionHandler];
    });
}

- (void)cancelEncode
{
    dispatch_sync(_queue, ^{
        [self.core cancelEncode];
    });
}

- (void)pauseEncode
{
    dispatch_sync(_queue, ^{
        [self.core pause];
    });
}

- (void)resumeEncode
{
    dispatch_sync(_queue, ^{
        [self.core resume];
    });
}

- (void)redirect:(nonnull NSString *)text type:(HBRedirectType)type
{
    if (type == HBRedirectTypeOutput)
    {
        [self.connection.remoteObjectProxy forwardError:text];
    }
    else
    {
        [self.connection.remoteObjectProxy forwardOutput:text];
    }
}

@end