Skip to content

Commit

Permalink
Add feature Pull Requests. spotify#38 spotify#34.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mad Mike committed Oct 12, 2021
1 parent 7dcd2c5 commit cb3f289
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/AudioStreamInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class FfmpegStreamInput : public AudioStreamInput {
// TODO: Windows
char message[4096] = {0};
if (_Offset_s == 0 && _Seconds == 0)
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le - 2>%s",
snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le - 2>%s",
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, DEVNULL);
else
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s",
snprintf(message, NELEM(message), "ffmpeg -i '%s' -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s",
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, _Seconds, _Offset_s, DEVNULL);

return std::string(message);
Expand Down
3 changes: 3 additions & 0 deletions src/functions.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ codegen_response_t *codegen_file(char* filename, int start_offset, int duration,
codegen_response_t *response = (codegen_response_t *)malloc(sizeof(codegen_response_t));
response->error = NULL;
response->codegen = NULL;
fprintf(stderr,"Processing \t%s\n", escape(filename).c_str());

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

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

if (numSamples < 1) {
char* output = (char*) malloc(16384);
fprintf(stderr,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}\n", tag, escape(filename).c_str());
sprintf(output,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}",
tag,
escape(filename).c_str());
Expand Down
151 changes: 76 additions & 75 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -157,92 +157,93 @@ int main(int argc, char** argv) {

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


#ifdef _WIN32
// Threading doesn't work in windows yet.
for(int i=0;i<count;i++) {
codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
char *output = make_json_string(response, human_readable_code
);
print_json_to_screen(output, count, i+1);
if (response->codegen) {
delete response->codegen;
}
free(response);
free(output);
}
return 0;

bool use_threads = false;
#else
bool use_threads = (count > 1);
#endif

// Figure out how many threads to use based on # of cores
int num_threads = getNumCores();
if (num_threads > 8) num_threads = 8;
if (num_threads < 2) num_threads = 2;
if (num_threads > count) num_threads = count;

// Setup threading
pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads);
thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads);
pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads);

// Kick off the first N threads
int still_left = count-1-already;
for(int i=0;i<num_threads;i++) {
parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t));
parm[i]->filename = (char*)files[still_left].c_str();
parm[i]->start_offset = start_offset;
parm[i]->tag = still_left;
parm[i]->duration = duration;
parm[i]->done = 0;
still_left--;
pthread_attr_init(&attr[i]);
pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED);
// Kick off the thread
if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]))
throw std::runtime_error("Problem creating thread\n");
}

int done = 0;
// Now wait for the threads to come back, and also kick off new ones
while(done<count) {
// Check which threads are done
if (!use_threads) {
for(int i=0;i<count;i++) {
codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
char *output = make_json_string(response, human_readable_code);
print_json_to_screen(output, count, i+1);
if (response->codegen) {
delete response->codegen;
}
free(response);
free(output);
}
return 0;
} else {
// Figure out how many threads to use based on # of cores
int num_threads = getNumCores();
if (num_threads > 8) num_threads = 8;
if (num_threads < 2) num_threads = 2;
if (num_threads > count) num_threads = count;

// Setup threading
pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads);
thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads);
pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads);

// Kick off the first N threads
int still_left = count-1-already;
for(int i=0;i<num_threads;i++) {
if (parm[i]->done) {
parm[i]->done = 0;
done++;
codegen_response_t *response = (codegen_response_t*)parm[i]->response;
char *json = make_json_string(response, human_readable_code);
print_json_to_screen(json, count, done);
if (response->codegen) {
delete response->codegen;
}
free(parm[i]->response);
free(json);
// More to do? Start a new one on this just finished thread
if(still_left >= 0) {
parm[i]->tag = still_left;
parm[i]->filename = (char*)files[still_left].c_str();
still_left--;
int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]);
if(err)
throw std::runtime_error("Problem creating thread\n");
parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t));
parm[i]->filename = (char*)files[still_left].c_str();
parm[i]->start_offset = start_offset;
parm[i]->tag = still_left;
parm[i]->duration = duration;
parm[i]->done = 0;
still_left--;
pthread_attr_init(&attr[i]);
pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED);
// Kick off the thread
if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]))
throw std::runtime_error("Problem creating thread\n");
}

int done = 0;
// Now wait for the threads to come back, and also kick off new ones
while(done<count) {
// Check which threads are done
for(int i=0;i<num_threads;i++) {
if (parm[i]->done) {
parm[i]->done = 0;
done++;
codegen_response_t *response = (codegen_response_t*)parm[i]->response;
char *json = make_json_string(response,human_readable_code);
print_json_to_screen(json, count, done);
if (response->codegen) {
delete response->codegen;
}
free(parm[i]->response);
free(json);
// More to do? Start a new one on this just finished thread
if(still_left >= 0) {
parm[i]->tag = still_left;
parm[i]->filename = (char*)files[still_left].c_str();
still_left--;
int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]);
if(err)
throw std::runtime_error("Problem creating thread\n");

}
}
}
}
}

// Clean up threads
for(int i=0;i<num_threads;i++) {
free(parm[i]);
// Clean up threads
for(int i=0;i<num_threads;i++) {
free(parm[i]);
}
free(t);
free(parm);
free(attr);
return 0;
}
free(t);
free(parm);
free(attr);
return 0;

#endif // _WIN32
}
catch(std::runtime_error& ex) {
fprintf(stderr, "%s\n", ex.what());
Expand Down

0 comments on commit cb3f289

Please sign in to comment.