Commit 58b954f2 authored by Mikhail Karpenko's avatar Mikhail Karpenko
Browse files

Update debug macro in audio, fix debug macro in other modules

parent 7e9dba1a
Loading
Loading
Loading
Loading
+106 −99
Original line number Diff line number Diff line
@@ -27,45 +27,58 @@
#include <sys/ioctl.h>
#include <arpa/inet.h>

#undef AUDIO_DEBUG
//#define AUDIO_DEBUG
#undef AUDIO_DEBUG_2
//#define AUDIO_DEBUG_2
//#undef AUDIO_DEBUG
#define AUDIO_DEBUG
//#undef AUDIO_DEBUG_2
#define AUDIO_DEBUG_2

#ifdef AUDIO_DEBUG
	#define D(a) a
	#define D(s_port, a) \
	do { \
		cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << ": sensor port: " << s_port << " "; \
		a; \
	} while (0)
#else
	#define D(a)
	#define D(s_port, a)
#endif

#ifdef AUDIO_DEBUG_2
	#define D2(a) a
	#define D2(s_port, a) \
	do { \
		cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << ": sensor port: " << s_port << " "; \
		a; \
	} while (0)
#else
	#define D2(a)
	#define D2(s_port, a)
#endif

//#define SAMPLE_TIME	50	// in milliseconds
#define SAMPLE_TIME	20	                                        // in milliseconds
#define BUFFER_TIME	1000                                        // in milliseconds

using namespace std;

Audio::Audio(bool enable, Parameters *pars, int sample_rate, int channels) {
//cerr << "Audio::Audio()" << endl;
Audio::Audio(int port, bool enable, Parameters *pars, int sample_rate, int channels) {
	snd_pcm_hw_params_t *hw_params;
	snd_pcm_sw_params_t *sw_params;
	_present = false;
	stream_name = "audio";
	params = pars;
	sensor_port = port;

	// normalize audio settings
	if(sample_rate == 0)	sample_rate = SAMPLE_RATE;
	if(sample_rate > 48000)	sample_rate = 48000;
	if(sample_rate < 11025)	sample_rate = 11025;
	if (sample_rate == 0)
		sample_rate = SAMPLE_RATE;
	if (sample_rate > 48000)
		sample_rate = 48000;
	if (sample_rate < 11025)
		sample_rate = 11025;
	_sample_rate = sample_rate;
	if(channels == 0)		channels = SAMPLE_CHANNELS;
	if(channels < 1)		channels = 1;
	if(channels > 2)		channels = 2;
	if (channels == 0)
		channels = SAMPLE_CHANNELS;
	if (channels < 1)
		channels = 1;
	if (channels > 2)
		channels = 2;
	_channels = channels;
	_volume = 65535;
	_volume *= 90;
@@ -76,7 +89,7 @@ Audio::Audio(bool enable, Parameters *pars, int sample_rate, int channels) {
	sbuffer_len = _sample_rate * SAMPLE_TIME;
	sbuffer_len /= 1000;
	sbuffer_len -= sbuffer_len % 2;
D(	cerr << "sbuffer_len == " << sbuffer_len << endl;)
	D(sensor_port, cerr << "sbuffer_len == " << sbuffer_len << endl);
	_ptype = 97;
	sbuffer = NULL;

@@ -101,11 +114,11 @@ D( cerr << "sbuffer_len == " << sbuffer_len << endl;)
			unsigned int period_time = SAMPLE_TIME * 1000;
			if ((err = snd_pcm_hw_params_set_period_time_near(capture_handle, hw_params, &period_time, 0)) < 0)
				break;
D(			cerr << "period_time == " << period_time << endl;)
			D(sensor_port, cerr << "period_time == " << period_time << endl);
			unsigned int buffer_time = BUFFER_TIME * 1000;
			if ((err = snd_pcm_hw_params_set_buffer_time_near(capture_handle, hw_params, &buffer_time, 0)) < 0)
				break;
D(			cerr << "buffer_time == " << buffer_time << endl;)
			D(sensor_port, cerr << "buffer_time == " << buffer_time << endl);
			_sample_rate = t;
			if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, _channels)) < 0)
				break;
@@ -119,34 +132,28 @@ D( cerr << "buffer_time == " << buffer_time << endl;)
				break;
			if ((err = snd_pcm_sw_params(capture_handle, sw_params)) < 0)
				break;
//			if((err = snd_pcm_prepare(capture_handle)) < 0)
//				break;
			init_ok = true;
			break;
		}
		if (init_ok) {
D(			cerr << endl << "Audio init: ok; with sample rate: " << _sample_rate << "; and channels: " << _channels << endl;)
//			cerr << endl << "Audio init: ok; with sample rate: " << _sample_rate << "; and channels: " << _channels << endl;
			D(sensor_port, cerr << "Audio init: ok; with sample rate: " << _sample_rate	<< "; and channels: " << _channels << endl);
			_present = true;
			_play = false;
			set_volume(_volume);
			// create thread...
			init_pthread((void *) this);
		} else {
D(			cerr << "Audio: init FAIL!" << endl;)
//			cerr << "Audio: init FAIL!" << endl;
			D(sensor_port, cerr << "Audio: init FAIL!" << endl);
			_present = false;
		}
	}
}

