OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // SeekableAudioBuffer to support forward seeking in a buffer for reading a | |
scherkus (not reviewing)
2013/06/19 23:38:06
this should go by the class as opposed to top of f
jrummell
2013/06/20 21:47:01
Done.
| |
6 // media data source. | |
7 // | |
8 // In order to support forward seeking, this class buffers data in the forward | |
9 // direction only. The current read position can be forwarded to anywhere in the | |
10 // buffered data. | |
11 // | |
12 // In the case of appending data to the buffer, there is an advisory limit of | |
13 // how many frames can be kept, regulated by |forward_capacity|. The append | |
14 // operation (by calling Append()) that caused frames to exceed | |
15 // |forward_capacity| will have a return value that advises a halt of append | |
16 // operation. Further append operations are allowed but are not advised. Since | |
17 // this class is used as a backend buffer for caching media files downloaded | |
18 // from network we cannot afford losing data, we can only advise a halt of | |
19 // further writing to this buffer. | |
20 // | |
21 // This class is not inherently thread-safe. Concurrent access must be | |
22 // externally serialized. | |
23 | |
24 #ifndef MEDIA_BASE_SEEKABLE_AUDIO_BUFFER_H_ | |
25 #define MEDIA_BASE_SEEKABLE_AUDIO_BUFFER_H_ | |
26 | |
27 #include <list> | |
28 | |
29 #include "base/basictypes.h" | |
30 #include "base/memory/scoped_ptr.h" | |
31 #include "media/base/audio_buffer.h" | |
scherkus (not reviewing)
2013/06/19 23:38:06
can we fwd decl AudioBuffer?
jrummell
2013/06/20 21:47:01
scoped_ptr (for list/deque) needs the class define
| |
32 #include "media/base/media_export.h" | |
33 | |
34 namespace media { | |
35 | |
36 class AudioBus; | |
37 | |
38 class MEDIA_EXPORT SeekableAudioBuffer { | |
scherkus (not reviewing)
2013/06/19 23:38:06
I've never particularly liked the "seekable" name,
jrummell
2013/06/20 21:47:01
Done.
| |
39 public: | |
40 // Constructs an instance with |forward_capacity|. The value is in frames. | |
41 SeekableAudioBuffer(int forward_capacity); | |
scherkus (not reviewing)
2013/06/19 23:38:06
one thing I've always wanted to consider is whethe
DaleCurtis
2013/06/20 00:49:49
+1 if we can. AudioRendererAlgorithm, AudioRendere
jrummell
2013/06/20 21:47:01
Done.
| |
42 | |
43 ~SeekableAudioBuffer(); | |
44 | |
45 // Clears the buffer queue. | |
46 void Clear(); | |
47 | |
48 // Appends |buffer_in| to this buffer. Returns false if forward_bytes() is | |
49 // greater than or equals to forward_capacity(), true otherwise. The data is | |
50 // added to the buffer in any case. | |
51 bool Append(const scoped_refptr<AudioBuffer>& buffer_in); | |
52 | |
53 // Reads a maximum of |frames| frames into |dest| from the current position. | |
54 // Returns the number of frames read. The current position will advance by the | |
55 // amount of frames read. | |
56 int ReadFrames(int frames, AudioBus* dest); | |
57 | |
58 // Copies up to |frames| frames from current position to |dest|. Returns | |
59 // number of frames copied. Doesn't advance current position. Optionally | |
60 // starts at a |forward_offset| from current position. | |
61 int PeekFrames(int frames, AudioBus* dest) { | |
62 return PeekFrames(frames, 0, dest); | |
63 } | |
64 int PeekFrames(int frames, int forward_offset, AudioBus* dest); | |
65 | |
66 // Moves the current position forward by |frames| frames. If |frames| exceeds | |
67 // frames available, the seek operation will fail and return value will be | |
68 // false. If the seek operation fails, the current position will not be | |
69 // updated. | |
70 bool SeekFrames(int frames); | |
71 | |
72 // Returns the suggested maximum number of frames that should be kept in the | |
73 // forward direction. | |
74 int forward_capacity() const { return forward_capacity_; } | |
scherkus (not reviewing)
2013/06/19 23:38:06
should we drop the forward prefix considering ther
jrummell
2013/06/20 21:47:01
Removed completely.
| |
75 | |
76 // Sets the forward_capacity to |new_forward_capacity| frames. | |
77 void set_forward_capacity(int new_forward_capacity) { | |
78 forward_capacity_ = new_forward_capacity; | |
79 } | |
80 | |
81 // Returns the number of frames buffered beyond the current position. | |
82 int forward_frames() const { return forward_frames_; } | |
83 | |
84 // Returns the current timestamp, taking into account current offset. The | |
85 // value calculated based on the timestamp of the current buffer. If timestamp | |
86 // for the current buffer is set to 0, then returns value that corresponds to | |
87 // the last position in a buffer that had timestamp set. kNoTimestamp() is | |
88 // returned if no buffers we read from had timestamp set. | |
89 base::TimeDelta current_time() const { return current_time_; } | |
90 | |
91 private: | |
92 // Definition of the buffer queue. | |
93 typedef std::list<scoped_refptr<AudioBuffer> > BufferQueue; | |
scherkus (not reviewing)
2013/06/19 23:38:06
this (and the regular SeekableBuffer!) should be a
jrummell
2013/06/20 21:47:01
Done.
| |
94 | |
95 // An internal method shared by ReadFrames() and SeekFrames() that actually | |
96 // does reading. It reads a maximum of |frames| frames into |dest|. Returns | |
97 // the number of frames read. The current position will be moved forward by | |
98 // the number of frames read if |advance_position| is set. If |dest| is NULL, | |
99 // only the current position will advance but no data will be copied. | |
100 // |forward_offset| can be used to skip frames before reading. | |
101 int InternalRead(int frames, | |
102 bool advance_position, | |
103 int forward_offset, | |
104 AudioBus* dest); | |
105 | |
106 // Updates |current_time_| with the time that corresponds to the specified | |
107 // position in the buffer. | |
108 void UpdateCurrentTime(BufferQueue::iterator buffer, int offset); | |
109 | |
110 BufferQueue::iterator current_buffer_; | |
111 BufferQueue buffers_; | |
112 int current_buffer_offset_; | |
113 | |
114 int forward_capacity_; | |
115 int forward_frames_; | |
116 | |
117 // Keeps track of the most recent time we've seen in case the |buffers_| is | |
118 // empty when our owner asks what time it is. | |
119 base::TimeDelta current_time_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(SeekableAudioBuffer); | |
scherkus (not reviewing)
2013/06/19 23:38:06
IMPLICIT as we don't define the default ctor
jrummell
2013/06/20 21:47:01
There is now a default ctor.
| |
122 }; | |
123 | |
124 } // namespace media | |
125 | |
126 #endif // MEDIA_BASE_SEEKABLE_AUDIO_BUFFER_H_ | |
OLD | NEW |