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 "audio_format.h" 20 : #include "media/media_buffer.h" 21 : #include "media/media_device.h" 22 : #include "media/media_codec.h" 23 : #include "noncopyable.h" 24 : #include "observer.h" 25 : #include "media/socket_pair.h" 26 : #include "threadloop.h" 27 : 28 : #include <functional> 29 : #include <sstream> 30 : 31 : namespace jami { 32 : 33 : class MediaDecoder; 34 : class MediaIOHandle; 35 : struct MediaStream; 36 : class RingBuffer; 37 : 38 : class AudioReceiveThread : public Observable<std::shared_ptr<MediaFrame>> 39 : { 40 : public: 41 : AudioReceiveThread(const std::string& streamId, 42 : const AudioFormat& format, 43 : const std::string& sdp, 44 : const uint16_t mtu); 45 : ~AudioReceiveThread(); 46 : 47 : MediaStream getInfo() const; 48 : 49 : void addIOContext(SocketPair& socketPair); 50 : void startReceiver(); 51 : void stopReceiver(); 52 : 53 191 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) 54 : { 55 191 : onSuccessfulSetup_ = cb; 56 191 : } 57 : 58 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 59 : 60 : private: 61 : NON_COPYABLE(AudioReceiveThread); 62 : 63 : static constexpr auto SDP_FILENAME = "dummyFilename"; 64 : 65 : static int interruptCb(void* ctx); 66 : static int readFunction(void* opaque, uint8_t* buf, int buf_size); 67 : 68 : /*-----------------------------------------------------------------*/ 69 : /* These variables should be used in thread (i.e. process()) only! */ 70 : /*-----------------------------------------------------------------*/ 71 : const std::string streamId_; 72 : const AudioFormat& format_; 73 : 74 : DeviceParams args_; 75 : 76 : std::istringstream stream_; 77 : mutable std::mutex mutex_; 78 : std::unique_ptr<MediaDecoder> audioDecoder_; 79 : std::unique_ptr<MediaIOHandle> sdpContext_; 80 : std::unique_ptr<MediaIOHandle> demuxContext_; 81 : 82 : std::shared_ptr<RingBuffer> ringbuffer_; 83 : 84 : uint16_t mtu_; 85 : 86 : ThreadLoop loop_; 87 : bool setup(); 88 : void process(); 89 : void cleanup(); 90 : 91 : std::function<void(MediaType, bool)> onSuccessfulSetup_; 92 : std::function<void(const MediaStream& ms)> recorderCallback_; 93 : }; 94 : 95 : } // namespace jami