cras_unified.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // Creates a unified stream based on the cras (ChromeOS audio server) interface.
  6. //
  7. // CrasUnifiedStream object is *not* thread-safe and should only be used
  8. // from the audio thread.
  9. #ifndef MEDIA_AUDIO_CRAS_CRAS_UNIFIED_H_
  10. #define MEDIA_AUDIO_CRAS_CRAS_UNIFIED_H_
  11. #include <cras_client.h>
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include "base/compiler_specific.h"
  16. #include "media/audio/audio_io.h"
  17. #include "media/base/audio_parameters.h"
  18. namespace media {
  19. class AudioManagerCrasBase;
  20. // Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio
  21. // server.
  22. // TODO(dgreid): This class is used for only output, either remove all the
  23. // relevant input code and change the class to CrasOutputStream or merge
  24. // cras_input.cc into this unified implementation.
  25. class MEDIA_EXPORT CrasUnifiedStream : public AudioOutputStream {
  26. public:
  27. // The ctor takes all the usual parameters, plus |manager| which is the
  28. // audio manager who is creating this object.
  29. CrasUnifiedStream(const AudioParameters& params,
  30. AudioManagerCrasBase* manager,
  31. const std::string& device_id);
  32. CrasUnifiedStream(const CrasUnifiedStream&) = delete;
  33. CrasUnifiedStream& operator=(const CrasUnifiedStream&) = delete;
  34. // The dtor is typically called by the AudioManager only and it is usually
  35. // triggered by calling AudioUnifiedStream::Close().
  36. ~CrasUnifiedStream() override;
  37. // Implementation of AudioOutputStream.
  38. bool Open() override;
  39. void Close() override;
  40. void Flush() override;
  41. void Start(AudioSourceCallback* callback) override;
  42. void Stop() override;
  43. void SetVolume(double volume) override;
  44. void GetVolume(double* volume) override;
  45. private:
  46. // Handles captured audio and fills the output with audio to be played.
  47. static int UnifiedCallback(struct libcras_stream_cb_data* data);
  48. // Handles notification that there was an error with the playback stream.
  49. static int StreamError(cras_client* client,
  50. cras_stream_id_t stream_id,
  51. int err,
  52. void* arg);
  53. // Writes audio for a playback stream.
  54. uint32_t WriteAudio(size_t frames,
  55. uint8_t* buffer,
  56. const timespec* latency_ts);
  57. // Deals with an error that occured in the stream. Called from StreamError().
  58. void NotifyStreamError(int err);
  59. // The client used to communicate with the audio server.
  60. struct libcras_client* client_;
  61. // ID of the playing stream.
  62. cras_stream_id_t stream_id_;
  63. // PCM parameters for the stream.
  64. AudioParameters params_;
  65. // True if stream is playing.
  66. bool is_playing_;
  67. // Volume level from 0.0 to 1.0.
  68. float volume_;
  69. // Audio manager that created us. Used to report that we've been closed.
  70. AudioManagerCrasBase* manager_;
  71. // Callback to get audio samples.
  72. AudioSourceCallback* source_callback_;
  73. // Container for exchanging data with AudioSourceCallback::OnMoreData().
  74. std::unique_ptr<AudioBus> output_bus_;
  75. // Direciton of the stream.
  76. CRAS_STREAM_DIRECTION stream_direction_;
  77. // Index of the CRAS device to stream output to.
  78. const int pin_device_;
  79. };
  80. } // namespace media
  81. #endif // MEDIA_AUDIO_CRAS_CRAS_UNIFIED_H_