Fixes for alsa device reservation
[jack2.git] / macosx / iphone / main_slave.mm
blobb9875c32edd14af9ca941f13e5a8d52fdefce7f0
1 //
2 //  main.m
3 //  iPhoneNet
4 //
5 //  Created by Stéphane  LETZ on 16/02/09.
6 //  Copyright Grame 2009. All rights reserved.
7 //
9 #import <UIKit/UIKit.h>
10 #include <jack/net.h>
12 #include "TiPhoneCoreAudioRenderer.h"
14 #define NUM_INPUT 0
15 #define NUM_OUTPUT 2
17 jack_net_slave_t* net = NULL;
18 jack_adapter_t* adapter = NULL;
20 int buffer_size;
21 int sample_rate;
23 static int net_process(jack_nframes_t buffer_size,
24                         int audio_input,
25                         float** audio_input_buffer,
26                         int midi_input,
27                         void** midi_input_buffer,
28                         int audio_output,
29                         float** audio_output_buffer,
30                         int midi_output,
31                         void** midi_output_buffer,
32                         void* data)
34     jack_adapter_pull_and_push(adapter, audio_output_buffer, audio_input_buffer, buffer_size);
36     // Process input, produce output
37     if (audio_input == audio_output) {
38         // Copy net input to net output
39         for (int i = 0; i < audio_input; i++) {
40             memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
41         }
42     }
43     return 0;
46 static void net_shutdown(void *arg)
48     if (adapter)
49         jack_flush_adapter(adapter);
52 static void SlaveAudioCallback(int frames, float** inputs, float** outputs, void* arg)
54     jack_adapter_push_and_pull(adapter, inputs, outputs, frames);
57 //http://www.securityfocus.com/infocus/1884
59 #define WIFI_MTU 1500
61 int main(int argc, char *argv[]) {
63     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
65     jack_slave_t request = { NUM_OUTPUT, NUM_INPUT, 0, 0, WIFI_MTU, -1, JackCeltEncoder, 128, 2 };
66     jack_master_t result;
68     //if ((net = jack_net_slave_open("169.254.112.119", DEFAULT_PORT, "iPhone", &request, &result))  == 0) {
69     if ((net = jack_net_slave_open(DEFAULT_MULTICAST_IP, DEFAULT_PORT, "iPod", &request, &result))  == 0) {
70         printf("jack_net_slave_open error...\n");
71         return -1;
72     }
74     if ((adapter = jack_create_adapter(NUM_INPUT,
75                                     NUM_OUTPUT,
76                                     result.buffer_size,
77                                     result.sample_rate,
78                                     result.buffer_size,
79                                     result.sample_rate)) == 0) {
80         return -1;
81     }
83     TiPhoneCoreAudioRenderer audio_device(NUM_INPUT, NUM_OUTPUT);
85     jack_set_net_slave_process_callback(net, net_process, NULL);
86     jack_set_net_slave_shutdown_callback(net, net_shutdown, NULL);
88     if (jack_net_slave_activate(net) != 0) {
89         printf("Cannot activate slave client\n");
90         return -1;
91     }
93     if (audio_device.Open(result.buffer_size, result.sample_rate) < 0) {
94         return -1;
95     }
97     audio_device.SetAudioCallback(SlaveAudioCallback, NULL);
99     if (audio_device.Start() < 0) {
100         return -1;
101     }
103     int retVal = UIApplicationMain(argc, argv, nil, nil);
104     [pool release];
106     audio_device.Stop();
107     audio_device.Close();
109     // Wait for application end
110     jack_net_slave_deactivate(net);
111     jack_net_slave_close(net);
112     jack_destroy_adapter(adapter);
113     return retVal;