Repository navigation
Expand file tree
/
Copy pathrtmpwrapper.cpp
More file actions
executable file
·143 lines (117 loc) · 4.12 KB
/
Copy pathrtmpwrapper.cpp
File metadata and controls
executable file
·143 lines (117 loc) · 4.12 KB
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
#include <jni.h>
#include <librtmp/rtmp.h>
#include <unistd.h>
#include <string.h>
#include <thread>
#include <iostream>
#include <rtmpcollection.cpp>
extern "C"
{
RTMPCollection collection;
int sending(RTMP *rtmp, RTMPPacket *packet, char *buffer)
{
RTMPPacket_Reset(packet);
packet->m_hasAbsTimestamp = 0;
packet->m_nChannel = 0x04;
packet->m_nInfoField2 = rtmp->m_stream_id;
packet->m_headerType = RTMP_PACKET_SIZE_LARGE;
uint8_t type;
memcpy(&type, buffer, 1);
if (type == 0x08)
packet->m_nChannel = 0x03;
uint32_t datalength;
memcpy(&datalength, buffer + 1, 3);
datalength = HTON24(datalength);
uint32_t timestamp;
memcpy(×tamp, buffer + 4, 4);
timestamp = HTONTIME(timestamp);
packet->m_packetType = type;
packet->m_nBodySize = datalength;
packet->m_nTimeStamp = timestamp;
memcpy(packet->m_body, buffer + 11, datalength);
free(buffer);
int result = RTMP_SendPacket(rtmp, packet, 0);
return result;
}
void run(int connection)
{
RTMP *rtmp = collection.getRTMPConnection(connection);
if (rtmp == NULL || !RTMP_IsConnected(rtmp))
{
collection.closeRTMPConnection(connection);
return;
}
RTMPPacket *packet = (RTMPPacket *)malloc(sizeof(RTMPPacket));
RTMPPacket_Alloc(packet, 1024 * 512);
int timeoutStep = 0;
while (1)
{
if (collection.isQueueEmpty(connection))
{
timeoutStep++;
if (timeoutStep > 500)
{
RTMP_Log(RTMP_LOGERROR, "Timeouted. Exit Loop.");
collection.closeRTMPConnection(connection);
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
timeoutStep = 0;
char *buffer = collection.dequeBuffer(connection);
if (buffer == NULL)
continue;
if (sending(rtmp, packet, buffer) <= 0)
{
RTMP_Log(RTMP_LOGERROR, "Packet Unsent. Exit Loop.");
collection.closeRTMPConnection(connection);
break;
}
}
RTMPPacket_Free(packet);
}
JNIEXPORT jint JNICALL Java_com_mceasy_jt1078_util_RTMPWrapper_connect(JNIEnv *env, jobject obj, jstring url)
{
const char *urlstr = env->GetStringUTFChars(url, 0);
jint connection = (jint)collection.addRTMPConnection(urlstr);
env->ReleaseStringUTFChars(url, urlstr);
std::thread t(run, connection);
t.detach();
return connection;
}
JNIEXPORT jint JNICALL Java_com_mceasy_jt1078_util_RTMPWrapper_close(JNIEnv *env, jobject obj, jint connection)
{
return (jint)collection.closeRTMPConnection(connection);
}
JNIEXPORT jboolean JNICALL Java_com_mceasy_jt1078_util_RTMPWrapper_isConnected(JNIEnv *env, jobject obj, jint connection)
{
RTMP *rtmp = collection.getRTMPConnection(connection);
if (rtmp == NULL)
return false;
return true;
}
JNIEXPORT jint JNICALL Java_com_mceasy_jt1078_util_RTMPWrapper_send(JNIEnv *env, jobject obj, jint connection, jbyteArray byteArray)
{
RTMP *rtmp = collection.getRTMPConnection(connection);
if (rtmp == NULL)
return -1;
jbyte *byteData = env->GetByteArrayElements(byteArray, NULL);
uint8_t type;
memcpy(&type, byteData, 1);
if (type != 0x09 && type != 0x08)
{
env->ReleaseByteArrayElements(byteArray, byteData, 0);
return 0;
}
uint32_t datalength;
memcpy(&datalength, byteData + 1, 3);
datalength = HTON24(datalength);
int size = 11 + datalength + 4;
char *buffer = (char *)malloc(size);
memcpy(buffer, byteData, size);
env->ReleaseByteArrayElements(byteArray, byteData, 0);
collection.enqueueBuffer(connection, buffer);
return size;
}
}