Skip to content

Commit cb3f289

Browse files
author
Mad Mike
committed
Add feature Pull Requests. spotify#38 spotify#34.
spotify#38 spotify#34
1 parent 7dcd2c5 commit cb3f289

File tree

3 files changed

+81
-77
lines changed

3 files changed

+81
-77
lines changed

src/AudioStreamInput.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ class FfmpegStreamInput : public AudioStreamInput {
6262
// TODO: Windows
6363
char message[4096] = {0};
6464
if (_Offset_s == 0 && _Seconds == 0)
65-
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le - 2>%s",
65+
snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le - 2>%s",
6666
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, DEVNULL);
6767
else
68-
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s",
68+
snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s",
6969
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, _Seconds, _Offset_s, DEVNULL);
7070

7171
return std::string(message);

src/functions.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ codegen_response_t *codegen_file(char* filename, int start_offset, int duration,
5353
codegen_response_t *response = (codegen_response_t *)malloc(sizeof(codegen_response_t));
5454
response->error = NULL;
5555
response->codegen = NULL;
56+
fprintf(stderr,"Processing \t%s\n", escape(filename).c_str());
5657

5758
auto_ptr<FfmpegStreamInput> pAudio(new FfmpegStreamInput());
5859
pAudio->ProcessFile(filename, start_offset, duration);
5960

6061
if (pAudio.get() == NULL) { // Unable to decode!
6162
char* output = (char*) malloc(16384);
63+
fprintf(stderr,"{\"error\":\"could not create decoder\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}\n", tag, escape(filename).c_str());
6264
sprintf(output,"{\"error\":\"could not create decoder\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}",
6365
tag,
6466
escape(filename).c_str());
@@ -70,6 +72,7 @@ codegen_response_t *codegen_file(char* filename, int start_offset, int duration,
7072

7173
if (numSamples < 1) {
7274
char* output = (char*) malloc(16384);
75+
fprintf(stderr,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}\n", tag, escape(filename).c_str());
7376
sprintf(output,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}",
7477
tag,
7578
escape(filename).c_str());

src/main.cxx

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -157,92 +157,93 @@ int main(int argc, char** argv) {
157157

158158
if(count == 0) throw std::runtime_error("No files given.\n");
159159

160-
161160
#ifdef _WIN32
162161
// Threading doesn't work in windows yet.
163-
for(int i=0;i<count;i++) {
164-
codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
165-
char *output = make_json_string(response, human_readable_code
166-
);
167-
print_json_to_screen(output, count, i+1);
168-
if (response->codegen) {
169-
delete response->codegen;
170-
}
171-
free(response);
172-
free(output);
173-
}
174-
return 0;
175-
162+
bool use_threads = false;
176163
#else
164+
bool use_threads = (count > 1);
165+
#endif
177166

178-
// Figure out how many threads to use based on # of cores
179-
int num_threads = getNumCores();
180-
if (num_threads > 8) num_threads = 8;
181-
if (num_threads < 2) num_threads = 2;
182-
if (num_threads > count) num_threads = count;
183-
184-
// Setup threading
185-
pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads);
186-
thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads);
187-
pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads);
188-
189-
// Kick off the first N threads
190-
int still_left = count-1-already;
191-
for(int i=0;i<num_threads;i++) {
192-
parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t));
193-
parm[i]->filename = (char*)files[still_left].c_str();
194-
parm[i]->start_offset = start_offset;
195-
parm[i]->tag = still_left;
196-
parm[i]->duration = duration;
197-
parm[i]->done = 0;
198-
still_left--;
199-
pthread_attr_init(&attr[i]);
200-
pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED);
201-
// Kick off the thread
202-
if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]))
203-
throw std::runtime_error("Problem creating thread\n");
204-
}
205-
206-
int done = 0;
207-
// Now wait for the threads to come back, and also kick off new ones
208-
while(done<count) {
209-
// Check which threads are done
167+
if (!use_threads) {
168+
for(int i=0;i<count;i++) {
169+
codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
170+
char *output = make_json_string(response, human_readable_code);
171+
print_json_to_screen(output, count, i+1);
172+
if (response->codegen) {
173+
delete response->codegen;
174+
}
175+
free(response);
176+
free(output);
177+
}
178+
return 0;
179+
} else {
180+
// Figure out how many threads to use based on # of cores
181+
int num_threads = getNumCores();
182+
if (num_threads > 8) num_threads = 8;
183+
if (num_threads < 2) num_threads = 2;
184+
if (num_threads > count) num_threads = count;
185+
186+
// Setup threading
187+
pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads);
188+
thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads);
189+
pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads);
190+
191+
// Kick off the first N threads
192+
int still_left = count-1-already;
210193
for(int i=0;i<num_threads;i++) {
211-
if (parm[i]->done) {
212-
parm[i]->done = 0;
213-
done++;
214-
codegen_response_t *response = (codegen_response_t*)parm[i]->response;
215-
char *json = make_json_string(response, human_readable_code);
216-
print_json_to_screen(json, count, done);
217-
if (response->codegen) {
218-
delete response->codegen;
219-
}
220-
free(parm[i]->response);
221-
free(json);
222-
// More to do? Start a new one on this just finished thread
223-
if(still_left >= 0) {
224-
parm[i]->tag = still_left;
225-
parm[i]->filename = (char*)files[still_left].c_str();
226-
still_left--;
227-
int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]);
228-
if(err)
229-
throw std::runtime_error("Problem creating thread\n");
194+
parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t));
195+
parm[i]->filename = (char*)files[still_left].c_str();
196+
parm[i]->start_offset = start_offset;
197+
parm[i]->tag = still_left;
198+
parm[i]->duration = duration;
199+
parm[i]->done = 0;
200+
still_left--;
201+
pthread_attr_init(&attr[i]);
202+
pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED);
203+
// Kick off the thread
204+
if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]))
205+
throw std::runtime_error("Problem creating thread\n");
206+
}
230207

208+
int done = 0;
209+
// Now wait for the threads to come back, and also kick off new ones
210+
while(done<count) {
211+
// Check which threads are done
212+
for(int i=0;i<num_threads;i++) {
213+
if (parm[i]->done) {
214+
parm[i]->done = 0;
215+
done++;
216+
codegen_response_t *response = (codegen_response_t*)parm[i]->response;
217+
char *json = make_json_string(response,human_readable_code);
218+
print_json_to_screen(json, count, done);
219+
if (response->codegen) {
220+
delete response->codegen;
221+
}
222+
free(parm[i]->response);
223+
free(json);
224+
// More to do? Start a new one on this just finished thread
225+
if(still_left >= 0) {
226+
parm[i]->tag = still_left;
227+
parm[i]->filename = (char*)files[still_left].c_str();
228+
still_left--;
229+
int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]);
230+
if(err)
231+
throw std::runtime_error("Problem creating thread\n");
232+
233+
}
231234
}
232235
}
233236
}
234-
}
235237

236-
// Clean up threads
237-
for(int i=0;i<num_threads;i++) {
238-
free(parm[i]);
238+
// Clean up threads
239+
for(int i=0;i<num_threads;i++) {
240+
free(parm[i]);
241+
}
242+
free(t);
243+
free(parm);
244+
free(attr);
245+
return 0;
239246
}
240-
free(t);
241-
free(parm);
242-
free(attr);
243-
return 0;
244-
245-
#endif // _WIN32
246247
}
247248
catch(std::runtime_error& ex) {
248249
fprintf(stderr, "%s\n", ex.what());

0 commit comments

Comments
 (0)