Streaming video over WiFi and Bluetooth on iOS - ios

This question is going to be quite generic since I'm novice in iOS, video-streaming, and Bluetooth (going to be an interesting project).
Basically I wish to be able to stream low-res video from one iOS device to another iOS device, either through WiFi or Bluetooth depending on which one is available. Bonjour is used for initial service discovery. (I know streaming video over Bluetooth is non-ideal but it's one of the project's requirements)
So the question is what video-streaming framework/library can be used in order to maximize the amount of code shared between streaming video over WiFi and streaming video over Bluetooth.

Here are the instructions to test video streaming through Multipeer Conectivity:
You need Cocoapods, if you have not installed it yet, go to http://cocoapods.org/#install
Clone the transmitter from https://github.com/pj4533/AVCaptureMultipeerVideoDataOutput
Navigate to AVCaptureMultipeerVideoDataOutput/Sample directory in the Terminal and execute pod install
Clone the receiver from https://github.com/pj4533/SGSMultipeerVideoMixer
Run the transmitter in a physical device, you will see the back camera on screen
Run one or more receivers in the simulator or physical devices, the image of the emitter should appear in the receivers.
NOTE: The Multipeer Connectivity requires iOS 7 and both devices should have either WiFi or Bluetooth activated, I have tested it successfully on WiFi, Bluetooth may be too slow.

I will suggest to use the MultipeerConnectivity framework.
Here are the few delegate methods provided by MCSessionDelegate
MCSessionDelegate <NSObject>
// Received a byte stream from remote peer
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID;
// Start receiving a resource from remote peer
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress;
// Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error;`
Try to read http://nshipster.com/multipeer-connectivity/

I wrote code that does just that; here's a video I made of my app streaming video from one device to another:
<iframe width="560" height="315" src="https://www.youtube.com/embed/mWyZ1z55chw?rel=0" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
The screen recorder (iOS 11.2) is causing the occasional stutter on the receiving end (it was recording video at the same time my app was displaying it).
Anyway, it uses whatever connection you have between devices (wireless, Bluetooth, smoke signals, etc.l, let me know and I'll get you started.
Can't see the video? https://youtu.be/mWyZ1z55chw

Related

Get cause of Multipeerconnectivity connection error

I am using the MultipeerConeectivity framework to transmit data between two iOS devices. Sometimes the connection is not getting established even after the receiver accepts the invitation.
i.e - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state is called with state as MCSessionStateNotConnected for sender.
Is there any workaround to get the reason of the connection failure, like dangling wifi, not in range, etc? Any help is greatly appreciated.

iOS Multipeer Connectivity both Advertiser and Browser

My setup is this: I have somewhere between 0 and 8 devices running at a time, and devices can be added or removed at any time.
I want to use the iOS 7 Multipeer framework to connect them. I have this working in a controlled environment, I can start 1-7 devices in advertiser mode, then start one in browser mode and they all link up.
What I'm unsure of is how I should know if the device needs to be in advertiser or browser mode when I start it? I've tried defaulting to advertiser mode for X seconds then switching to browser, the problem with this is that it's possible that all the devices started at the same time and turn off advertiser mode at the same time.
I've also considered running devices in both advertiser and browser mode, but the initial problem is that the device discovers itself. Also I believe I have 1 less device to connect this way.
I'm sure there's a recommended way to set this up but I've been unable to find anything that doesn't assume there's a set browser and advertiser, anyone have suggestions for this?
It is easy to make all devices both advertiser and browsers, and it's a normal behavior (I'm pretty sure there's a mention of this in the documentation, I'll search it and add the link later).
I haven't had problems with the device discovering itself, maybe you're creating two peerIDs for the same device?
You may like to check PLPartyTime's implementation of this. It just has a few simple checks to see if it needs to connect/accept a connection.
Advertiser Delegate
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
withContext:(NSData *)context
invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
// Only accept invitations with IDs lower than the current host
// If both people accept invitations, then connections are lost
// However, this should always be the case since we only send invites in one direction
if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedDescending)
{
invitationHandler(YES, self.session);
}
}
Browser Delegate
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
// Whenever we find a peer, let's just send them an invitation
// But only send invites one way
// TODO: What if display names are the same?
// TODO: Make timeout configurable
if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedAscending)
{
NSLog(#"Sending invite: Self: %#", self.peerID.displayName);
[browser invitePeer:peerID
toSession:self.session
withContext:nil
timeout:10];
}
}
You may also want to check out my fork, which is a little bit smaller and both browser and advertiser are separate objects.

Chromecast - receive playback error callbacks from iOS framework

Is there a callback for failed media playbacks in the Google Cast iOS Framework? I wasn't able to find anything useful in the documentation or the sample apps on github.
Specifically, I'm looking for load errors of HLS streams... So in case CORS isn't properly configured, the parsing of *.m3u8 files fails, I want the sender iOS app to know about.
This is the closest I have found:
- (void)deviceManager:(GCKDeviceManager *)deviceManager didFailToConnectToApplicationWithError:(NSError *)error
- (void)deviceManager:(GCKDeviceManager *)deviceManager didFailToConnectWithError:(NSError *)error
However, if connecting to the device, and launching the receiver application succeed, these callbacks will never fire.
Thanks for your help!
Found it!
- (void)mediaControlChannel:(GCKMediaControlChannel *)mediaControlChannel didFailToLoadMediaWithError:(NSError *)error

Disable connection alert Multipeer Connectivity

