| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // The object has one error state: |state_| == kInError. When |state_| == | 5 // The object has one error state: |state_| == kInError. When |state_| == |
| 6 // kInError, all public API functions will fail with an error (Start() will call | 6 // kInError, all public API functions will fail with an error (Start() will call |
| 7 // the OnError() function on the callback immediately), or no-op themselves with | 7 // the OnError() function on the callback immediately), or no-op themselves with |
| 8 // the exception of Close(). Even if an error state has been entered, if Open() | 8 // the exception of Close(). Even if an error state has been entered, if Open() |
| 9 // has previously returned successfully, Close() must be called. | 9 // has previously returned successfully, Close() must be called. |
| 10 | 10 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 // Prepare |audio_format| and |stream_params| for the stream we | 175 // Prepare |audio_format| and |stream_params| for the stream we |
| 176 // will create. | 176 // will create. |
| 177 cras_audio_format* audio_format = cras_audio_format_create( | 177 cras_audio_format* audio_format = cras_audio_format_create( |
| 178 pcm_format_, | 178 pcm_format_, |
| 179 frame_rate_, | 179 frame_rate_, |
| 180 num_channels_); | 180 num_channels_); |
| 181 if (audio_format == NULL) { | 181 if (audio_format == NULL) { |
| 182 LOG(WARNING) << "Error setting up audio parameters."; | 182 LOG(WARNING) << "Error setting up audio parameters."; |
| 183 TransitionTo(kInError); | 183 TransitionTo(kInError); |
| 184 callback->OnError(this, -ENOMEM); | 184 callback->OnError(this); |
| 185 return; | 185 return; |
| 186 } | 186 } |
| 187 cras_stream_params* stream_params = cras_client_stream_params_create( | 187 cras_stream_params* stream_params = cras_client_stream_params_create( |
| 188 CRAS_STREAM_OUTPUT, | 188 CRAS_STREAM_OUTPUT, |
| 189 samples_per_packet_ * 2, // Total latency. | 189 samples_per_packet_ * 2, // Total latency. |
| 190 samples_per_packet_ / 2, // Call back when this many left. | 190 samples_per_packet_ / 2, // Call back when this many left. |
| 191 samples_per_packet_, // Call back with at least this much space. | 191 samples_per_packet_, // Call back with at least this much space. |
| 192 CRAS_STREAM_TYPE_DEFAULT, | 192 CRAS_STREAM_TYPE_DEFAULT, |
| 193 0, | 193 0, |
| 194 this, | 194 this, |
| 195 CrasOutputStream::PutSamples, | 195 CrasOutputStream::PutSamples, |
| 196 CrasOutputStream::StreamError, | 196 CrasOutputStream::StreamError, |
| 197 audio_format); | 197 audio_format); |
| 198 if (stream_params == NULL) { | 198 if (stream_params == NULL) { |
| 199 LOG(WARNING) << "Error setting up stream parameters."; | 199 LOG(WARNING) << "Error setting up stream parameters."; |
| 200 TransitionTo(kInError); | 200 TransitionTo(kInError); |
| 201 callback->OnError(this, -ENOMEM); | 201 callback->OnError(this); |
| 202 cras_audio_format_destroy(audio_format); | 202 cras_audio_format_destroy(audio_format); |
| 203 return; | 203 return; |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Before starting the stream, save the number of bytes in a frame for use in | 206 // Before starting the stream, save the number of bytes in a frame for use in |
| 207 // the callback. | 207 // the callback. |
| 208 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); | 208 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); |
| 209 | 209 |
| 210 // Adding the stream will start the audio callbacks requesting data. | 210 // Adding the stream will start the audio callbacks requesting data. |
| 211 int err = cras_client_add_stream(client_, &stream_id_, stream_params); | 211 int err = cras_client_add_stream(client_, &stream_id_, stream_params); |
| 212 if (err < 0) { | 212 if (err < 0) { |
| 213 LOG(WARNING) << "Failed to add the stream"; | 213 LOG(WARNING) << "Failed to add the stream"; |
| 214 TransitionTo(kInError); | 214 TransitionTo(kInError); |
| 215 callback->OnError(this, err); | 215 callback->OnError(this); |
| 216 cras_audio_format_destroy(audio_format); | 216 cras_audio_format_destroy(audio_format); |
| 217 cras_client_stream_params_destroy(stream_params); | 217 cras_client_stream_params_destroy(stream_params); |
| 218 return; | 218 return; |
| 219 } | 219 } |
| 220 | 220 |
| 221 // Set initial volume. | 221 // Set initial volume. |
| 222 cras_client_set_stream_volume(client_, stream_id_, volume_); | 222 cras_client_set_stream_volume(client_, stream_id_, volume_); |
| 223 | 223 |
| 224 // Done with config params. | 224 // Done with config params. |
| 225 cras_audio_format_destroy(audio_format); | 225 cras_audio_format_destroy(audio_format); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 frames_filled, bytes_per_frame_ / num_channels_, buffer); | 295 frames_filled, bytes_per_frame_ / num_channels_, buffer); |
| 296 return frames_filled; | 296 return frames_filled; |
| 297 } | 297 } |
| 298 | 298 |
| 299 void CrasOutputStream::NotifyStreamError(int err) { | 299 void CrasOutputStream::NotifyStreamError(int err) { |
| 300 // This will remove the stream from the client. | 300 // This will remove the stream from the client. |
| 301 if (state_ == kIsClosed || state_ == kInError) | 301 if (state_ == kIsClosed || state_ == kInError) |
| 302 return; // Don't care about error if we aren't using it. | 302 return; // Don't care about error if we aren't using it. |
| 303 TransitionTo(kInError); | 303 TransitionTo(kInError); |
| 304 if (source_callback_) | 304 if (source_callback_) |
| 305 source_callback_->OnError(this, err); | 305 source_callback_->OnError(this); |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool CrasOutputStream::CanTransitionTo(InternalState to) { | 308 bool CrasOutputStream::CanTransitionTo(InternalState to) { |
| 309 switch (state_) { | 309 switch (state_) { |
| 310 case kCreated: | 310 case kCreated: |
| 311 return to == kIsOpened || to == kIsClosed || to == kInError; | 311 return to == kIsOpened || to == kIsClosed || to == kInError; |
| 312 | 312 |
| 313 case kIsOpened: | 313 case kIsOpened: |
| 314 return to == kIsPlaying || to == kIsStopped || | 314 return to == kIsPlaying || to == kIsStopped || |
| 315 to == kIsClosed || to == kInError; | 315 to == kIsClosed || to == kInError; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 339 state_ = to; | 339 state_ = to; |
| 340 } | 340 } |
| 341 return state_; | 341 return state_; |
| 342 } | 342 } |
| 343 | 343 |
| 344 CrasOutputStream::InternalState CrasOutputStream::state() { | 344 CrasOutputStream::InternalState CrasOutputStream::state() { |
| 345 return state_; | 345 return state_; |
| 346 } | 346 } |
| 347 | 347 |
| 348 } // namespace media | 348 } // namespace media |
| OLD | NEW |