|
87 | 87 | }; |
88 | 88 | static GParamSpec *obj_props[N_PROPS] = { NULL, }; |
89 | 89 |
|
| 90 | +enum { |
| 91 | + MONITOR_UPDATE, |
| 92 | + MONITOR_SUSPEND, |
| 93 | + LAST_SIGNAL |
| 94 | +}; |
| 95 | + |
| 96 | +static guint signals [LAST_SIGNAL] = { 0, }; |
| 97 | + |
90 | 98 | static void gvc_mixer_stream_finalize (GObject *object); |
91 | 99 |
|
92 | 100 | G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GvcMixerStream, gvc_mixer_stream, G_TYPE_OBJECT) |
@@ -887,6 +895,152 @@ gvc_mixer_stream_is_running (GvcMixerStream *stream) |
887 | 895 | return FALSE; |
888 | 896 | } |
889 | 897 |
|
| 898 | +static void |
| 899 | +on_monitor_suspended_callback (pa_stream *s, |
| 900 | + void *userdata) |
| 901 | +{ |
| 902 | + GvcMixerStream *stream; |
| 903 | + |
| 904 | + stream = userdata; |
| 905 | + |
| 906 | + if (pa_stream_is_suspended (s)) { |
| 907 | + g_debug ("Stream suspended"); |
| 908 | + g_signal_emit (stream, signals[MONITOR_SUSPEND], 0); |
| 909 | + } |
| 910 | +} |
| 911 | + |
| 912 | +static void |
| 913 | +on_monitor_read_callback (pa_stream *s, |
| 914 | + size_t length, |
| 915 | + void *userdata) |
| 916 | +{ |
| 917 | + GvcMixerStream *stream; |
| 918 | + const void *data; |
| 919 | + double v; |
| 920 | + |
| 921 | + stream = userdata; |
| 922 | + |
| 923 | + if (pa_stream_peek (s, &data, &length) < 0) { |
| 924 | + g_warning ("Failed to read data from stream"); |
| 925 | + return; |
| 926 | + } |
| 927 | + |
| 928 | + g_assert (length > 0); |
| 929 | + g_assert (length % sizeof (float) == 0); |
| 930 | + |
| 931 | + v = ((const float *) data)[length / sizeof (float) -1]; |
| 932 | + |
| 933 | + pa_stream_drop (s); |
| 934 | + |
| 935 | + if (v < 0) { |
| 936 | + v = 0; |
| 937 | + } |
| 938 | + if (v > 1) { |
| 939 | + v = 1; |
| 940 | + } |
| 941 | + g_signal_emit (stream, signals[MONITOR_UPDATE], 0, v); |
| 942 | +} |
| 943 | + |
| 944 | +/** |
| 945 | + * gvc_mixer_stream_create_monitor: |
| 946 | + * @stream: |
| 947 | + */ |
| 948 | +void |
| 949 | +gvc_mixer_stream_create_monitor (GvcMixerStream *stream) |
| 950 | +{ |
| 951 | + pa_stream *s; |
| 952 | + char t[16]; |
| 953 | + pa_buffer_attr attr; |
| 954 | + pa_sample_spec ss; |
| 955 | + pa_context *context; |
| 956 | + int res; |
| 957 | + pa_proplist *proplist; |
| 958 | + gboolean has_monitor; |
| 959 | + |
| 960 | + if (stream == NULL) { |
| 961 | + return; |
| 962 | + } |
| 963 | + has_monitor = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (stream), "has-monitor")); |
| 964 | + if (has_monitor != FALSE) { |
| 965 | + return; |
| 966 | + } |
| 967 | + |
| 968 | + g_debug ("Create monitor for %u", |
| 969 | + gvc_mixer_stream_get_index (stream)); |
| 970 | + |
| 971 | + context = gvc_mixer_stream_get_pa_context (stream); |
| 972 | + |
| 973 | + if (pa_context_get_server_protocol_version (context) < 13) { |
| 974 | + return; |
| 975 | + } |
| 976 | + |
| 977 | + ss.channels = 1; |
| 978 | + ss.format = PA_SAMPLE_FLOAT32; |
| 979 | + ss.rate = 25; |
| 980 | + |
| 981 | + memset (&attr, 0, sizeof (attr)); |
| 982 | + attr.fragsize = sizeof (float); |
| 983 | + attr.maxlength = (uint32_t) -1; |
| 984 | + |
| 985 | + snprintf (t, sizeof (t), "%u", gvc_mixer_stream_get_index (stream)); |
| 986 | + |
| 987 | + proplist = pa_proplist_new (); |
| 988 | + pa_proplist_sets (proplist, PA_PROP_APPLICATION_ID, "org.gnome.VolumeControl"); |
| 989 | + s = pa_stream_new_with_proplist (context, "Peak detect", &ss, NULL, proplist); |
| 990 | + pa_proplist_free (proplist); |
| 991 | + if (s == NULL) { |
| 992 | + g_warning ("Failed to create monitoring stream"); |
| 993 | + return; |
| 994 | + } |
| 995 | + |
| 996 | + pa_stream_set_read_callback (s, on_monitor_read_callback, stream); |
| 997 | + pa_stream_set_suspended_callback (s, on_monitor_suspended_callback, stream); |
| 998 | + |
| 999 | + res = pa_stream_connect_record (s, |
| 1000 | + t, |
| 1001 | + &attr, |
| 1002 | + (pa_stream_flags_t) (PA_STREAM_DONT_MOVE |
| 1003 | + |PA_STREAM_PEAK_DETECT |
| 1004 | + |PA_STREAM_ADJUST_LATENCY)); |
| 1005 | + if (res < 0) { |
| 1006 | + g_warning ("Failed to connect monitoring stream"); |
| 1007 | + pa_stream_unref (s); |
| 1008 | + } else { |
| 1009 | + g_object_set_data (G_OBJECT (stream), "has-monitor", GINT_TO_POINTER (TRUE)); |
| 1010 | + g_object_set_data (G_OBJECT (stream), "pa_stream", s); |
| 1011 | + } |
| 1012 | +} |
| 1013 | + |
| 1014 | +/** |
| 1015 | + * gvc_mixer_stream_remove_monitor: |
| 1016 | + * @stream: |
| 1017 | + */ |
| 1018 | +void |
| 1019 | +gvc_mixer_stream_remove_monitor (GvcMixerStream *stream) |
| 1020 | +{ |
| 1021 | + pa_stream *s; |
| 1022 | + pa_context *context; |
| 1023 | + int res; |
| 1024 | + |
| 1025 | + s = g_object_get_data (G_OBJECT (stream), "pa_stream"); |
| 1026 | + if (s == NULL) |
| 1027 | + return; |
| 1028 | + g_assert (stream != NULL); |
| 1029 | + |
| 1030 | + g_debug ("Stopping monitor for %u", pa_stream_get_index (s)); |
| 1031 | + |
| 1032 | + context = gvc_mixer_stream_get_pa_context (stream); |
| 1033 | + |
| 1034 | + if (pa_context_get_server_protocol_version (context) < 13) { |
| 1035 | + return; |
| 1036 | + } |
| 1037 | + |
| 1038 | + res = pa_stream_disconnect (s); |
| 1039 | + if (res == 0) |
| 1040 | + g_object_set_data (G_OBJECT (stream), "has-monitor", GINT_TO_POINTER (FALSE)); |
| 1041 | + g_object_set_data (G_OBJECT (stream), "pa_stream", NULL); |
| 1042 | +} |
| 1043 | + |
890 | 1044 | static void |
891 | 1045 | gvc_mixer_stream_class_init (GvcMixerStreamClass *klass) |
892 | 1046 | { |
@@ -998,6 +1152,21 @@ gvc_mixer_stream_class_init (GvcMixerStreamClass *klass) |
998 | 1152 | G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS); |
999 | 1153 |
|
1000 | 1154 | g_object_class_install_properties (gobject_class, N_PROPS, obj_props); |
| 1155 | + |
| 1156 | + signals [MONITOR_UPDATE] = |
| 1157 | + g_signal_new ("monitor-update", |
| 1158 | + G_TYPE_FROM_CLASS (klass), |
| 1159 | + G_SIGNAL_RUN_LAST, |
| 1160 | + G_STRUCT_OFFSET (GvcMixerStreamClass, monitor_update), |
| 1161 | + NULL, NULL, NULL, |
| 1162 | + G_TYPE_NONE, 1, G_TYPE_DOUBLE); |
| 1163 | + signals [MONITOR_SUSPEND] = |
| 1164 | + g_signal_new ("monitor-suspend", |
| 1165 | + G_TYPE_FROM_CLASS (klass), |
| 1166 | + G_SIGNAL_RUN_LAST, |
| 1167 | + G_STRUCT_OFFSET (GvcMixerStreamClass, monitor_suspend), |
| 1168 | + NULL, NULL, NULL, |
| 1169 | + G_TYPE_NONE, 0, G_TYPE_NONE); |
1001 | 1170 | } |
1002 | 1171 |
|
1003 | 1172 | static void |
|
0 commit comments