xingzilong
2017-08-18 9e5babf9db52e64bdae60137be7696e56241fca6
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "PL_H264FileFramer.h"
#include "MaterialBuffer.h"
#include "logger.h"
#include "MediaHelper.h"
 
#include "my_timer.hpp"
#include <stdlib.h>
#include <fstream>
using namespace std;
 
#define H264_HEAD "01"
 
struct PL_AMCE_Internal
{
    uint8_t buffer[1920*1080*3];//#todo new from config
    size_t buffSize;
    const size_t buffSizeMax;
    
    size_t inputFrameCount;
 
    PL_H264FileFramer_Config config;
 
    bool payOK;
 
    MB_Frame tempFrame; // frame for gain
 
    PL_AMCE_Internal() : 
        buffSize(0), buffSizeMax(sizeof(buffer)), 
        inputFrameCount(0),
        config(), 
        codec(nullptr),
        payOK(false),
        tempFrame()
    {
    }
    
    ~PL_AMCE_Internal()
    {
    }
    
    void reset()
    {
        buffSize = 0;
        
        inputFrameCount = 0;
 
        PL_H264FileFramer_Config _config;
        config = _config;
        
        codec = nullptr;//#todo destory
 
        payOK = false;
 
        MB_Frame _tempFrame;
        tempFrame = _tempFrame;
    }
};
 
PipeLineElem* create_PL_H264FileFramer()
{
    return new PL_H264FileFramer;
}
 
PL_H264FileFramer::PL_H264FileFramer() : internal(new PL_AMCE_Internal)
,m_b_loop_play(false),m_begin(-1),m_current_time(0)
{
}
 
PL_H264FileFramer::~PL_H264FileFramer()
{
    delete (PL_AMCE_Internal*)internal;
    internal= nullptr;
}
 
bool PL_H264FileFramer::init(void* args)
{
    PL_AMCE_Internal* in = (PL_AMCE_Internal*)internal;
    in->reset();
 
    PL_H264FileFramer_Config* config = (PL_H264FileFramer_Config*)args;
 
    int ret = read_file(config->sz_path);
    if(1!=ret)
    {
        LOG_ERROR << "read_file error return length:" <<ret<< LOG_ENDL;
        return false;
    }
    m_b_loop_play = config->b_loop_play;
 
//    AMediaFormat* format = AMediaFormat_new();
//
//    AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, config->ak_mime.c_str());
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, config->ak_height);
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, config->ak_width);
//
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, config->ak_bit_rate);
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_FRAME_RATE, config->ak_frame_rate);
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, config->ak_i_frame_interval);
//    //AMediaFormat_setInt32(format, "profile", 0x00000100);
//
//// see: https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html#COLOR_FormatYUV420Flexible
//#define AMEDIA_COLOR_FormatYUV420Flexible 0x7f420888
//    AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, config->ak_color_format);
//    //AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_STRIDE, config->ak_width * 2);
//
//    //uint8_t sps[] = {0x0,0x0,0x0,0x1, 0x67, 0x42, 0x00, 0x2A, 0x95, 0xA8, 0x1E, 0x00, 0x89, 0xF9, 0x61, 0x00, 0x00, 0x07, 0x08, 0x00, 0x01, 0x5F, 0x90, 0x04};
//    //uint8_t pps[] = {0x0,0x0,0x0,0x1, 0x68, 0xCE, 0x3C, 0x80};
//    //AMediaFormat_setBuffer(format, "csd-0", sps, sizeof(sps)); // sps
//    //AMediaFormat_setBuffer(format, "csd-1", pps, sizeof(pps)); // pps
//
//    // should like:
//    // mime: string(video/avc), durationUs: int64(10000000), width: int32(480), height: int32(360), max-input-size: int32(55067), csd-0: data, csd-1: data}
//    LOG_INFO << "AMediaFormat_toString: " << AMediaFormat_toString(format) << LOG_ENDL;
//
//    in->codec = AMediaCodec_createEncoderByType(config->ak_mime.c_str());
//
//    if (AMediaCodec_configure(in->codec, format, nullptr, nullptr, AMEDIACODEC_CONFIGURE_FLAG_ENCODE) != AMEDIA_OK)
//    {
//        AMediaFormat_delete(format);
//        LOG_ERROR << "AMediaCodec_configure error" << LOG_ENDL;
//        return false;
//    }
//
//    if (AMediaCodec_start(in->codec) != AMEDIA_OK)
//    {
//        AMediaFormat_delete(format);
//        LOG_ERROR << "AMediaCodec_start error" << LOG_ENDL;
//        return false;
//    }
//
//    AMediaFormat_delete(format);
    return true;
}
 
