1 |
|
2 #include <gst/gst_global.h> |
|
3 #include <stdlib.h> |
|
4 #include <gst/gst.h> |
|
5 #include <gst/gstelement.h> |
|
6 #include <string.h> |
|
7 #define LOG_FILE "c:\\logs\\wav_record_logs.txt" |
|
8 #include "std_log_result.h" |
|
9 #define LOG_FILENAME_LINE __FILE__, __LINE__ |
|
10 |
|
11 void create_xml(int result) |
|
12 { |
|
13 |
|
14 if(result) |
|
15 { |
|
16 assert_failed = 1; |
|
17 } |
|
18 |
|
19 testResultXml(xmlfile); |
|
20 close_log_file(); |
|
21 |
|
22 if(result) |
|
23 { |
|
24 exit (-1); |
|
25 } |
|
26 |
|
27 } |
|
28 |
|
29 GstElement *pipeline, *source, *wavenc,*sink; |
|
30 GstBus *bus; |
|
31 GMainLoop *loop; |
|
32 gint error; |
|
33 static gboolean |
|
34 bus_call (GstBus *bus, |
|
35 GstMessage *msg, |
|
36 gpointer data) |
|
37 { |
|
38 switch (GST_MESSAGE_TYPE (msg)) { |
|
39 case GST_MESSAGE_EOS: |
|
40 gst_element_set_state (pipeline, GST_STATE_NULL); |
|
41 g_main_loop_quit(loop); |
|
42 gst_object_unref (GST_OBJECT (pipeline)); |
|
43 //std_log(LOG_FILENAME_LINE, "Test Successful"); |
|
44 create_xml(error); |
|
45 break; |
|
46 case GST_MESSAGE_ERROR: { |
|
47 gchar *debug; |
|
48 GError *err; |
|
49 gst_message_parse_error (msg, &err, &debug); |
|
50 g_free (debug); |
|
51 g_print ("Error: %s\n", err->message); |
|
52 error = err->code; |
|
53 g_error_free (err); |
|
54 //std_log(LOG_FILENAME_LINE, "Test Failed"); |
|
55 |
|
56 break; |
|
57 } |
|
58 default: |
|
59 break; |
|
60 } |
|
61 |
|
62 return TRUE; |
|
63 } |
|
64 |
|
65 static gboolean |
|
66 quit_loop (gpointer data) |
|
67 { |
|
68 gst_element_send_event (pipeline, gst_event_new_eos ()); |
|
69 |
|
70 } |
|
71 |
|
72 |
|
73 /// exe name record_duration sampling_rate chans width |
|
74 int main (int argc, char *argv[]) |
|
75 { |
|
76 char filename[1024]; |
|
77 GstCaps* caps; |
|
78 int sampling_rate = 8000; |
|
79 int chans = 1; |
|
80 int width = 16; |
|
81 guint record_duration = 10000; |
|
82 |
|
83 error = 0; |
|
84 if (argc > 1) { |
|
85 record_duration = (guint) atoi( argv[1] ) * 1000; |
|
86 } |
|
87 |
|
88 if (argc > 2) { |
|
89 sampling_rate = (int) atoi( argv[2] ); |
|
90 } |
|
91 |
|
92 if (argc > 3) { |
|
93 chans = (int) atoi( argv[3] ); |
|
94 } |
|
95 |
|
96 if (argc > 4) { |
|
97 width = (int) atoi( argv[4] ); |
|
98 } |
|
99 |
|
100 |
|
101 gst_init (&argc, &argv); |
|
102 loop = g_main_loop_new (NULL, FALSE); |
|
103 |
|
104 /* create elements */ |
|
105 pipeline = gst_pipeline_new ("audio-player"); |
|
106 source = gst_element_factory_make ("devsoundsrc", "audio-source"); |
|
107 wavenc = gst_element_factory_make ("wavenc", "mp3parse"); |
|
108 sink = gst_element_factory_make ("filesink", "sink"); |
|
109 |
|
110 if (!pipeline || !source || !wavenc || !sink) { |
|
111 g_print ("One element could not be created\n"); |
|
112 return -1; |
|
113 } |
|
114 snprintf(filename, 1024, "C:\\data\\wav_record_samp-%d-chans-%d-duration-%dsec.wav", |
|
115 sampling_rate, chans,record_duration/1000 ); |
|
116 |
|
117 /* set filename property on the file source. Also add a message handler. */ |
|
118 g_object_set (G_OBJECT (sink), "location", filename, NULL); |
|
119 /* put all elements in a bin */ |
|
120 gst_bin_add_many (GST_BIN (pipeline),source, wavenc,sink, NULL); |
|
121 |
|
122 caps = gst_caps_new_simple ("audio/x-raw-int", |
|
123 "width", G_TYPE_INT, width, |
|
124 "depth", G_TYPE_INT, width, |
|
125 "signed",G_TYPE_BOOLEAN, TRUE, |
|
126 "endianness",G_TYPE_INT, G_BYTE_ORDER, |
|
127 "rate", G_TYPE_INT, sampling_rate, |
|
128 "channels", G_TYPE_INT, chans, NULL); |
|
129 |
|
130 gst_element_link_filtered (source, wavenc, caps); |
|
131 gst_element_link (wavenc, sink); |
|
132 |
|
133 gst_bus_add_watch (gst_pipeline_get_bus (GST_PIPELINE (pipeline)), bus_call, loop); |
|
134 //g_signal_connect (decoder, "pad-added", G_CALLBACK (new_pad_cb),pipeline); |
|
135 /* Now set to playing and iterate. */ |
|
136 g_print ("Setting to PLAYING\n"); |
|
137 gst_element_set_state (pipeline, GST_STATE_PLAYING); |
|
138 g_timeout_add (record_duration, quit_loop, loop); |
|
139 g_print ("Running\n"); |
|
140 g_main_loop_run (loop); |
|
141 /* clean up nicely */ |
|
142 g_print ("Returned, stopping playback\n"); |
|
143 gst_element_set_state (pipeline, GST_STATE_NULL); |
|
144 g_print ("Deleting pipeline\n"); |
|
145 gst_object_unref (GST_OBJECT (pipeline)); |
|
146 |
|
147 g_print ("completed palying audio\n"); |
|
148 return 0; |
|
149 } |
|
150 |
|