summaryrefslogtreecommitdiff
path: root/transcode.c
blob: c02f137bbddf21c0aac8a0622d1a5a161d4ee000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "transcode.h"

#include <glib.h>

void transcode(GInputStream *input, const struct decoder_plugin *_decoder,
		GOutputStream *output, const struct encoder_plugin *_encoder) {
	GError *error = NULL;

	/*DecodeConverter *dc = decode_converter_new(decoder);
	GInputStream *decoded_stream = g_converter_input_stream_new(input,
			(GConverter*)dc);

	EncodeConverter *ec = encode_converter_new(encoder);
	GInputStream *encoded_stream = g_converter_input_stream_new(decoded_stream,
			(GConverter*)ec);

	gssize size = g_output_stream_splice(output, encoded_stream,
			G_OUTPUT_STREAM_SPLICE_NONE, NULL, &error);*/

	gboolean decode_done = FALSE, encode_done = FALSE;
	struct decoder *decoder = g_new0(struct decoder, 1);
	struct encoder *encoder = g_new0(struct encoder, 1);
	decoder->decoder = _decoder;
	encoder->encoder = _encoder;
	decoder_init(decoder);
	encoder_init(encoder);

	/* holds decoded audio passed to encoder */
	GInputStream *decoded_in = g_memory_input_stream_new();

	while(decode_done == FALSE || encode_done == FALSE) {
		/* holds decoded audio from decoder */
		GOutputStream *decoded_out = g_memory_output_stream_new(NULL, 0, g_realloc, g_free);

		if(decode_done == FALSE && decoder_decode(decoder, input, decoded_out) <= 0) {
			g_debug("decoding done");
			decode_done = TRUE;
		}

		/* temp variables */
		gpointer data = g_memory_output_stream_get_data((GMemoryOutputStream*)decoded_out);
		gsize size = g_memory_output_stream_get_data_size((GMemoryOutputStream*)decoded_out);

		if(size > 0) {
			data = g_memdup(data, size);
			g_memory_input_stream_add_data((GMemoryInputStream*)decoded_in, data, size, g_free);
		}

		g_object_unref(decoded_out);

		if(encode_done == FALSE && encoder_encode(encoder, decoded_in, output) <= 0) {
			g_debug("encoding done");
			encode_done = TRUE;
		}
	}

	g_object_unref(decoded_in);

	/*if(size == -1 && error != NULL) {
		g_warning(error->message);
		g_error_free(error);
	}*/

	decoder_close(decoder);
	encoder_close(encoder);
	g_free(decoder);
	g_free(encoder);

	/*g_object_unref(encoded_stream);
	g_object_unref(ec);

	g_object_unref(decoded_stream);
	g_object_unref(dc);*/
}