1. 目標
在Basic和Playback的教程中(注:另外兩個教程),GStreamer可以和GLib的主循環完美的集成起來,這樣能用一種簡單的方法同時監控pipeline的操作和UI的處理。而在不支持GLib的iOS和Android平台上就必須小心對於pipeline的操作——不能阻塞住UI。
這份教程講述了:
如何把GStreamer的相關處理代碼放到其他的線程(DispatchQueue),保持UI仍然保留在主線程(MainDispatchQueue)在ObjC的UI代碼中如何和GStreamer的C代碼通信 2. 介紹 當由UI界面的時候,如果應用等待GStreamer的回傳消息然後進行UI的處理是非常悲催的。通常的做法是(用GTK+toolkit做例子哈)讓GMainLoop(GLib)來處理收到的event,無論是UI的還是GStreamer發出的。 悲催的是這個方法不適合其他非基於GLib的圖形系統(比如iOS的GocoaTouch框架),我們的方法是在另一個線程裡面簡單的調用GMainLoop,確保不會阻塞UI主線程。 這個教程還會指出幾個ObjC和C互相調用時需要注意的幾個地方。 下面的代碼使用的pipeline僅僅使用了audiotestsrc和autoaudiosink兩個element。UI上包含兩個按鈕,用來設置pipeline的狀態PLAYING/ PAUSED。還有一個Label用來顯示一些信息(錯誤信息或狀態改變)。#import "ViewController.h" #import "GStreamerBackend.h" #import在這個類裡面包含了一個GStreamerBackend的實例,@interface ViewController () { GStreamerBackend *gst_backend; } @end @implementation ViewController /* * Methods from UIViewController */ - (void)viewDidLoad { [super viewDidLoad]; play_button.enabled = FALSE; pause_button.enabled = FALSE; gst_backend = [[GStreamerBackend alloc] init:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* Called when the Play button is pressed */ -(IBAction) play:(id)sender { [gst_backend play]; } /* Called when the Pause button is pressed */ -(IBAction) pause:(id)sender { [gst_backend pause]; } /* * Methods from GstreamerBackendDelegate */ -(void) gstreamerInitialized { dispatch_async(dispatch_get_main_queue(), ^{ play_button.enabled = TRUE; pause_button.enabled = TRUE; message_label.text = @"Ready"; }); } -(void) gstreamerSetUIMessage:(NSString *)message { dispatch_async(dispatch_get_main_queue(), ^{ message_label.text = message; }); } @end
@interface ViewController () { GStreamerBackend *gst_backend; }在viewDidLoad方法裡面創建並調用了自定義的init方法
- (void)viewDidLoad { [super viewDidLoad]; play_button.enabled = FALSE; pause_button.enabled = FALSE; gst_backend = [[GStreamerBackend alloc] init:self]; }這個自定義的init方法必須傳入一個對象作為UI的delegate(本例是使用了self)
/* Called when the Play button is pressed */ -(IBAction) play:(id)sender { [gst_backend play]; } /* Called when the Pause button is pressed */ -(IBAction) pause:(id)sender { [gst_backend pause]; }在用戶按下Play/Pause按鈕時,上面的方法會被調用。我們看到僅僅就是簡單的調用了GStreamerBackend裡面對應的方法。-(void) gstreamerInitialized { dispatch_async(dispatch_get_main_queue(), ^{ play_button.enabled = TRUE; pause_button.enabled = TRUE; message_label.text = @"Ready"; }); }gstreamerInitialized方法是定義在GStreamerBackendDelegate協議裡面的,用來標識後台已經准備好,可以接受命令了。在這個例子中我們把Play/Pause按鈕激活,並顯示“Ready”信息。這個方法不是在UI線程裡面運行的,所以要用dispatch_async()方法把UI的內容封起來。-(void) gstreamerSetUIMessage:(NSString *)message { dispatch_async(dispatch_get_main_queue(), ^{ message_label.text = message; }); }gstreamerSetUIMessage:方法同樣是定義在GStreamerBackendDelegate協議裡面的,當後台有消息通知UI的時候會調用這個方法。在這個例子裡面消息會拷貝到界面的Label控件上,當然,也同樣要用dispatch_async()方法來封裝。
4. GStreamer後端 GStreamerBackend類處理了所有和GStreamer相關的內容,並給應用提供了一個簡單的接口。這個接口並不需要實現所有的GStreamer的細節,當需要引起UI的變化的時候,調用GSreamerBackendDelegate協議來解決。 GStreamerBackend.m
#import "GStreamerBackend.h" #include其中,接口方法是:GST_DEBUG_CATEGORY_STATIC (debug_category); #define GST_CAT_DEFAULT debug_category @interface GStreamerBackend() -(void)setUIMessage:(gchar*) message; -(void)app_function; -(void)check_initialization_complete; @end @implementation GStreamerBackend { id ui_delegate; /* Class that we use to interact with the user interface */ GstElement *pipeline; /* The running pipeline */ GMainContext *context; /* GLib context used to run the main loop */ GMainLoop *main_loop; /* GLib main loop */ gboolean initialized; /* To avoid informing the UI multiple times about the initialization */ } /* * Interface methods */ -(id) init:(id) uiDelegate { if (self = [super init]) { self->ui_delegate = uiDelegate; GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "iOS tutorial 2"); gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG); /* Start the bus monitoring task */ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self app_function]; }); } return self; } -(void) dealloc { if (pipeline) { GST_DEBUG("Setting the pipeline to NULL"); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); pipeline = NULL; } } -(void) play { if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { [self setUIMessage:"Failed to set pipeline to playing"]; } } -(void) pause { if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) { [self setUIMessage:"Failed to set pipeline to paused"]; } } /* * Private methods */ /* Change the message on the UI through the UI delegate */ -(void)setUIMessage:(gchar*) message { NSString *string = [NSString stringWithUTF8String:message]; if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)]) { [ui_delegate gstreamerSetUIMessage:string]; } } /* Retrieve errors from the bus and show them on the UI */ static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { GError *err; gchar *debug_info; gchar *message_string; gst_message_parse_error (msg, &err, &debug_info); message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message); g_clear_error (&err); g_free (debug_info); [self setUIMessage:message_string]; g_free (message_string); gst_element_set_state (self->pipeline, GST_STATE_NULL); } /* Notify UI about pipeline state changes */ static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); /* Only pay attention to messages coming from the pipeline, not its children */ if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) { gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state)); [self setUIMessage:message]; g_free (message); } } /* Check if all conditions are met to report GStreamer as initialized. * These conditions will change depending on the application */ -(void) check_initialization_complete { if (!initialized && main_loop) { GST_DEBUG ("Initialization complete, notifying application."); if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)]) { [ui_delegate gstreamerInitialized]; } initialized = TRUE; } } /* Main method for the bus monitoring code */ -(void) app_function { GstBus *bus; GSource *bus_source; GError *error = NULL; GST_DEBUG ("Creating pipeline"); /* Create our own GLib Main Context and make it the default one */ context = g_main_context_new (); g_main_context_push_thread_default(context); /* Build pipeline */ pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error); if (error) { gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message); g_clear_error (&error); [self setUIMessage:message]; g_free (message); return; } /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (pipeline); bus_source = gst_bus_create_watch (bus); g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL); g_source_attach (bus_source, context); g_source_unref (bus_source); g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self); g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self); gst_object_unref (bus); /* Create a GLib Main Loop and set it to run */ GST_DEBUG ("Entering main loop..."); main_loop = g_main_loop_new (context, FALSE); [self check_initialization_complete]; g_main_loop_run (main_loop); GST_DEBUG ("Exited main loop"); g_main_loop_unref (main_loop); main_loop = NULL; /* Free resources */ g_main_context_pop_thread_default(context); g_main_context_unref (context); gst_element_set_state (pipeline, GST_STATE_NULL); gst_object_unref (pipeline); return; } @end -(id) init:(id) uiDelegate { if (self = [super init]) { self->ui_delegate = uiDelegate; GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "iOS tutorial 2"); gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG); /* Start the bus monitoring task */ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self app_function]; }); } return self; }這個init方法通過調用[super init]來生成實例,保存delegate的對象用來做UI互動,接著調用app_function並運行在一個獨立的並發的線程裡面,app_function會一直監聽GStreamer總線,看看有沒有應用需要處理的消息或者警告發生。 init:方法同樣注冊了一個新的GStreamer調試類別並設置了吐出的信息的等級,我們就可以在Xcode裡面看到打印信息了。
-(void) dealloc { if (pipeline) { GST_DEBUG("Setting the pipeline to NULL"); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); pipeline = NULL; } }dealloc方法把pipeline置成NULL狀態並釋放它。
-(void) play { if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { [self setUIMessage:"Failed to set pipeline to playing"]; } } -(void) pause { if(gst_element_set_state(pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) { [self setUIMessage:"Failed to set pipeline to paused"]; } }play/pause方法僅僅簡單的設置pipeline狀態的該變,並在出錯的時候通知UI 下面是文件中幾個私有方法:-(void)setUIMessage:(gchar*) message { NSString *string = [NSString stringWithUTF8String:message]; if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)]) { [ui_delegate gstreamerSetUIMessage:string]; } }setUIMessage:方法是把GStreamer使用的C的字符串轉變成NSString*字符串,然後調用GStreamerBackendProtocal協議裡面的gstreamerSetUIMessage:方法來在屏幕上顯示出來。 因為這個方法是optional的,所以需要用respondsToSelector來判一下是否存在。
/* Retrieve errors from the bus and show them on the UI */ static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { GError *err; gchar *debug_info; gchar *message_string; gst_message_parse_error (msg, &err, &debug_info); message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message); g_clear_error (&err); g_free (debug_info); [self setUIMessage:message_string]; g_free (message_string); gst_element_set_state (self->pipeline, GST_STATE_NULL); } /* Notify UI about pipeline state changes */ static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); /* Only pay attention to messages coming from the pipeline, not its children */ if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) { gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state)); [self setUIMessage:message]; g_free (message); } }error_cb()和state_changed_cb()是注冊的兩個回調,分別在GStreamer出錯和狀態變化的時候被調用。這兩個回調的目的是當事件發生時能通知到用戶。 這兩個回調函數在Base的教程中出現了多次,實現起來也除了下面2點之外基本一致:一個是消息使用私有方法setUIMessage:來傳遞到UI;第二個是要調用setUIMessage:就需要一個GStreamerBackend的實例,通過callback的userdata來傳遞,這個在下面討論app_function裡回調的注冊時可以看到-(void) check_initialization_complete { if (!initialized && main_loop) { GST_DEBUG ("Initialization complete, notifying application."); if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)]) { [ui_delegate gstreamerInitialized]; } initialized = TRUE; } }check_initialization_complete()方法確認滿足所有的條件之後通知UI後台GStreamer准備完成。在這個教程裡面這個條件非常簡單,僅僅是主循環存在並且沒有通知過UI。後續的教程這裡會更加復雜。 絕大部分的GStreamer的行為都是在app_function裡面實現的,這些代碼和android的教程幾乎一致。
/* Create our own GLib Main Context and make it the default one */ context = g_main_context_new (); g_main_context_push_thread_default(context);這裡第一次創建了一個GLib的上下文,使用g_main_context_new(),然後用g_main_context_push_thread_default()來創建了一個線程
/* Build pipeline */ pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error); if (error) { gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message); g_clear_error (&error); [self setUIMessage:message]; g_free (message); return; }這裡用gst_arse_launch()方法很輕易的創建了一個pipeline。在這個教程裡面僅僅audiotestsrc和autoaudiosink兩個element需要完成適配。
/* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (pipeline); bus_source = gst_bus_create_watch (bus); g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL); g_source_attach (bus_source, context); g_source_unref (bus_source); g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self); g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self); gst_object_unref (bus);這幾行創建了一個總線信號的監視器,並和設置了需要監視的信號,這些和Basic教程裡面的做法也是一致的。這個監視器我們這裡是一步一步創建的,並非調用gst_bus_add_signal_watch()來創建,這樣可以看清如何使用GLib的一些內容。這裡需要指出的是使用了__bridge來把一個ObjC的對象指針轉換成C語言裡面的指針。這裡我們不需要太擔心對象的所有權的轉移問題,因為在回調裡面userdata會把這個指針帶回來,重新轉換成GStreamerBackend的對象指針
/* Create a GLib Main Loop and set it to run */ GST_DEBUG ("Entering main loop..."); main_loop = g_main_loop_new (context, FALSE); [self check_initialization_complete]; g_main_loop_run (main_loop); GST_DEBUG ("Exited main loop"); g_main_loop_unref (main_loop); main_loop = NULL;最後,主循環創建並開始運行,在進入主循環之前,我們調用了check_initialization_complete()方法。主循環會一直運行,直到退出為止。這篇教程有點長了,主要是需要講清楚一系列的基礎內容。在這個基礎之上,後面的會比較短一些,並且會只關注新的內容。
5. 結論
這篇教程主要講述了:
使用DispatchQueue如何讓GStreamer的代碼單獨運行在子線程中如何在ObjC的UI代碼和GStreamer的C代碼中傳遞對象
在這篇教程裡面的方法,象check_initialization_complete()和app_function(),init:,play;,pause:,gstreamerInitialized:和setUIMessage:等接口後續會簡單修改一下繼續使用,所以最好熟悉一下。