blob: 1ae9a6cf74f88ae32dccd393db41044ebac4f71c [file] [log] [blame]
Benny Prijonocbc1c472006-03-19 00:50:23 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <pjmedia.h>
Benny Prijonobc797312006-03-24 20:44:27 +000021#include <pjlib-util.h>
22#include <pjlib.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26#include "util.h"
27
Benny Prijonocbc1c472006-03-19 00:50:23 +000028
29/*
30 * playfile.c
31 *
32 * PURPOSE:
33 * Play a WAV file to sound player device.
34 *
35 * USAGE:
36 * playfile FILE.WAV
37 *
38 * The WAV file could have mono or stereo channels with arbitrary
39 * sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM.
40 *
41 */
42
Benny Prijonocbc1c472006-03-19 00:50:23 +000043
44/* For logging purpose. */
45#define THIS_FILE "playfile.c"
46
47
Benny Prijonobc797312006-03-24 20:44:27 +000048static const char *desc =
49" FILE \n"
50" \n"
51" playfile.c \n"
52" \n"
53" PURPOSE \n"
54" \n"
55" Demonstrate how to play a WAV file. \n"
56" \n"
57" USAGE \n"
58" \n"
59" playfile FILE.WAV \n"
60" \n"
61" The WAV file could have mono or stereo channels with arbitrary \n"
62" sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n";
Benny Prijonocbc1c472006-03-19 00:50:23 +000063
64
65/*
66 * main()
67 */
68int main(int argc, char *argv[])
69{
70 pj_caching_pool cp;
71 pjmedia_endpt *med_endpt;
72 pj_pool_t *pool;
73 pjmedia_port *file_port;
74 pjmedia_snd_port *snd_port;
75 char tmp[10];
76 pj_status_t status;
77
78
Benny Prijonocbc1c472006-03-19 00:50:23 +000079 /* Must init PJLIB first: */
80 status = pj_init();
81 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
82
83 /* Must create a pool factory before we can allocate any memory. */
84 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
85
86 /*
87 * Initialize media endpoint.
88 * This will implicitly initialize PJMEDIA too.
89 */
Benny Prijono275fd682006-03-22 11:59:11 +000090 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijonocbc1c472006-03-19 00:50:23 +000091 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
92
93 /* Create memory pool for our file player */
94 pool = pj_pool_create( &cp.factory, /* pool factory */
95 "wav", /* pool name. */
96 4000, /* init size */
97 4000, /* increment size */
98 NULL /* callback on error */
99 );
100
101 /* Create file media port from the WAV file */
102 status = pjmedia_file_player_port_create( pool, /* memory pool */
103 argv[1], /* file to play */
104 0, /* flags */
105 0, /* default buffer */
106 NULL, /* user data */
107 &file_port/* returned port */
108 );
109 if (status != PJ_SUCCESS) {
110 app_perror(THIS_FILE, "Unable to use WAV file", status);
111 return 1;
112 }
113
114 /* Create sound player port. */
115 status = pjmedia_snd_port_create_player(
116 pool, /* pool */
117 -1, /* use default dev. */
118 file_port->info.sample_rate, /* clock rate. */
119 file_port->info.channel_count, /* # of channels. */
120 file_port->info.samples_per_frame, /* samples per frame. */
121 file_port->info.bits_per_sample, /* bits per sample. */
122 0, /* options */
123 &snd_port /* returned port */
124 );
125 if (status != PJ_SUCCESS) {
126 app_perror(THIS_FILE, "Unable to open sound device", status);
127 return 1;
128 }
129
130 /* Connect file port to the sound player.
131 * Stream playing will commence immediately.
132 */
133 status = pjmedia_snd_port_connect( snd_port, file_port);
134 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
135
136
137
138 /*
139 * File should be playing and looping now, using sound device's thread.
140 */
141
142
143 /* Sleep to allow log messages to flush */
144 pj_thread_sleep(100);
145
146
147 printf("Playing %s..\n", argv[1]);
148 puts("");
149 puts("Press <ENTER> to stop playing and quit");
150
151 fgets(tmp, sizeof(tmp), stdin);
152
153
154 /* Start deinitialization: */
155
156 /* Destroy sound device */
157 status = pjmedia_snd_port_destroy( snd_port );
158 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
159
160
161 /* Destroy file port */
162 status = pjmedia_port_destroy( file_port );
163 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
164
165
166 /* Release application pool */
167 pj_pool_release( pool );
168
169 /* Destroy media endpoint. */
170 pjmedia_endpt_destroy( med_endpt );
171
172 /* Destroy pool factory */
173 pj_caching_pool_destroy( &cp );
174
175
176 /* Done. */
177 return 0;
178}
Benny Prijonobf13fee2006-04-20 11:13:32 +0000179