Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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 176 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) { onSuccessfulSetup_ = cb; }
54 :
55 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb);
56 :
57 : private:
58 : NON_COPYABLE(AudioReceiveThread);
59 :
60 : static constexpr auto SDP_FILENAME = "dummyFilename";
61 :
62 : static int interruptCb(void* ctx);
63 : static int readFunction(void* opaque, uint8_t* buf, int buf_size);
64 :
65 : /*-----------------------------------------------------------------*/
66 : /* These variables should be used in thread (i.e. process()) only! */
67 : /*-----------------------------------------------------------------*/
68 : const std::string streamId_;
69 : const AudioFormat& format_;
70 :
71 : DeviceParams args_;
72 :
73 : std::istringstream stream_;
74 : mutable std::mutex mutex_;
75 : std::unique_ptr<MediaDecoder> audioDecoder_;
76 : std::unique_ptr<MediaIOHandle> sdpContext_;
77 : std::unique_ptr<MediaIOHandle> demuxContext_;
78 :
79 : std::shared_ptr<RingBuffer> ringbuffer_;
80 :
81 : uint16_t mtu_;
82 :
83 : ThreadLoop loop_;
84 : bool setup();
85 : void process();
86 : void cleanup();
87 :
88 : std::function<void(MediaType, bool)> onSuccessfulSetup_;
89 : std::function<void(const MediaStream& ms)> recorderCallback_;
90 : };
91 :
92 : } // namespace jami
|