blob: 06f4c1f20caae992c0e504c350e6579a13db3fdb [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 Prijono15953012006-04-27 22:37:08 +000079 PJ_UNUSED_ARG(argc);
80
81
Benny Prijonocbc1c472006-03-19 00:50:23 +000082 /* Must init PJLIB first: */
83 status = pj_init();
84 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
85
86 /* Must create a pool factory before we can allocate any memory. */
87 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
88
89 /*
90 * Initialize media endpoint.
91 * This will implicitly initialize PJMEDIA too.
92 */
Benny Prijono275fd682006-03-22 11:59:11 +000093 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijonocbc1c472006-03-19 00:50:23 +000094 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
95
96 /* Create memory pool for our file player */
97 pool = pj_pool_create( &cp.factory, /* pool factory */
98 "wav", /* pool name. */
99 4000, /* init size */
100 4000, /* increment size */
101 NULL /* callback on error */
102 );
103
104 /* Create file media port from the WAV file */
Benny Prijono15953012006-04-27 22:37:08 +0000105 status = pjmedia_wav_player_port_create( pool, /* memory pool */
Benny Prijonocbc1c472006-03-19 00:50:23 +0000106 argv[1], /* file to play */
Benny Prijono15953012006-04-27 22:37:08 +0000107 20, /* ptime. */
Benny Prijonocbc1c472006-03-19 00:50:23 +0000108 0, /* flags */
109 0, /* default buffer */
110 NULL, /* user data */
111 &file_port/* returned port */
112 );
113 if (status != PJ_SUCCESS) {
114 app_perror(THIS_FILE, "Unable to use WAV file", status);
115 return 1;
116 }
117
118 /* Create sound player port. */
119 status = pjmedia_snd_port_create_player(
120 pool, /* pool */
121 -1, /* use default dev. */
Benny Prijono15953012006-04-27 22:37:08 +0000122 file_port->info.clock_rate, /* clock rate. */
Benny Prijonocbc1c472006-03-19 00:50:23 +0000123 file_port->info.channel_count, /* # of channels. */
124 file_port->info.samples_per_frame, /* samples per frame. */
125 file_port->info.bits_per_sample, /* bits per sample. */
126 0, /* options */
127 &snd_port /* returned port */
128 );
129 if (status != PJ_SUCCESS) {
130 app_perror(THIS_FILE, "Unable to open sound device", status);
131 return 1;
132 }
133
134 /* Connect file port to the sound player.
135 * Stream playing will commence immediately.
136 */
137 status = pjmedia_snd_port_connect( snd_port, file_port);
138 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
139
140
141
142 /*
143 * File should be playing and looping now, using sound device's thread.
144 */
145
146
147 /* Sleep to allow log messages to flush */
148 pj_thread_sleep(100);
149
150
151 printf("Playing %s..\n", argv[1]);
152 puts("");
153 puts("Press <ENTER> to stop playing and quit");
154
155 fgets(tmp, sizeof(tmp), stdin);
156
157
158 /* Start deinitialization: */
159
160 /* Destroy sound device */
161 status = pjmedia_snd_port_destroy( snd_port );
162 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
163
164
165 /* Destroy file port */
166 status = pjmedia_port_destroy( file_port );
167 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
168
169
170 /* Release application pool */
171 pj_pool_release( pool );
172
173 /* Destroy media endpoint. */
174 pjmedia_endpt_destroy( med_endpt );
175
176 /* Destroy pool factory */
177 pj_caching_pool_destroy( &cp );
178
179
180 /* Done. */
181 return 0;
182}
Benny Prijonobf13fee2006-04-20 11:13:32 +0000183