forked from netease-im/NERecord-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerecordEventHandlerImpl.cpp
278 lines (228 loc) · 10.3 KB
/
nerecordEventHandlerImpl.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "nerecordEventHandlerImpl.h"
void TestEventHandler::onError(int error_code, const char* msg)
{
printf("*****************[%d]TestEventHandler::onError: %d - %s\n", getpid(), error_code, msg);
}
void TestEventHandler::onWarning(int warn_code, const char* msg)
{
printf("*****************[%d]TestEventHandler::onWarning: %d - %s\n", getpid(), warn_code, msg);
}
void TestEventHandler::onDisconnect(NERtcErrorCode reason)
{
printf("*****************[%d]TestEventHandler::onDisconnect\n", getpid());
}
void TestEventHandler::onConnectInterrupted()
{
printf("*****************[%d]TestEventHandler::onConnectInterrupted\n", getpid());
}
void TestEventHandler::onJoinChannel(channel_id_t cid, uint64_t uid, NERtcErrorCode result, uint64_t elapsed)
{
printf("*****************pid: [%d]TestEventHandler::onJoinChannel: cid:%lu - uid:%lu - result:%d -elapsed:%lu\n", getpid(), cid, uid, result, elapsed);
}
void TestEventHandler::onConnectionStateChanged(nerecord::NERtcConnectionStateType connection_state, nerecord::NERtcErrorCode reason)
{
printf("*****************[%d]TestEventHandler::onConnectionStateChanged: state %u- reason %u\n", getpid(), connection_state, reason);
}
void TestEventHandler::onMixingLayoutChanged(uint32_t regionCount, const NERtcRegion *regions)
{
printf("*****************[%d]TestEventHandler::onMixingLayoutChanged: region count %u- regions: %p\n", getpid(), regionCount, regions);
}
void TestEventHandler::onRejoinChannel(channel_id_t cid, uint64_t uid, NERtcErrorCode result, uint64_t elapsed)
{
printf("*****************[%d]TestEventHandler::onRejoinChannel: %lu - %lu - %d - %lu\n", getpid(), cid, uid, result, elapsed);
}
void TestEventHandler::onLeaveChannel(NERtcErrorCode result)
{
printf("*****************[%d]TestEventHandler::onLeaveChannel: %d\n", getpid(), result);
}
void TestEventHandler::onUserJoined(uint64_t uid, const char * user_name)
{
printf("*****************[%d]TestEventHandler::onUserJoined: %lu\n", getpid(), uid);
if (g_written_file)
{
struct timeval tv;
gettimeofday(&tv, NULL);
char log[256];
sprintf(log, "%s%u-%lu-audio-%ld", g_log_dir, getpid(), uid, tv.tv_sec);
printf("audio file:%s\n", log);
g_audio_fd_map[uid] = open(log, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (g_audio_fd_map[uid] < 0)
{
printf("[%d]create audio file error!!!\n", getpid());
g_video_file_map.erase(uid);
return;
}
}
}
void TestEventHandler::onUserLeft(uint64_t uid, NERtcSessionLeaveReason reason)
{
printf("*****************[%d]TestEventHandler::onUserLeft: %lu - %d\n", getpid(), uid, reason);
if (g_written_file)
{
int ret = close(g_audio_fd_map[uid]);
if (ret < 0)
{
printf("[%d]close file error!!!\n", getpid());
return;
}
g_audio_fd_map.erase(uid);
if (g_video_file_map.end() != g_video_file_map.find(uid))
{
close(g_video_file_map[uid].fd);
g_video_file_map.erase(uid);
}
}
}
void TestEventHandler::onUserAudioStart(uint64_t uid)
{
printf("*****************[%d]TestEventHandler::onUserAudioStart: %lu\n", getpid(), uid);
}
void TestEventHandler::onUserAudioStop(uint64_t uid)
{
printf("*****************[%d]TestEventHandler::onUserAudioStop: %lu\n", getpid(), uid);
}
void TestEventHandler::onUserAudioMute(nerecord::uid_t uid, bool mute)
{
printf("*****************[%d]TestEventHandler::onUserAudioMute: %lu - mute:%d\n", getpid(), uid, mute);
}
void TestEventHandler::audioFrameReceived(nerecord::uid_t uid, const NERtcAudioFrame *frame)
{
if (verbose) printf("*****************[%d]TestEventHandler::audioFrameReceived: %lu [sample_rate: %d, ts: %lu, samples:%d]\n",
getpid(), uid, frame->format.sample_rate, frame->format.timestamp, frame->format.samples_per_channel);
// std::cout << "tid = " << std::this_thread::get_id() << "\t";
//printf("[%s:%s:%d, uid=%lu] timestamp = %lu\n", __FILE__, __func__, __LINE__, uid, frame->format.timestamp);
if (false && g_written_file)
{
int len = frame->format.channels * frame->format.channels * frame->format.samples_per_channel * sizeof(int16_t);
// printf("[%d]audioFrameReceived: %lu - %d - cn %d sam %d\n", getpid(), uid, len, frame->format.channels, frame->format.sample_rate);
int ret = write(g_audio_fd_map[uid], (const void *)frame->data, len);
if (ret < 0)
{
printf("*****************[%d]write data error!!!%d\n", getpid(), errno);
return;
}
}
}
void TestEventHandler::onUserVideoStart(uint64_t uid, NERtcVideoProfileType max_profile)
{
printf("*****************[%d]TestEventHandler::onUserVideoStart: %lu - max_profile = %d\n", getpid(), uid, max_profile);
}
void TestEventHandler::onUserVideoStop(uint64_t uid)
{
printf("*****************[%d]TestEventHandler::onUserVideoStop: %lu\n", getpid(), uid);
if (g_written_file)
{
if (g_video_file_map.end() != g_video_file_map.find(uid))
{
close(g_video_file_map[uid].fd);
g_video_file_map.erase(uid);
}
}
}
void TestEventHandler::onUserVideoMute(nerecord::uid_t uid, bool mute)
{
printf("*****************[%d]TestEventHandler::onUserVideoMute: %lu - mute = %s\n", getpid(), uid, mute? "true " : "false");
}
void TestEventHandler::onRemoteAudioVolumeIndication(const NERtcAudioVolumeInfo *speakers, unsigned int speaker_number, int total_volume)
{
if (verbose) printf("*****************[%d]TestEventHandler::onRemoteAudioVolumeIndication: total_volume = %d\n", getpid(), total_volume);
for (unsigned int i = 0; i < speaker_number; ++i)
{
printf("\t%lu\tvol: %u\n", speakers[i].uid, speakers[i].volume);
}
}
void TestEventHandler::onActiveSpeaker(nerecord::uid_t uid)
{
if (verbose) printf("*****************[%d]TestEventHandler::onActiveSpeaker: uid = %lu\n", getpid(), uid);
}
void TestEventHandler::onFirstAudioDataReceived(nerecord::uid_t uid)
{
printf("*****************[%d]TestEventHandler::onFirstAudioDataReceived: %lu\n", getpid(), uid);
}
void TestEventHandler::onFirstVideoDataReceived(nerecord::uid_t uid)
{
printf("*****************[%d]TestEventHandler::onFirstVideoDataReceived: %lu\n", getpid(), uid);
}
void TestEventHandler::onFirstAudioFrameDecoded(nerecord::uid_t uid)
{
printf("*****************[%d]TestEventHandler::onFirstAudioFrameDecoded: %lu\n", getpid(), uid);
}
void TestEventHandler::onFirstVideoFrameDecoded(nerecord::uid_t uid, uint32_t width, uint32_t height)
{
printf("*****************[%d]TestEventHandler::onFirstVideoFrameDecoded: %lu - %ux%u\n", getpid(), uid, width, height);
}
void TestEventHandler::onRecordStats(const nerecord::NERtcStats *stats)
{
if (verbose) printf("*****************[%d]TestEventHandler::onRtcStats\n", getpid());
if (verbose) printf("RtcStats:\t duration: %dS\n"
" [cpu] app:%d%%,\t idle:%d%%,\t total:%d%%\n"
" [memory] app:%d%%(%dMB),\t total:%d%%\n"
" [TX] %lldKB(rtt:%dms),\t audio:%lldKB,\t video:%lldKB\n"
" [RX] %lldKB,\t audio:%lldKB,\t video:%lldKB\n"
" [BitRate] a_tx:%d a_rx:%dKbps,\t v_tx:%d v_rx:%dKbps\n"
" [TXloss] audio:%d%%(%d),\t video:%d%%(%d)\n"
" [RXloss] audio:%d%%(%d),\t video:%d%%(%d)\n"
" [Jitter] tx:a-%d v-%d,\t rx:a-%d v-%d\n",
stats->total_duration,
stats->cpu_app_usage,
stats->cpu_idle_usage,
stats->cpu_total_usage,
stats->memory_app_usage,
stats->memory_app_kbytes / 1024,
stats->memory_total_usage,
stats->tx_bytes / 1024, stats->up_rtt, stats->tx_audio_bytes / 1024, stats->tx_video_bytes / 1024,
stats->rx_bytes / 1024, stats->rx_audio_bytes / 1024, stats->rx_video_bytes / 1024,
stats->tx_audio_kbitrate, stats->rx_audio_kbitrate, stats->tx_video_kbitrate, stats->rx_video_kbitrate,
stats->tx_audio_packet_loss_rate, stats->tx_audio_packet_loss_sum, stats->tx_video_packet_loss_rate, stats->tx_video_packet_loss_sum,
stats->rx_audio_packet_loss_rate, stats->rx_audio_packet_loss_sum, stats->rx_video_packet_loss_rate, stats->rx_video_packet_loss_sum,
stats->tx_audio_jitter, stats->tx_video_jitter, stats->rx_audio_jitter, stats->rx_video_jitter);
}
void TestEventHandler::onLocalAudioStats(const nerecord::NERtcAudioSendStats *stats)
{
//todo
}
void TestEventHandler::onRemoteAudioStats(const nerecord::NERtcAudioRecvStats *stats, unsigned int user_count)
{
}
void TestEventHandler::onLocalVideoStats(const nerecord::NERtcVideoSendStats *stats)
{
}
void TestEventHandler::onRemoteVideoStats(const nerecord::NERtcVideoRecvStats *stats_vec, unsigned int user_count)
{
}
void TestEventHandler::videoFrameReceived(nerecord::uid_t uid, const NERtcVideoFrame *frame)
{
}
void TestEventHandler::videoSubstreamFrameReceived(nerecord::uid_t uid, const NERtcVideoFrame *frame)
{
if (verbose) printf("*****************[%d]TestEventHandler::videoSubstreamFrameReceived [ts:%lu]\n", getpid(), frame->timestamp);
}
int TestEventHandler::get_video_file_fd(nerecord::uid_t uid, const NERtcVideoFrame *frame)
{
if (g_video_file_map.find(uid) == g_video_file_map.end())
{
char log[512];
struct timeval tv;
gettimeofday(&tv, NULL);
sprintf(log, "%s%u-%lu-video-%ux%u-%ld", g_log_dir, getpid(), uid, frame->width, frame->height, tv.tv_sec);
printf("video file:%s\n", log);
video_file_struct vfd = video_file_struct();
vfd.height = frame->height;
vfd.width = frame->width;
vfd.fd = open(log, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (vfd.fd < 0)
{
int errno_ = vfd.fd;
printf("[%d]create video file error!!!\n", getpid());
g_video_file_map.erase(uid);
return errno_;
}
g_video_file_map[uid] = vfd;
}
if ((g_video_file_map[uid].height != frame->height) || (g_video_file_map[uid].width != frame->width))
{
if (verbose) printf("Frame format is mismatch: expect [%ux%u], frame[%ux%u]\n", g_video_file_map[uid].width, g_video_file_map[uid].height, frame->width, frame->height);
return -1;
}
return g_video_file_map[uid].fd;
}