1 /*
2  * Copyright (C) 2004 Nathan Lutchansky <lutchann@litech.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #include <sys/types.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <pthread.h>
26 
27 #include <event.h>
28 #include <log.h>
29 #include <frame.h>
30 #include <stream.h>
31 #include <encoders.h>
32 #include <conf_parse.h>
33 
34 struct alaw_encoder {
35 	struct stream *output;
36 	struct stream_destination *input;
37 };
38 
alaw_encode(struct frame * pcm,void * d)39 static void alaw_encode( struct frame *pcm, void *d )
40 {
41 	struct alaw_encoder *en = (struct alaw_encoder *)d;
42 	struct frame *alaw;
43 
44 	//alaw = new_frame( pcm->length / 2 );
45 	alaw = new_frame();
46 	alaw->format = FORMAT_ALAW;
47 	alaw->width = 0;
48 	alaw->height = 0;
49 	alaw->length = pcm->length / 2;
50 	alaw->key = 1;
51 	alaw_enc( pcm->d, alaw->d, pcm->length );
52 	unref_frame( pcm );
53 	deliver_frame_to_stream( alaw, en->output );
54 }
55 
get_framerate(struct stream * s,int * fincr,int * fbase)56 static void get_framerate( struct stream *s, int *fincr, int *fbase )
57 {
58 	struct alaw_encoder *en = (struct alaw_encoder *)s->private;
59 
60 	en->input->stream->get_framerate( en->input->stream, fincr, fbase );
61 }
62 
set_running(struct stream * s,int running)63 static void set_running( struct stream *s, int running )
64 {
65 	struct alaw_encoder *en = (struct alaw_encoder *)s->private;
66 
67 	set_waiting( en->input, running );
68 }
69 
70 /************************ CONFIGURATION DIRECTIVES ************************/
71 
start_block(void)72 static void *start_block(void)
73 {
74 	struct alaw_encoder *en;
75 
76 	en = (struct alaw_encoder *)malloc( sizeof( struct alaw_encoder ) );
77 	en->input = NULL;
78 	en->output = NULL;
79 
80 	return en;
81 }
82 
end_block(void * d)83 static int end_block( void *d )
84 {
85 	struct alaw_encoder *en = (struct alaw_encoder *)d;
86 
87 	if( ! en->input )
88 	{
89 		spook_log( SL_ERR, "alaw: missing input stream name" );
90 		return -1;
91 	}
92 	if( ! en->output )
93 	{
94 		spook_log( SL_ERR, "alaw: missing output stream name" );
95 		return -1;
96 	}
97 
98 	return 0;
99 }
100 
set_input(int num_tokens,struct token * tokens,void * d)101 static int set_input( int num_tokens, struct token *tokens, void *d )
102 {
103 	struct alaw_encoder *en = (struct alaw_encoder *)d;
104 	int format = FORMAT_PCM;
105 
106 	if( ! ( en->input = connect_to_stream( tokens[1].v.str, alaw_encode,
107 						en, &format, 1 ) ) )
108 	{
109 		spook_log( SL_ERR, "alaw: unable to connect to stream \"%s\"",
110 				tokens[1].v.str );
111 		return -1;
112 	}
113 	return 0;
114 }
115 
set_output(int num_tokens,struct token * tokens,void * d)116 static int set_output( int num_tokens, struct token *tokens, void *d )
117 {
118 	struct alaw_encoder *en = (struct alaw_encoder *)d;
119 
120 	en->output = new_stream( tokens[1].v.str, FORMAT_ALAW, en );
121 	if( ! en->output )
122 	{
123 		spook_log( SL_ERR, "alaw: unable to create stream \"%s\"",
124 				tokens[1].v.str );
125 		return -1;
126 	}
127 	en->output->get_framerate = get_framerate;
128 	en->output->set_running = set_running;
129 	return 0;
130 }
131 
132 static struct statement config_statements[] = {
133 	/* directive name, process function, min args, max args, arg types */
134 	{ "input", set_input, 1, 1, { TOKEN_STR } },
135 	{ "output", set_output, 1, 1, { TOKEN_STR } },
136 
137 	/* empty terminator -- do not remove */
138 	{ NULL, NULL, 0, 0, {} }
139 };
140 
alaw_init(void)141 int alaw_init(void)
142 {
143 	register_config_context( "encoder", "alaw", start_block, end_block,
144 					config_statements );
145 	return 0;
146 }
147