forked from mzakharo/videoenc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout_writer.cpp
192 lines (172 loc) · 4.52 KB
/
out_writer.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
/*
* Copyright (c) 2016 Rosimildo DaSilva <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "out_writer.h"
#include <string>
#include <fcntl.h>
#include <stdint.h>
StreamWriterThread::StreamWriterThread( int buf_len, int fd )
: m_cond( PTHREAD_COND_INITIALIZER ), m_mutex( PTHREAD_MUTEX_INITIALIZER )
{
m_fd = fd;
m_initLen = buf_len;
m_hasBuf = false;
m_end = false;
threadid = 0;
m_nv12 = false;
for( int i = 0; i < QE_SIZE; i++ )
{
m_qFree.enqueue( FIFO_Element( malloc( m_initLen ), m_initLen ) );
}
m_headerSize = 0;
}
StreamWriterThread::~StreamWriterThread()
{
end();
FIFO_Element myBuf;
//printf( "Dtor - free = %d\n", m_qFree.count() );
while( m_qFree.count() )
{
myBuf = m_qFree.dequeue();
free( myBuf.buf );
}
//printf( "Dtor - ready = %d\n", m_qReady.count() );
while( m_qReady.count() )
{
myBuf = m_qReady.dequeue();
free( myBuf.buf );
}
if( m_fd != -1 )
{
close( m_fd );
m_fd = -1;
}
}
void StreamWriterThread::end()
{
//printf( "end...\n" );
if( !threadid ) return;
m_end = true;
pthread_cond_signal( &m_cond );
pthread_join( threadid, NULL );
threadid = 0;
//printf( "end done...\n" );
}
void StreamWriterThread::setH264Header( unsigned char *buf, int len )
{
memcpy( m_h264Header, buf, len );
m_headerSize = len;
}
void StreamWriterThread::pushHeader()
{
//printf( "Push header: %d\n", m_headerSize );
if( !isNV12() )
{
pushBuffer( m_h264Header, m_headerSize, 0, 0 );
}
}
void StreamWriterThread::begin()
{
m_end = false;
pthread_create( &threadid, NULL, StreamWriterThread::thread_Out, this );
}
void StreamWriterThread::threadLoop()
{
FIFO_Element myBuf;
bool doBuffer = false;
//printf( "thread loop starting\n" );
while ( !m_end )
{
doBuffer = false;
//printf( "loop locking\n" );
pthread_mutex_lock( &m_mutex );
/*
* Copy the buffer locally, since we might block
* in case of a FIFO type of file output!
*/
//printf( "loop waiting\n" );
if( !m_qReady.count() )
{
pthread_cond_wait( &m_cond, &m_mutex );
}
if( m_qReady.count() )
{
myBuf = m_qReady.dequeue();
doBuffer = true;
}
pthread_mutex_unlock( &m_mutex );
//printf( "loop unlock\n" );
if( doBuffer )
{
write( m_fd, myBuf.buf, myBuf.size );
doBuffer = false;
pthread_mutex_lock( &m_mutex );
m_qFree.enqueue( myBuf );
pthread_mutex_unlock( &m_mutex );
}
}
}
void *StreamWriterThread::thread_Out(void *parm)
{
StreamWriterThread *THIS = (StreamWriterThread*)parm;
THIS->threadLoop();
return NULL;
}
void StreamWriterThread::pushBuffer( void *buf0, int len0, void *buf1, int len1 )
{
if( !m_qFree.count() )
{
//printf( "F" );
return;
}
/* Usually worker threads will loop on these operations */
pthread_mutex_lock( &m_mutex );
FIFO_Element myBuf;
if( len0 )
{
myBuf = m_qFree.dequeue();
memcpy( myBuf.buf, buf0, len0 );
myBuf.size = len0;
m_qReady.enqueue( myBuf );
}
if( len1 && m_qFree.count() )
{
myBuf = m_qFree.dequeue();
memcpy( myBuf.buf, buf1, len1 );
myBuf.size = len1;
m_qReady.enqueue( myBuf );
}
pthread_cond_signal( &m_cond );
pthread_mutex_unlock( &m_mutex );
}
bool StreamWriterThread::openStream( const char *fn )
{
m_fd = open( fn, O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
std::string fnStr = fn;
//printf( "Stream: %s\n", fn );
if(fnStr.substr( fnStr.find_last_of(".") + 1) != "h264") {
setNV12();
//printf( "Stream: %s IS nv12\n", fn );
}
return m_fd != -1;
}