forked from idapgroup/VideoCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTMPSession.h
190 lines (150 loc) · 6.62 KB
/
RTMPSession.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Video Core
Copyright (c) 2014 James G. Hurley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef __videocore__RTMSession__
#define __videocore__RTMSession__
#include <iostream>
#include <videocore/stream/IStreamSession.hpp>
#include <videocore/stream/TCPThroughputAdaptation.h>
#include <UriParser/UriParser.hpp>
#include <functional>
#include <deque>
#include <queue>
#include <map>
#include <chrono>
#include <videocore/system/JobQueue.hpp>
#include <cstdlib>
#include <videocore/rtmp/RTMPTypes.h>
#include <videocore/system/Buffer.hpp>
#include <videocore/system/PreBuffer.hpp>
#include <videocore/transforms/IOutputSession.hpp>
namespace videocore
{
class RTMPSession;
using BufStruct = struct { std::shared_ptr<Buffer> buf; std::chrono::steady_clock::time_point time; };
enum {
kRTMPSessionParameterWidth=0,
kRTMPSessionParameterHeight,
kRTMPSessionParameterFrameDuration,
kRTMPSessionParameterVideoBitrate,
kRTMPSessionParameterAudioFrequency,
kRTMPSessionParameterStereo,
kRTMPSessionParameterAudioBitrate
};
typedef MetaData<'rtmp', int32_t, int32_t, double, int32_t, double, bool, double> RTMPSessionParameters_t;
enum {
kRTMPMetadataTimestamp=0,
kRTMPMetadataMsgLength,
kRTMPMetadataMsgTypeId,
kRTMPMetadataMsgStreamId,
kRTMPMetadataIsKeyframe
};
typedef MetaData<'rtmp', int32_t, int32_t, uint8_t, int32_t, bool> RTMPMetadata_t;
using RTMPSessionStateCallback = std::function<void(RTMPSession& session, ClientState_t state)>;
class RTMPSession : public IOutputSession
{
public:
RTMPSession(std::string uri, int maxSendBufferSize, RTMPSessionStateCallback callback);
~RTMPSession();
void connectServer();
public:
// Requires RTMPMetadata_t
void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata);
void setSessionParameters(IMetadata& parameters);
void setBandwidthCallback(BandwidthCallback callback);
void setMaxSendBufferSize(size_t size) { m_maxSendBufferSize = size; printf("MAX SEND BUFFER %lu\n", size); };
void disconnectServer();
private:
// Deprecate sendPacket
void sendPacket(uint8_t* data, size_t size, RTMPChunk_0 metadata);
void streamStatusChanged(StreamStatus_T status);
void write(uint8_t* data, size_t size, std::chrono::steady_clock::time_point packetTime = std::chrono::steady_clock::now(), bool isKeyframe = false, int msgType = 0);
void dataReceived();
void setClientState(ClientState_t state);
void handshake();
void handshake0();
void handshake1();
void handshake2();
void sendReleaseStream();
void sendConnectPacket();
void sendFCPublish();
void sendCreateStream();
void sendPublish();
void sendHeaderPacket();
void sendSetChunkSize(int32_t chunkSize);
void sendPong();
void sendDeleteStream();
void sendSetBufferTime(int milliseconds);
void increaseBuffer(int64_t size);
int reassembleBuffer(uint8_t *p, int msgSize, int packageSize);
int tryReadOneMessage(uint8_t *msg, int msgsize, int from_offset);
bool parseCurrentData();
void handleInvoke(uint8_t* p);
bool handleMessage(uint8_t* p, uint8_t msgTypeId);
std::string parseStatusCode(uint8_t *p);
int32_t amfPrimitiveObjectSize(uint8_t* p);
int32_t trackCommand(const std::string& cmd);
private:
JobQueue m_networkQueue;
JobQueue m_jobQueue;
std::chrono::steady_clock::time_point m_sentKeyframe;
#ifdef __APPLE__
dispatch_semaphore_t m_networkWaitSemaphore;
#else
std::condition_variable m_networkCond;
std::mutex m_networkMutex;
#endif
RingBuffer m_streamOutRemainder;
Buffer m_s1, m_c1;
TCPThroughputAdaptation m_throughputSession;
uint64_t m_previousTs;
RTMPChunk_0 m_previousChunk;
std::deque<BufStruct> m_streamOutQueue;
std::map<int, uint64_t> m_previousChunkData;
// std::unique_ptr<RingBuffer> m_streamInBuffer;
std::unique_ptr<PreallocBuffer> m_streamInBuffer;
std::unique_ptr<IStreamSession> m_streamSession;
std::vector<uint8_t> m_outBuffer;
http::url m_uri;
RTMPSessionStateCallback m_callback;
BandwidthCallback m_bandwidthCallback;
std::string m_playPath;
std::string m_app;
std::map<int32_t, std::string> m_trackedCommands;
size_t m_outChunkSize;
size_t m_inChunkSize;
size_t m_maxSendBufferSize;
int64_t m_bufferSize;
int64_t m_dropped;
int32_t m_streamId;
// int32_t m_createStreamInvoke;
int32_t m_numberOfInvokes;
int32_t m_frameWidth;
int32_t m_frameHeight;
int32_t m_bitrate;
double m_frameDuration;
double m_audioRate;
double m_audioSampleRate;
bool m_audioStereo;
ClientState_t m_state;
bool m_clearing;
bool m_ending;
};
}
#endif /* defined(__videocore__RTMSession__) */