summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/media/android/media_source_delegate.h
blob: 317ef6544df26c1042b9914fe4ba2abf041bd378 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
#define CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_

#include <string>
#include <vector>

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "media/base/decryptor.h"
#include "media/base/demuxer.h"
#include "media/base/media_keys.h"
#include "media/base/pipeline_status.h"
#include "media/base/ranges.h"
#include "media/base/text_track.h"
#include "third_party/WebKit/public/platform/WebMediaPlayer.h"

namespace media {
class ChunkDemuxer;
class DecoderBuffer;
class DecryptingDemuxerStream;
class DemuxerStream;
class MediaLog;
struct DemuxerConfigs;
struct DemuxerData;
}

namespace content {

class RendererDemuxerAndroid;

class MediaSourceDelegate : public media::DemuxerHost {
 public:
  typedef base::Callback<void(blink::WebMediaSource*)>
      MediaSourceOpenedCB;
  typedef base::Callback<void(blink::WebMediaPlayer::NetworkState)>
      UpdateNetworkStateCB;
  typedef base::Callback<void(const base::TimeDelta&)> DurationChangeCB;

  // Helper class used by scoped_ptr to destroy an instance of
  // MediaSourceDelegate.
  class Destroyer {
   public:
    inline void operator()(void* media_source_delegate) const {
      static_cast<MediaSourceDelegate*>(media_source_delegate)->Destroy();
    }
  };

  MediaSourceDelegate(RendererDemuxerAndroid* demuxer_client,
                      int demuxer_client_id,
                      const scoped_refptr<base::MessageLoopProxy>& media_loop,
                      media::MediaLog* media_log);

  // Initialize the MediaSourceDelegate. |media_source| will be owned by
  // this object after this call.
  void InitializeMediaSource(
      const MediaSourceOpenedCB& media_source_opened_cb,
      const media::Demuxer::NeedKeyCB& need_key_cb,
      const media::SetDecryptorReadyCB& set_decryptor_ready_cb,
      const UpdateNetworkStateCB& update_network_state_cb,
      const DurationChangeCB& duration_change_cb);

#if defined(GOOGLE_TV)
  void InitializeMediaStream(
      media::Demuxer* demuxer,
      const UpdateNetworkStateCB& update_network_state_cb);
#endif

  const blink::WebTimeRanges& Buffered();
  size_t DecodedFrameCount() const;
  size_t DroppedFrameCount() const;
  size_t AudioDecodedByteCount() const;
  size_t VideoDecodedByteCount() const;

  // In MSE case, calls ChunkDemuxer::CancelPendingSeek(). Also sets the
  // expectation that a regular seek will be arriving and to trivially finish
  // any browser seeks that may be requested prior to the regular seek.
  void CancelPendingSeek(const base::TimeDelta& seek_time);

  // In MSE case, calls ChunkDemuxer::StartWaitingForSeek(), first calling
  // ChunkDemuxer::CancelPendingSeek() if a browser seek is in progress.
  // Also sets the expectation that a regular seek will be arriving and to
  // trivially finish any browser seeks that may be requested prior to the
  // regular seek.
  void StartWaitingForSeek(const base::TimeDelta& seek_time);

  // Seeks the demuxer and later calls OnDemuxerSeekDone() after the seek has
  // been completed. There must be no other seek of the demuxer currently in
  // process when this method is called.
  // If |is_browser_seek| is true, then this is a short-term hack browser
  // seek.
  // TODO(wolenetz): Instead of doing browser seek, browser player should replay
  // cached data since last keyframe. See http://crbug.com/304234.
  void Seek(const base::TimeDelta& seek_time, bool is_browser_seek);

  void NotifyKeyAdded(const std::string& key_system);

  // Called when DemuxerStreamPlayer needs to read data from ChunkDemuxer.
  void OnReadFromDemuxer(media::DemuxerStream::Type type);

  // Called when the player needs the new config data from ChunkDemuxer.
  void OnMediaConfigRequest();

  // Called by the Destroyer to destroy an instance of this object.
  void Destroy();

 private:
  typedef base::Callback<void(scoped_ptr<media::DemuxerData> data)>
      ReadFromDemuxerAckCB;
  typedef base::Callback<void(scoped_ptr<media::DemuxerConfigs> configs)>
      DemuxerReadyCB;

  // This is private to enforce use of the Destroyer.
  virtual ~MediaSourceDelegate();

  // Methods inherited from DemuxerHost.
  virtual void SetTotalBytes(int64 total_bytes) OVERRIDE;
  virtual void AddBufferedByteRange(int64 start, int64 end) OVERRIDE;
  virtual void AddBufferedTimeRange(base::TimeDelta start,
                                    base::TimeDelta end) OVERRIDE;
  virtual void SetDuration(base::TimeDelta duration) OVERRIDE;
  virtual void OnDemuxerError(media::PipelineStatus status) OVERRIDE;
  virtual void AddTextStream(media::DemuxerStream* text_stream,
                             const media::TextTrackConfig& config) OVERRIDE;
  virtual void RemoveTextStream(media::DemuxerStream* text_stream) OVERRIDE;

  // Notifies |demuxer_client_| and fires |duration_changed_cb_|.
  void OnDurationChanged(const base::TimeDelta& duration);

