-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRTMPPushH264.cpp
More file actions
123 lines (118 loc) · 3.05 KB
/
RTMPPushH264.cpp
File metadata and controls
123 lines (118 loc) · 3.05 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
/*
* RTMPPushH264.cpp
*
* Created on: Jan 12, 2017
* Author: tla001
*/
#include "RTMPPushH264.h"
#include "librtmp/log.h"
#include <time.h>
#include "rtmplive.h"
#include <QDebug>
extern RtmpLive *w;
PushStream::PushStream(int widths,int heigths,enum AVPixelFormat format){
// this->rtmpUrl=url;
this->width=widths;
this->height=heigths;
this->src_pix_fmt=format;
this->baseFrameSzie=width*height;
this->buffer=new char[baseFrameSzie*3];
// this->fps=25;
this->rate=200;
this->outSize=2048;
tick=0;
tick_gap=1000/this->fps;
nowTime=0;
lastTime=0;
frameIndex=0;
stopFlag=false;
runFlag=false;
}
PushStream::~PushStream(){
delete []buffer;
}
void PushStream::init(string url,int fps){
this->rtmpUrl=url;
this->fps=fps;
RTMP_CreatePublish(const_cast<char*>(rtmpUrl.c_str()),outSize,1,RTMP_LOGINFO);
RTMP_InitVideoParams(width,height,fps,rate,src_pix_fmt,false);
}
void PushStream::worker(){
tick=0;
tick_gap=1000/this->fps;
nowTime=0;
lastTime=0;
frameIndex=0;
while(!stopFlag){
lastTime=RTMP_GetTime();
if(frameIndex!=0){
RTMP_SendScreenCapture((char*)buffer,height,tick);
}
switch(src_pix_fmt){
case AV_PIX_FMT_YUV420P:
//memcpy(buffer,ct.yuv420Frame,baseFrameSzie*3/2);
buffer=(char *)w->vbuf;
break;
case AV_PIX_FMT_YUV422P:
buffer=(char *)w->vbuf;
break;
case AV_PIX_FMT_RGB24:
buffer=(char *)w->vbuf;
break;
default:
printf("Not supports this format \n");
break;
}
tick +=tick_gap;
nowTime=RTMP_GetTime();
sleepTime=tick_gap-nowTime+lastTime;
printf("tick_gap %d nowTime %d lastTime %d sleep time %d\n",tick_gap,nowTime,lastTime,sleepTime);
if(sleepTime>0)
usleep(sleepTime*1000);
frameIndex++;
}
}
void PushStream::run(){
runFlag=true;
worker();
runFlag=false;
}
void PushStream::doPush(){
stopFlag=false;
this->start();
}
void PushStream::dowork(char *buf){
lastTime=RTMP_GetTime();
if(frameIndex!=0){
RTMP_SendScreenCapture((char*)buffer,height,tick);
printf("send frame index -- %d\n",frameIndex);
}
switch(src_pix_fmt){
case AV_PIX_FMT_YUV420P:
memcpy(buffer,buf,baseFrameSzie*3/2);
break;
case AV_PIX_FMT_YUV422P:
memcpy(buffer,buf,baseFrameSzie*2);
break;
case AV_PIX_FMT_RGB24:
memcpy(buffer,buf,baseFrameSzie*3);
break;
default:
printf("Not supports this format \n");
break;
}
tick +=tick_gap;
nowTime=RTMP_GetTime();
sleepTime=tick_gap-nowTime+lastTime;
printf("tick_gap %d nowTime %d lastTime %d sleep time %d\n",tick_gap,nowTime,lastTime,sleepTime);
if(sleepTime>0){
usleep(sleepTime*1000);
}
frameIndex++;
}
void PushStream::doStop(){
stopFlag=true;
while(runFlag)
msleep(10);
RTMP_DeletePublish();
}