void PL_H264FileFramer::finit()
{
    PL_AMCE_Internal* in = (PL_AMCE_Internal*)internal;
    //todo release codec
    // call AMediaCodec_stop
}
 
bool amce_pay_file_frame_breaker(const PipeMaterial* pm, void* args)
{
    PL_AMCE_Internal* in = (PL_AMCE_Internal*)args;
    MB_Frame* frame = (MB_Frame*)pm->buffer;
 
//    ssize_t bufidx = AMediaCodec_dequeueInputBuffer(in->codec, 2000);
//    LOGP(DEBUG, "input buffer bufidx=%zd, inputFrameCount=%d", bufidx, in->inputFrameCount++);
//
//    if (bufidx >= 0)
//    {
//        size_t bufsize;
//        uint8_t* inputBuff = AMediaCodec_getInputBuffer(in->codec, bufidx, &bufsize);
//        size_t sampleSize = std::min(bufsize, frame->buffSize);
//        memcpy(inputBuff, frame->buffer, sampleSize); // fill buffer
//
//        uint64_t presentationTimeUs = timeval_to_microseconds(frame->pts); //microseconds
//
//        media_status_t ms = AMediaCodec_queueInputBuffer(in->codec, bufidx, 0, sampleSize, presentationTimeUs, 0);
//
//        in->payOK = true;
//        LOGP(DEBUG, "media_status_t=%d", ms);
//    }
//    else
//    {
//        LOG_WARN << "bufidx=" << bufidx << LOG_ENDL;
//        in->payOK = false;
//        return false;
//    }
 
    return false;
}
 
bool PL_H264FileFramer::pay(const PipeMaterial& pm)
{
    PL_AMCE_Internal* in = (PL_AMCE_Internal*)internal;
 
    in->payOK = false;
    if (!in->payOK)
        pm.breake(PipeMaterial::PMT_FRAME, MB_Frame::MBFT_NV12, amce_pay_file_frame_breaker, in);
 
    return in->payOK;
}
 
