forked from wjhwsh/VideoPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideo.m
254 lines (220 loc) · 6.54 KB
/
Video.m
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
//
// FFVideo.m
// FFmpegPlayTest
//
// Created by Jack on 11/12/12.
// Copyright (c) 2012 Jack. All rights reserved.
//
#import "Video.h"
#import "FFMpegEngine.h"
#pragma mark - Video Extension
@interface Video ()
{
AVIOInterruptCB interuptCB;
}
@end
#pragma mark - Video Implementation
@implementation Video
@synthesize status=_status;
@synthesize videoStreamIndex=_videoStreamIndex;
@synthesize audioStreamIndex=_audioStreamIndex;
#pragma mark Init and dealloc
//---------------------------------------------------------------------
- (id) initWithUrl: (NSURL *)url
interuptCallback: (AVIOInterruptCB) callback
{
self = [self initWithUrl:url];
if (self) {
interuptCB = callback;
[self openVideoWithCallback:callback];
}
return self;
}
//---------------------------------------------------------------------
- (id) initWithUrl: (NSURL*) url
{
self = [super init];
if (self) {
/// Using path for local file
_fileUrl = [url path];
_formatContext = nil;
_videoStreamIndex = -1;
_audioStreamIndex = -1;
_videoStream = nil;
_audioStream = nil;
}
return self;
}
- (void) dealloc
{
if (_formatContext) {
avcodec_close(_videoCodecCtx);
avcodec_close(_audioCodecCtx);
avformat_close_input(&_formatContext);
}
[super dealloc];
}
#pragma mark Utilites methods
//---------------------------------------------------------------------
- (void) close
{
if (_formatContext)
{
if (_videoCodecCtx) {
avcodec_close(_videoCodecCtx);
_videoCodecCtx = NULL;
}
if (_audioCodecCtx) {
avcodec_close(_audioCodecCtx);
_audioCodecCtx = NULL;
}
avformat_close_input(&_formatContext);
}
_formatContext = NULL;
_videoStreamIndex = -1;
_audioStreamIndex = -1;
_videoStream = NULL;
_audioStream = NULL;
}
//---------------------------------------------------------------------
- (BOOL) openVideoStream
{
NSLog(@"Opening video stream");
BOOL ret = YES;
AVCodecContext* codecContext = nil;
AVCodec* codec = nil;
if (_videoStreamIndex < 0 || _videoStreamIndex >= _formatContext->nb_streams)
{
return NO;
}
codecContext = _formatContext->streams[_videoStreamIndex]->codec;
codec = avcodec_find_decoder(codecContext->codec_id);
if (!codec || (avcodec_open2(codecContext, codec, NULL) < 0)) {
NSLog(@"Unsupported codec!");
return NO;
}
_videoCodecCtx = codecContext;
_videoStream = _formatContext->streams[_videoStreamIndex];
//TODO: Need to wired up custom frame buffer allocation and release
return ret;
}
//---------------------------------------------------------------------
- (BOOL) openAudioStream
{
NSLog(@"Opening audio stream");
BOOL ret = YES;
AVCodecContext* codecContext = nil;
AVCodec* codec = nil;
if (_audioStreamIndex < 0 || _audioStreamIndex >= _formatContext->nb_streams)
{
return NO;
}
codecContext = _formatContext->streams[_audioStreamIndex]->codec;
codec = avcodec_find_decoder(codecContext->codec_id);
if (!codec || (avcodec_open2(codecContext, codec, NULL) < 0)) {
NSLog(@"Unsupported codec!");
return NO;
}
_audioCodecCtx = codecContext;
_audioStream = _formatContext->streams[_audioStreamIndex];
//TODO: Need to wired up custom frame buffer allocation and release
return ret;
}
//---------------------------------------------------------------------
- (BOOL) openVideoWithCallback: (AVIOInterruptCB) interuptCallback;
{
NSLog(@"Opening video: %@", _fileUrl);
if (![[FFMpegEngine shareInstance] isInitialized]) {
[[FFMpegEngine shareInstance] initFFmpegEngine];
}
interuptCB = interuptCallback;
BOOL ret = YES;
_formatContext = avformat_alloc_context();
_formatContext->interrupt_callback = interuptCB;
// Init I/O Context
if (avio_open2(&_formatContext->pb,
[_fileUrl UTF8String],
AVIO_FLAG_READ,
&_formatContext->interrupt_callback,
NULL)) {
NSLog(@"Couldnt init IO context.");
ret = NO;
goto doneOpen;
}
// Open video file
if (avformat_open_input(&_formatContext,
[_fileUrl UTF8String],
NULL,
NULL)) {
NSLog(@"Couldnt open input file");
ret = NO;
goto doneOpen;
}
//NSLog(@"File media info:\n----------------");
//av_dump_format(_formatContext, 0, [_fileUrl UTF8String], 0);
if (avformat_find_stream_info(_formatContext, NULL) < 0) {
NSLog(@"Couldn't find stream info in input file");
ret = NO;
goto doneOpen;
}
NSLog(@"File media info:\n----------------");
av_dump_format(_formatContext, 0, [_fileUrl UTF8String], 0);
//Find video stream
for (int i = 0; i < _formatContext->nb_streams; i++) {
if (_formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO
&& _videoStreamIndex < 0)
{
_videoStreamIndex = i;
}
if (_formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
&& _audioStreamIndex < 0)
{
_audioStreamIndex = i;
}
}
if (_videoStreamIndex >= 0) {
if (![self openVideoStream]) {
NSLog(@"Couldn't open video stream");
ret = NO;
goto doneOpen;
}
}
if (_audioStreamIndex >= 0) {
if (![self openAudioStream]) {
NSLog(@"Couldn't open audio stream");
ret = NO;
goto doneOpen;
}
}
doneOpen:
if(!ret){
[self close];
}
return ret;
}
//--------------------------------------------------------------------
- ( AVCodecContext * const) audioCodecContext
{
return _audioCodecCtx;
}
//--------------------------------------------------------------------
- ( AVCodecContext * const) videoCodecContext
{
return _videoCodecCtx;
}
//--------------------------------------------------------------------
- ( AVFormatContext * const) formatContext
{
return _formatContext;
}
//--------------------------------------------------------------------
- ( AVStream * const) audioStream
{
return _audioStream;
}
//--------------------------------------------------------------------
- ( AVStream * const) videoStream
{
return _videoStream;
}
@end