Esempio n. 1
0
void runRecording() {
	cout << "running callback" << endl;

    _Info("runRecording");
	sc->SwitchToRecordMode();
	sc->SetKeyboardHook(121);

	AudioCap* workers[10];
	int dev_count = 0;
	char *devices = argv[3];
	if (strcmp(devices, "-1")) {
		while (1) {
			char *ind = strsep(&devices, ":");
			if (!ind)
				break;
			int index = atoi(ind);
			if (index >= device_count) {
				_ErrorF("Invalid index %d", index);
				continue;
			}
#ifdef _WIN32
			workers[dev_count] = (AudioCap*)new AudioCapWindows();
#else
            workers[dev_count] = (AudioCap*)new AudioCapMac();
#endif
			if (!workers[dev_count]) {
				_ErrorF("Unable to create device no %d for source no %d", dev_count, index);
                exit(-1);;
			}
			int res = workers[dev_count]->StartRecording(index);
            if (-1==res) {
                _InfoF("Unable to start recording for source no %d", index);
            }
			dev_count++;
		}
	}

	int fps = atoi((const char*)argv[2]);
	uint64_t interval = 1000 / uint64_t(fps);
   
	av_register_all();
    
     Sleep(interval);

	char *fname = argv[1];

	BOOL highlight = true;
	if (atoi(argv[5]) == 0) {
		highlight = false;
	}

    int out_w=r.w;
    int out_h=r.h;
    // no support for 4K videos, and Retina is cropped 2x
    if (out_w>=2560) {
        out_w/=2;
        out_h/=2;
    }
	
    VideoEncoder *ve = new VideoEncoder(out_w, out_h, fps, fname);
    ve->Init(dev_count>0);
    
	AVFrame *scaled_frame = AllocFrame(out_w, out_h, AV_PIX_FMT_YUV420P);

	// continue while the user hasn't pressed F10 to stop or
	// ctrl-c didn't come from main app
	uint64_t audio_pts = 0;
	AVFrame *f = NULL;
	f = sc->GetNextFrame(r, highlight);
	ResizeFrame(f, scaled_frame);
	// initialize audio and video input at the same time
	uint64_t start_time = TimeMs();
	uint64_t zero_pts = start_time;
	int number = -1;
	SoundFrame out;
	while (!atomic_quit && !sc->GetFlag() && !feof(stdin)) {
		number++;
		uint64_t t = TimeMs();
		double pts = double(number) / (double)fps;
		ve->encodeFrame(scaled_frame, pts, false);
		while (dev_count) {
			memset(&out.data, 0, kAudioPacketSamples * 4);
			for (int i = 0; i < dev_count; i++) {
				AudioCap *ac = workers[i];
				SoundFrame* f = ac->GetNextFrame();
                if (f) {
                    _InfoF("frame from %d", i);
                    putOverlay((short*)&out.data, (short*)f->data, 1.0);
					ac->TruncateHead();
                } else {
                    _Info("no frames yet!");
                }
			}
			out.pts = audio_pts;
			audio_pts+=1024;
			ve->encodeAudioFrame(&out);
			bool quit = false;
			double cmp = (double)(number+1)/(double)fps*44100.0;
			if (audio_pts >= cmp)
				break;
		}
		fprintf(stderr, "OK\n");
		int rest = 0;
		f = sc->GetNextFrame(r, highlight);
		ResizeFrame(f, scaled_frame);
		do {
			sc->HandleEvents();
			t = TimeMs();
			rest = (size_t)(start_time + interval - t);
			if (rest > 0) {
				Sleep(1);
				rest--;
			}
		} while (rest > 0);
		start_time += interval;
	};
	_InfoF("quit is %d", atomic_quit);
	sc->Unhook();
	delete ve;
	delete sc;
}