VLCPlayerControlWebSocket.m 9.14 KB
Newer Older
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
/*****************************************************************************
 * VLCPlayerControlWebSocket.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCPlayerControlWebSocket.h"

@implementation VLCPlayerControlWebSocket

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didOpen
{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self
                           selector:@selector(playbackStarted)
                               name:VLCPlaybackControllerPlaybackDidStart
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(playbackStarted)
                               name:VLCPlaybackControllerPlaybackDidResume
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(_respondToPlaying)
                               name:VLCPlaybackControllerPlaybackMetadataDidChange
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(playbackPaused)
                               name:VLCPlaybackControllerPlaybackDidPause
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(playbackEnded)
                               name:VLCPlaybackControllerPlaybackDidStop
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(playbackEnded)
                               name:VLCPlaybackControllerPlaybackDidFail
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(playbackSeekTo)
                               name:VLCPlaybackControllerPlaybackPositionUpdated
                             object:nil];

    APLog(@"web socket did open");

    [super didOpen];
}

- (void)didReceiveMessage:(NSString *)msg
{
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    NSError *error;
    NSDictionary *receivedDict = [NSJSONSerialization JSONObjectWithData:[msg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];

    if (error != nil) {
        APLog(@"JSON deserialization failed for %@", msg);
        return;
    }

    NSString *type = receivedDict[@"type"];
    if (!type) {
        APLog(@"No type in received JSON dict %@", receivedDict);
    }

    if ([type isEqualToString:@"playing"]) {
75
        [self _respondToPlaying];
76
    } else if ([type isEqualToString:@"play"]) {
77
        [self _respondToPlay];
78
    } else if ([type isEqualToString:@"pause"]) {
79
        [self _respondToPause];
80
    } else if ([type isEqualToString:@"ended"]) {
81
        [self _respondToEnded];
82 83 84
    } else if ([type isEqualToString:@"seekTo"]) {
        [self _respondToSeek:receivedDict];
    } else if ([type isEqualToString:@"volume"]) {
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
        [self sendMessage:@"VOLUME CONTROL NOT SUPPORTED ON THIS DEVICE"];
    } else
        [self sendMessage:@"INVALID REQUEST!"];
}

#ifndef NDEBUG
- (void)didClose
{
    APLog(@"web socket did close");

    [super didClose];
}
#endif

- (void)_respondToPlaying
{
    /* JSON response
     {
        "type": "playing",
        "currentTime": 42,
        "media": {
            "id": "some id",
            "title": "some title",
            "duration": 120000
        }
     }
     */

    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    NSDictionary *returnDict;

    if (vpc.activePlaybackSession) {
        VLCMediaPlayer *player = vpc.mediaPlayer;
        if (player) {
            VLCMedia *media = player.media;

            if (media) {
122
                NSURL *url = media.url;
123 124
                NSString *mediaTitle = vpc.mediaTitle;
                if (!mediaTitle)
125 126
                    mediaTitle = url.lastPathComponent;
                NSDictionary *mediaDict = @{ @"id" : url.absoluteString,
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
                                             @"title" : mediaTitle,
                                             @"duration" : @(media.length.intValue)};
                returnDict = @{ @"currentTime" : @(player.time.intValue),
                                @"type" : @"playing",
                                @"media" : mediaDict };
            }
        }
    }
    if (!returnDict) {
        returnDict = [NSDictionary dictionary];
    }

    NSError *error;
    NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
    if (error != nil) {
        APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
    }

    [self sendData:returnData];
}

#pragma mark - play

- (void)_respondToPlay
{
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaListPlayer *listPlayer = vpc.listPlayer;
    if (listPlayer) {
        [listPlayer play];
    }
}

- (void)playbackStarted
{
    /*
     {
        "type": "play",
164
        "currentTime": 42
165 166 167 168 169 170 171 172 173
     }
     */

    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaPlayer *player = vpc.mediaPlayer;
    if (player) {
        VLCMedia *media = player.media;
        if (media) {
            NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
174
                                          @"type" : @"play" };
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

            NSError *error;
            NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
            if (error != nil) {
                APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
            }

            [self sendData:returnData];
        }
    }
}

#pragma mark - pause

- (void)_respondToPause
{
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaListPlayer *listPlayer = vpc.listPlayer;
    if (listPlayer) {
        [listPlayer pause];
    }
}

- (void)playbackPaused
{
    /*
     {
        "type": "pause",
        "currentTime": 42,
     }
     */

    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaPlayer *player = vpc.mediaPlayer;
    if (player) {
        VLCMedia *media = player.media;
        if (media) {
            NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
213
                                          @"type" : @"pause" };
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

            NSError *error;
            NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
            if (error != nil) {
                APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
            }

            [self sendData:returnData];
        }
    }
}

#pragma mark - ended

- (void)_respondToEnded
{
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    [vpc stopPlayback];
}

- (void)playbackEnded
{
    /*
     {
238
        "type": "ended"
239 240 241 242 243 244 245 246
     }
     */

    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaPlayer *player = vpc.mediaPlayer;
    if (player) {
        VLCMedia *media = player.media;
        if (media) {
247
            NSDictionary *returnDict = @{ @"type" : @"ended" };
248 249 250 251 252 253 254 255 256 257 258 259 260 261

            NSError *error;
            NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
            if (error != nil) {
                APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
            }

            [self sendData:returnData];
        }
    }
}

#pragma mark - seek

262
- (void)_respondToSeek:(NSDictionary *)dictionary
263
{
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    /*
     {
        "currentTime" = 12514;
        "type" = seekTo;
     }
     */
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaPlayer *player = vpc.mediaPlayer;

    if (!player)
        return;

    VLCMedia *media = player.media;
    if (!media)
        return;

    player.position = [dictionary[@"currentTime"] floatValue] / (CGFloat)media.length.intValue;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
}

- (void)playbackSeekTo
{
    /* 
     {
        "type": "seekTo",
        "currentTime": 42,
        "media": {
            "id": 42
        }
     }
     */

    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    VLCMediaPlayer *player = vpc.mediaPlayer;
    if (player) {
        VLCMedia *media = player.media;
        if (media) {
            NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString};
            NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
                                          @"type" : @"seekTo",
                                          @"media" : mediaDict };

            NSError *error;
            NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
            if (error != nil) {
                APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
            }

            [self sendData:returnData];
        }
    }
}

@end