memory_data_source.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2016 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. #ifndef MEDIA_FILTERS_MEMORY_DATA_SOURCE_H_
  5. #define MEDIA_FILTERS_MEMORY_DATA_SOURCE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <atomic>
  9. #include <string>
  10. #include "media/base/data_source.h"
  11. namespace media {
  12. // Basic data source that treats the URL as a file path, and uses the file
  13. // system to read data for a media pipeline.
  14. class MEDIA_EXPORT MemoryDataSource final : public DataSource {
  15. public:
  16. // Construct MemoryDataSource with |data| and |size|. The data is guaranteed
  17. // to be valid during the lifetime of MemoryDataSource.
  18. MemoryDataSource(const uint8_t* data, size_t size);
  19. // Similar to the above, but takes ownership of the std::string.
  20. explicit MemoryDataSource(std::string data);
  21. MemoryDataSource(const MemoryDataSource&) = delete;
  22. MemoryDataSource& operator=(const MemoryDataSource&) = delete;
  23. ~MemoryDataSource() final;
  24. // Implementation of DataSource.
  25. void Read(int64_t position,
  26. int size,
  27. uint8_t* data,
  28. DataSource::ReadCB read_cb) final;
  29. void Stop() final;
  30. void Abort() final;
  31. [[nodiscard]] bool GetSize(int64_t* size_out) final;
  32. bool IsStreaming() final;
  33. void SetBitrate(int bitrate) final;
  34. private:
  35. const std::string data_string_;
  36. const uint8_t* data_ = nullptr;
  37. const size_t size_ = 0;
  38. // Stop may be called from the render thread while this class is being used by
  39. // the media thread. It's harmless if we fulfill a read after Stop() has been
  40. // called, so an atomic without a lock is safe.
  41. std::atomic<bool> is_stopped_{false};
  42. };
  43. } // namespace media
  44. #endif // MEDIA_FILTERS_MEMORY_DATA_SOURCE_H_