Home | History | Annotate | Download | only in src
      1 #include "../../src/mixframemanager.h"
      2 
      3 gboolean stop_thread = FALSE;
      4 GCond* data_cond = NULL;
      5 GMutex* data_mutex = NULL;
      6 
      7 
      8 void *deque_function(void *data) {
      9 
     10 	MixFrameManager *fm = (MixFrameManager *) data;
     11 	MIX_RESULT mixresult;
     12 	MixVideoFrame *mvf = NULL;
     13 	guint64 pts;
     14 	while(!stop_thread) {
     15 
     16 		g_mutex_lock (data_mutex);
     17 
     18 		mixresult = mix_framemanager_dequeue(fm, &mvf);
     19 		if(mixresult == MIX_RESULT_SUCCESS) {
     20 			mixresult = mix_videoframe_get_timestamp(mvf, &pts);
     21 			g_print("dequeued timestamp = %"G_GINT64_FORMAT"\n", pts);
     22 			/* mix_videoframe_unref(mvf); */
     23 		} else if(mixresult == MIX_RESULT_FRAME_NOTAVAIL) {
     24 			g_print("mixresult == MIX_RESULT_FRAME_NOTAVAIL\n");
     25 			g_cond_wait (data_cond, data_mutex);
     26 		}
     27 
     28 		g_mutex_unlock (data_mutex);
     29 
     30 	}
     31 }
     32 
     33 void shuffle(GPtrArray *list) {
     34 	guint idx, jdx;
     35 	guint len = list->len;
     36 	for (idx = 0; idx < len - 1; idx++) {
     37 		jdx = rand() % len;
     38 		if (idx != jdx) {
     39 			gpointer tmp = g_ptr_array_index(list, jdx);
     40 			g_ptr_array_index(list, jdx) = g_ptr_array_index(list, idx);
     41 			g_ptr_array_index(list, idx) = tmp;
     42 		}
     43 	}
     44 }
     45 
     46 int main() {
     47 	MIX_RESULT mixresult;
     48 
     49 	gint fps_n = 24000;
     50 	gint fps_d = 1001;
     51 
     52 /*
     53 	gint fps_n = 2500000;
     54 	gint fps_d = 104297;
     55 */
     56 	GPtrArray *fa = NULL;
     57 	MixFrameManager *fm = NULL;
     58 	MixVideoFrame *mvf = NULL;
     59 	MixVideoFrame *mvf_1st = NULL;
     60 
     61 	gint idx = 0;
     62 	guint64 pts = 0;
     63 
     64 	GThread *deque_thread = NULL;
     65 	GError *deque_thread_error = NULL;
     66 
     67 	/* first ting first */
     68 	g_type_init();
     69 
     70 	/* create frame manager */
     71 	fm = mix_framemanager_new();
     72 	if (!fm) {
     73 		goto cleanup;
     74 	}
     75 
     76 	/* initialize frame manager */
     77 	mixresult = mix_framemanager_initialize(fm,
     78 			MIX_FRAMEORDER_MODE_DISPLAYORDER, fps_n, fps_d);
     79 	if (mixresult != MIX_RESULT_SUCCESS) {
     80 		goto cleanup;
     81 	}
     82 
     83 	/* create frame_array */
     84 	fa = g_ptr_array_sized_new(64);
     85 	if (!fa) {
     86 		goto cleanup;
     87 	}
     88 
     89 	for (idx = 0; idx < 16; idx++) {
     90 		/* generate MixVideoFrame */
     91 		mvf = mix_videoframe_new();
     92 		if (!mvf) {
     93 			goto cleanup;
     94 		}
     95 
     96 		pts = idx * G_USEC_PER_SEC * G_GINT64_CONSTANT(1000) * fps_d / fps_n;
     97 		mixresult = mix_videoframe_set_timestamp(mvf, pts);
     98 		if (mixresult != MIX_RESULT_SUCCESS) {
     99 			goto cleanup;
    100 		}
    101 
    102 		g_print("original timestamp = %"G_GINT64_FORMAT"\n", pts);
    103 
    104 		if (idx == 0) {
    105 			mvf_1st = mvf;
    106 		} else {
    107 			g_ptr_array_add(fa, (gpointer) mvf);
    108 		}
    109 	}
    110 
    111 	/* shuffle the array */
    112 	shuffle( fa);
    113 
    114 	data_mutex = g_mutex_new ();
    115 	if(!data_mutex) {
    116 		goto cleanup;
    117 	}
    118 
    119 	data_cond =  g_cond_new();
    120 	if(!data_cond) {
    121 		goto cleanup;
    122 	}
    123 
    124 
    125 	/* create another thread to dequeue */
    126 	deque_thread = g_thread_create((GThreadFunc) deque_function, (void *) fm,
    127 			TRUE, &deque_thread_error);
    128 	if (!deque_thread) {
    129 		goto cleanup;
    130 	}
    131 
    132 	/* enqueue */
    133 	mixresult = mix_framemanager_enqueue(fm, mvf_1st);
    134 	if (mixresult != MIX_RESULT_SUCCESS) {
    135 		goto cleanup;
    136 	}
    137 
    138 	mixresult = mix_videoframe_get_timestamp(mvf_1st, &pts);
    139 	if (mixresult != MIX_RESULT_SUCCESS) {
    140 		goto cleanup;
    141 	}
    142 	g_print("shuffled timestamp = %"G_GINT64_FORMAT"\n", pts);
    143 
    144 	for (idx = 0; idx < fa->len; idx++) {
    145 
    146 		g_mutex_lock (data_mutex);
    147 
    148 		/* wait for 100ms to enqueue another frame */
    149 		g_usleep(G_USEC_PER_SEC / 10 );
    150 
    151 		mvf = (MixVideoFrame *) g_ptr_array_index(fa, idx);
    152 		mixresult = mix_framemanager_enqueue(fm, mvf);
    153 
    154 		/* wake up deque thread */
    155 		g_cond_signal (data_cond);
    156 
    157 
    158 		g_mutex_unlock (data_mutex);
    159 
    160 		if (mixresult != MIX_RESULT_SUCCESS) {
    161 			goto cleanup;
    162 		}
    163 
    164 		mixresult = mix_videoframe_get_timestamp(mvf, &pts);
    165 		if (mixresult != MIX_RESULT_SUCCESS) {
    166 			goto cleanup;
    167 		}
    168 
    169 		g_print("shuffled timestamp = %"G_GINT64_FORMAT"\n", pts);
    170 	}
    171 
    172 	getchar();
    173 
    174 	stop_thread = TRUE;
    175 
    176 	/* wake up deque thread */
    177 	g_cond_signal (data_cond);
    178 
    179 	g_thread_join(deque_thread);
    180 
    181 cleanup:
    182 
    183 	if(data_mutex) {
    184 		g_mutex_free(data_mutex);
    185 	}
    186 
    187 	if(data_cond) {
    188 		g_cond_free(data_cond);
    189 	}
    190 
    191 	if (fm) {
    192 		mix_framemanager_unref(fm);
    193 	}
    194 
    195 	if (fa) {
    196 		g_ptr_array_free(fa, TRUE);
    197 	}
    198 
    199 	return 0;
    200 }
    201