Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audio_input.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
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 3 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, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <atomic>
20#include <future>
21#include <mutex>
22#include <chrono>
23
24#include "audio_format.h"
25#include "media/media_device.h"
26#include "media/media_buffer.h"
27#include "observer.h"
28#include "threadloop.h"
29#include "media/media_codec.h"
30
31namespace jami {
32class AudioDeviceGuard;
33class AudioFrameResizer;
34class MediaDemuxer;
35class MediaDecoder;
36class MediaRecorder;
37struct MediaStream;
38class Resampler;
39class RingBuffer;
40
41class AudioInput : public Observable<std::shared_ptr<MediaFrame>>
42{
43public:
44 AudioInput(const std::string& id);
45 AudioInput(const std::string& id, const std::string& resource);
47
48 std::shared_future<DeviceParams> switchInput(const std::string& resource);
49 void start() { loop_.start(); };
50
51 bool isCapturing() const { return loop_.isRunning(); }
52 void setFormat(const AudioFormat& fmt);
53 void setMuted(bool isMuted);
54 MediaStream getInfo() const;
55 MediaStream getInfo(const std::string& name) const;
57 void setPaused(bool paused);
58 void configureFilePlayback(const std::string& path,
59 std::shared_ptr<MediaDemuxer>& demuxer,
60 int index);
61 void flushBuffers();
63
64 void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb)
65 {
66 onSuccessfulSetup_ = cb;
67 }
68
69 void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb);
70
71 std::string getId() const { return id_; };
72
73private:
74 void readFromDevice();
75 void readFromFile();
76 void readFromQueue();
77 bool initDevice(const std::string& device);
78 bool initFile(const std::string& path);
79 bool createDecoder();
80 void frameResized(std::shared_ptr<AudioFrame>&& ptr);
81
82 std::string id_;
83 std::shared_ptr<RingBuffer> ringBuf_;
84 bool muteState_ {false};
85 uint64_t sent_samples = 0;
86 mutable std::mutex fmtMutex_ {};
87 AudioFormat format_;
88 int frameSize_;
89 std::atomic_bool paused_ {true};
90
91 std::unique_ptr<Resampler> resampler_;
92 std::unique_ptr<AudioFrameResizer> resizer_;
93 std::unique_ptr<MediaDecoder> decoder_;
94
95 std::string resource_;
96 std::mutex resourceMutex_ {};
97 DeviceParams devOpts_;
98 std::promise<DeviceParams> foundDevOpts_;
99 std::shared_future<DeviceParams> futureDevOpts_;
100 std::atomic_bool devOptsFound_ {false};
101 void foundDevOpts(const DeviceParams& params);
102
103 std::atomic_bool playingDevice_ {false};
104 std::atomic_bool decodingFile_ {false};
105 std::atomic_bool playingFile_ {false};
106 std::unique_ptr<AudioDeviceGuard> deviceGuard_;
107
108 ThreadLoop loop_;
109 void process();
110
111 std::chrono::time_point<std::chrono::steady_clock> wakeUp_;
112
113 std::function<void(MediaType, bool)> onSuccessfulSetup_;
114 std::function<void(const MediaStream& ms)> recorderCallback_;
115 std::atomic_bool settingMS_ {true};
116};
117
118} // namespace jami
void updateStartTime(int64_t start)
void setRecorderCallback(const std::function< void(const MediaStream &ms)> &cb)
void setMuted(bool isMuted)
void setPaused(bool paused)
void setSuccessfulSetupCb(const std::function< void(MediaType, bool)> &cb)
Definition audio_input.h:64
void setSeekTime(int64_t time)
std::string getId() const
Definition audio_input.h:71
void configureFilePlayback(const std::string &path, std::shared_ptr< MediaDemuxer > &demuxer, int index)
void setFormat(const AudioFormat &fmt)
std::shared_future< DeviceParams > switchInput(const std::string &resource)
MediaStream getInfo() const
bool isCapturing() const
Definition audio_input.h:51
bool isRunning() const noexcept
void emitSignal(Args... args)
Definition ring_signal.h:64
Structure to hold sample rate and channel number associated with audio data.