From 90b654e923f321349d03779f4c8b402f5f6df869 Mon Sep 17 00:00:00 2001
From: Seven Du <dujinfang@gmail.com>
Date: Tue, 28 Aug 2012 16:12:12 +0800
Subject: [PATCH] FS-7502: add simple video media bug implementation, should
 work with .fsv, be sure to set enable_file_write_buffering=false

Conflicts:
	src/include/switch_types.h
---
 src/include/switch_types.h |  3 ++-
 src/switch_core_io.c       | 43 ++++++++++++++++++++++++++++++++++++++
 src/switch_ivr_async.c     | 18 ++++++++++++++++
 3 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index e0d11ca934..adedb0b1be 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -483,7 +483,8 @@ typedef enum {
 	SWITCH_ABC_TYPE_READ_PING,
 	SWITCH_ABC_TYPE_TAP_NATIVE_READ,
 	SWITCH_ABC_TYPE_TAP_NATIVE_WRITE,
-	SWITCH_ABC_TYPE_CLOSE
+	SWITCH_ABC_TYPE_CLOSE,
+	SWITCH_ABC_TYPE_READ_VIDEO_PING
 } switch_abc_type_t;
 
 typedef struct {
diff --git a/src/switch_core_io.c b/src/switch_core_io.c
index 4c39bbbb42..35d348e751 100644
--- a/src/switch_core_io.c
+++ b/src/switch_core_io.c
@@ -111,6 +111,49 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_video_frame(switch_core
 		goto done;
 	}
 
+	if (session->bugs) {
+		switch_media_bug_t *bp;
+		switch_bool_t ok = SWITCH_TRUE;
+		int prune = 0;
+		switch_thread_rwlock_rdlock(session->bug_rwlock);
+		for (bp = session->bugs; bp; bp = bp->next) {
+			if (switch_channel_test_flag(session->channel, CF_PAUSE_BUGS) && !switch_core_media_bug_test_flag(bp, SMBF_NO_PAUSE)) {
+				continue;
+			}
+
+			if (!switch_channel_test_flag(session->channel, CF_ANSWERED) && switch_core_media_bug_test_flag(bp, SMBF_ANSWER_REQ)) {
+				continue;
+			}
+
+			if (switch_test_flag(bp, SMBF_PRUNE)) {
+				prune++;
+				continue;
+			}
+
+			if (bp->ready && switch_test_flag(bp, SMBF_READ_PING)) {
+				switch_mutex_lock(bp->read_mutex);
+				bp->ping_frame = *frame;
+				if (bp->callback) {
+					if (bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ_VIDEO_PING) == SWITCH_FALSE
+						|| (bp->stop_time && bp->stop_time <= switch_epoch_time_now(NULL))) {
+						ok = SWITCH_FALSE;
+					}
+				}
+				bp->ping_frame = NULL;;
+				switch_mutex_unlock(bp->read_mutex);
+			}
+
+			if (ok == SWITCH_FALSE) {
+				switch_set_flag(bp, SMBF_PRUNE);
+				prune++;
+			}
+		}
+		switch_thread_rwlock_unlock(session->bug_rwlock);
+		if (prune) {
+			switch_core_media_bug_prune(session);
+		}
+	}
+
   done:
 
 	return status;
diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c
index bc4d724a50..a8244076a8 100644
--- a/src/switch_ivr_async.c
+++ b/src/switch_ivr_async.c
@@ -34,6 +34,7 @@
  */
 
 #include <switch.h>
+#include "private/switch_core_pvt.h"
 #include <speex/speex_preprocess.h>
 #include <speex/speex_echo.h>
 
@@ -1478,6 +1479,23 @@ static switch_bool_t record_callback(switch_media_bug_t *bug, void *user_data, s
 			}
 		}
 		break;
+	case SWITCH_ABC_TYPE_READ_VIDEO_PING:
+
+		if (rh->fh) {
+			switch_size_t len;
+
+			if (!bug->ping_frame) break;
+
+			len = bug->ping_frame->packetlen;
+
+			if (len && switch_core_file_write_video(rh->fh, bug->ping_frame->packet, &len) != SWITCH_STATUS_SUCCESS && rh->hangup_on_error) {
+				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error writing video to %s\n", rh->file);
+				switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
+				switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
+				return SWITCH_FALSE;
+			}
+		}
+		break;
 	case SWITCH_ABC_TYPE_WRITE:
 	default:
 		break;