forward port 2.X changes up to and including rev 6714
[ardour.git] / libs / ardour / internal_return.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <glibmm/thread.h>
20
21 #include "pbd/failed_constructor.h"
22
23 #include "ardour/internal_return.h"
24 #include "ardour/mute_master.h"
25 #include "ardour/session.h"
26
27 using namespace std;
28 using namespace ARDOUR;
29
30 PBD::Signal1<void,nframes_t> InternalReturn::CycleStart;
31
32 InternalReturn::InternalReturn (Session& s)
33         : Return (s, true)
34         , user_count (0)
35 {
36         CycleStart.connect_same_thread (*this, boost::bind (&InternalReturn::cycle_start, this, _1));
37 }
38
39 void
40 InternalReturn::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool)
41 {
42         if (!_active && !_pending_active) {
43                 return;
44         }
45
46         /* no lock here, just atomic fetch */
47
48         if (g_atomic_int_get(&user_count) == 0) {
49                 /* nothing to do - nobody is feeding us anything */
50                 return;
51         }
52
53         bufs.merge_from (buffers, nframes);
54         _active = _pending_active;
55 }
56
57 bool
58 InternalReturn::configure_io (ChanCount in, ChanCount out)
59 {
60         IOProcessor::configure_io (in, out);
61         allocate_buffers (_session.engine().frames_per_cycle());
62         return true;
63 }
64
65 int
66 InternalReturn::set_block_size (nframes_t nframes)
67 {
68         allocate_buffers (nframes);
69         return 0;
70 }
71
72 void
73 InternalReturn::allocate_buffers (nframes_t nframes)
74 {
75         buffers.ensure_buffers (_configured_input, nframes);
76         buffers.set_count (_configured_input);
77 }
78
79 BufferSet*
80 InternalReturn::get_buffers ()
81 {
82         Glib::Mutex::Lock lm (_session.engine().process_lock());
83         /* use of g_atomic here is just for code consistency - its protected by the lock
84            for writing.
85         */
86         g_atomic_int_inc (&user_count);
87         return &buffers;
88 }
89
90 void
91 InternalReturn::release_buffers ()
92 {
93         Glib::Mutex::Lock lm (_session.engine().process_lock());
94         if (user_count) {
95                 /* use of g_atomic here is just for code consistency - its protected by the lock
96                    for writing.
97                 */
98                 (void) g_atomic_int_dec_and_test (&user_count);
99         }
100 }
101
102 void
103 InternalReturn::cycle_start (nframes_t nframes)
104 {
105         /* called from process cycle - no lock necessary */
106         if (user_count) {
107                 /* don't bother with this if nobody is going to feed us anything */
108                 buffers.silence (nframes, 0);
109         }
110 }
111
112 XMLNode&
113 InternalReturn::state (bool full)
114 {
115         XMLNode& node (Return::state (full));
116         /* override type */
117         node.add_property("type", "intreturn");
118         return node;
119 }
120
121 XMLNode&
122 InternalReturn::get_state()
123 {
124         return state (true);
125 }
126
127 int
128 InternalReturn::set_state (const XMLNode& node, int version)
129 {
130         return Return::set_state (node, version);
131 }
132
133 bool
134 InternalReturn::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
135 {
136         out = in;
137         return true;
138 }
139
140 bool
141 InternalReturn::visible () const
142 {
143         return false;
144 }