  // Callback for ChunkDemuxer initialization.
  void OnDemuxerInitDone(media::PipelineStatus status);

  // Initializes DecryptingDemuxerStreams if audio/video stream is encrypted.
  void InitAudioDecryptingDemuxerStream();
  void InitVideoDecryptingDemuxerStream();

  // Callbacks for DecryptingDemuxerStream::Initialize().
  void OnAudioDecryptingDemuxerStreamInitDone(media::PipelineStatus status);
  void OnVideoDecryptingDemuxerStreamInitDone(media::PipelineStatus status);

  // Callback for ChunkDemuxer::Seek() and callback chain for resetting
  // decrypted audio/video streams if present.
  //
  // Runs on the media thread.
  void OnDemuxerSeekDone(media::PipelineStatus status);
  void ResetAudioDecryptingDemuxerStream();
  void ResetVideoDecryptingDemuxerStream();
  void FinishResettingDecryptingDemuxerStreams();

  void OnDemuxerStopDone();
  void OnDemuxerOpened();
  void OnNeedKey(const std::string& type,
                 const std::vector<uint8>& init_data);
  void NotifyDemuxerReady();
  bool CanNotifyDemuxerReady();

  void StopDemuxer();
  void InitializeDemuxer();
  void SeekInternal(const base::TimeDelta& seek_time);
  // Reads an access unit from the demuxer stream |stream| and stores it in
  // the |index|th access unit in |params|.
  void ReadFromDemuxerStream(media::DemuxerStream::Type type,
                             scoped_ptr<media::DemuxerData> data,
                             size_t index);
  void OnBufferReady(media::DemuxerStream::Type type,
                     scoped_ptr<media::DemuxerData> data,
                     size_t index,
                     media::DemuxerStream::Status status,
                     const scoped_refptr<media::DecoderBuffer>& buffer);

  // Helper function for calculating duration.
  int GetDurationMs();

  bool HasEncryptedStream();

  bool IsSeeking() const;

  // Returns |seek_time| if it is still buffered or if there is no currently
  // buffered range including or soon after |seek_time|. If |seek_time| is not
  // buffered, but there is a later range buffered near to |seek_time|, returns
  // next buffered range's start time instead. Only call this for browser seeks.
  // |seeking_lock_| must be held by caller.
  base::TimeDelta FindBufferedBrowserSeekTime_Locked(
      const base::TimeDelta& seek_time) const;

  // Message loop for main renderer thread and corresponding weak pointer.
  const scoped_refptr<base::MessageLoopProxy> main_loop_;
  base::WeakPtrFactory<MediaSourceDelegate> main_weak_factory_;
  base::WeakPtr<MediaSourceDelegate> main_weak_this_;

  // Message loop for media thread and corresponding weak pointer.
  const scoped_refptr<base::MessageLoopProxy> media_loop_;
  base::WeakPtrFactory<MediaSourceDelegate> media_weak_factory_;

  RendererDemuxerAndroid* demuxer_client_;
  int demuxer_client_id_;

  scoped_refptr<media::MediaLog> media_log_;
  UpdateNetworkStateCB update_network_state_cb_;
  DurationChangeCB duration_change_cb_;

  scoped_ptr<media::ChunkDemuxer> chunk_demuxer_;
  media::Demuxer* demuxer_;
  bool is_demuxer_ready_;

  media::SetDecryptorReadyCB set_decryptor_ready_cb_;

  scoped_ptr<media::DecryptingDemuxerStream> audio_decrypting_demuxer_stream_;
  scoped_ptr<media::DecryptingDemuxerStream> video_decrypting_demuxer_stream_;

  media::DemuxerStream* audio_stream_;
  media::DemuxerStream* video_stream_;

  media::PipelineStatistics statistics_;
  media::Ranges<base::TimeDelta> buffered_time_ranges_;
  // Keep a list of buffered time ranges.
  blink::WebTimeRanges buffered_web_time_ranges_;

  MediaSourceOpenedCB media_source_opened_cb_;
  media::Demuxer::NeedKeyCB need_key_cb_;

  // The currently selected key system. Empty string means that no key system
  // has been selected.
  blink::WebString current_key_system_;

  // Temporary for EME v0.1. In the future the init data type should be passed
  // through GenerateKeyRequest() directly from WebKit.
  std::string init_data_type_;

  // Lock used to serialize access for |seeking_|.
  mutable base::Lock seeking_lock_;
  bool seeking_;

  // Track if we are currently performing a browser seek, and track whether or
  // not a regular seek is expected soon. If a regular seek is expected soon,
  // then any in-progress browser seek will be canceled pending the
  // regular seek, if using |chunk_demuxer_|, and any requested browser seek
  // will be trivially finished. Access is serialized by |seeking_lock_|.
  bool doing_browser_seek_;
  base::TimeDelta browser_seek_time_;
  bool expecting_regular_seek_;

#if defined(GOOGLE_TV)
  bool key_added_;
  std::string key_system_;
#endif  // defined(GOOGLE_TV)

  size_t access_unit_size_;

  DISALLOW_COPY_AND_ASSIGN(MediaSourceDelegate);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_