Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
pump.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_sndio/pump.h
10 //! @brief Pump.
11 
12 #ifndef ROC_SNDIO_PUMP_H_
13 #define ROC_SNDIO_PUMP_H_
14 
16 #include "roc_audio/sample.h"
17 #include "roc_audio/sample_spec.h"
18 #include "roc_core/atomic.h"
19 #include "roc_core/attributes.h"
20 #include "roc_core/ipool.h"
21 #include "roc_core/noncopyable.h"
22 #include "roc_core/slice.h"
23 #include "roc_core/stddefs.h"
24 #include "roc_packet/units.h"
25 #include "roc_sndio/isink.h"
26 #include "roc_sndio/isource.h"
27 
28 namespace roc {
29 namespace sndio {
30 
31 //! Audio pump.
32 //! @remarks
33 //! Reads frames from source and writes them to sink.
34 class Pump : public core::NonCopyable<> {
35 public:
36  //! Pump mode.
37  enum Mode {
38  // Run until the source return EOF.
39  ModePermanent = 0,
40 
41  // Run until the source return EOF or become inactive first time.
42  ModeOneshot = 1
43  };
44 
45  //! Initialize.
46  Pump(core::IPool& buffer_pool,
47  ISource& source,
48  ISource* backup_source,
49  ISink& sink,
50  core::nanoseconds_t frame_length,
51  const audio::SampleSpec& sample_spec,
52  Mode mode);
53 
54  //! Check if the object was successfulyl constructed.
55  bool is_valid() const;
56 
57  //! Run the pump.
58  //! @remarks
59  //! Run until the stop() is called or, if oneshot mode is enabled,
60  //! the source becomes inactive.
62 
63  //! Stop the pump.
64  //! @remarks
65  //! May be called from any thread.
66  void stop();
67 
68 private:
69  bool transfer_frame_(ISource& current_source);
70 
71  audio::FrameFactory frame_factory_;
72 
73  ISource& main_source_;
74  ISource* backup_source_;
75  ISink& sink_;
76 
77  audio::SampleSpec sample_spec_;
78 
79  core::Slice<audio::sample_t> frame_buffer_;
80 
81  size_t n_bufs_;
82  const bool oneshot_;
83 
84  core::Atomic<int> stop_;
85 };
86 
87 } // namespace sndio
88 } // namespace roc
89 
90 #endif // ROC_SNDIO_PUMP_H_
Atomic.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
Memory pool interface.
Definition: ipool.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Sink interface.
Definition: isink.h:22
Source interface.
Definition: isource.h:23
Audio pump.
Definition: pump.h:34
void stop()
Stop the pump.
ROC_ATTR_NODISCARD bool run()
Run the pump.
bool is_valid() const
Check if the object was successfulyl constructed.
Mode
Pump mode.
Definition: pump.h:37
Pump(core::IPool &buffer_pool, ISource &source, ISource *backup_source, ISink &sink, core::nanoseconds_t frame_length, const audio::SampleSpec &sample_spec, Mode mode)
Initialize.
Frame factory.
Memory pool interface.
Sink interface.
Source interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Audio sample.
Sample specifications.
Slice.
Commonly used types and functions.
Various units used in packets.