Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 10 : 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, 59 : std::shared_ptr<MediaDemuxer>& demuxer, 60 : int index); 61 : void flushBuffers(); 62 : void setSeekTime(int64_t time); 63 : 64 191 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) 65 : { 66 191 : onSuccessfulSetup_ = cb; 67 191 : } 68 : 69 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 70 : 71 191 : std::string getId() const { return id_; }; 72 : 73 : private: 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