I am currently building an http server in iPhone that allows wifi speakers to download music from it. This is so far what I have done:
This is the part that initializes the http server:
- (void)startHttpServer
{
[DDLog addLogger:[DDTTYLogger sharedInstance]];
_httpServer = [[HTTPServer alloc] init];
[_httpServer setType:@"_http._tcp."];
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[_httpServer setDocumentRoot:documentPath];
[_httpServer setPort:9088];
[_httpServer setName:@"Music Server"];
[self startServer]; }
This is the part that gets the asset URL of the music file:
if (currentGroupIndex < currentGroupPlayList.count)
{
[self.savedIndexInfo setObject:[NSNumber numberWithInteger:currentGroupIndex] forKey:groupIDNum];
//makes sure that there is no existing duplicated url;
NSURL *oneURL = [self.savedURLInfo objectForKey:groupIDNum];
[[NSFileManager defaultManager] removeItemAtURL:oneURL error:nil];
MPMediaItem *music = [currentGroupPlayList objectAtIndex:currentGroupIndex];
NSURL *assetURL = [music valueForProperty:MPMediaItemPropertyAssetURL];
[self exportAssetAtURL:assetURL
withTitle:[NSString stringWithFormat:@"song_%lld_%ld", groupID, (long)currentGroupIndex]
groupAdress:groupIP
groupID:groupID
songDuration:[[music valueForProperty:MPMediaItemPropertyPlaybackDuration]intValue]];
}
This is the part that imports the music file and exports the output url:
- (void)exportAssetAtURL:(NSURL*)assetURL withTitle:(NSString*)title groupAdress:(NSString *)groupAdress groupID:(UInt64)groupID songDuration:(NSInteger)duration
{
NSString *ext = [TSLibraryImport extensionForAssetURL:assetURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *outURL = [[NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:title]] URLByAppendingPathExtension:ext];
// we're responsible for making sure the destination url doesn't already exist
[[NSFileManager defaultManager] removeItemAtURL:outURL error:nil];
// create the import object
TSLibraryImport* import = [[TSLibraryImport alloc] init];
[import importAsset:assetURL toURL:outURL completionBlock:^(TSLibraryImport* import)
{
if (import.status == AVAssetExportSessionStatusCompleted)
{
NSString *musicURL = [NSString stringWithFormat:@"http://%@:9088/%@.%@", [self currentIPAddress], title, ext];
NSLog(@"URL: %@", musicURL);
cntl_http_pb_pkt pb;
pb.header.packet_type = CONTROL_PACKET;
pb.header.op = CNTL_PB_HTTP_PLAY;
pb.header.body_len = sizeof(pb.body);
const char *adressChar = [groupAdress cStringUsingEncoding:NSASCIIStringEncoding];
pb.body.group_addr = inet_addr(adressChar);
pb.body.group_port = htons(INIT_GROUP_PORT);
pb.body.seq = [NSDate timeIntervalSinceReferenceDate];
self.playSeq = pb.body.seq;
const char *urlChar = [musicURL cStringUsingEncoding:NSASCIIStringEncoding];
memcpy(pb.body.url, urlChar, 200);
NSData *data = [NSData dataWithBytes:&pb length:sizeof(pb)];
[self.savedURLInfo setObject:outURL forKey:[NSNumber numberWithUnsignedLongLong:groupID]];
[self.savedPlayData setObject:musicURL forKey:[NSNumber numberWithUnsignedLongLong:groupID]];
[self performSelectorOnMainThread:@selector(sendUdpData:) withObject:data waitUntilDone:YES];
_acting = NO;
}
else
{
_acting = NO;
NSLog(@"Error importing: %@", import.error);
}
import = nil;
}];
}
My Problem:
50% of the time, its either the music file cannot be downloaded from the server, or the download speed is extremely slow (less than 10kb/s). My problem is I don't know what are the possible causes of this problem, how am I able to solve this.
Different Instances:
Sometimes at the beginning, the download speed is ok(download speed is at 200+kb/s) and then without doing anything at all, the speed would suddenly drop, making the speakers totally impossible to fetch the file from the server. Take note, there are no other devices connected to the router, only speakers and the phone.
Sometimes at the beginning, the song was not playing, and then after a couple of secs the playing would begin.
There is one time I tried connecting 8 speakers to one router, the playing was smooth and fine. But then after an hour or less, Problems would occur, like a lot of stops would occur.
Solutions I have tried:
Tried connecting all speakers under different types of routers
(TP-LINK, Tenda, Netgear, mi, etc...). above instances still occurs.When the song stops playing or when the data received is incredibly slow, I tried relaunching the app, restarting and phone, and even restarting the router, but then speed is still very slow at the beginning.
Tried disconnecting the router from modem.
Tried connecting only four devices and one phone only.
5.Tried turning off all other wifi devices that are not even under the same network.
Tried changing the port of the http server.
When the problem occurs, I tried checking whether the connection is gone, or the http server is down. But both are ok.
Take Note:
I tried accessing the url by using a browser, the download speed was as slow as the time the speaker was not playing the song smoothly. So I guess the problem is not on the speaker.
There are no error messages returned at all when the problem occurs
No connection terminated when checked on NSLog.
My Request:
So please, if you guys have an idea of what might cause this problem please tell me, or the better, if you know the solution to this. Its really hard to determine what the problem is. Because no error messages are reflected, and I tried removing a lot things that could become possible cause to this problem, but still no good result.
Any help would be highly appreciated.