1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127

media / base / data_source.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_DATA_SOURCE_H_
#define MEDIA_BASE_DATA_SOURCE_H_

#include <stdint.h>

#include "base/functional/callback_forward.h"
#include "media/base/media_export.h"
#include "url/gurl.h"

namespace media {

class CrossOriginDataSource;

// Abstracting informational methods into DataSourceInfo allows
// "meta-datasource" objects like HlsDataSourceProvider to query it's entire
// set of dependent data sources when calculating the data here.
class MEDIA_EXPORT DataSourceInfo {
 public:
  virtual ~DataSourceInfo();

  // The total memory size used.
  virtual int64_t GetMemoryUsage() = 0;

  // DataSource implementations that might make requests must ensure the value
  // is accurate for cross origin resources.
  virtual bool WouldTaintOrigin() = 0;

  // Returns true if we are performing streaming. In this case seeking is
  // not possible.
  virtual bool IsStreaming() = 0;
};

class MEDIA_EXPORT DataSource : public DataSourceInfo {
 public:
  using ReadCB = base::OnceCallback<void(int)>;

  enum { kReadError = -1, kAborted = -2 };

  // Used to specify video preload states. They are "hints" to the browser about
  // how aggressively the browser should load and buffer data.
  // Please see the HTML5 spec for the descriptions of these values:
  // http://www.w3.org/TR/html5/video.html#attr-media-preload
  //
  // Enum values must match the values in WebMediaPlayer::Preload and
  // there will be assertions at compile time if they do not match.
  enum Preload {
    NONE,
    METADATA,
    AUTO,
  };

  DataSource();

  DataSource(const DataSource&) = delete;
  DataSource& operator=(const DataSource&) = delete;

  ~DataSource() override;

  // Reads |size| bytes from |position| into |data|. And when the read is done
  // or failed, |read_cb| is called with the number of bytes read or
  // kReadError in case of error.
  virtual void Read(int64_t position,
                    int size,
                    uint8_t* data,
                    DataSource::ReadCB read_cb) = 0;

  // Stops the DataSource. Once this is called all future Read() calls will
  // return an error. This is a synchronous call and may be called from any
  // thread. Once called, the DataSource may no longer be used and should be
  // destructed shortly thereafter.
  virtual void Stop() = 0;

  // Similar to Stop(), but only aborts current reads and not future reads.
  virtual void Abort() = 0;

  // Returns true and the file size, false if the file size could not be
  // retrieved.
  [[nodiscard]] virtual bool GetSize(int64_t* size_out) = 0;

  // Notify the DataSource of the bitrate of the media.
  // Values of |bitrate| <= 0 are invalid and should be ignored.
  virtual void SetBitrate(int bitrate) = 0;

  // If there is a MultiBuffer associated with this data source, then defer to
  // it. This will return false if any HTTP response so far has failed the TAO
  // check.
  virtual bool PassedTimingAllowOriginCheck() = 0;

  // Assume fully buffered by default.
  virtual bool AssumeFullyBuffered() const;

  // By default this just returns GetSize().
  int64_t GetMemoryUsage() override;

  // Adjusts the buffering algorithm (if there is one) based on the given
  // preload value.
  virtual void SetPreload(media::DataSource::Preload preload);

  // Gets the url for this data source, if it exists. By default this returns
  // an empty GURL.
  virtual GURL GetUrlAfterRedirects() const;

  // Cancels any open network connections once reaching the deferred state. If
  // |must_cancel_netops| is false this is done only for preload=metadata, non-
  // streaming resources that have not started playback. If |must_cancel_netops|
  // is true, all resource types will have their connections canceled. If
  // already deferred, connections will be immediately closed. Most data source
  // implementations do not need to override this, as they do not open other
  // network connections.
  virtual void OnBufferingHaveEnough(bool must_cancel_netops);

  // Allows a holder to notify certain events to the data source. Most data
  // sources won't care too much about these events though.
  virtual void OnMediaPlaybackRateChanged(double playback_rate);
  virtual void OnMediaIsPlaying();

  // Gets a CrossOriginDataSource version of |this|, or nullptr if it isn't one.
  virtual CrossOriginDataSource* GetAsCrossOriginDataSource();
};

}  // namespace media

#endif  // MEDIA_BASE_DATA_SOURCE_H_