summaryrefslogtreecommitdiff
path: root/dxr3syncbuffer.h
blob: fe680d44224b6f7503525723afdbdfd599f2a8f1 (plain)
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
/*
 * dxr3syncbuffer.h
 *
 * Copyright (C) 2002-2004 Kai Möller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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 Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef _DXR3SYNCBUFFER_H_
#define _DXR3SYNCBUFFER_H_

#include <stdint.h>

#include "dxr3vdrincludes.h"
#include "dxr3interface.h"
#include "dxr3generaldefines.h"
#include "dxr3nextpts.h"
#include "dxr3log.h"

// ==================================
const uint32_t UNKNOWN_CHANNEL_COUNT = 0xFFFFFFFF;
const uint32_t UNKNOWN_DATA_RATE = 0xFFFFFFFF;
const uint32_t UNKNOWN_ASPECT_RATIO = 0xFFFFFFFF;

// ==================================
class cFixedLengthFrame 
{    
public: 
    cFixedLengthFrame();
    ~cFixedLengthFrame();
   
    void Init(uint32_t lenght);

    void CopyFrame(const uint8_t* pStart, int length, uint32_t pts, eFrameType type);
    uint8_t* GetData(void);
    int GetCount(void);
    uint32_t GetPts(void);
    void SetPts(uint32_t pts);
    void SetChannelCount(uint32_t channelCount) {if (channelCount != UNKNOWN_CHANNEL_COUNT) m_staticAudioChannelCount = m_audioChannelCount = channelCount; else m_audioChannelCount = m_staticAudioChannelCount; };
    void SetDataRate(uint32_t dataRate) {if (m_audioDataRate != UNKNOWN_DATA_RATE) m_staticAudioDataRate = m_audioDataRate = dataRate; else m_audioDataRate = m_staticAudioDataRate;};
    void SetAspectRatio(uint32_t aspectRatio) {m_videoAspectRatio = aspectRatio;};
    uint32_t GetChannelCount(void) {return ((m_audioChannelCount == m_staticAudioChannelCount || !m_staticAudioChannelCount)? m_audioChannelCount : m_staticAudioChannelCount) ;};
    uint32_t GetDataRate(void) {return ((m_audioDataRate == m_staticAudioDataRate || !m_staticAudioDataRate) ? m_audioDataRate : m_staticAudioDataRate);};
    uint32_t GetAspectRatio(void) {return m_videoAspectRatio;};
    eFrameType GetFrameType(void) {return m_type;}

    static void Clear(void) {m_staticAudioDataRate = 0; m_staticAudioChannelCount = 0;};

protected:
    uint8_t* m_pData;
    int m_count;
    int m_length;
    uint32_t m_pts;
    eFrameType m_type;
    
    uint32_t m_audioChannelCount;
    uint32_t m_audioDataRate;
    uint32_t m_videoAspectRatio;

    static uint32_t m_staticAudioChannelCount;
    static uint32_t m_staticAudioDataRate;
    
private:
//    cFixedLengthFrame(); // you are not allowed to use this constructor
    cFixedLengthFrame(cFixedLengthFrame&); // no copy constructor
        
};

// ==================================
class cDxr3SyncBuffer : public cRingBuffer 
{
public:
    enum eSyncBufferException 
	{
        SYNC_BUFFER_OVERRUN
    };    
public:
    cDxr3SyncBuffer(int frameCount, int frameLength, cDxr3Interface& dxr3Device);
    ~cDxr3SyncBuffer();        

    virtual int Available(void);
    cFixedLengthFrame* Push(const uint8_t* pStart, int length, uint32_t pts, eFrameType type = ftUnknown) throw (eSyncBufferException);
    void Pop(void);
    cFixedLengthFrame* Get(void); 
    void Clear(void);
    void Stop(void) { m_bStopped = true;};
    void Start(void);
    void WaitForSysClock(uint32_t pts, uint32_t delta);
    void WaitForNextPut(void);
    void WakeUp(void);
    void WaitForReceiverStopped(void);
    void SetDemuxMode(eDxr3DemuxMode demuxMode) {m_demuxMode = demuxMode;};
    eDxr3DemuxMode GetDemuxMode(void) {return m_demuxMode;};
    bool Poll(int TimeoutMs);
    bool IsPolled(void) { return m_bPollSync;};
    uint32_t GetFillLevel(void) { return Available() * 100 / Size();};

protected:
    void ReceiverStopped(void);
    
    cFixedLengthFrame* m_pBuffer;
    int m_count;
    int m_nextFree;
    int m_next;
    bool m_bWaitPts;
    bool m_bPutBlock;
    bool m_bGetBlock;
    bool m_bStartReceiver;
    bool m_bStopped;
    uint32_t m_waitPts;
    uint32_t m_waitDelta;
    uint32_t m_lastPts;
    eDxr3DemuxMode m_demuxMode;

    cCondVar receiverStopped;
    cMutex receiverStoppedMutex;
    cDxr3Interface& m_dxr3Device;
    bool m_bPollSync;
    
private:
    cDxr3SyncBuffer(); // you are not allowed to use this constructor
    cDxr3SyncBuffer(cDxr3SyncBuffer&); // no constructor
};

#endif /*_DXR3SYNCBUFFER_H_*/

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// indent-tabs-mode: t
// End: