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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Filters are connected in a strongly typed manner, with downstream filters
// always reading data from upstream filters. Upstream filters have no clue
// who is actually reading from them, and return the results via callbacks.
//
// DemuxerStream(Video) <- VideoDecoder <- VideoRenderer
// DataSource <- Demuxer <
// DemuxerStream(Audio) <- AudioDecoder <- AudioRenderer
//
// Upstream -------------------------------------------------------> Downstream
// <- Reads flow this way
// Buffer assignments flow this way ->
//
// Every filter maintains a reference to the scheduler, who maintains data
// shared between filters (i.e., reference clock value, playback state). The
// scheduler is also responsible for scheduling filter tasks (i.e., a read on
// a VideoDecoder would result in scheduling a Decode task). Filters can also
// use the scheduler to signal errors and shutdown playback.
#ifndef MEDIA_BASE_FILTERS_H_
#define MEDIA_BASE_FILTERS_H_
#include <limits>
#include <string>
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/ref_counted.h"
#include "base/time.h"
#include "media/base/media_format.h"
namespace media {
class Buffer;
class Decoder;
class DemuxerStream;
class FilterHost;
class VideoFrame;
class WritableBuffer;
// Identifies the type of filter implementation. Used in conjunction with some
// template wizardry to enforce strongly typed operations. More or less a
// knock off of MSVC's __uuidof() operator.
enum FilterType {
FILTER_DATA_SOURCE,
FILTER_DEMUXER,
FILTER_AUDIO_DECODER,
FILTER_VIDEO_DECODER,
FILTER_AUDIO_RENDERER,
FILTER_VIDEO_RENDERER
};
class MediaFilter : public base::RefCountedThreadSafe<MediaFilter> {
public:
MediaFilter() : host_(NULL), message_loop_(NULL) {}
// Sets the protected member |host_|. This is the first method called by
// the FilterHost after a filter is created. The host holds a strong
// reference to the filter. The reference held by the host is guaranteed
// to be released before the host object is destroyed by the pipeline.
virtual void SetFilterHost(FilterHost* host) {
DCHECK(host);
DCHECK(!host_);
host_ = host;
}
// Sets the protected member |message_loop_|, which is used by filters for
// processing asynchronous tasks and maintaining synchronized access to
// internal data members. The message loop should be running and exceed the
// lifetime of the filter.
virtual void SetMessageLoop(MessageLoop* message_loop) {
DCHECK(message_loop);
DCHECK(!message_loop_);
message_loop_ = message_loop;
}
// The pipeline is being stopped either as a result of an error or because
// the client called Stop().
virtual void Stop() = 0;
// The pipeline playback rate has been changed. Filters may implement this
// method if they need to respond to this call.
virtual void SetPlaybackRate(float playback_rate) {}
// The pipeline is seeking to the specified time. Filters may implement
// this method if they need to respond to this call.
virtual void Seek(base::TimeDelta time) {}
protected:
// Only allow scoped_refptr<> to delete filters.
friend class base::RefCountedThreadSafe<MediaFilter>;
virtual ~MediaFilter() {}
// TODO(scherkus): make these private with public/protected accessors.
FilterHost* host_;
MessageLoop* message_loop_;
private:
DISALLOW_COPY_AND_ASSIGN(MediaFilter);
};
class DataSource : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_DATA_SOURCE;
}
static bool IsMediaFormatSupported(const MediaFormat& media_format) {
std::string mime_type;
return (media_format.GetAsString(MediaFormat::kMimeType, &mime_type) &&
mime_type == mime_type::kURL);
}
static const size_t kReadError = static_cast<size_t>(-1);
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(const std::string& url) = 0;
// Returns the MediaFormat for this filter.
virtual const MediaFormat& media_format() = 0;
// Read the given amount of bytes into data, returns the number of bytes read
// if successful, kReadError otherwise.
virtual size_t Read(uint8* data, size_t size) = 0;
// Returns true and the current file position for this file, false if the
// file position could not be retrieved.
virtual bool GetPosition(int64* position_out) = 0;
// Returns true if the file position could be set, false otherwise.
virtual bool SetPosition(int64 position) = 0;
// Returns true and the file size, false if the file size could not be
// retrieved.
virtual bool GetSize(int64* size_out) = 0;
// Returns true if this data source supports random seeking.
virtual bool IsSeekable() = 0;
};
class Demuxer : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_DEMUXER;
}
static bool IsMediaFormatSupported(const MediaFormat& media_format) {
std::string mime_type;
return (media_format.GetAsString(MediaFormat::kMimeType, &mime_type) &&
mime_type == mime_type::kApplicationOctetStream);
}
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(DataSource* data_source) = 0;
// Returns the number of streams available
virtual size_t GetNumberOfStreams() = 0;
// Returns the stream for the given index, NULL otherwise
virtual scoped_refptr<DemuxerStream> GetStream(int stream_id) = 0;
};
class DemuxerStream : public base::RefCountedThreadSafe<DemuxerStream> {
public:
// Returns the MediaFormat for this filter.
virtual const MediaFormat& media_format() = 0;
// Schedules a read. When the |read_callback| is called, the downstream
// filter takes ownership of the buffer by AddRef()'ing the buffer.
//
// TODO(scherkus): switch Read() callback to scoped_refptr<>.
virtual void Read(Callback1<Buffer*>::Type* read_callback) = 0;
// Given a class that supports the |Interface| and a related static method
// interface_id(), which returns a const char*, this method returns true if
// the class returns an interface pointer and assigns the pointer to
// |interface_out|. Otherwise this method returns false.
template <class Interface>
bool QueryInterface(Interface** interface_out) {
void* i = QueryInterface(Interface::interface_id());
*interface_out = reinterpret_cast<Interface*>(i);
return (NULL != i);
};
protected:
// Optional method that is implemented by filters that support extended
// interfaces. The filter should return a pointer to the interface
// associated with the |interface_id| string if they support it, otherwise
// return NULL to indicate the interface is unknown. The derived filter
// should NOT AddRef() the interface. The DemuxerStream::QueryInterface()
// public template function will assign the interface to a scoped_refptr<>.
virtual void* QueryInterface(const char* interface_id) { return NULL; }
friend class base::RefCountedThreadSafe<DemuxerStream>;
virtual ~DemuxerStream() {}
};
class VideoDecoder : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_VIDEO_DECODER;
}
static const char* major_mime_type() {
return mime_type::kMajorTypeVideo;
}
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(DemuxerStream* demuxer_stream) = 0;
// Returns the MediaFormat for this filter.
virtual const MediaFormat& media_format() = 0;
// Schedules a read. Decoder takes ownership of the callback.
//
// TODO(scherkus): switch Read() callback to scoped_refptr<>.
virtual void Read(Callback1<VideoFrame*>::Type* read_callback) = 0;
};
class AudioDecoder : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_AUDIO_DECODER;
}
static const char* major_mime_type() {
return mime_type::kMajorTypeAudio;
}
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(DemuxerStream* demuxer_stream) = 0;
// Returns the MediaFormat for this filter.
virtual const MediaFormat& media_format() = 0;
// Schedules a read. Decoder takes ownership of the callback.
//
// TODO(scherkus): switch Read() callback to scoped_refptr<>.
virtual void Read(Callback1<Buffer*>::Type* read_callback) = 0;
};
class VideoRenderer : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_VIDEO_RENDERER;
}
static const char* major_mime_type() {
return mime_type::kMajorTypeVideo;
}
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(VideoDecoder* decoder) = 0;
};
class AudioRenderer : public MediaFilter {
public:
static const FilterType filter_type() {
return FILTER_AUDIO_RENDERER;
}
static const char* major_mime_type() {
return mime_type::kMajorTypeAudio;
}
// Initializes this filter, returns true if successful, false otherwise.
virtual bool Initialize(AudioDecoder* decoder) = 0;
// Sets the output volume.
virtual void SetVolume(float volume) = 0;
};
} // namespace media
#endif // MEDIA_BASE_FILTERS_H_
|