好的,我已经编写了一些应该打开和音频输出设备的代码,每当实际的PCM数据可用时,我会调用Sound_WriteFrame()并堆积更多要播放的数据。
数据是原始的,没有标题,所以当我调用Sound_Open()时,我会传递这些信息,让GStreamer知道将会到达什么样的数据。
这段代码不起作用 - 坦率地说我不知道我在做什么,我发现GStreamer有点难以使用,我希望最终可以改变。
我正在使用GStreamer 1.0。感谢帮助。
#include <gstreamer-1.0/gst/gst.h>
#include <gstreamer-1.0/gst/gstelement.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
typedef struct _sound_t {
GstElement *source, *sink, *pipeline;
GMemoryInputStream *giostream;
GstPad *sourcepad;
GstCaps *gcaps;
int bDeviceOpen;
} SOUND_CTX;
int Sound_Close(SOUND_CTX *p){
printf("Closing\n");
gst_element_set_state(p->pipeline, GST_STATE_NULL);
return 1;
}
void Sound_SetState(SOUND_CTX *p, GstState state){
GstStateChangeReturn r = gst_element_set_state(p->pipeline, state);
switch(r){
case GST_STATE_CHANGE_FAILURE: printf("GST_STATE_CHANGE_FAILURE\n"); break;
case GST_STATE_CHANGE_SUCCESS: printf("GST_STATE_CHANGE_SUCCESS\n"); break;
case GST_STATE_CHANGE_ASYNC: printf("GST_STATE_CHANGE_ASYNC\n"); break;
case GST_STATE_CHANGE_NO_PREROLL: printf("GST_STATE_CHANGE_NO_PREROLL\n"); break;
default: printf("Unknown state\n"); break;
}
return;
}
int Sound_Open(SOUND_CTX *p, int nSamplesPerSec, int nChannels){
p->source = gst_element_factory_make("giostreamsrc", "source");
p->giostream = G_MEMORY_INPUT_STREAM(g_memory_input_stream_new());
g_object_set(G_OBJECT(p->source), "stream", G_INPUT_STREAM(p->giostream), NULL);
p->sourcepad = gst_element_get_static_pad(p->source, "src");
p->gcaps = gst_caps_new_simple(
"audio/x-raw",
"rate", G_TYPE_INT, nSamplesPerSec,
"channels", G_TYPE_INT, nChannels,
"width", G_TYPE_INT, 16,
"depth", G_TYPE_INT, 16,
"signed", G_TYPE_BOOLEAN, TRUE,
NULL
);
gst_pad_set_caps(p->sourcepad, p->gcaps);
gst_object_unref(p->sourcepad);
p->sink = gst_element_factory_make("alsasink", "sink");
p->pipeline = gst_pipeline_new("pipeline_name");
gst_bin_add_many(GST_BIN(p->pipeline), p->source, p->sink, NULL);
gst_element_link_many(p->source, p->sink, NULL);
Sound_SetState(p, GST_STATE_PLAYING);
return 1;
}
int Sound_WriteFrame(SOUND_CTX *p, void *lpData, unsigned int size){
g_memory_input_stream_add_data(
G_MEMORY_INPUT_STREAM(p->giostream),
lpData, size, NULL
);
return 0;
}
int timer_callback(const void *data){
g_main_loop_quit((GMainLoop *)data);
return FALSE;
}
int main(int argc, char *argv[]){
gst_init(&argc, &argv);
GMainLoop *loop = NULL;
SOUND_CTX a;
memset(&a, 0x00, sizeof(SOUND_CTX));
if(Sound_Open(&a, 44100, 2)){
FILE *handle;
unsigned char tmp[4096];
if((handle = fopen("test.pcm", "rb")) != NULL){
while(fread(tmp, 1, sizeof(tmp), handle) == sizeof(tmp)){
Sound_WriteFrame(&a, tmp, sizeof(tmp));
}
fclose(handle);
}
}
loop = g_main_loop_new(NULL, FALSE);
g_timeout_add(5500, (GSourceFunc)timer_callback, loop);
g_main_loop_run(loop);
Sound_Close(&a);
g_main_loop_unref(loop);
return 0;
}
好的,所以这里有一个更新的来源,但扬声器没有声音
#include <gstreamer-1.0/gst/gst.h>
#include <gstreamer-1.0/gst/gstelement.h>
#include <gstreamer-1.0/gst/app/gstappsrc.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
typedef struct _sound_t {
GstElement *sink, *pipeline;
GstAppSrc *src;
GstCaps *pcm_caps;
} SOUND_CTX;
int Sound_Close(SOUND_CTX *p){
printf("Closing\n");
gst_element_set_state(p->pipeline, GST_STATE_NULL);
return 1;
}
void Sound_SetState(SOUND_CTX *p, GstState state){
GstStateChangeReturn r = gst_element_set_state(p->pipeline, state);
switch(r){
case GST_STATE_CHANGE_FAILURE: printf("GST_STATE_CHANGE_FAILURE\n"); break;
case GST_STATE_CHANGE_SUCCESS: printf("GST_STATE_CHANGE_SUCCESS\n"); break;
case GST_STATE_CHANGE_ASYNC: printf("GST_STATE_CHANGE_ASYNC\n"); break;
case GST_STATE_CHANGE_NO_PREROLL: printf("GST_STATE_CHANGE_NO_PREROLL\n"); break;
default: printf("Unknown state\n"); break;
}
return;
}
int Sound_Open(SOUND_CTX *p, int nSamplesPerSec, int nChannels){
p->pipeline = gst_pipeline_new("pipeline_name");
p->sink = gst_element_factory_make("alsasink", "sink");
p->src = (GstAppSrc*) gst_element_factory_make("appsrc", "source");
gst_app_src_set_stream_type(p->src, GST_APP_STREAM_TYPE_STREAM);
// I am hardcoding the format, channels, and rate for now
p->pcm_caps = gst_caps_from_string("audio/x-raw,format=S16LE,rate=44100,channels=2");
gst_app_src_set_caps(p->src, p->pcm_caps);
gst_bin_add_many(GST_BIN(p->pipeline), (GstElement*)p->src, p->sink, NULL);
gst_element_link_many((GstElement*)p->src, p->sink, NULL);
Sound_SetState(p, GST_STATE_PLAYING);
return 1;
}
int Sound_WriteFrame(SOUND_CTX *p, void *lpData, unsigned int size){
GstBuffer *buf = NULL;
void *lpHeapData = NULL;
if((lpHeapData = g_malloc(size)) == NULL) return 0;
memcpy(lpHeapData, lpData, size);
buf = gst_buffer_new_wrapped(lpHeapData, size);
if(buf == NULL){
g_free(lpHeapData);
return 0;
}
gst_app_src_push_buffer(p->src, buf);
return 0;
}
int timer_callback(const void *data){
g_main_loop_quit((GMainLoop *)data);
return FALSE;
}
int main(int argc, char *argv[]){
gst_init(&argc, &argv);
GMainLoop *loop = NULL;
SOUND_CTX a;
memset(&a, 0x00, sizeof(SOUND_CTX));
if(Sound_Open(&a, 44100, 2)){
FILE *handle;
unsigned char tmp[4096];
if((handle = fopen("test.pcm", "rb")) != NULL){
while(fread(tmp, 1, sizeof(tmp), handle) == sizeof(tmp)){
Sound_WriteFrame(&a, tmp, sizeof(tmp));
}
fclose(handle);
}
}
loop = g_main_loop_new(NULL, FALSE);
g_timeout_add(5500, (GSourceFunc)timer_callback, loop);
g_main_loop_run(loop);
Sound_Close(&a);
g_main_loop_unref(loop);
return 0;
}
答案 0 :(得分:0)
您正在使用GStreamer 0.10 Caps字段,其内容类似于GStreamer 1.0 Code。感兴趣的是,宽度/深度/有符号已被称为格式的单个字段所取代。有关完整列表,请参阅文档,大写字母中的名称与枚举相同,但没有名称空间GST_AUDIO_FORMAT _。