This example demonstrate how to create a custom media port (in this case, a sine wave generator) and connect it to the sound device.
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <pjmedia.h>
00048 #include <pjlib.h>
00049
00050 #include <stdlib.h>
00051 #include <stdio.h>
00052 #include <math.h>
00053
00054
00055 #define THIS_FILE "playsine.c"
00056
00057
00058
00059 static int app_perror( const char *sender, const char *title,
00060 pj_status_t status)
00061 {
00062 char errmsg[PJ_ERR_MSG_SIZE];
00063
00064 PJ_UNUSED_ARG(sender);
00065
00066 pj_strerror(status, errmsg, sizeof(errmsg));
00067
00068 printf("%s: %s [code=%d]\n", title, errmsg, status);
00069 return 1;
00070 }
00071
00072
00073
00074 typedef struct
00075 {
00076 pj_int16_t *samples;
00077 } port_data;
00078
00079
00080
00081 static pj_status_t sine_get_frame( pjmedia_port *port,
00082 pjmedia_frame *frame)
00083 {
00084 port_data *sine = port->port_data.pdata;
00085 pj_int16_t *samples = frame->buf;
00086 unsigned i, count, left, right;
00087
00088
00089 count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
00090
00091 left = 0;
00092 right = 0;
00093
00094 for (i=0; i<count; ++i) {
00095 *samples++ = sine->samples[left];
00096 ++left;
00097
00098 if (PJMEDIA_PIA_CCNT(&port->info) == 2) {
00099 *samples++ = sine->samples[right];
00100 right += 2;
00101 if (right >= count)
00102 right = 0;
00103 }
00104 }
00105
00106
00107
00108
00109 frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
00110
00111 return PJ_SUCCESS;
00112 }
00113
00114 #ifndef M_PI
00115 #define M_PI (3.14159265)
00116 #endif
00117
00118
00119
00120
00121 static pj_status_t create_sine_port(pj_pool_t *pool,
00122 unsigned sampling_rate,
00123 unsigned channel_count,
00124 pjmedia_port **p_port)
00125 {
00126 pjmedia_port *port;
00127 unsigned i;
00128 unsigned count;
00129 pj_str_t name;
00130 port_data *sine;
00131
00132 PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
00133 PJ_EINVAL);
00134
00135 port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
00136 PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
00137
00138
00139 name = pj_str("sine generator");
00140 pjmedia_port_info_init(&port->info, &name,
00141 PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'),
00142 sampling_rate,
00143 channel_count,
00144 16, sampling_rate * 20 / 1000 * channel_count);
00145
00146
00147 port->get_frame = &sine_get_frame;
00148
00149
00150 port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
00151
00152
00153 count = PJMEDIA_PIA_SPF(&port->info) / channel_count;
00154 sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
00155 PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
00156
00157
00158 for( i=0; i<count; i++ )
00159 {
00160 sine->samples[i] = (pj_int16_t) (10000.0 *
00161 sin(((double)i/(double)count) * M_PI * 8.) );
00162 }
00163
00164 *p_port = port;
00165
00166 return PJ_SUCCESS;
00167 }
00168
00169
00170
00171 static void usage(void)
00172 {
00173 puts("");
00174 puts("Usage: playsine [nchannel]");
00175 puts("");
00176 puts("where");
00177 puts(" nchannel is number of audio channels (1 for mono, or 2 for stereo).");
00178 puts(" Default is 1 (mono).");
00179 puts("");
00180 }
00181
00182
00183
00184
00185
00186 int main(int argc, char *argv[])
00187 {
00188 pj_caching_pool cp;
00189 pjmedia_endpt *med_endpt;
00190 pj_pool_t *pool;
00191 pjmedia_port *sine_port;
00192 pjmedia_snd_port *snd_port;
00193 char tmp[10];
00194 int channel_count = 1;
00195 pj_status_t status;
00196
00197 if (argc == 2) {
00198 channel_count = atoi(argv[1]);
00199 if (channel_count < 1 || channel_count > 2) {
00200 puts("Error: invalid arguments");
00201 usage();
00202 return 1;
00203 }
00204 }
00205
00206
00207 status = pj_init();
00208 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00209
00210
00211 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00212
00213
00214
00215
00216
00217 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00218 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00219
00220
00221 pool = pj_pool_create( &cp.factory,
00222 "wav",
00223 4000,
00224 4000,
00225 NULL
00226 );
00227
00228
00229 status = create_sine_port( pool,
00230 11025,
00231 channel_count,
00232 &sine_port
00233 );
00234 if (status != PJ_SUCCESS) {
00235 app_perror(THIS_FILE, "Unable to create sine port", status);
00236 return 1;
00237 }
00238
00239
00240 status = pjmedia_snd_port_create_player(
00241 pool,
00242 -1,
00243 PJMEDIA_PIA_SRATE(&sine_port->info),
00244 PJMEDIA_PIA_CCNT(&sine_port->info),
00245 PJMEDIA_PIA_SPF(&sine_port->info),
00246 PJMEDIA_PIA_BITS(&sine_port->info),
00247 0,
00248 &snd_port
00249 );
00250 if (status != PJ_SUCCESS) {
00251 app_perror(THIS_FILE, "Unable to open sound device", status);
00252 return 1;
00253 }
00254
00255
00256
00257
00258 status = pjmedia_snd_port_connect( snd_port, sine_port);
00259 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 pj_thread_sleep(100);
00270
00271
00272 puts("Playing sine wave..");
00273 puts("");
00274 puts("Press <ENTER> to stop playing and quit");
00275
00276 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
00277 puts("EOF while reading stdin, will quit now..");
00278 }
00279
00280
00281
00282
00283
00284 status = pjmedia_snd_port_disconnect(snd_port);
00285 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00286
00287
00288
00289
00290 pj_thread_sleep(100);
00291
00292
00293 status = pjmedia_snd_port_destroy( snd_port );
00294 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00295
00296
00297
00298 status = pjmedia_port_destroy( sine_port );
00299 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00300
00301
00302
00303 pj_pool_release( pool );
00304
00305
00306 pjmedia_endpt_destroy( med_endpt );
00307
00308
00309 pj_caching_pool_destroy( &cp );
00310
00311
00312 pj_shutdown();
00313
00314
00315
00316 return 0;
00317 }