Home | History | Annotate | Download | only in android
      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 #ifndef CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
      6 #define CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/time/time.h"
     14 #include "media/base/decryptor.h"
     15 #include "media/base/demuxer.h"
     16 #include "media/base/media_keys.h"
     17 #include "media/base/pipeline_status.h"
     18 #include "media/base/ranges.h"
     19 #include "media/base/text_track.h"
     20 #include "third_party/WebKit/public/web/WebMediaPlayer.h"
     21 
     22 namespace media {
     23 class ChunkDemuxer;
     24 class DecoderBuffer;
     25 class DecryptingDemuxerStream;
     26 class DemuxerStream;
     27 class MediaLog;
     28 struct MediaPlayerHostMsg_DemuxerReady_Params;
     29 struct MediaPlayerHostMsg_ReadFromDemuxerAck_Params;
     30 }
     31 
     32 namespace content {
     33 
     34 class WebMediaPlayerProxyAndroid;
     35 
     36 class MediaSourceDelegate : public media::DemuxerHost {
     37  public:
     38   typedef base::Callback<void(WebKit::WebMediaPlayer::NetworkState)>
     39       UpdateNetworkStateCB;
     40   typedef base::Callback<void(const base::TimeDelta&)> DurationChangeCB;
     41 
     42   // Helper class used by scoped_ptr to destroy an instance of
     43   // MediaSourceDelegate.
     44   class Destroyer {
     45    public:
     46     inline void operator()(void* media_source_delegate) const {
     47       static_cast<MediaSourceDelegate*>(media_source_delegate)->Destroy();
     48     }
     49   };
     50 
     51   MediaSourceDelegate(WebMediaPlayerProxyAndroid* proxy,
     52                       int player_id,
     53                       const scoped_refptr<base::MessageLoopProxy>& media_loop,
     54                       media::MediaLog* media_log);
     55 
     56   // Initialize the MediaSourceDelegate. |media_source| will be owned by
     57   // this object after this call.
     58   void InitializeMediaSource(
     59       WebKit::WebMediaSource* media_source,
     60       const media::NeedKeyCB& need_key_cb,
     61       const media::SetDecryptorReadyCB& set_decryptor_ready_cb,
     62       const UpdateNetworkStateCB& update_network_state_cb,
     63       const DurationChangeCB& duration_change_cb);
     64 
     65 #if defined(GOOGLE_TV)
     66   void InitializeMediaStream(
     67       media::Demuxer* demuxer,
     68       const UpdateNetworkStateCB& update_network_state_cb);
     69 #endif
     70 
     71   const WebKit::WebTimeRanges& Buffered();
     72   size_t DecodedFrameCount() const;
     73   size_t DroppedFrameCount() const;
     74   size_t AudioDecodedByteCount() const;
     75   size_t VideoDecodedByteCount() const;
     76 
     77   // Seeks the demuxer and acknowledges the seek request with |seek_request_id|
     78   // after the seek has been completed. This method can be called during pending
     79   // seeks, in which case only the last seek request will be acknowledged.
     80   void Seek(base::TimeDelta time, unsigned seek_request_id);
     81 
     82   void NotifyKeyAdded(const std::string& key_system);
     83 
     84   // Called when DemuxerStreamPlayer needs to read data from ChunkDemuxer.
     85   void OnReadFromDemuxer(media::DemuxerStream::Type type);
     86 
     87   // Called when the player needs the new config data from ChunkDemuxer.
     88   void OnMediaConfigRequest();
     89 
     90   // Called by the Destroyer to destroy an instance of this object.
     91   void Destroy();
     92 
     93  private:
     94   typedef base::Callback<void(
     95       scoped_ptr<media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params> params)>
     96           ReadFromDemuxerAckCB;
     97   typedef base::Callback<void(
     98       scoped_ptr<media::MediaPlayerHostMsg_DemuxerReady_Params> params)>
     99           DemuxerReadyCB;
    100 
    101   // This is private to enforce use of the Destroyer.
    102   virtual ~MediaSourceDelegate();
    103 
    104   // Methods inherited from DemuxerHost.
    105   virtual void SetTotalBytes(int64 total_bytes) OVERRIDE;
    106   virtual void AddBufferedByteRange(int64 start, int64 end) OVERRIDE;
    107   virtual void AddBufferedTimeRange(base::TimeDelta start,
    108                                     base::TimeDelta end) OVERRIDE;
    109   virtual void SetDuration(base::TimeDelta duration) OVERRIDE;
    110   virtual void OnDemuxerError(media::PipelineStatus status) OVERRIDE;
    111 
    112   // Callback for ChunkDemuxer initialization.
    113   void OnDemuxerInitDone(media::PipelineStatus status);
    114 
    115   // Initializes DecryptingDemuxerStreams if audio/video stream is encrypted.
    116   void InitAudioDecryptingDemuxerStream();
    117   void InitVideoDecryptingDemuxerStream();
    118 
    119   // Callbacks for DecryptingDemuxerStream::Initialize().
    120   void OnAudioDecryptingDemuxerStreamInitDone(media::PipelineStatus status);
    121   void OnVideoDecryptingDemuxerStreamInitDone(media::PipelineStatus status);
    122 
    123   // Callback for Demuxer seek. It will call ResetAudioDecryptingDemuxerStream()
    124   // as part of the reset callback chain.
    125   void OnDemuxerSeekDone(unsigned seek_request_id,
    126                          media::PipelineStatus status);
    127 
    128   // Resets AudioDecryptingDemuxerStream if it exists. Then it will call
    129   // ResetVideoDecryptingDemuxerStream() as part of the reset callback chain.
    130   void ResetAudioDecryptingDemuxerStream();
    131 
    132   // Resets VideoDecryptingDemuxerStream if it exists. Then it will call
    133   // SendSeekRequestAck() as part of the reset callback chain.
    134   void ResetVideoDecryptingDemuxerStream();
    135 
    136   // Sends SeekRequestAck to the browser.
    137   void SendSeekRequestAck();
    138 
    139   void OnDemuxerStopDone();
    140   void OnDemuxerOpened();
    141   void OnNeedKey(const std::string& type,
    142                  const std::string& session_id,
    143                  scoped_ptr<uint8[]> init_data,
    144                  int init_data_size);
    145   scoped_ptr<media::TextTrack> OnAddTextTrack(media::TextKind kind,
    146                                               const std::string& label,
    147                                               const std::string& language);
    148   void NotifyDemuxerReady();
    149   bool CanNotifyDemuxerReady();
    150   void SendDemuxerReady(
    151       scoped_ptr<media::MediaPlayerHostMsg_DemuxerReady_Params> params);
    152 
    153   void StopDemuxer();
    154   void InitializeDemuxer();
    155   void SeekInternal(base::TimeDelta time, unsigned seek_request_id);
    156   void OnReadFromDemuxerInternal(media::DemuxerStream::Type type);
    157   // Reads an access unit from the demuxer stream |stream| and stores it in
    158   // the |index|th access unit in |params|.
    159   void ReadFromDemuxerStream(
    160       media::DemuxerStream::Type type,
    161       scoped_ptr<media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params> params,
    162       size_t index);
    163   void OnBufferReady(
    164       media::DemuxerStream::Type type,
    165       scoped_ptr<media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params> params,
    166       size_t index,
    167       media::DemuxerStream::Status status,
    168       const scoped_refptr<media::DecoderBuffer>& buffer);
    169 
    170   void SendReadFromDemuxerAck(
    171       scoped_ptr<media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params> params);
    172 
    173   // Helper function for calculating duration.
    174   int GetDurationMs();
    175 
    176   bool HasEncryptedStream();
    177 
    178   void SetSeeking(bool seeking);
    179   bool IsSeeking() const;
    180 
    181   // Weak pointer must be dereferenced and invalidated on the same thread.
    182   base::WeakPtrFactory<MediaSourceDelegate> main_weak_this_;
    183   base::WeakPtrFactory<MediaSourceDelegate> media_weak_this_;
    184 
    185   // Message loop for main renderer thread.
    186   const scoped_refptr<base::MessageLoopProxy> main_loop_;
    187 #if defined(GOOGLE_TV)
    188   // Message loop for the media thread.
    189   // When there is high load in the render thread, the reading from |demuxer_|
    190   // and its read-callback loops run very slowly.  To improve the response time
    191   // of the readings, we run tasks related to |demuxer_| in the media thread.
    192   const scoped_refptr<base::MessageLoopProxy> media_loop_;
    193 
    194   ReadFromDemuxerAckCB send_read_from_demuxer_ack_cb_;
    195   base::Closure send_seek_request_ack_cb_;
    196   DemuxerReadyCB send_demuxer_ready_cb_;
    197 #endif
    198 
    199   WebMediaPlayerProxyAndroid* proxy_;
    200   int player_id_;
    201 
    202   scoped_refptr<media::MediaLog> media_log_;
    203   UpdateNetworkStateCB update_network_state_cb_;
    204   DurationChangeCB duration_change_cb_;
    205 
    206   scoped_ptr<media::ChunkDemuxer> chunk_demuxer_;
    207   scoped_ptr<WebKit::WebMediaSource> media_source_;
    208   media::Demuxer* demuxer_;
    209   bool is_demuxer_ready_;
    210 
    211   media::SetDecryptorReadyCB set_decryptor_ready_cb_;
    212 
    213   scoped_ptr<media::DecryptingDemuxerStream> audio_decrypting_demuxer_stream_;
    214   scoped_ptr<media::DecryptingDemuxerStream> video_decrypting_demuxer_stream_;
    215 
    216   media::DemuxerStream* audio_stream_;
    217   media::DemuxerStream* video_stream_;
    218 
    219   media::PipelineStatistics statistics_;
    220   media::Ranges<base::TimeDelta> buffered_time_ranges_;
    221   // Keep a list of buffered time ranges.
    222   WebKit::WebTimeRanges buffered_web_time_ranges_;
    223 
    224   media::NeedKeyCB need_key_cb_;
    225 
    226   // The currently selected key system. Empty string means that no key system
    227   // has been selected.
    228   WebKit::WebString current_key_system_;
    229 
    230   // Temporary for EME v0.1. In the future the init data type should be passed
    231   // through GenerateKeyRequest() directly from WebKit.
    232   std::string init_data_type_;
    233 
    234   // Lock used to serialize access for |seeking_|.
    235   mutable base::Lock seeking_lock_;
    236   bool seeking_;
    237 
    238   base::TimeDelta last_seek_time_;
    239   unsigned last_seek_request_id_;
    240 
    241   bool key_added_;
    242   std::string key_system_;
    243 
    244   size_t access_unit_size_;
    245 
    246   DISALLOW_COPY_AND_ASSIGN(MediaSourceDelegate);
    247 };
    248 
    249 }  // namespace content
    250 
    251 #endif  // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_SOURCE_DELEGATE_H_
    252