houxiao
2016-12-26 993a7850d6cffb341fefabb68fbb97168c4a461c
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
//
//  FFmpegH264Source.cpp
//  FFmpegRTSPServer
//
//  Created by Mina Saad on 9/22/15.
//  Copyright (c) 2015 Mina Saad. All rights reserved.
//
 
#include "FFmpegH264Source.h"
 
namespace MESAI
{
    FFmpegH264Source * FFmpegH264Source::createNew(UsageEnvironment& env, IEncoder * E_Source) {
        return new FFmpegH264Source(env, E_Source);
    }
 
    FFmpegH264Source::FFmpegH264Source(UsageEnvironment& env, IEncoder * E_Source) : FramedSource(env), Encoding_Source(E_Source)
    {
        m_eventTriggerId = envir().taskScheduler().createEventTrigger(FFmpegH264Source::deliverFrameStub);
        std::function<void()> callback1 = std::bind(&FFmpegH264Source::onFrame,this);
        Encoding_Source -> setCallbackFunctionFrameIsReady(callback1);
    }
 
    FFmpegH264Source::~FFmpegH264Source()
    {
 
    }
 
    void FFmpegH264Source::doStopGettingFrames()
    {
        FramedSource::doStopGettingFrames();
    }
 
    void FFmpegH264Source::onFrame()
    {
        envir().taskScheduler().triggerEvent(m_eventTriggerId, this);
    }
 
    void FFmpegH264Source::doGetNextFrame()
    {
        deliverFrame();
    }
 
    void FFmpegH264Source::deliverFrame()
    {
        if (!isCurrentlyAwaitingData()) return; // we're not ready for the data yet
 
        static uint8_t* newFrameDataStart;
        static unsigned newFrameSize = 0;
 
        /* get the data frame from the Encoding thread.. */
        if (Encoding_Source->GetFrame(&newFrameDataStart, &newFrameSize)){
            if (newFrameDataStart!=NULL) {
                /* This should never happen, but check anyway.. */
                if (newFrameSize > fMaxSize) {
                    fFrameSize = fMaxSize;
                    fNumTruncatedBytes = newFrameSize - fMaxSize;
                } else {
                    fFrameSize = newFrameSize;
                }
 
                gettimeofday(&fPresentationTime, NULL);
                memcpy(fTo, newFrameDataStart, fFrameSize);
                
                //delete newFrameDataStart;
                //newFrameSize = 0;
                
                Encoding_Source->ReleaseFrame();
            }
            else {
                fFrameSize=0;
                fTo=NULL;
                handleClosure(this);
            }
        }else
        {
            fFrameSize = 0;
        }
        
        if(fFrameSize>0)
            FramedSource::afterGetting(this);
 
    }
}