~audio-recorder/audio-recorder/trunk

« back to all changes in this revision

Viewing changes to src/audio-sources.c

  • Committer: Osmo Antero
  • Date: 2015-02-06 14:13:34 UTC
  • Revision ID: osmoma@gmail.com-20150206141334-ifaeqyan4ygxo49g
Updated README and INSTALL files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *
9
9
 * This library is distributed in the hope that it will be useful,
10
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
12
 * See the GNU Library General Public License 3 for more details.
13
13
 *
14
14
 * You should have received a copy of the GNU Library General Public
401
401
static gchar *audio_sources_get_default_monitor_dev() {
402
402
    // Return default .monitor device (audio card that we can record from).
403
403
 
404
 
    gchar *def_sink = get_default_sink_device();    
 
404
    gchar *def_sink = get_default_sink_device();
405
405
    if (def_sink == NULL) {
406
406
        // Take last audio card. Try to record from it.
407
407
        return audio_sources_get_last_audio_card();
408
 
    }  
 
408
    }
409
409
 
410
410
    // Make source_dev from sink_dev by adding a ".monitor" suffix.
411
411
    gchar *def_source = g_strdup_printf("%s.monitor", def_sink);
416
416
    DeviceItem *item = audio_sources_find_id(def_source);
417
417
    if (item) {
418
418
        return def_source;
419
 
    }  
420
 
    
 
419
    }
 
420
 
421
421
    g_free(def_source);
422
 
    return NULL;   
 
422
    return NULL;
423
423
}
424
424
 
425
425
static gchar *audio_sources_get_first_microphone(gboolean find_webcam) {
682
682
        gtk_list_store_append(GTK_LIST_STORE(model), &iter);
683
683
 
684
684
        // Load icon
685
 
        const gchar *p = item->icon_name; 
 
685
        const gchar *p = item->icon_name;
686
686
 
687
687
        GdkPixbuf *pixbuf = NULL;
688
688
        if (item->type == MEDIA_PLAYER || item->type == COMM_PROGRAM) {
689
689
 
690
690
            pixbuf = load_icon_pixbuf((gchar*)p);
691
 
        
 
691
 
692
692
            // Got icon??
693
693
            if (!GDK_IS_PIXBUF(pixbuf)) {
694
694
                // No. Display default icon.
717
717
        // Pixbuf has a reference count of 2 now, as the list store has added its own
718
718
        if (GDK_IS_PIXBUF(pixbuf)) {
719
719
            g_object_unref(pixbuf);
720
 
        } 
 
720
        }
721
721
 
722
722
        // Next item
723
723
        n = g_list_next(n);
761
761
    }
762
762
}
763
763
 
764
 
gboolean audio_sources_combo_get_values(GtkWidget *combo, gchar **device_name, 
 
764
gboolean audio_sources_combo_get_values(GtkWidget *combo, gchar **device_name,
765
765
                                        gchar **device_id, gint *device_type) {
766
766
    *device_name = NULL;
767
767
    *device_id = NULL;
822
822
 
823
823
static gchar *FIXME_find_sink_file() {
824
824
    // FIXME because this function pokes directly pulseaudio's config file.
825
 
    // We should not do this! Do not even mension you've seen this horrible piece of code ;-) 
 
825
    // We should not do this! Do not even mension you've seen this horrible piece of code ;-)
826
826
    //
827
 
    // The question is: How to find default (or running) sink-device by using GStreamer functions? 
 
827
    // The question is: How to find default (or running) sink-device by using GStreamer functions?
828
828
    // Please tell me if you know.
829
829
    //
830
830
    // $ pactl list | grep -A6 State
831
831
    //
832
832
    // State: RUNNING
833
 
        // Name: alsa_output.pci-0000_00_1b.0.analog-stereo
834
 
        // Description: Built-in Audio Analog Stereo
835
 
 
836
 
    #define PULSEAUDIO_LOCAL_CONFIG ".config/pulse/"
837
 
    
 
833
    // Name: alsa_output.pci-0000_00_1b.0.analog-stereo
 
834
    // Description: Built-in Audio Analog Stereo
 
835
 
 
836
#define PULSEAUDIO_LOCAL_CONFIG ".config/pulse/"
 
837
 
838
838
    // $ cat  ~/.config/pulse/*-default-sink
839
839
 
840
 
    gchar *home = get_home_dir(); 
 
840
    gchar *home = get_home_dir();
841
841
    gchar *path = g_build_path("/", home, PULSEAUDIO_LOCAL_CONFIG, NULL);
842
842
    gchar *ret = NULL;
843
843
 
846
846
    if (error) {
847
847
        g_error_free(error);
848
848
        goto LBL_1;
849
 
    }  
850
 
    
 
849
    }
 
850
 
851
851
    const gchar *fname = g_dir_read_name(dir);
852
852
    while (fname) {
853
 
    
 
853
 
854
854
        if (g_str_has_suffix(fname, "-default-sink")) {
855
855
            ret = g_build_path("/", path, fname, NULL);
856
856
            goto LBL_1;
857
 
        }  
 
857
        }
858
858
 
859
859
        fname = g_dir_read_name(dir);
860
 
    }  
 
860
    }
861
861
 
862
862
LBL_1:
863
863
    g_free(home);
864
864
    g_free(path);
865
865
 
866
866
    if (dir) {
867
 
        g_dir_close(dir); dir = NULL;
 
867
        g_dir_close(dir);
 
868
        dir = NULL;
868
869
    }
869
 
    
 
870
 
870
871
    return ret;
871
872
}
872
873
 