Audio::~Audio(void) {
//cerr << "Audio::~Audio()" << endl;
	if (_present) {
		snd_pcm_drop(capture_handle);
		snd_pcm_close(capture_handle);
		snd_config_update_free_global();
//cerr << "--------------->> close audio" << endl;
	}
	if (sbuffer != NULL)
		delete[] sbuffer;
@@ -190,9 +197,13 @@ void Audio::set_capture_volume(int nvolume) {
			long vol = 0;
			snd_mixer_selem_get_capture_volume(selem, SND_MIXER_SCHN_FRONT_LEFT, &vol);
			snd_mixer_selem_set_capture_volume_all(selem, vol_new);
D(			cerr << "element " << snd_mixer_selem_id_get_name(sid) << " - OLD min vol == " << volume_min << "; max vol == " << volume_max << "; volume == " << vol << endl;)
D(			snd_mixer_selem_get_capture_volume(selem, SND_MIXER_SCHN_FRONT_LEFT, &vol);)
D(			cerr << "element " << snd_mixer_selem_id_get_name(sid) << " - NEW min vol == " << volume_min << "; max vol == " << volume_max << "; volume == " << vol << endl;)
			D(sensor_port, cerr << "element " << snd_mixer_selem_id_get_name(sid) << " - OLD min vol == "
							<< volume_min << "; max vol == " << volume_max << "; volume == " << vol
							<< endl);
			D(sensor_port, snd_mixer_selem_get_capture_volume(selem, SND_MIXER_SCHN_FRONT_LEFT, &vol));
			D(sensor_port, cerr << "element " << snd_mixer_selem_id_get_name(sid) << " - NEW min vol == "
							<< volume_min << "; max vol == " << volume_max << "; volume == " << vol
							<< endl);
		}
	}
	_volume = nvolume;
