file_data_source.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FILTERS_FILE_DATA_SOURCE_H_
  5. #define MEDIA_FILTERS_FILE_DATA_SOURCE_H_
  6. #include <stdint.h>
  7. #include "base/files/file.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/memory_mapped_file.h"
  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 FileDataSource : public DataSource {
  15. public:
  16. FileDataSource();
  17. FileDataSource(const FileDataSource&) = delete;
  18. FileDataSource& operator=(const FileDataSource&) = delete;
  19. ~FileDataSource() override;
  20. [[nodiscard]] bool Initialize(const base::FilePath& file_path);
  21. [[nodiscard]] bool Initialize(base::File file);
  22. // Implementation of DataSource.
  23. void Stop() override;
  24. void Abort() override;
  25. void Read(int64_t position,
  26. int size,
  27. uint8_t* data,
  28. DataSource::ReadCB read_cb) override;
  29. [[nodiscard]] bool GetSize(int64_t* size_out) override;
  30. bool IsStreaming() override;
  31. void SetBitrate(int bitrate) override;
  32. // Unit test helpers. Recreate the object if you want the default behaviour.
  33. void force_read_errors_for_testing() { force_read_errors_ = true; }
  34. void force_streaming_for_testing() { force_streaming_ = true; }
  35. uint64_t bytes_read_for_testing() { return bytes_read_; }
  36. void reset_bytes_read_for_testing() { bytes_read_ = 0; }
  37. private:
  38. base::MemoryMappedFile file_;
  39. bool force_read_errors_;
  40. bool force_streaming_;
  41. uint64_t bytes_read_;
  42. };
  43. } // namespace media
  44. #endif // MEDIA_FILTERS_FILE_DATA_SOURCE_H_