873
874
static gchar *FIXME_get_default_sink_dev() {
874
875
    // FIXME because this function pokes directly pulseaudio's config file.
875
876
    // We should not do this! Do not even mension you've seen this horrible piece of code ;-)
876
 
    // 
 
877
    //
877
878
    // Possible solutions:
878
879
    // 1) Create a pipeline with pulsesink element, make it rolling and read its device id (it may still be NULL !).
879
 
    // 
 
880
    //
880
881
    // 2) Better idea:
881
882
    // Create a pipeline with a LEVEL element for each audio-card (that are .monitor devices). And check if it produces sound (if we find pulse on the pipe).
882
883
    //
883
884
 
884
 
    gchar *fname = FIXME_find_sink_file();    
 
885
    gchar *fname = FIXME_find_sink_file();
885
886
 
886
887
    gchar *sink_dev = NULL;
887
888
    GError *error = NULL;
888
889
    gsize siz = NULL;
889
890
    g_file_get_contents(fname, &sink_dev, &siz, &error);
890
 
    
 
891
 
891
892
    g_free(fname);
892
 
    
 
893
 
893
894
    if (error) {
894
895
        g_error_free(error);
895
 
    }  
 
896
    }
896
897
 
897
898
    str_trim(sink_dev);
898
899
    return sink_dev;
899
 
 }
 
900
}
900
901
 
901
902
#if 0
902
903
 
903
904
audiotestsrc wave=sine freq=512 ! audioconvert ! audioresample ! pulsesink
904
905
 
905
 
gst-launch audiotestsrc ! audioconvert ! pulsesink
 
906
                            gst-launch audiotestsrc ! audioconvert ! pulsesink
906
907
 
907
908
 
908
909
#endif
909
 
 
910
 
 
911
 
 
912
 
#if 0 
913
 
    Ideas:
914
 
    How to find DEFAULT sink-device by using GStreamer functions? 
915
 
 
916
 
    gchar *name = NULL;
917
 
 
918
 
    GError *error = NULL;
919
 
    GstElement *p = gst_parse_launch("fakesrc ! audioconvert ! audioresample ! pulsesink name=plssnk", &error);
920
 
    gst_element_set_state(p, GST_STATE_PLAYING);
921
 
 
922
 
    GstElement *sink = gst_bin_get_by_name(GST_BIN(p), "plssnk");
923
 
    if (sink) {
924
 
 
925
 
        g_object_get(G_OBJECT(sink), "device", &dev_id, "name", &name, NULL); 
926
 
 
927
 
        GValue value = {0, }; 
928
 
        g_value_init(&value, G_TYPE_STRING);
929
 
        g_object_get_property(G_OBJECT(sink), "device", &value);               
930
 
             
931
 
        dev_id = g_value_dup_string(&value);
932
 
 
933
 
        g_value_unset(&value);
934
 
 
935
 
    }    
936
 
    
937
 
    GstElement *e = gst_element_factory_make("pulsesink", "pulsesink");
938
 
    if (GST_IS_ELEMENT(e)) {
939
 
    
940
 
        g_object_get(G_OBJECT(e), "device", &dev_id, NULL); 
941
 
 
942
 
 
943
 
        dev_id = (gchar*)g_object_get(G_OBJECT(e), "device", NULL);
944
 
    
945
 
        GValue value = {0, }; 
946
 
        g_value_init(&value, G_TYPE_STRING);
947
 
        g_object_get_property(G_OBJECT(e), "device", &value);               
948
 
             
949
 
        dev_id = g_value_dup_string(&value);
950
 
 
951
 
        g_value_unset(&value);
952
 
 
953
 
        gst_object_unref(GST_OBJECT(e));
954
 
    }
955
 
 
956
 
    g_print("SINK DEVICE=%s - %s\n", dev_id, name);
957
 
 
958
 
    return dev_id;
 
910
 
 
911
 
 
912
 
 
913
#if 0
 
914
                            Ideas:
 
915
                            How to find DEFAULT sink-device by using GStreamer functions?
 
916
 
 
917
                            gchar *name = NULL;
 
918
 
 
919
GError *error = NULL;
 
920
GstElement *p = gst_parse_launch("fakesrc ! audioconvert ! audioresample ! pulsesink name=plssnk", &error);
 
921
gst_element_set_state(p, GST_STATE_PLAYING);
 
922
 
 
923
GstElement *sink = gst_bin_get_by_name(GST_BIN(p), "plssnk");
 
924
if (sink) {
 
925
 
 
926
    g_object_get(G_OBJECT(sink), "device", &dev_id, "name", &name, NULL);
 
927
 
 
928
    GValue value = {0, };
 
929
    g_value_init(&value, G_TYPE_STRING);
 
930
    g_object_get_property(G_OBJECT(sink), "device", &value);
 
931
 
 
932
    dev_id = g_value_dup_string(&value);
 
933
 
 
934
    g_value_unset(&value);
 
935
 
 
936
}
 
937
 
 
938
GstElement *e = gst_element_factory_make("pulsesink", "pulsesink");
 
939
if (GST_IS_ELEMENT(e)) {
 
940
 
 
941
    g_object_get(G_OBJECT(e), "device", &dev_id, NULL);
 
942
 
 
943
 
 
944
    dev_id = (gchar*)g_object_get(G_OBJECT(e), "device", NULL);
 
945
 
 
946
    GValue value = {0, };
 
947
    g_value_init(&value, G_TYPE_STRING);
 
948
    g_object_get_property(G_OBJECT(e), "device", &value);
 
949
 
 
950
    dev_id = g_value_dup_string(&value);
 
951
 
 
952
    g_value_unset(&value);
 
953
 
 
954
    gst_object_unref(GST_OBJECT(e));
 
955
}
 
956
 
 
957
g_print("SINK DEVICE=%s - %s\n", dev_id, name);
 
958
 
 
959
return dev_id;
959
960
#endif