<commit_before>//
//  CineBroadcasterViewController.m
//  Broadcaster
//
//  Created by <NAME> on 6/4/14.
//  Copyright (c) 2014 cine.io. All rights reserved.
//

#import "CineBroadcasterViewController.h"
#import <cineio/CineIO.h>
#import <AVFoundation/AVFoundation.h>

@interface CineBroadcasterViewController ()
{
    CineClient *_cine;
    CineStream *_stream;
}

@end

@implementation CineBroadcasterViewController

@synthesize broadcasterView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.broadcasterView.controlsView.recordButton.button addTarget:self action:@selector(onRecord:) forControlEvents:UIControlEventTouchUpInside];

    // cine.io setup
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cineio-settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"settings: %@", settings);
    _cine = [[CineClient alloc] initWithSecretKey:settings[@"CINE_IO_SECRET_KEY"]];
    broadcasterView.status.text = [NSString stringWithFormat:@"Getting cine.io stream info"];
    [_cine getStream:settings[@"CINE_IO_STREAM_ID"] withCompletionHandler:^(NSError *error, CineStream *stream) {
        if (error) {
            NSLog(@"Couldn't get stream information from cine.io.");
            broadcasterView.status.text = @"ERROR: couldn't get stream information from cine.io";
        } else {
            _stream = stream;
            broadcasterView.controlsView.recordButton.enabled = YES;
            broadcasterView.status.text = @"Ready";
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (BOOL) prefersStatusBarHidden {
    return YES;
}

- (void)onRecord:(id)sender
{
    NSLog(@"Record touched");
    
    if (!broadcasterView.controlsView.recordButton.recording) {
        broadcasterView.controlsView.recordButton.recording = YES;
        NSString* rtmpUrl = [NSString stringWithFormat:@"%@/%@", [_stream publishUrl], [_stream publishStreamName]];
        
        NSLog(@"%@", rtmpUrl);
        broadcasterView.status.text = [NSString stringWithFormat:@"Connecting to %@", rtmpUrl];

        
        pipeline.reset(new Broadcaster::CineBroadcasterPipeline([self](Broadcaster::SessionState state){
            [self connectionStatusChange:state];
        }));
        
        
        pipeline->setPBCallback([=](const uint8_t* const data, size_t size) {
            [self gotPixelBuffer: data withSize: size];
        });
        
        pipeline->startRtmpSession([rtmpUrl UTF8String], 1280, 720, 1500000 /* video bitrate */, 30 /* video fps */, 2, 44100);
    } else {
        broadcasterView.controlsView.recordButton.recording = NO;
        // disconnect
        pipeline.reset();
        NSLog(@"Stopped");
        broadcasterView.status.text = @"Stopped";
    }
}

- (void) connectionStatusChange:(Broadcaster::SessionState) state
{
    NSLog(@"Connection status: %d", state);
    if(state == Broadcaster::kSessionStateStarted) {
        NSLog(@"Connected");
        broadcasterView.status.text = [NSString stringWithFormat:@"Connected"];
    } else if(state == Broadcaster::kSessionStateError || state == Broadcaster::kSessionStateEnded) {
        NSLog(@"Disconnected");
        broadcasterView.status.text = [NSString stringWithFormat:@"Disconnected"];
        pipeline.reset();
    }
}

- (void) gotPixelBuffer: (const uint8_t* const) data withSize: (size_t) size {
    @autoreleasepool {<commit_msg>more user-friendly status messages<commit_after>//
//  CineBroadcasterViewController.m
//  Broadcaster
//
//  Created by <NAME> on 6/4/14.
//  Copyright (c) 2014 cine.io. All rights reserved.
//

#import "CineBroadcasterViewController.h"
#import <cineio/CineIO.h>
#import <AVFoundation/AVFoundation.h>

@interface CineBroadcasterViewController ()
{
    CineClient *_cine;
    CineStream *_stream;
}

@end

@implementation CineBroadcasterViewController

@synthesize broadcasterView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.broadcasterView.controlsView.recordButton.button addTarget:self action:@selector(onRecord:) forControlEvents:UIControlEventTouchUpInside];

    // cine.io setup -- BELONGS IN SUBCLASS
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cineio-settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"settings: %@", settings);
    _cine = [[CineClient alloc] initWithSecretKey:settings[@"CINE_IO_SECRET_KEY"]];
    broadcasterView.status.text = [NSString stringWithFormat:@"Getting cine.io stream info"];
    [_cine getStream:settings[@"CINE_IO_STREAM_ID"] withCompletionHandler:^(NSError *error, CineStream *stream) {
        if (error) {
            NSLog(@"Couldn't get stream information from cine.io.");
            broadcasterView.status.text = @"ERROR: couldn't get stream information from cine.io";
        } else {
            _stream = stream;
            broadcasterView.controlsView.recordButton.enabled = YES;
            broadcasterView.status.text = @"Ready";
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (BOOL) prefersStatusBarHidden {
    return YES;
}

- (void)onRecord:(id)sender
{
    NSLog(@"Record touched");
    
    if (!broadcasterView.controlsView.recordButton.recording) {
        broadcasterView.controlsView.recordButton.recording = YES;
        NSString* rtmpUrl = [NSString stringWithFormat:@"%@/%@", [_stream publishUrl], [_stream publishStreamName]];
        
        NSLog(@"%@", rtmpUrl);
        broadcasterView.status.text = @"Connecting to server ...";

        
        pipeline.reset(new Broadcaster::CineBroadcasterPipeline([self](Broadcaster::SessionState state){
            [self connectionStatusChange:state];
        }));
        
        
        pipeline->setPBCallback([=](const uint8_t* const data, size_t size) {
            [self gotPixelBuffer: data withSize: size];
        });
        
        pipeline->startRtmpSession([rtmpUrl UTF8String], 1280, 720, 1500000 /* video bitrate */, 30 /* video fps */, 2, 44100);
    } else {
        NSLog(@"Stopping ...");
        broadcasterView.status.text = @"Stopping ...";
        broadcasterView.controlsView.recordButton.recording = NO;
        // disconnect
        pipeline.reset();
        NSLog(@"Stopped");
        broadcasterView.status.text = @"Stopped";
    }
}

- (void) connectionStatusChange:(Broadcaster::SessionState) state
{
    NSLog(@"Connection status: %d", state);
    if(state == Broadcaster::kSessionStateStarted) {
        NSLog(@"Streaming");
        broadcasterView.status.text = @"Streaming";
    } else if(state == Broadcaster::kSessionStateError || state == Broadcaster::kSessionStateEnded) {
        NSLog(@"Disconnected");
        broadcasterView.status.text = @"Disconnected";
        pipeline.reset();
    }
}

- (void) gotPixelBuffer: (const uint8_t* const) data withSize: (size_t) size {
    @autoreleasepool {
{'fraction_non_alphanumeric': 0.13179501698754248, 'fraction_numerical': 0.009343148357870895, 'mean_word_length': 3.4183864915572233, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '413060', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2073, 'n_words': 394}