arcam / CocoaUPnP

CocoaUPnP is a logical progression of upnpx; designed to be easy, modern and block-based.
MIT License
85 stars 40 forks source link

Trying to stream an audio file to a renderer #69

Closed fareast555 closed 2 years ago

fareast555 commented 2 years ago

Hi all.

I'm trying to incorporate CocoaUPnP into a project that requires streaming an mp3 audio file to a Raspberry Pi media renderer. I can connect and play an mp3 file that is hosted on a remote server (See hackey code below), but I'm not seeing anything in the framework to allow me to set data to the UPPAVTransportService to stream.

Super new to all this, and there's no documentation of how to stream a file. Any suggestions?

Note that if I can get this up and running, I'd be happy to write a full set-up guide for your wiki in exchange for help getting this working and/or shout a round of beers!

- (void) playTestSound {
    UPPMediaItem *item = [UPPMediaItem new];
    item.itemTitle = @"This is a test";
    item.objectClass = @"object.item.audioItem.musicTrack";
    item.objectID = @"0";
    item.parentID = @"1";
    item.isContainer = NO;
    item.durationInSeconds = (NSInteger) 20;
    item.artworkResources = [NSArray new];
    NSString *protocolInfo = @"http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000";
    UPPMediaItemResource *resource = [UPPMediaItemResource new];
    resource.protocolInfo = protocolInfo;
    resource.resourceURLString = @"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
    resource.duration = @"00:00:20";

    NSMutableArray *resources = [[NSMutableArray alloc] init];
    [resources addObject:resource];
    item.resources = resources;

    [self.playbackManager playItem:item];
}
squarefrog commented 2 years ago

I’m out on vacation at the moment so I can’t give you code level advice, but I think I can give you some pointers.

It’s been a while since I implemented this but UPnP works as follows:

that should get you up and running. I think the example project should give you guidance on that, although I’m not sure it compiles these days.

fareast555 commented 2 years ago

Hi @squarefrog Thanks for the quick reply. Appreciated!

Yeah, I did exactly that, but that only seems to work if it's a file hosted where I can just pass a url. The Media object and AVTRansport objects don't have any properties to wrap or deal with any kind of streaming data. There doesn't seem to be anything in the project that demos how one might stream a file on the local iOS device.

squarefrog commented 2 years ago

Ahh yes, that’s how UPnP works. You point a URL to a file and send that URL to the transport service. The renderer then fetches the data and plays it.

If you’re wanting to send a file, that’s trickier. You can use something like GCDWebServer to host the file from your phone, and send out that URL to your raspberry pi.

squarefrog commented 2 years ago

If it’s audio hosted in the music library itself, this is significantly more complicated and requires heavy workarounds like streaming the file to the documents directory, then serving that file up.

At that point I’d probably recommend you find an AirPlay compatible raspberry pi OS and use that as it will be significantly easier.

fareast555 commented 2 years ago

Yeah, the basic thing we wanted to do was take an mp3 from the apps file directory and pass it to the renderer on the pi.

Will take a look into GCDWebServer. If I can get something working, I'll give you a shout here. Thanks for the intel so far!

squarefrog commented 2 years ago

Yep you should be able to do that with GCDWebServer no problem at all!

One thing you may need to do is request multicast entitlements from Apple in order to discovery devices on your network, but that’s easy enough. Look at some of the other issues here and you might find a guide.

fareast555 commented 2 years ago

Thanks for the tip, @squarefrog!

fareast555 commented 2 years ago

Hi @squarefrog. OK, the GCDWebserver was a good call. I can now stream to multiple devices simultaneously when connected to the same network.

If you guys would like a write up about how to get this working in Swift, just let me know and tell me how to go about it. And I owe you a beer, bud!

squarefrog commented 2 years ago

Thats great news, well done! If you'd like to write a guide, feel free to on the Wiki, although theres no expectation for you to do so.