Search code examples
iosgoogle-castcocoahttpserver

Chrome Cast won't play served video


Okay so I'm developing an app for the chrome cast on IOS. One of the functions my app will perform will be to play local videos from your device. To do this I'm using an external source found on github called CocoaHttpServer. This Http server allows me to upload files to a localhost service. To do this i use the following code to start my server:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        // Configure our logging framework.
    // To keep things simple and fast, we're just going to log to the Xcode console.
    [DDLog addLogger:[DDTTYLogger sharedInstance]];

    // Create server using our custom MyHTTPServer class
    httpServer = [[HTTPServer alloc] init];


    // Tell the server to broadcast its presence via Bonjour.
    // This allows browsers such as Safari to automatically discover our service.
    [httpServer setType:@"_http._tcp."];

    // Normally there's no need to run our server on any specific port.
    // Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
    // However, for easy testing you may want force a certain port so you can just hit the refresh button.
    // [httpServer setPort:12345];

    // Serve files from our embedded Web folder
    NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
    DDLogInfo(@"Setting document root: %@", webPath);

    [httpServer setDocumentRoot:webPath];
    [self startServer];

    gblvb = [GlobalVariables singleobj];

    self.mediaControlChannel = [[GCKMediaControlChannel alloc] init];
    self.mediaControlChannel.delegate = self;
    [gblvb.deviceManager addChannel:self.mediaControlChannel];
    [self.mediaControlChannel requestStatus];
}

This code then sets up my http server and points it to look in the web folder which is imported into my main bundle and contains a test video i download from google for testing purposes(this video google use in their sample of streaming to the chrome cast through the web). I got this video from http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4. As well as this the code also sets up a new media channel ready for the chrome cast to cast to...

Next I have my void function startServer which really establishes the connection and publishes the server...

- (void)startServer
{
    // Start the server (and check for problems)

    NSError *error;
    if([httpServer start:&error])
    {
        DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);

    }
    else
    {
        DDLogError(@"Error starting HTTP Server: %@", error);
    }
}

Lastly the code bellow sets the path to equal the localhost url and acctuallly cast the video to the chrome cast:

-(void)viewDidAppear:(BOOL)animated
{
path = [NSString stringWithFormat:@"localhost:%hu%@%@", [httpServer listeningPort], @"/", @"BigBuckBunny.mp4"];


    // Do any additional setup after loading the view from its nib.

    gblvb = [ GlobalVariables singleobj];


    deviceScanner = [[GCKDeviceScanner alloc] init];


    [deviceScanner addListener:self];
    [deviceScanner startScan];
    NSString *image;
    NSString *type;
    GCKMediaMetadata *metadata = [[GCKMediaMetadata alloc] init];


        image = @"Folder-Video-icon.png";

        [metadata setString:@"The MP4 file format defined some extensions over the ISO Base Media File Format to support MPEG-4 visual/audio codecs and various MPEG-4 Systems features such as object descriptors and scene descriptions."
                     forKey:kGCKMetadataKeySubtitle];
        type = @"video/mp4";
[metadata setString:[NSString stringWithFormat:@"%@%@", @"Casting " , gblvb.FileType]forKey:kGCKMetadataKeyTitle];



    [metadata addImage:[[GCKImage alloc]
                        initWithURL:[[NSURL alloc] initWithString:image]
                        width:480
                        height:360]];

    //define Media information
    sleep(2);
    GCKMediaInformation *mediaInformation =
    [[GCKMediaInformation alloc] initWithContentID:path
                                        streamType:GCKMediaStreamTypeNone
                                       contentType:type
                                          metadata:metadata
                                    streamDuration:0
                                        customData:nil];

    //cast video
    [_mediaControlChannel loadMedia:mediaInformation autoplay:TRUE playPosition:0];
    NSLog(@"Full Path : %@", path);
}

Now my problem is that when this http server is published and ready to cast the chrome cast does not play it even though when i actually navigate to the path on safari the video displays perfectly. As well as this i know that the chrome cast streams fine due to the fact it streams googles online example video just fine.

EDIT Here is my debugging log form the chrome cast:

Failed to load resource: the server responded with a status of 404 (Not Found) https://www.gstatic.com/eureka/player/undefined
 [  0.259s] [goog.net.WebSocket] Opening the WebSocket on ws://localhost:8008/v2/ipc
 cast_receiver.js:18
 [  0.580s] [goog.net.WebSocket] WebSocket opened on ws://localhost:8008/v2/ipc
 cast_receiver.js:18
GET https://www.gstatic.com/eureka/player/Folder-Video-icon.png 404 (Not Found) player.js:31
The page at 'https://www.gstatic.com/eureka/player/player.html?skin' was loaded over HTTPS, but displayed insecure content from 'http://localhost:49598/BigBuckBunny.mp4': this content should also be loaded over HTTPS.
 cast_receiver.js:69
GET http://localhost:49598/BigBuckBunny.mp4  cast_receiver.js:69
 [ 21.834s] [cast.receiver.MediaManager] Load metadata error
 cast_receiver.js:18
GET https://www.gstatic.com/eureka/player/Folder-Video-icon.png 404 (Not Found) 

Any Help will be appreciated.


Solution

  • I worked it out instead of testing with localhost on the simulator write a function to get the current devices IP address and where the localhost part was within this line path = [NSString stringWithFormat:@"localhost:%hu%@%@", [httpServer listeningPort], @"/", @"BigBuckBunny.mp4"]; replace it with the devices ip address.