@@ -202,15 +213,12 @@ D( cerr << "element " << snd_mixer_selem_id_get_name(sid) << " - NEW min vol =
void Audio::Start(string ip, long port, int ttl) {
	if (!_present)
		return;
D(	cerr << "Audio ---> Start !!!" << endl;)
//cerr << "Audio ---> Start !!!" << endl;
//is_first = true;
	D(sensor_port, cerr << "Audio ---> Start !!!" << endl);
	timestamp_rtcp = 0;
	f_tv.tv_sec = 0;
	f_tv.tv_usec = 0;
	snd_pcm_prepare(capture_handle);
	snd_pcm_reset(capture_handle);
//cerr << "Audio ---> Start !!! - done" << endl;

	// get FPGA/sys time delta
//	Parameters *params = Parameters::instance();
@@ -261,6 +269,11 @@ D( cerr << "Audio ---> Start !!!" << endl;)
	if (fpga_gt)
		delta_fpga_sys = -delta_fpga_sys;

	D2(sensor_port, cerr << "first time = " << tv_1.tv_sec << ":" << tv_1.tv_usec
			<< ", second time = " << tv_2.tv_sec << ":" << tv_2.tv_usec << endl);
	D2(sensor_port, cerr << "FPGA time = " << tv_fpga.tv_sec << ":" << tv_fpga.tv_usec
			<< ", system time = " << tv_sys.tv_sec << ":" << tv_sys.tv_usec << endl);
	D2(sensor_port, cerr << "time delta = " << delta_fpga_sys << endl << endl);
//fprintf(stderr, "first time == %d:%06d; second time == %d:%06d\n", tv_1.tv_sec, tv_1.tv_usec, tv_2.tv_sec, tv_2.tv_usec);
//fprintf(stderr, "FPGA time == %d:%06d; system time == %d:%06d\n", tv_fpga.tv_sec, tv_fpga.tv_usec, tv_sys.tv_sec, tv_sys.tv_usec);
//fprintf(stderr, "times delta == %06d\n\n", delta_fpga_sys);
@@ -271,21 +284,18 @@ D( cerr << "Audio ---> Start !!!" << endl;)
void Audio::Stop(void) {
	if (!_present)
		return;
//	cerr << "Audio ---> Stop !!!" << endl;
D(	cerr << "Audio ---> Stop !!!" << endl;)
	D(sensor_port, cerr << "Audio ---> Stop !!!" << endl);
	RTP_Stream::Stop();
	f_tv.tv_sec = 0;
	f_tv.tv_usec = 0;
}

long Audio::process(void) {
//	static timeval ts_old = {0, 0};
	long ret = 0;
	snd_pcm_status_t *status;
	snd_pcm_status_alloca(&status);
	snd_timestamp_t ts;
	unsigned long avail;
//bool first = true;

	for(int i = 0; i < 4; i++) {
		snd_pcm_status(capture_handle, status);
@@ -344,16 +354,13 @@ fprintf(stderr, "status timestamp == %d:%06d at %d:%06d; delta == %d:%06d, slen
			// --==--
			ret += slen;
			process_send(slen);
			// check again what buffer is not empty
			// check again that buffer is not empty
			snd_pcm_status(capture_handle, status);
			avail = (unsigned long)snd_pcm_status_get_avail(status);
//c++;
			if(avail >= (unsigned long)sbuffer_len) {
//cerr << "process() again - available " << avail << " samples" << endl;
				D2(sensor_port, cerr << "process() again - available " << avail << " samples" << endl);
				continue;
			}
//if(c != 0)
//cerr << "process " << c << " times" << endl;
			return ret;
		}
		if(slen < 0) {
+7 −7
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ using namespace std;

class Audio : public RTP_Stream {
public:
	Audio(bool enable, Parameters *pars, int sample_rate = SAMPLE_RATE, int channels = SAMPLE_CHANNELS);
	Audio(int port, bool enable, Parameters *pars, int sample_rate = SAMPLE_RATE, int channels = SAMPLE_CHANNELS);
	virtual ~Audio(void);
	long sample_rate(void) { return _sample_rate; };
	long channels(void) { return _channels; };
@@ -51,11 +51,11 @@ public:
	void Start(string ip, long port, int ttl = -1);
	void Stop(void);
protected:
	int fd;
	snd_pcm_t *capture_handle;
//	int fd;
	snd_pcm_t *capture_handle;                                  // PCM handle, returned from snd_pcm_open
	short *sbuffer;
	long sbuffer_len;
	bool _present;
	bool _present;                                              // flag indicating that audio interface has been initialized
	long _sample_rate;
	long _channels;
	long _volume;
@@ -67,8 +67,8 @@ protected:

	uint64_t timestamp_rtcp;
	long delta_fpga_sys;	                                    // A/V clocks delta for RTCP
	bool is_first;
	bool is_first2;
//	bool is_first;
//	bool is_first2;
};

#endif // _AUDIO__H_
+2 −1
Original line number Diff line number Diff line
@@ -73,7 +73,8 @@ int main(int argc, char *argv[]) {
		cerr << "|" << (*it).first << "| == |" << (*it).second << "|" << endl;
	}

	for (int i = 0; i < SENSOR_PORTS; i++) {
	for (int i = 0; i < 1; i++) {
//	for (int i = 0; i < SENSOR_PORTS; i++) {
		pthread_attr_t attr;
		cout << "Start thread " << i << endl;
		streamers[i] = new Streamer(args, i);
+3 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ using namespace std;
		a; \
	} while (0)
#else
	#define D(a)
	#define D(s_port, a)
#endif

RTP_Stream::RTP_Stream(void) {
@@ -200,6 +200,8 @@ void RTP_Stream::rtcp_send_sr(void) {
	ul = htonl(rtp_octets);
	memcpy((void *) &packet[24], (void *) &ul, 4);
	rtcp_socket->send(packet, packet_len);
	D(sensor_port, cerr << "stream name: " << stream_name << ", f_tv: " << f_tv.tv_sec << ":" << f_tv.tv_usec
			<< ", timestamp: " << timestamp << endl);
}

void RTP_Stream::rtcp_send_sdes(void) {
+10 −2
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ Streamer::Streamer(const map<string, string> &_args, int port_num) {
	params = new Parameters(sensor_port);
	args = _args;
	audio = NULL;
	session->process_audio = false;
	session->process_audio = true;
	session->audio.sample_rate = 0;
	session->audio.channels = 0;
	session->rtp_out.ip_custom = false;
@@ -83,6 +83,13 @@ Streamer::Streamer(const map<string, string> &_args, int port_num) {
	}
	rtsp_server = NULL;
	connected_count = 0;

	// DEBUG FEATURE: self-enable audio processing, this should be done elsewhere, probably from camvc
	unsigned long snd_en = 0;
	if (session->process_audio)
		snd_en = 1;
	unsigned long params_array[2] = {P_STROP_AUDIO_EN, snd_en};
	params->setPValue(params_array, 2);
}

void Streamer::audio_init(void) {
@@ -91,7 +98,7 @@ void Streamer::audio_init(void) {
		delete audio;
	}
	D(sensor_port, cout << "audio_enabled == " << session->process_audio << endl);
	audio = new Audio(session->process_audio, params, session->audio.sample_rate, session->audio.channels);
	audio = new Audio(sensor_port, session->process_audio, params, session->audio.sample_rate, session->audio.channels);
	if (audio->present() && session->process_audio) {
		session->process_audio = true;
		session->audio.type = audio->ptype();
@@ -271,6 +278,7 @@ int Streamer::update_settings(bool apply) {
		f_audio_channels = true;
	if ((audio_proc || session->process_audio) && (f_audio_rate || f_audio_channels))
		audio_was_changed = true;
	D(sensor_port, cerr << "audio_proc = " << audio_proc << ", process_audio = " << session->process_audio << ", f_audio_rate = " << f_audio_rate << "f_audio_channels = " << f_audio_channels << endl);
	if (apply) {
		bool audio_restarted = false;
		if (audio_was_changed) {
Loading