locking fixes needed for newer glib version with more strict locking rules .. no...
[gst-plugin-dreamsource.git] / src / gstdreamsource.h
1 /*
2  * GStreamer dreamsource
3  * Copyright 2014-2015 Andreas Frisch <fraxinas@opendreambox.org>
4  *
5  * This program is licensed under the Creative Commons
6  * Attribution-NonCommercial-ShareAlike 3.0 Unported
7  * License. To view a copy of this license, visit
8  * http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
9  * Creative Commons,559 Nathan Abbott Way,Stanford,California 94305,USA.
10  *
11  * Alternatively, this program may be distributed and executed on
12  * hardware which is licensed by Dream Property GmbH.
13  *
14  * This program is NOT free software. It is open source, you are allowed
15  * to modify it (if you keep the license), but it may not be commercially
16  * distributed other than under the conditions noted above.
17  */
18
19 #ifndef __GST_DREAMSOURCE_H__
20 #define __GST_DREAMSOURCE_H__
21
22 #include <gst/gst.h>
23 #include <gst/base/gstpushsrc.h>
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <sys/poll.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/socket.h>
37
38 #include "gstdreamsource-marshal.h"
39
40 #define CONTROL_RUN            'R'     /* start producing frames */
41 #define CONTROL_PAUSE          'P'     /* pause producing frames */
42 #define CONTROL_STOP           'S'     /* stop the select call */
43 #define CONTROL_SOCKETS(src)   src->control_sock
44 #define WRITE_SOCKET(src)      src->control_sock[1]
45 #define READ_SOCKET(src)       src->control_sock[0]
46
47 #define CLEAR_COMMAND(src)                  \
48 G_STMT_START {                              \
49   char c;                                   \
50   read(READ_SOCKET(src), &c, 1);            \
51 } G_STMT_END
52
53 #define SEND_COMMAND(src, command)          \
54 G_STMT_START {                              \
55   int G_GNUC_UNUSED _res; unsigned char c; c = command;   \
56   _res = write (WRITE_SOCKET(src), &c, 1);  \
57 } G_STMT_END
58
59 #define READ_COMMAND(src, command, res)        \
60 G_STMT_START {                                 \
61   res = read(READ_SOCKET(src), &command, 1);   \
62 } G_STMT_END
63
64 typedef enum
65 {
66         READTHREADSTATE_NONE = 0,
67         READTRREADSTATE_PAUSED,
68         READTRREADSTATE_RUNNING,
69         READTHREADSTATE_STOP
70 } GstDreamSourceReadthreadState;
71
72 G_BEGIN_DECLS
73
74 typedef struct _CompressedBufferDescriptor CompressedBufferDescriptor;
75 typedef struct _EncoderInfo                EncoderInfo;
76
77 #define ENCTIME_TO_GSTTIME(time)           (gst_util_uint64_scale ((time), GST_USECOND, 27LL))
78 #define MPEGTIME_TO_GSTTIME(time)          (gst_util_uint64_scale ((time), GST_MSECOND/10, 9LL))
79
80 /* validity flags */
81 #define CDB_FLAG_ORIGINALPTS_VALID         0x00000001
82 #define CDB_FLAG_PTS_VALID                 0x00000002
83 #define CDB_FLAG_ESCR_VALID                0x00000004
84 #define CDB_FLAG_TICKSPERBIT_VALID         0x00000008
85 #define CDB_FLAG_SHR_VALID                 0x00000010
86 #define CDB_FLAG_STCSNAPSHOT_VALID         0x00000020
87
88 /* indicator flags */
89 #define CDB_FLAG_FRAME_START               0x00010000
90 #define CDB_FLAG_EOS                       0x00020000
91 #define CDB_FLAG_EMPTY_FRAME               0x00040000
92 #define CDB_FLAG_FRAME_END                 0x00080000
93 #define CDB_FLAG_EOC                       0x00100000
94
95 #define CDB_FLAG_METADATA                  0x40000000
96 #define CDB_FLAG_EXTENDED                  0x80000000
97
98 struct _CompressedBufferDescriptor
99 {
100         uint32_t uiFlags;
101
102         /* Timestamp Parameters */
103         uint32_t uiOriginalPTS;      /* 32-bit original PTS value (in 45 Khz or 27Mhz?) */
104         uint64_t uiPTS;              /* 33-bit PTS value (in 90 Khz) */
105         uint64_t uiSTCSnapshot;      /* 42-bit STC snapshot when frame received by the encoder (in 27Mhz) */
106
107         /* Transmission Parameters */
108         uint32_t uiESCR;             /* Expected mux transmission start time for the first bit of the data (in 27 Mhz) */
109
110         uint16_t uiTicksPerBit;
111         int16_t iSHR;
112
113         /* Buffer Parameters */
114         unsigned uiOffset;           /* REQUIRED: offset of frame data from frame buffer base address (in bytes) */
115         size_t uiLength;             /* REQUIRED: 0 if fragment is empty, e.g. for EOS entry (in bytes) */
116         unsigned uiReserved;         /* Unused field */
117 };
118
119 struct _EncoderInfo {
120         int fd;
121
122         /* descriptor space */
123         unsigned char *buffer;
124
125         /* mmapp'ed data buffer */
126         unsigned char *cdb;
127
128         guint         used_range_min;
129         guint         used_range_max;
130 };
131
132 #define ENC_GET_STC      _IOR('v', 141, uint32_t)
133
134 #define GST_TYPE_DREAMSOURCE_CLOCK \
135   (gst_dreamsource_clock_get_type())
136 #define GST_DREAMSOURCE_CLOCK(obj) \
137   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DREAMSOURCE_CLOCK,GstDreamSourceClock))
138 #define GST_DREAMSOURCE_CLOCK_CLASS(klass) \
139   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DREAMSOURCE_CLOCK,GstDreamSourceClockClass))
140 #define GST_IS_DreamSource_CLOCK(obj) \
141   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DREAMSOURCE_CLOCK))
142 #define GST_IS_DreamSource_CLOCK_CLASS(klass) \
143   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DREAMSOURCE_CLOCK))
144 #define GST_DREAMSOURCE_CLOCK_CAST(obj) \
145   ((GstDreamSourceClock*)(obj))
146
147 typedef struct _GstDreamSourceClock GstDreamSourceClock;
148 typedef struct _GstDreamSourceClockClass GstDreamSourceClockClass;
149
150 struct _GstDreamSourceClock
151 {
152         GstSystemClock clock;
153
154         uint32_t prev_stc;
155         uint32_t first_stc;
156         uint64_t stc_offset;
157         int fd;
158 };
159
160 struct _GstDreamSourceClockClass
161 {
162         GstSystemClockClass parent_class;
163 };
164
165 GType gst_dreamsource_clock_get_type (void);
166 GstClock *gst_dreamsource_clock_new (const gchar * name, int fd);
167
168 G_END_DECLS
169
170 #endif /* __GST_DREAMSOURCE_H__ */