Line data Source code
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 : 31 : namespace jami { 32 : class AudioDeviceGuard; 33 : class AudioFrameResizer; 34 : class MediaDemuxer; 35 : class MediaDecoder; 36 : class MediaRecorder; 37 : struct MediaStream; 38 : class Resampler; 39 : class RingBuffer; 40 : 41 : class AudioInput : public Observable<std::shared_ptr<MediaFrame>> 42 : { 43 : public: 44 : AudioInput(const std::string& id); 45 : AudioInput(const std::string& id, const std::string& resource); 46 : ~AudioInput(); 47 : 48 : std::shared_future<DeviceParams> switchInput(const std::string& resource); 49 5 : void start() { loop_.start(); }; 50 : 51 0 : 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; 56 : void updateStartTime(int64_t start); 57 : void setPaused(bool paused); 58 : void configureFilePlayback(const std::string& path, std::shared_ptr<MediaDemuxer>& demuxer, int index); 59 : void flushBuffers(); 60 : void setSeekTime(int64_t time); 61 : 62 180 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) { onSuccessfulSetup_ = cb; } 63 : 64 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 65 : 66 180 : std::string getId() const { return id_; }; 67 : 68 : private: 69 : void readFromDevice(); 70 : void readFromFile(); 71 : void readFromQueue(); 72 : bool initDevice(const std::string& device); 73 : bool initFile(const std::string& path); 74 : bool createDecoder(); 75 : void frameResized(std::shared_ptr<AudioFrame>&& ptr); 76 : 77 : std::string id_; 78 : std::shared_ptr<RingBuffer> ringBuf_; 79 : bool muteState_ {false}; 80 : uint64_t sent_samples = 0; 81 : mutable std::mutex fmtMutex_ {}; 82 : AudioFormat format_; 83 : int frameSize_; 84 : std::atomic_bool paused_ {true}; 85 : 86 : std::unique_ptr<Resampler> resampler_; 87 : std::unique_ptr<AudioFrameResizer> resizer_; 88 : std::unique_ptr<MediaDecoder> decoder_; 89 : 90 : std::string resource_; 91 : std::mutex resourceMutex_ {}; 92 : DeviceParams devOpts_; 93 : std::promise<DeviceParams> foundDevOpts_; 94 : std::shared_future<DeviceParams> futureDevOpts_; 95 : std::atomic_bool devOptsFound_ {false}; 96 : void foundDevOpts(const DeviceParams& params); 97 : 98 : std::atomic_bool playingDevice_ {false}; 99 : std::atomic_bool decodingFile_ {false}; 100 : std::atomic_bool playingFile_ {false}; 101 : std::unique_ptr<AudioDeviceGuard> deviceGuard_; 102 : 103 : ThreadLoop loop_; 104 : void process(); 105 : 106 : std::chrono::time_point<std::chrono::steady_clock> wakeUp_; 107 : 108 : std::function<void(MediaType, bool)> onSuccessfulSetup_; 109 : std::function<void(const MediaStream& ms)> recorderCallback_; 110 : std::atomic_bool settingMS_ {true}; 111 : }; 112 : 113 : } // namespace jami