From 14a04940f20599963db960aaa7f7182c7d0803ff Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Thu, 7 May 2015 14:50:27 +0200 Subject: [PATCH] More work on MS2 example --- submodules/MS2/MS2/Base.lproj/Main.storyboard | 14 +++- submodules/MS2/MS2/ViewController.h | 2 + submodules/MS2/MS2/ViewController.m | 79 ++++++++----------- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/submodules/MS2/MS2/Base.lproj/Main.storyboard b/submodules/MS2/MS2/Base.lproj/Main.storyboard index c0c388e46..a8ce1fb77 100644 --- a/submodules/MS2/MS2/Base.lproj/Main.storyboard +++ b/submodules/MS2/MS2/Base.lproj/Main.storyboard @@ -45,7 +45,7 @@ - + + + + @@ -103,9 +113,11 @@ + + diff --git a/submodules/MS2/MS2/ViewController.h b/submodules/MS2/MS2/ViewController.h index 80492282e..847a94e48 100644 --- a/submodules/MS2/MS2/ViewController.h +++ b/submodules/MS2/MS2/ViewController.h @@ -16,8 +16,10 @@ @property (weak, nonatomic) IBOutlet UIView *remoteView; @property (weak, nonatomic) IBOutlet UIView *localView; +@property (weak, nonatomic) IBOutlet UIButton *startStreamLabel; @property (weak, nonatomic) IBOutlet UILabel *infoLabel; +@property (weak, nonatomic) IBOutlet UILabel *bandwidthLabel; @end diff --git a/submodules/MS2/MS2/ViewController.m b/submodules/MS2/MS2/ViewController.m index 7aad4aaa8..5999f6225 100644 --- a/submodules/MS2/MS2/ViewController.m +++ b/submodules/MS2/MS2/ViewController.m @@ -11,6 +11,7 @@ #include #import "mediastreamer2/mediastream.h" +#import "oRTP/rtpsession.h" #import "UITextField+DoneButton.h" extern void libmsopenh264_init(); @@ -21,15 +22,15 @@ extern void libmsopenh264_init(); MSFilter* noWebCamFilter; MSFilter* videoCamFilter; - RtpProfile* profile; + RtpProfile* profile; VideoStream* currentStream; - IceSession* iceSession; + IceSession* iceSession; PayloadType* vp8_pt; } @end @implementation ViewController -#define CAM_NAME "AV Capture: com.apple.avfoundation.avcapturedevice.built-in_video:0" /*"AV Capture: Back Camera"*/ +#define CAM_NAME "AV Capture: com.apple.avfoundation.avcapturedevice.built-in_video:1" - (void)viewDidLoad { [super viewDidLoad]; @@ -50,8 +51,6 @@ extern void libmsopenh264_init(); videoCam = ms_web_cam_manager_get_cam(ms_web_cam_manager_get(), CAM_NAME); noWebCamFilter = ms_web_cam_create_reader(noCam); - - // open the cam immediately videoCamFilter = ms_web_cam_create_reader(videoCam); currentStream = NULL; @@ -70,6 +69,7 @@ extern void libmsopenh264_init(); vp8_pt->normal_bitrate = 500000; [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(timer:) userInfo:nil repeats:TRUE]; + [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(infoTimer:) userInfo:nil repeats:TRUE]; } - (void)didReceiveMemoryWarning { @@ -77,59 +77,47 @@ extern void libmsopenh264_init(); // Dispose of any resources that can be recreated. } -- (void)timer:(NSTimer*)timer { - if( currentStream) media_stream_iterate((MediaStream*)currentStream); -} - - -- (NSString *)getIPAddress { - - NSString *address = @"error"; - struct ifaddrs *interfaces = NULL; - struct ifaddrs *temp_addr = NULL; - int success = 0; - // retrieve the current interfaces - returns 0 on success - success = getifaddrs(&interfaces); - if (success == 0) { - // Loop through linked list of interfaces - temp_addr = interfaces; - while(temp_addr != NULL) { - if(temp_addr->ifa_addr->sa_family == AF_INET) { - // Check if interface is en0 which is the wifi connection on the iPhone - NSString* ifa = [NSString stringWithUTF8String:temp_addr->ifa_name]; - if([ifa isEqualToString:@"en0"] || [ifa isEqualToString:@"en1"]) { - // Get NSString from C String - address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; - break; - - } - - } - - temp_addr = temp_addr->ifa_next; - } - } - // Free memory - freeifaddrs(interfaces); - return address; - -} - - (void)updateInfo { - if( currentStream == NULL )return; + if( currentStream == NULL ){ + self.infoLabel.text = @"No stream"; + self.bandwidthLabel.text = @""; + return; + + } const MSWebCam* currentCam = video_stream_get_camera(currentStream); if( currentCam ) self.infoLabel.text = [NSString stringWithFormat:@"Stream running, current cam: %s", currentCam->name]; else self.infoLabel.text = @"No Webcam ?!"; + MediaStream* ms = (MediaStream*)currentStream; + float upload = rtp_session_get_rtp_send_bandwidth(ms->sessions.rtp_session); + float download = rtp_session_get_rtp_recv_bandwidth(ms->sessions.rtp_session); + + self.bandwidthLabel.text = [NSString stringWithFormat:@"Upload: %f kbit/s, Download: %f kbit/s", upload/1000.0, download/1000.0]; + } +#pragma mark - Timer callbacks +- (void)timer:(NSTimer*)timer { + if( currentStream) { + media_stream_iterate((MediaStream*)currentStream); + } +} + +- (void)infoTimer:(NSTimer*)timer { + [self updateInfo]; +} + + +#pragma mark - Actions + - (IBAction)onStartStreamsClick:(id)sender { if( currentStream ){ VideoStream* stream = currentStream; currentStream = NULL; - video_stream_stop(stream); + video_stream_stop_keep_source(stream); + [self.startStreamLabel setTitle:@"Start streams" forState:UIControlStateNormal]; } else { VideoStream * stream = video_stream_new(3456, 3457, FALSE); @@ -137,6 +125,7 @@ extern void libmsopenh264_init(); video_stream_set_native_preview_window_id(stream, (unsigned long)self.localView); video_stream_start_with_source(stream, profile, "127.0.0.1", 3456, "127.0.0.1", 3457, 103, 30, noCam, noWebCamFilter); currentStream = stream; + [self.startStreamLabel setTitle:@"Stop streams" forState:UIControlStateNormal]; [self updateInfo]; }