Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com> 5 : * Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com> 6 : * 7 : * This program is free software; you can redistribute it and/or modify 8 : * it under the terms of the GNU General Public License as published by 9 : * the Free Software Foundation; either version 3 of the License, or 10 : * (at your option) any later version. 11 : * 12 : * This program is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : * GNU General Public License for more details. 16 : * 17 : * You should have received a copy of the GNU General Public License 18 : * along with this program; if not, write to the Free Software 19 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 : */ 21 : #pragma once 22 : 23 : #include "audio_format.h" 24 : #include "media/media_buffer.h" 25 : #include "media/media_device.h" 26 : #include "media/media_codec.h" 27 : #include "noncopyable.h" 28 : #include "observer.h" 29 : #include "media/socket_pair.h" 30 : #include "threadloop.h" 31 : 32 : #include <functional> 33 : #include <sstream> 34 : 35 : namespace jami { 36 : 37 : class MediaDecoder; 38 : class MediaIOHandle; 39 : struct MediaStream; 40 : class RingBuffer; 41 : 42 : class AudioReceiveThread : public Observable<std::shared_ptr<MediaFrame>> 43 : { 44 : public: 45 : AudioReceiveThread(const std::string& streamId, 46 : const AudioFormat& format, 47 : const std::string& sdp, 48 : const uint16_t mtu); 49 : ~AudioReceiveThread(); 50 : 51 : MediaStream getInfo() const; 52 : 53 : void addIOContext(SocketPair& socketPair); 54 : void startReceiver(); 55 : void stopReceiver(); 56 : 57 187 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) 58 : { 59 187 : onSuccessfulSetup_ = cb; 60 187 : } 61 : 62 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 63 : 64 : private: 65 : NON_COPYABLE(AudioReceiveThread); 66 : 67 : static constexpr auto SDP_FILENAME = "dummyFilename"; 68 : 69 : static int interruptCb(void* ctx); 70 : static int readFunction(void* opaque, uint8_t* buf, int buf_size); 71 : 72 : /*-----------------------------------------------------------------*/ 73 : /* These variables should be used in thread (i.e. process()) only! */ 74 : /*-----------------------------------------------------------------*/ 75 : const std::string streamId_; 76 : const AudioFormat& format_; 77 : 78 : DeviceParams args_; 79 : 80 : std::istringstream stream_; 81 : mutable std::mutex mutex_; 82 : std::unique_ptr<MediaDecoder> audioDecoder_; 83 : std::unique_ptr<MediaIOHandle> sdpContext_; 84 : std::unique_ptr<MediaIOHandle> demuxContext_; 85 : 86 : std::shared_ptr<RingBuffer> ringbuffer_; 87 : 88 : uint16_t mtu_; 89 : 90 : ThreadLoop loop_; 91 : bool setup(); 92 : void process(); 93 : void cleanup(); 94 : 95 : std::function<void(MediaType, bool)> onSuccessfulSetup_; 96 : std::function<void(const MediaStream& ms)> recorderCallback_; 97 : }; 98 : 99 : } // namespace jami