1 /*
2  * GStreamer
3  * Copyright (C) 2012 Fluendo S.A. <support@fluendo.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef __GST_CORE_AUDIO_H__
23 #define __GST_CORE_AUDIO_H__
24 
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28 
29 #include <gst/gst.h>
30 #include <gst/audio/audio-channels.h>
31 #ifdef HAVE_IOS
32   #include <CoreAudio/CoreAudioTypes.h>
33   #define AudioDeviceID gint
34   #define kAudioDeviceUnknown 0
35 #else
36   #include <CoreAudio/CoreAudio.h>
37   #include <AudioToolbox/AudioToolbox.h>
38   #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
39     #include <CoreServices/CoreServices.h>
40     #define AudioComponentFindNext FindNextComponent
41     #define AudioComponentInstanceNew OpenAComponent
42     #define AudioComponentInstanceDispose CloseComponent
43     #define AudioComponent Component
44     #define AudioComponentDescription ComponentDescription
45   #endif
46 #endif
47 #include <AudioUnit/AudioUnit.h>
48 #include "gstosxaudioelement.h"
49 
50 
51 G_BEGIN_DECLS
52 
53 #define GST_TYPE_CORE_AUDIO \
54   (gst_core_audio_get_type())
55 #define GST_CORE_AUDIO(obj) \
56   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CORE_AUDIO,GstCoreAudio))
57 #define GST_CORE_AUDIO_CLASS(klass) \
58   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CORE_AUDIO,GstCoreAudioClass))
59 #define GST_CORE_AUDIO_GET_CLASS(obj) \
60   (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_CORE_AUDIO,GstCoreAudioClass))
61 #define GST_IS_CORE_AUDIO(obj) \
62   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CORE_AUDIO))
63 #define GST_IS_CORE_AUDIO_CLASS(klass) \
64   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CORE_AUDIO))
65 
66 /* TODO: Consider raising to 64 */
67 #define GST_OSX_AUDIO_MAX_CHANNEL (9)
68 
69 #define CORE_AUDIO_FORMAT_IS_SPDIF(f) ((f).mFormat.mFormatID == 'IAC3' || (f).mFormat.mFormatID == 'iac3' || (f).mFormat.mFormatID == kAudioFormat60958AC3 || (f).mFormat.mFormatID == kAudioFormatAC3)
70 
71 #define CORE_AUDIO_FORMAT "FormatID: %" GST_FOURCC_FORMAT " rate: %f flags: 0x%x BytesPerPacket: %u FramesPerPacket: %u BytesPerFrame: %u ChannelsPerFrame: %u BitsPerChannel: %u"
72 #define CORE_AUDIO_FORMAT_ARGS(f) GST_FOURCC_ARGS((unsigned int)(f).mFormatID),(f).mSampleRate,(unsigned int)(f).mFormatFlags,(unsigned int)(f).mBytesPerPacket,(unsigned int)(f).mFramesPerPacket,(unsigned int)(f).mBytesPerFrame,(unsigned int)(f).mChannelsPerFrame,(unsigned int)(f).mBitsPerChannel
73 
74 #define CORE_AUDIO_INNER_SCOPE(core_audio) ((core_audio)->is_src ? kAudioUnitScope_Output : kAudioUnitScope_Input)
75 #define CORE_AUDIO_OUTER_SCOPE(core_audio) ((core_audio)->is_src ? kAudioUnitScope_Input : kAudioUnitScope_Output)
76 #define CORE_AUDIO_ELEMENT(core_audio) ((core_audio)->is_src ? 1 : 0)
77 
78 typedef struct _GstCoreAudio GstCoreAudio;
79 typedef struct _GstCoreAudioClass GstCoreAudioClass;
80 
81 struct _GstCoreAudio
82 {
83   GObject object;
84 
85   GstObject *osxbuf;
86   GstOsxAudioElementInterface *element;
87 
88   gboolean is_src;
89   gboolean is_passthrough;
90   AudioDeviceID device_id;
91   gboolean cached_caps_valid; /* thread-safe flag */
92   GstCaps *cached_caps;
93   gint stream_idx;
94   gboolean io_proc_active;
95   gboolean io_proc_needs_deactivation;
96 
97   /* For LPCM in/out */
98   AudioUnit audiounit;
99   UInt32 recBufferSize; /* AudioUnitRender clobbers mDataByteSize */
100   AudioBufferList *recBufferList;
101 
102 #ifndef HAVE_IOS
103   /* For SPDIF out */
104   pid_t hog_pid;
105   gboolean disabled_mixing;
106   AudioStreamID stream_id;
107   gboolean revert_format;
108   AudioStreamBasicDescription original_format, stream_format;
109   AudioDeviceIOProcID procID;
110 #endif
111 };
112 
113 struct _GstCoreAudioClass
114 {
115   GObjectClass parent_class;
116 };
117 
118 GType gst_core_audio_get_type                                (void);
119 
120 void gst_core_audio_init_debug (void);
121 
122 GstCoreAudio * gst_core_audio_new                            (GstObject *osxbuf);
123 
124 gboolean gst_core_audio_open                                 (GstCoreAudio *core_audio);
125 
126 gboolean gst_core_audio_close                                (GstCoreAudio *core_audio);
127 
128 gboolean gst_core_audio_initialize                           (GstCoreAudio *core_audio,
129                                                               AudioStreamBasicDescription format,
130                                                               GstCaps *caps,
131                                                               gboolean is_passthrough);
132 
133 void gst_core_audio_uninitialize                             (GstCoreAudio *core_audio);
134 
135 gboolean gst_core_audio_start_processing                     (GstCoreAudio *core_audio);
136 
137 gboolean gst_core_audio_pause_processing                     (GstCoreAudio *core_audio);
138 
139 gboolean gst_core_audio_stop_processing                      (GstCoreAudio *core_audio);
140 
141 gboolean gst_core_audio_get_samples_and_latency              (GstCoreAudio * core_audio,
142                                                               gdouble rate,
143                                                               guint *samples,
144                                                               gdouble *latency);
145 
146 void  gst_core_audio_set_volume                              (GstCoreAudio *core_audio,
147                                                               gfloat volume);
148 
149 gboolean gst_core_audio_audio_device_is_spdif_avail          (AudioDeviceID device_id);
150 
151 
152 gboolean gst_core_audio_select_device                        (GstCoreAudio * core_audio);
153 
154 GstCaps *
155 gst_core_audio_probe_caps (GstCoreAudio * core_audio, GstCaps * in_caps);
156 
157 AudioChannelLayout *
158 gst_core_audio_get_channel_layout (GstCoreAudio * core_audio, gboolean outer);
159 
160 gboolean gst_core_audio_parse_channel_layout (AudioChannelLayout * layout,
161     guint * channels, guint64 * channel_mask, GstAudioChannelPosition * pos);
162 GstCaps * gst_core_audio_asbd_to_caps (AudioStreamBasicDescription * asbd,
163     AudioChannelLayout * layout);
164 
165 G_END_DECLS
166 
167 #endif /* __GST_CORE_AUDIO_H__ */
168