-
Notifications
You must be signed in to change notification settings - Fork 9
/
play_audio.c
183 lines (152 loc) · 4.46 KB
/
play_audio.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2004 Nathan Lutchansky <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#include <event.h>
#include <log.h>
#include <frame.h>
#include <stream.h>
#include <outputs.h>
#include <rtp.h>
#include <rtp_media.h>
#include <conf_parse.h>
#include "include/libcomponent.h"
#include "include/list.h"
#include "include/av_type.h"
#include "include/c_audio.h"
#include "include/c_user.h"
#include "include/c_video.h"
#include "include/utility.h"
struct play_audio_source;
struct play_audio_track {
int index;
struct play_audio_source *source;
struct stream_destination *stream;
int ready;
struct rtp_media *rtp;
};
struct play_audio_source {
struct play_audio_track track[MAX_TRACKS];
};
static void next_play_audio_frame( struct frame *f, void *d )
{
struct play_audio_track *track = (struct play_audio_track *)d;
AUDIO_CHUNK *pChunk = malloc(sizeof(AUDIO_CHUNK));
if(pChunk)
{
switch(f->format)
{
case FORMAT_PCM:
pChunk->codec = PCM;
break;
case FORMAT_ADPCM:
pChunk->codec = ADPCM;
break;
default:
break;
}
pChunk->seq = f->seq;
pChunk->tick = f->timestamp>>3;
pChunk->t = 0;
pChunk->sample = ntohs(*(unsigned short*)f->d);
pChunk->index = ntohs(*(unsigned short*)(f->d + 2));
pChunk->len = f->length - 4;
pChunk->data = malloc(pChunk->len);
if(pChunk->data)
{
memcpy(pChunk->data, f->d + 4, pChunk->len);
INIT_LIST_HEAD(&pChunk->list);
c_speak(pChunk);
}
else
{
free(pChunk);
}
}
unref_frame( f );
}
/************************ CONFIGURATION DIRECTIVES ************************/
static void *start_block(void)
{
struct play_audio_source *source;
int i;
source = (struct play_audio_source *)malloc( sizeof( struct play_audio_source ) );
source->sess_list = NULL;
for( i = 0; i < MAX_TRACKS; ++i )
{
source->track[i].index = i;
source->track[i].source = source;
source->track[i].stream = NULL;
source->track[i].ready = 0;
source->track[i].rtp = NULL;
}
return source;
}
static int end_block( void *d )
{
struct play_audio_source *source = (struct play_audio_source *)d;
if( ! source->track[0].stream )
{
spook_log( SL_ERR, "live: no media sources specified!" );
return -1;
}
return 0;
}
static int set_track( int num_tokens, struct token *tokens, void *d )
{
struct play_audio_source *source = (struct play_audio_source *)d;
int t, formats[] = { FORMAT_PCM, FORMAT_ADPCM };
for( t = 0; t < MAX_TRACKS && source->track[t].rtp; ++t );
if( t == MAX_TRACKS )
{
spook_log( SL_ERR, "live: exceeded maximum number of tracks" );
return -1;
}
if( ! ( source->track[t].stream = connect_to_stream( tokens[1].v.str,
next_play_audio_frame, &source->track[t], formats, 2 ) ) )
{
spook_log( SL_ERR,
"live: unable to connect to stream \"%s\"",
tokens[1].v.str );
return -1;
}
set_waiting( source->track[t].stream, 1 );
return 0;
}
static struct statement config_statements[] = {
/* directive name, process function, min args, max args, arg types */
{ "track", set_track, 1, 1, { TOKEN_STR } },
//{ "path", set_path, 1, 4, { TOKEN_STR, TOKEN_STR, TOKEN_STR, TOKEN_STR } },
/* { "sipline", set_sip_line, 1, 1, { TOKEN_STR } }, */
/* empty terminator -- do not remove */
{ NULL, NULL, 0, 0, {} }
};
int play_audio_init(void)
{
register_config_context( "play", "audio", start_block, end_block,
config_statements );
return 0;
}