-
Notifications
You must be signed in to change notification settings - Fork 18
/
FLVPacket.h
171 lines (147 loc) · 5.28 KB
/
FLVPacket.h
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
//
// FLVPacket.h
// PresentRTMPStreamModule
//
// Created by Justin Makaila on 7/20/13.
// Copyright (c) 2013 Present, Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <librtmp/amf.h>
// H264 Constant
#define MAX_H264_FRAMESIZE 131072
//Offsets
#define FLV_AUDIO_SAMPLESIZE_OFFSET 1
#define FLV_AUDIO_SAMPLERATE_OFFSET 2
#define FLV_AUDIO_CODECID_OFFSET 4
#define FLV_VIDEO_FRAMETYPE_OFFSET 4
// Bitmasks to isolate specific values
#define FLV_AUDIO_CHANNEL_MASK 0x01
#define FLV_AUDIO_SAMPLESIZE_MASK 0x02
#define FLV_AUDIO_SAMPLERATE_MASK 0x0c
#define FLV_AUDIO_CODEC_ID_MASK 0xf0
#define FLV_VIDEO_CODECID_MASK 0x0f
#define FLV_VIDEO_FRAMETYPE_MASK 0xf0
#define AMF_END_OF_OBJECT 0x09
enum {
FLV_HEADER_FLAG_HASVIDEO = 1,
FLV_HEADER_FLAG_HASAUDIO = 4,
};
enum {
FLV_TAG_TYPE_AUDIO = 0x08,
FLV_TAG_TYPE_VIDEO = 0x09,
FLV_TAG_TYPE_META = 0x12,
};
enum {
FLV_MONO = 0,
FLV_STEREO = 1,
};
enum {
FLV_SAMPLESIZE_8BIT = 0,
FLV_SAMPLESIZE_16BIT = 1 << FLV_AUDIO_SAMPLESIZE_OFFSET
};
enum {
FLV_SAMPLERATE_SPECIAL = 0,
FLV_SAMPLERATE_11025HZ = 1 << FLV_AUDIO_SAMPLERATE_OFFSET,
FLV_SAMPLERATE_22050HZ = 2 << FLV_AUDIO_SAMPLERATE_OFFSET,
FLV_SAMPLERATE_44100HZ = 3 << FLV_AUDIO_SAMPLERATE_OFFSET,
};
enum {
FLV_CODECID_PCM = 0,
FLV_CODECID_ADPCM = 1 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_MP3 = 2 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_PCM_LE = 3 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_NELLYMOSER_16KHZ_MONO = 4 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_NELLYMOSER_8KHZ_MONO = 5 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_NELLYMOSER = 6 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_PCM_ALAW = 7 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_PCM_MULAW = 8 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_AAC = 10<< FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_SPEEX = 11<< FLV_AUDIO_CODECID_OFFSET,
};
enum {
FLV_CODECID_H263 = 2,
FLV_CODECID_SCREEN = 3,
FLV_CODECID_VP6 = 4,
FLV_CODECID_VP6A = 5,
FLV_CODECID_SCREEN2 = 6,
FLV_CODECID_H264 = 7,
};
enum {
FLV_FRAME_KEY = 1 << FLV_VIDEO_FRAMETYPE_OFFSET,
FLV_FRAME_INTER = 2 << FLV_VIDEO_FRAMETYPE_OFFSET,
FLV_FRAME_DISP_INTER = 3 << FLV_VIDEO_FRAMETYPE_OFFSET,
};
typedef struct FLVContext {
int64_t duration_offset;
int64_t filesize_offset;
int64_t ts;
int64_t ts_last;
int64_t duration;
int64_t delay;
} FLVContext;
@interface FLVPacket : NSObject
/*
* Type: Instance Method
* Usage: [[FLVPacket alloc]init]
* Desc: Initializes and returns an instance of
* FLVPacket
* ------------------------------------------------
*/
- (id)init;
/*
* Type: Instance Method
* Usage: [FLVPacket writeStreamHeader:buffer]
* Desc: Writes all stream metadata to the supplied buffer
* ----------------------------------------------------------
* Parameters:
* uint8_t* buffer: the buffer to write the metadata to
*
* Return Value:
* int bufferSize: the size of the metadata written to
* the buffer
*/
- (int)writeStreamHeader:(uint8_t**)buffer ofSize:(int)bufferSize withAvcC:(NSData*)avcC ofSize:(int)avcCSize;
/*
* Type: Instance Method
* Usage: [FLVPacket writeAvcCHeader:avcC toBuffer:buffer]
* Desc: Writes the SPS and PPS data to the supplied buffer
* -----------------------------------------------------------
* Parameters:
* NSData* avcC: the avcC data to write to the buffer
* uint8_t* buffer: the buffer to write the metadata to
*
* Return Value:
* int bufferSize: the size of the metadata written to
* the buffer
*/
- (int)writeAvcCHeader:(NSData*)avcC toBuffer:(uint8_t**)buffer;
/*
* Type: Instance Method
* Usage: [FLVPacket writeNALU:nalu toPacket:packet time:pts keyframe:keyframe]
* Desc: Writes the header and NALU payload to supplied packet
* ------------------------------------------------------------------------------
* Parameters:
* uint8_t* nalu: the NALU data to write to the packet
* uint8_t* buffer: the buffer to write the data to
* double pts: the presentation time stamp
* BOOL isKeyframe: flag indicating whether this frame is a keyframe
*
* Return Value:
* int bufferSize: the size of the packet
*/
- (int)writeNALU:(uint8_t*)nalu ofSize:(int)naluSize toPacket:(uint8_t**)packet time:(double)pts keyframe:(BOOL)isKeyframe;
- (int)writeAAC:(uint8_t*)aacData ofSize:(int)aacSize toPacket:(uint8_t**)packet time:(double)pts;
/*
* Type: Instance Method
* Usage: [FLVPacket writeStreamTrailer:buffer]
* Desc: Writes the stream trailer to supplied buffer,
updating duration and filesize metadata
* ------------------------------------------------------
* Parameters:
* uint8_t* buffer: the buffer to write the data to
*
* Return Value:
* int bufferSize: the size of the packet
*/
- (int)writeStreamTrailer:(uint8_t**)buffer time:(unsigned)pts;
@end