| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ | 6 #define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/lazy_instance.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/synchronization/lock.h" |
| 14 #include "base/values.h" | 16 #include "base/values.h" |
| 15 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 16 #include "content/public/common/media_stream_request.h" | 18 #include "media/audio/audio_logging.h" |
| 17 | 19 |
| 18 namespace media { | 20 namespace media { |
| 19 class AudioParameters; | 21 class AudioParameters; |
| 20 struct MediaLogEvent; | 22 struct MediaLogEvent; |
| 21 } | 23 } |
| 22 | 24 |
| 23 namespace content { | 25 namespace content { |
| 24 | 26 |
| 25 // This class stores information about currently active media. | 27 // This class stores information about currently active media. |
| 26 // It's constructed on the UI thread but all of its methods are called on the IO | 28 class CONTENT_EXPORT MediaInternals |
| 27 // thread. | 29 : NON_EXPORTED_BASE(public media::AudioLogFactory) { |
| 28 class CONTENT_EXPORT MediaInternals { | |
| 29 public: | 30 public: |
| 31 static MediaInternals* GetInstance(); |
| 32 |
| 30 virtual ~MediaInternals(); | 33 virtual ~MediaInternals(); |
| 31 | 34 |
| 32 static MediaInternals* GetInstance(); | |
| 33 | |
| 34 // The following methods are virtual for gmock. | |
| 35 | |
| 36 // Called when an audio stream is deleted. | |
| 37 virtual void OnDeleteAudioStream(void* host, int stream_id); | |
| 38 | |
| 39 // Called when an audio stream is set to playing or paused. | |
| 40 virtual void OnSetAudioStreamPlaying(void* host, int stream_id, bool playing); | |
| 41 | |
| 42 // Called when an audio stream is created with the parameters that | |
| 43 // it attempts to use to make the stream. | |
| 44 virtual void OnAudioStreamCreated(void* host, | |
| 45 int stream_id, | |
| 46 const media::AudioParameters& params, | |
| 47 const std::string& input_device_id); | |
| 48 | |
| 49 // Called when the status of an audio stream is set to "created", "closed", or | |
| 50 // "error". | |
| 51 virtual void OnSetAudioStreamStatus(void* host, int stream_id, | |
| 52 const std::string& status); | |
| 53 | |
| 54 // Called when the volume of an audio stream is set. | |
| 55 virtual void OnSetAudioStreamVolume(void* host, int stream_id, | |
| 56 double volume); | |
| 57 | |
| 58 // Called when a MediaEvent occurs. | 35 // Called when a MediaEvent occurs. |
| 59 virtual void OnMediaEvents(int render_process_id, | 36 void OnMediaEvents(int render_process_id, |
| 60 const std::vector<media::MediaLogEvent>& events); | 37 const std::vector<media::MediaLogEvent>& events); |
| 61 | 38 |
| 62 // Called with the update string. | 39 // Called with the update string. |
| 63 typedef base::Callback<void(const string16&)> UpdateCallback; | 40 typedef base::Callback<void(const string16&)> UpdateCallback; |
| 64 | 41 |
| 65 // Add/remove update callbacks (see above). | 42 // Add/remove update callbacks (see above). Must be called on the IO thread. |
| 66 void AddUpdateCallback(const UpdateCallback& callback); | 43 void AddUpdateCallback(const UpdateCallback& callback); |
| 67 void RemoveUpdateCallback(const UpdateCallback& callback); | 44 void RemoveUpdateCallback(const UpdateCallback& callback); |
| 45 |
| 46 // Sends all cached data to each registered UpdateCallback. |
| 68 void SendEverything(); | 47 void SendEverything(); |
| 69 | 48 |
| 49 // AudioLogFactory implementation. Safe to call from any thread. |
| 50 virtual scoped_ptr<media::AudioLog> CreateAudioLog( |
| 51 AudioComponent component) OVERRIDE; |
| 52 |
| 70 private: | 53 private: |
| 71 friend class MockMediaInternals; | 54 friend class AudioLogImpl; |
| 72 friend class MediaInternalsTest; | 55 friend class MediaInternalsTest; |
| 73 friend struct DefaultSingletonTraits<MediaInternals>; | 56 friend struct base::DefaultLazyInstanceTraits<MediaInternals>; |
| 74 | 57 |
| 75 MediaInternals(); | 58 MediaInternals(); |
| 76 | 59 |
| 77 // Sets |property| of an audio stream to |value| and notifies observers. | 60 // Sends |update| to each registered UpdateCallback. Safe to call from any |
| 78 // (host, stream_id) is a unique id for the audio stream. | 61 // thread, but will forward to the IO thread. |
| 79 // |host| will never be dereferenced. | 62 void SendUpdate(const string16& update); |
| 80 void UpdateAudioStream(void* host, int stream_id, | |
| 81 const std::string& property, base::Value* value); | |
| 82 | 63 |
| 83 // See UpdateAudioStream. The difference is that StoreAudioStream does not | 64 // Caches |value| under |cache_key| so that future SendEverything() calls will |
| 84 // immediately send the data to the client, instead it waits until | 65 // include the current data. Calls JavaScript |function|(|value|) for each |
| 85 // SendEverything is called. | 66 // registered UpdateCallback. SendUpdateAndPurgeCache() is similar but purges |
| 86 void StoreAudioStream(void* host, | 67 // the cache entry after completion instead. |
| 87 int stream_id, | 68 void SendUpdateAndCache(const std::string& cache_key, |
| 88 const std::string& property, | 69 const std::string& function, |
| 89 base::Value* value); | 70 const base::DictionaryValue* value); |
| 71 void SendUpdateAndPurgeCache(const std::string& cache_key, |
| 72 const std::string& function, |
| 73 const base::DictionaryValue* value); |
| 74 // Must only be accessed on the IO thread. |
| 75 std::vector<UpdateCallback> update_callbacks_; |
| 90 | 76 |
| 91 // Removes |item| from |data_|. | 77 // All variables below must be accessed under |lock_|. |
| 92 void DeleteItem(const std::string& item); | 78 base::Lock lock_; |
| 93 | 79 base::DictionaryValue cached_data_; |
| 94 base::DictionaryValue* StoreItem(const std::string& id, | 80 int owner_ids_[AUDIO_COMPONENT_MAX]; |
| 95 const std::string& property, | |
| 96 base::Value* value); | |
| 97 | |
| 98 // Sets data_.id.property = value and notifies attached UIs using update_fn. | |
| 99 // id may be any depth, e.g. "video.decoders.1.2.3" | |
| 100 void UpdateItem(const std::string& update_fn, const std::string& id, | |
| 101 const std::string& property, base::Value* value); | |
| 102 | |
| 103 // Calls javascript |function|(|value|) on each attached UI. | |
| 104 void SendUpdate(const std::string& function, base::Value* value); | |
| 105 | |
| 106 base::DictionaryValue data_; | |
| 107 | |
| 108 std::vector<UpdateCallback> update_callbacks_; | |
| 109 | 81 |
| 110 DISALLOW_COPY_AND_ASSIGN(MediaInternals); | 82 DISALLOW_COPY_AND_ASSIGN(MediaInternals); |
| 111 }; | 83 }; |
| 112 | 84 |
| 113 } // namespace content | 85 } // namespace content |
| 114 | 86 |
| 115 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ | 87 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_ |
| OLD | NEW |