I'd like to use the Multipeer Connectivity functionality for my app. Brief intro to the functionality of the app:
The app should scan for other devices running the app (in background), connect to them and transfer a bit of data. All without interaction with the user.
Question: is it possible to connect to other devices using multipeer but without having to show the alert view that another device wants to connect and forcing the user to accept or decline the connection? Is there a way who I can programmatically accept all incoming connections from other devices? If so, how?
Thanks a lot in advance!
You have two questions here:
The app should scan for other devices running the app (in background)
The answer here is NO - MPC does not work in the background (see this so response)
For your second question:
is it possible to connect to other devices using multipeer but without having to show the alert
The answer is Yes .. all the detail you need can be found in the apple docs. Here's some snippets on what I do:
On one device - start the browser
_serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:_peerID
serviceType:_sessionName];
[_serviceBrowser startBrowsingForPeers];
On the other device - start the advertiser
_serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_peerID
discoveryInfo:nil
serviceType:_sessionName];
[_serviceAdvertiser startAdvertisingPeer];
These services implement delegate functions to advise your app of a possible connection. Here the browser is advised of an advertiser and now invites the peer to join in a session
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
[browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0];
}
The advertiser then responds
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer: (MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
invitationHandler(YES, _session);
}
Now you will receive a session delegate call to advise you of the state of your peer connectivity. At this point you should be connected - no "real" user interaction required.
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state

Reconnecting to disconnected peers

I'm using the iOS 7 Multipeer framework in my app but I'm experiencing a problem with devices disconnecting. If I open the app in two devices: device A and device B the two devices connect to each other automatically. However, after several seconds device A disconnects from device B. i.e. At first the connection is like this:
A ---> B
A <--- B
After several seconds:
A ---> B
A B
Device A maintains it's connection but device B get's a MCSessionStateNotConnected.
This means that A can send data to B but B can't reply. I tried to get around this by checking if the device is connected and if it's not, re-initiating the connection using:
[browser invitePeer:peerID toSession:_session withContext:Nil timeout:10];
But the didChangeState callback just get's called with MCSessionStateNotConnected.
Strangely if I send app A to the background, then re-open it, B reconnects to it and the connection is maintained.
The Multipeer API (and documentation) seems a bit sparse so I was assuming that it would just work. In this situation how should I re-connect the device?
I was having the same problem, and it seems to have been related to my app browsing and advertising at the same time, and two invitations being sent/accepted. When I stopped doing this and let one peer defer to the other for invitations the devices stayed connected.
In my browser delegate I'm checking the hash value of the discovered peer's displayName and only sending an invitation if my peer has a higher hash value:
Edit
As pointed out by #Masa the hash value of an NSString will be different on 32 and 64 bit devices, so it's safer to use the compare: method on displayName.
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
NSLog(#"Browser found peer ID %#",peerID.displayName);
//displayName is created with [[NSUUID UUID] UUIDString]
BOOL shouldInvite = ([_myPeerID.displayName compare:peerID.displayName]==NSOrderedDescending);
if (shouldInvite){
[browser invitePeer:peerID toSession:_session withContext:nil timeout:1.0];
}
else {
NSLog(#"Not inviting");
}
}
As you say, the documentation is sparse so who knows what Apple really wants us to do, but I've experimented with both sending and accepting invitations using a single session, and also creating a new session for each invitation accepted/sent, but this particular way of doing things has given me the most success.
For anyone interested, I created MCSessionP2P, a demo app that illustrates the ad-hoc networking features of MCSession. The app both advertises itself on the local network and programmatically connects to available peers, establishing a peer-to-peer network. Hat tip to #ChrisH for his technique of comparing hash values for inviting peers.
I liked ChrisH's solution, which reveals the key insight that only one peer should connect to the other peer, not both. Mutual connection attempts results in mutual disconnection (though not that a single-sided connection actually is, counter-intuitively, a mutual connection in terms of status and communication, so that works fine).
However, I think a better approach than one peer inviting is for both peers to invite but only one peer to accept. I use this method now and it works great, because both peers have an opportunity to pass rich information to the other via the context parameter of the invitation, as opposed to having to rely on scant information available in the foundPeer delegate method.
Therefore, I recommend a solution like so:
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
[self invitePeer:peerID];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL accept, MCSession *session))invitationHandler
{
NSDictionary *hugePackageOfInformation = [NSKeyedUnarchiver unarchiveObjectWithData:context];
BOOL shouldAccept = ([hugePackageOfInformation.UUID.UUIDString compare:self.user.UUID.UUIDString] == NSOrderedDescending);
invitationHandler(shouldAccept && ![self isPeerConnected:peerID], [self openSession]);
}
I have the same issue when devices trying to connect to each other at the same time and I don't know how to find a reason because we don't have any errors with MCSessionStateNotConnected.
We can use some crafty way to solve this issue:
Put into txt records ( discovery info ) a time [[NSDate date] timeIntervalSince1970] when app started. Who started first - send invitation to others.
But I think it's not a right way ( if apps start at the same time, unlikely... :) ). We need to figure out the reason.
This is the result of a bug, which I've reported to Apple. I've explained how to fix it in my response to another question: Why does my MCSession peer disconnect randomly?
I have not flagged these questions for merging, because while the underlying bug and solution are the same, the two questions describe different problems.
Save the hash of the peer B. Using a timer check the state of the connection continuously if is not connected try to reconnect with each given period of time.
According to apple document Choosing an inviter when using Multipeer Connectivity
“In iOS 7, sending simultaneous invites can cause both invites to fail, leaving both peers unable to communicate with each other.”
But iOS 8 has fixed it.
It seems that the .notConnected message is a false positive in that the device is still receiving data. So, I manually updated local connection state to .connected
It was hard to factor out other state from other examples. So, I wrote a bare bones MCSession example for SwiftUI, here: MultiPeer

Resources