OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // AudioRendererBase takes care of the tricky queuing work and provides simple | 5 // AudioRendererBase takes care of the tricky queuing work and provides simple |
6 // methods for subclasses to peek and poke at audio data. In addition to | 6 // methods for subclasses to peek and poke at audio data. In addition to |
7 // AudioRenderer interface methods this classes doesn't implement, subclasses | 7 // AudioRenderer interface methods this classes doesn't implement, subclasses |
8 // must also implement the following methods: | 8 // must also implement the following methods: |
9 // OnInitialized | 9 // OnInitialized |
10 // OnStop | 10 // OnStop |
11 // | 11 // |
12 // The general assumption is that subclasses start a callback-based audio thread | 12 // The general assumption is that subclasses start a callback-based audio thread |
13 // which needs to be filled with decoded audio data. AudioDecoderBase provides | 13 // which needs to be filled with decoded audio data. AudioDecoderBase provides |
14 // FillBuffer which handles filling the provided buffer, dequeuing items, | 14 // FillBuffer which handles filling the provided buffer, dequeuing items, |
15 // scheduling additional reads and updating the clock. In a sense, | 15 // scheduling additional reads and updating the clock. In a sense, |
16 // AudioRendererBase is the producer and the subclass is the consumer. | 16 // AudioRendererBase is the producer and the subclass is the consumer. |
17 | 17 |
18 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 18 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
19 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 19 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
20 | 20 |
21 #include <deque> | 21 #include <deque> |
22 | 22 |
23 #include "base/lock.h" | 23 #include "base/lock.h" |
24 #include "media/base/buffers.h" | 24 #include "media/base/buffers.h" |
25 #include "media/base/factory.h" | 25 #include "media/base/factory.h" |
26 #include "media/base/filters.h" | 26 #include "media/base/filters.h" |
| 27 #include "media/filters/audio_renderer_algorithm_base.h" |
27 | 28 |
28 namespace media { | 29 namespace media { |
29 | 30 |
30 class AudioRendererBase : public AudioRenderer { | 31 class AudioRendererBase : public AudioRenderer { |
31 public: | 32 public: |
32 // MediaFilter implementation. | 33 // MediaFilter implementation. |
33 virtual void Play(FilterCallback* callback); | 34 virtual void Play(FilterCallback* callback); |
34 virtual void Pause(FilterCallback* callback); | 35 virtual void Pause(FilterCallback* callback); |
35 virtual void Stop(); | 36 virtual void Stop(); |
36 | 37 |
37 virtual void Seek(base::TimeDelta time, FilterCallback* callback); | 38 virtual void Seek(base::TimeDelta time, FilterCallback* callback); |
38 | 39 |
39 // AudioRenderer implementation. | 40 // AudioRenderer implementation. |
40 virtual void Initialize(AudioDecoder* decoder, FilterCallback* callback); | 41 virtual void Initialize(AudioDecoder* decoder, FilterCallback* callback); |
41 | 42 |
42 protected: | 43 protected: |
43 // The default maximum size of the queue. | |
44 static const size_t kDefaultMaxQueueSize; | |
45 | |
46 // Only allow a factory to create this class. | 44 // Only allow a factory to create this class. |
47 explicit AudioRendererBase(size_t max_queue_size); | 45 AudioRendererBase(); |
48 virtual ~AudioRendererBase(); | 46 virtual ~AudioRendererBase(); |
49 | 47 |
50 // Called by Initialize(). |media_format| is the format of the AudioDecoder. | 48 // Called by Initialize(). |media_format| is the format of the AudioDecoder. |
51 // Subclasses should return true if they were able to initialize, false | 49 // Subclasses should return true if they were able to initialize, false |
52 // otherwise. | 50 // otherwise. |
53 virtual bool OnInitialize(const MediaFormat& media_format) = 0; | 51 virtual bool OnInitialize(const MediaFormat& media_format) = 0; |
54 | 52 |
55 // Called by Stop(). Subclasses should perform any necessary cleanup during | 53 // Called by Stop(). Subclasses should perform any necessary cleanup during |
56 // this time, such as stopping any running threads. | 54 // this time, such as stopping any running threads. |
57 virtual void OnStop() = 0; | 55 virtual void OnStop() = 0; |
58 | 56 |
59 // Called when a AudioDecoder::Read() completes and decrements | 57 // Called when a AudioDecoder::Read() completes and decrements |
60 // |pending_reads_|. | 58 // |pending_reads_|. |
61 virtual void OnReadComplete(Buffer* buffer_in); | 59 virtual void OnReadComplete(Buffer* buffer_in); |
62 | 60 |
63 // Fills the given buffer with audio data by dequeuing buffers and copying the | 61 // Fills the given buffer with audio data by delegating to its |algorithm_|. |
64 // data into the |dest|. FillBuffer() also takes care of updating the clock. | 62 // FillBuffer() also takes care of updating the clock. Returns the number of |
65 // Returns the number of bytes copied into |dest|, which may be less than | 63 // bytes copied into |dest|, which may be less than or equal to |len|. |
66 // equal to |len|. | |
67 // | 64 // |
68 // If this method is returns less bytes than |len| (including zero), it could | 65 // If this method is returns less bytes than |len| (including zero), it could |
69 // be a sign that the pipeline is stalled or unable to stream the data fast | 66 // be a sign that the pipeline is stalled or unable to stream the data fast |
70 // enough. In such scenarios, the callee should zero out unused portions | 67 // enough. In such scenarios, the callee should zero out unused portions |
71 // of their buffer to playback silence. | 68 // of their buffer to playback silence. |
72 // | 69 // |
73 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is | 70 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is |
74 // not called at the same rate as audio samples are played, then the reported | 71 // not called at the same rate as audio samples are played, then the reported |
75 // timestamp in the pipeline will be ahead of the actual audio playback. In | 72 // timestamp in the pipeline will be ahead of the actual audio playback. In |
76 // this case |playback_delay| should be used to indicate when in the future | 73 // this case |playback_delay| should be used to indicate when in the future |
77 // should the filled buffer be played. If FillBuffer() is called as the audio | 74 // should the filled buffer be played. If FillBuffer() is called as the audio |
78 // hardware plays the buffer, then |playback_delay| should be zero. | 75 // hardware plays the buffer, then |playback_delay| should be zero. |
79 // | 76 // |
80 // Safe to call on any thread. | 77 // Safe to call on any thread. |
81 size_t FillBuffer(uint8* dest, | 78 size_t FillBuffer(uint8* dest, |
82 size_t len, | 79 size_t len, |
83 float rate, | |
84 const base::TimeDelta& playback_delay); | 80 const base::TimeDelta& playback_delay); |
85 | 81 |
86 // Helper to parse a media format and return whether we were successful | 82 // Helper to parse a media format and return whether we were successful |
87 // retrieving all the information we care about. | 83 // retrieving all the information we care about. |
88 static bool ParseMediaFormat(const MediaFormat& media_format, | 84 static bool ParseMediaFormat(const MediaFormat& media_format, |
89 int* channels_out, int* sample_rate_out, | 85 int* channels_out, int* sample_rate_out, |
90 int* sample_bits_out); | 86 int* sample_bits_out); |
91 | 87 |
| 88 // Get/Set the playback rate of |algorithm_|. |
| 89 virtual void SetPlaybackRate(float playback_rate); |
| 90 virtual float GetPlaybackRate(); |
| 91 |
92 private: | 92 private: |
93 // Helper method that schedules an asynchronous read from the decoder and | 93 // Helper method that schedules an asynchronous read from the decoder and |
94 // increments |pending_reads_|. | 94 // increments |pending_reads_|. |
95 // | 95 // |
96 // Safe to call from any thread. | 96 // Safe to call from any thread. |
97 void ScheduleRead_Locked(); | 97 void ScheduleRead_Locked(); |
98 | 98 |
99 // Audio decoder. | 99 // Audio decoder. |
100 scoped_refptr<AudioDecoder> decoder_; | 100 scoped_refptr<AudioDecoder> decoder_; |
101 | 101 |
102 // Maximum queue size, configuration parameter passed in during construction. | 102 // Algorithm for scaling audio. |
103 size_t max_queue_size_; | 103 scoped_ptr<AudioRendererAlgorithmBase> algorithm_; |
104 | 104 |
105 // Queued audio data. | |
106 typedef std::deque< scoped_refptr<Buffer> > BufferQueue; | |
107 BufferQueue queue_; | |
108 Lock lock_; | 105 Lock lock_; |
109 | 106 |
110 // Remembers the amount of remaining audio data for the front buffer. | |
111 size_t data_offset_; | |
112 | |
113 // Simple state tracking variable. | 107 // Simple state tracking variable. |
114 enum State { | 108 enum State { |
115 kUninitialized, | 109 kUninitialized, |
116 kPaused, | 110 kPaused, |
117 kSeeking, | 111 kSeeking, |
118 kPlaying, | 112 kPlaying, |
119 kStopped, | 113 kStopped, |
120 kError, | 114 kError, |
121 }; | 115 }; |
122 State state_; | 116 State state_; |
(...skipping 12 matching lines...) Expand all Loading... |
135 // Filter callbacks. | 129 // Filter callbacks. |
136 scoped_ptr<FilterCallback> pause_callback_; | 130 scoped_ptr<FilterCallback> pause_callback_; |
137 scoped_ptr<FilterCallback> seek_callback_; | 131 scoped_ptr<FilterCallback> seek_callback_; |
138 | 132 |
139 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); | 133 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); |
140 }; | 134 }; |
141 | 135 |
142 } // namespace media | 136 } // namespace media |
143 | 137 |
144 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 138 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
OLD | NEW |