bool PL_H264FileFramer::gain(PipeMaterial& pm)
{
    PL_AMCE_Internal* in = (PL_AMCE_Internal*)internal;
 
//    if (!in->payOK)
//    {
//        LOG_WARN << "not in->payOK" << LOG_ENDL;
//        return false;
//    }
 
    pm.deleter = nullptr;
    pm.former = this;
 
//    AMediaCodecBufferInfo info;
//    ssize_t outputBuffIdx = AMediaCodec_dequeueOutputBuffer(in->codec, &info, 0);
    //if (outputBuffIdx >= 0)
    {
        string str_buf;
//        LOG_WARN << "hello" << LOG_ENDL;
//        LOGP(WARNING,"hello %s",m_str_context.c_str());
//        LOG_WARN << "world" << LOG_ENDL;
        std::string data({0,0,0,1});
        m_begin = m_str_context.find(data,m_begin+1);
        static int _count = 0;
        char sz_path[256]={0};
        if((m_begin!=string::npos)&&(m_begin!=0)&&(m_begin!=-1))
        {
            my_module_space::Timer _timer;
            long long tmp = _timer.tell_ms()-m_current_time;
            if(tmp<40)
            {
                long long _value = (40-tmp)*1000;
                //usleep(_value);
            }
            m_current_time = _timer.tell_ms();
            str_buf = m_str_context.substr(0, m_begin);
            LOGP(WARNING,"still running...");
//            memset(sz_path,0,256);
//            sprintf(sz_path,"/data/stream_content/%d",_count);
//            write_file(sz_path,str_buf.c_str(),str_buf.length());
//            _count++;
            m_str_context = m_str_context.substr(m_begin, m_str_context.length() - m_begin);
            in->buffSize = std::min(str_buf.length(), in->buffSizeMax);
            memcpy(in->buffer, &str_buf[0], in->buffSize);
 
            in->tempFrame.type = MB_Frame::MBFT_H264_NALU_AUX;
            in->tempFrame.buffer = in->buffer;
            in->tempFrame.buffSize = in->buffSize;
           // microseconds_to_timeval(info.presentationTimeUs, in->tempFrame.pts);
            pm.type = PipeMaterial::PMT_FRAME;
            pm.buffer = &(in->tempFrame);
            pm.buffSize = 0;
            return true;
        }
        else if((m_begin==string::npos)&&m_b_loop_play)
        {
            m_str_context = m_str_old_context;
        }
    }
 
//    ssize_t outputBuffIdx = AMediaCodec_dequeueOutputBuffer(in->codec, &info, 0);
//    if (outputBuffIdx >= 0)
//    {
//        if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
//        {
//            LOGP(WARNING, "output EOS");
//        }
//
//       //AMediaFormat* format = AMediaCodec_getOutputFormat(in->codec);
//       //if (format != NULL)
//       //{
//       //    int32_t width, height, color;
//       //    AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_WIDTH, &width);
//       //    AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_HEIGHT, &height);
//       //    AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, &color);
//       //    AMediaFormat_delete(format);
//       //    format = nullptr;
//       //    LOGP(DEBUG, "output media format, w=%d, h=%d, c=%d", width, height, color);
//       //}
//
//        in->tempFrame.reset();
//
//        size_t outSize = in->buffSizeMax;
//        uint8_t* outputBuff = AMediaCodec_getOutputBuffer(in->codec, outputBuffIdx, &outSize);
//        if (outputBuff != nullptr)
//        {
//            in->buffSize = std::min((size_t) info.size, in->buffSizeMax);
//            memcpy(in->buffer, outputBuff + info.offset, in->buffSize);
//
//            in->tempFrame.type = MB_Frame::MBFT_H264_NALU_AUX;
//            in->tempFrame.buffer = in->buffer;
//            in->tempFrame.buffSize = in->buffSize;
//            in->tempFrame.width = in->config.ak_width;
//            in->tempFrame.height = in->config.ak_height;
//            microseconds_to_timeval(info.presentationTimeUs, in->tempFrame.pts);
//
//            pm.type = PipeMaterial::PMT_FRAME;
//            pm.buffer = &(in->tempFrame);
//            pm.buffSize = 0;
//
//            //static size_t f = 0;
//            //static FILE *pFile = fopen("/data/aa.264", "wb");
//            //fwrite(in->buffer, sizeof(char), in->buffSize, pFile);
//            //if (++f > 400){
//            //    fclose(pFile);
//            //    exit(0);
//            //}
//        }
//
//        AMediaCodec_releaseOutputBuffer(in->codec, outputBuffIdx, false);
//
//        return true;
//    }
//    else if (outputBuffIdx == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED)
//    {
//        LOGP(DEBUG, "output buffers changed");
//    }
//    else if (outputBuffIdx == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED)
//    {
//        auto format = AMediaCodec_getOutputFormat(in->codec);
//        LOGP(INFO, "format changed to: %s", AMediaFormat_toString(format));
//
//        uint8_t* sps = nullptr;
//        size_t spsSize = 0;
//        uint8_t* pps = nullptr;
//        size_t ppsSize = 0;
//
//        AMediaFormat_getBuffer(format, "csd-0", (void**)&sps, &spsSize); // sps
//        AMediaFormat_getBuffer(format, "csd-1", (void**)&pps, &ppsSize); // pps
//
//        if (spsSize != 0)
//        {
//            std::string spsStr = base64_encode(((const char*)sps) + 4, spsSize - 4);//#todo aux
//            std::string ppsStr = base64_encode(((const char*)pps) + 4, ppsSize - 4);
//
//            this->manager->set_param(PLGP_ENC_SPS_B64, spsStr);
//            this->manager->set_param(PLGP_ENC_PPS_B64, ppsStr);
//        }
//
//        AMediaFormat_delete(format);
//    }
//    else if (outputBuffIdx == AMEDIACODEC_INFO_TRY_AGAIN_LATER)
//    {
//        LOGP(DEBUG, "no output buffer right now");
//    }
//    else
//    {
//        LOGP(WARNING, "unexpected info code: %zd", outputBuffIdx);
//    }
    
    return false;
}
int PL_H264FileFramer::write_file(const char* p_path,const char* p_content,int nlength)
{
    std::fstream outfile(p_path, std::ios_base::out | std::ios_base::binary);
    outfile.write(p_content, nlength);
    outfile.close();
}
int PL_H264FileFramer::read_file(const char* p_path)
{
    std::fstream infile(p_path, std::ios_base::in | std::ios_base::binary | ios::ate);
    size_t nSize = infile.tellg();
    infile.seekg(0, ios::beg);
    if (nSize <= 0)
    {
        infile.close();
        return nSize;
    }
    m_str_context.resize(nSize);
    infile.read(&(m_str_context[0]), nSize);
    m_str_old_context = m_str_context;
    infile.close();
 
    return 1;
}
 
int PL_H264FileFramer::analyse_context()
{
    int _count = 0;
    int _begin = -1;
    while((_begin=m_str_context.find(H264_HEAD,_begin+1))!=string::npos)
    {
        _count++;
    }
    return 1;
}