sbBaseMediacorePlaybackControl.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
32 
33 #include <nsIWritablePropertyBag2.h>
34 
35 #include <nsComponentManagerUtils.h>
36 
37 #include "sbMediacoreEvent.h"
38 #include "sbVariantUtils.h"
39 
44 #ifdef PR_LOGGING
45 static PRLogModuleInfo* gBaseMediacorePlaybackControl = nsnull;
46 #define TRACE(args) PR_LOG(gBaseMediacorePlaybackControl , PR_LOG_DEBUG, args)
47 #define LOG(args) PR_LOG(gBaseMediacorePlaybackControl , PR_LOG_WARN, args)
48 #ifdef __GNUC__
49 #define __FUNCTION__ __PRETTY_FUNCTION__
50 #endif
51 #else
52 #define TRACE(args) /* nothing */
53 #define LOG(args) /* nothing */
54 #endif
55 
58 
60 : mMonitor(nsnull)
61 , mPosition(0)
62 , mDuration(0)
63 {
64  MOZ_COUNT_CTOR(sbBaseMediacorePlaybackControl);
65 
66 #ifdef PR_LOGGING
67  if (!gBaseMediacorePlaybackControl)
68  gBaseMediacorePlaybackControl= PR_NewLogModule("sbBaseMediacorePlaybackControl");
69 #endif
70 
71  TRACE(("%s[%p]", __FUNCTION__, this));
72 }
73 
75 {
76  TRACE(("%s[%p]", __FUNCTION__, this));
77 
78  MOZ_COUNT_DTOR(sbBaseMediacorePlaybackControl);
79 
80  if(mMonitor) {
81  nsAutoMonitor::DestroyMonitor(mMonitor);
82  }
83 }
84 
85 nsresult
87 {
88  TRACE(("%s[%p]", __FUNCTION__, this));
89 
90  mMonitor = nsAutoMonitor::NewMonitor("sbBaseMediacorePlaybackControl::mMonitor");
91  NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
92 
94 }
95 
96 NS_IMETHODIMP
97 sbBaseMediacorePlaybackControl::GetUri(nsIURI * *aUri)
98 {
99  TRACE(("%s[%p]", __FUNCTION__, this));
100  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
101  NS_ENSURE_ARG_POINTER(aUri);
102 
103  nsAutoMonitor mon(mMonitor);
104  NS_IF_ADDREF(*aUri = mUri);
105 
106  return NS_OK;
107 }
108 
109 NS_IMETHODIMP
110 sbBaseMediacorePlaybackControl::SetUri(nsIURI * aUri)
111 {
112  TRACE(("%s[%p]", __FUNCTION__, this));
113  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
114  NS_ENSURE_ARG_POINTER(aUri);
115 
116  nsresult rv = OnSetUri(aUri);
117  NS_ENSURE_SUCCESS(rv, rv);
118 
119  nsAutoMonitor mon(mMonitor);
120  mUri = aUri;
121 
122  return NS_OK;
123 }
124 
125 NS_IMETHODIMP
126 sbBaseMediacorePlaybackControl::GetPosition(PRUint64 *aPosition)
127 {
128  TRACE(("%s[%p]", __FUNCTION__, this));
129  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
130  NS_ENSURE_ARG_POINTER(aPosition);
131 
132  nsAutoMonitor mon(mMonitor);
133  nsresult rv = OnGetPosition(aPosition);
134 
135  if(NS_FAILED(rv)) {
136  *aPosition = 0;
137  }
138 
139  return NS_OK;
140 }
141 
142 NS_IMETHODIMP
143 sbBaseMediacorePlaybackControl::SetPosition(PRUint64 aPosition)
144 {
145  TRACE(("%s[%p] (%llu)", __FUNCTION__, this, aPosition));
146  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
147 
148  nsresult rv = OnSetPosition(aPosition);
149  NS_ENSURE_SUCCESS(rv, rv);
150 
151  nsAutoMonitor mon(mMonitor);
152  mPosition = aPosition;
153 
154  return NS_OK;
155 }
156 
157 NS_IMETHODIMP
158 sbBaseMediacorePlaybackControl::Seek(PRUint64 aPosition, PRUint32 aFlags)
159 {
160  TRACE(("%s[%p] (%llu, %llu)", __FUNCTION__, this, aPosition, aFlags));
161  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
162 
163  nsresult rv = OnSeek(aPosition, aFlags);
164  NS_ENSURE_SUCCESS(rv, rv);
165 
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  nsAutoMonitor mon(mMonitor);
170  mPosition = aPosition;
171 
172  return NS_OK;
173 }
174 
175 NS_IMETHODIMP
176 sbBaseMediacorePlaybackControl::GetDuration(PRUint64 *aDuration)
177 {
178  TRACE(("%s[%p]", __FUNCTION__, this));
179  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
180  NS_ENSURE_ARG_POINTER(aDuration);
181 
182  nsAutoMonitor mon(mMonitor);
183  nsresult rv = OnGetDuration(aDuration);
184 
185  if(NS_FAILED(rv)) {
186  *aDuration = 0;
187  }
188 
189  return NS_OK;
190 }
191 
192 NS_IMETHODIMP
193 sbBaseMediacorePlaybackControl::GetIsPlayingAudio(PRBool *aIsPlayingAudio)
194 {
195  TRACE(("%s[%p]", __FUNCTION__, this));
196  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
197  NS_ENSURE_ARG_POINTER(aIsPlayingAudio);
198 
199  nsAutoMonitor mon(mMonitor);
200  nsresult rv = OnGetIsPlayingAudio(aIsPlayingAudio);
201 
202  if(NS_FAILED(rv)) {
203  *aIsPlayingAudio = PR_FALSE;
204  }
205 
206  return NS_OK;
207 }
208 
209 NS_IMETHODIMP
210 sbBaseMediacorePlaybackControl::GetIsPlayingVideo(PRBool *aIsPlayingVideo)
211 {
212  TRACE(("%s[%p]", __FUNCTION__, this));
213  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
214  NS_ENSURE_ARG_POINTER(aIsPlayingVideo);
215 
216  nsAutoMonitor mon(mMonitor);
217  nsresult rv = OnGetIsPlayingVideo(aIsPlayingVideo);
218 
219  if(NS_FAILED(rv)) {
220  *aIsPlayingVideo = PR_FALSE;
221  }
222 
223  return NS_OK;
224 }
225 
226 NS_IMETHODIMP
227 sbBaseMediacorePlaybackControl::Play()
228 {
229  TRACE(("%s[%p]", __FUNCTION__, this));
230  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
231 
232  nsresult rv;
234  NS_ENSURE_SUCCESS(rv, rv);
235 
236  nsAutoMonitor mon(mMonitor);
237  return OnPlay();
238 }
239 
240 NS_IMETHODIMP
241 sbBaseMediacorePlaybackControl::Pause()
242 {
243  TRACE(("%s[%p]", __FUNCTION__, this));
244  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
245 
246  nsresult rv;
248  NS_ENSURE_SUCCESS(rv, rv);
249 
250  nsAutoMonitor mon(mMonitor);
251  return OnPause();
252 }
253 
254 NS_IMETHODIMP
255 sbBaseMediacorePlaybackControl::Stop()
256 {
257  TRACE(("%s[%p]", __FUNCTION__, this));
258  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
259 
260  nsresult rv;
262  NS_ENSURE_SUCCESS(rv, rv);
263 
264  nsAutoMonitor mon(mMonitor);
265  return OnStop();
266 }
267 
268 /*virtual*/ nsresult
270 {
276  return NS_ERROR_NOT_IMPLEMENTED;
277 }
278 
279 /*virtual*/ nsresult
281 {
288  return NS_ERROR_NOT_IMPLEMENTED;
289 }
290 
291 /*virtual*/ nsresult
293 {
298  return NS_ERROR_NOT_IMPLEMENTED;
299 }
300 
301 /*virtual*/ nsresult
303 {
308  return NS_ERROR_NOT_IMPLEMENTED;
309 }
310 
311 /*virtual*/ nsresult
313 {
319  return NS_ERROR_NOT_IMPLEMENTED;
320 }
321 
322 /*virtual*/ nsresult
324 {
329  return NS_ERROR_NOT_IMPLEMENTED;
330 }
331 
332 /*virtual*/ nsresult
334 {
339  return NS_ERROR_NOT_IMPLEMENTED;
340 }
341 
342 /*virtual*/ nsresult
344 {
359  return NS_ERROR_NOT_IMPLEMENTED;
360 }
361 
362 /*virtual*/ nsresult
364 {
379  return NS_ERROR_NOT_IMPLEMENTED;
380 }
381 
382 /*virtual*/ nsresult
384 {
405  return NS_ERROR_NOT_IMPLEMENTED;
406 }
407 
408 /*virtual*/ nsresult
409 sbBaseMediacorePlaybackControl::OnSeek(PRUint64 aPosition, PRUint32 aFlags)
410 {
423  return OnSetPosition(aPosition);
424 }
425 
426 nsresult
428 {
429  TRACE(("%s[%p] (%08x)", __FUNCTION__, this, aType));
430 
431  nsresult rv;
432 
433  // it's not fatal if this fails
434  nsCOMPtr<sbIMediacore> core =
435  do_QueryInterface(NS_ISUPPORTS_CAST(sbIMediacorePlaybackControl*, this));
436 
437  nsCOMPtr<nsIWritablePropertyBag2> bag =
438  do_CreateInstance("@mozilla.org/hash-property-bag;1", &rv);
439  NS_ENSURE_SUCCESS(rv, rv);
440 
441  switch (aType) {
443  PRUint64 number;
444  rv = GetPosition(&number);
445  if (NS_SUCCEEDED(rv)) {
446  rv = bag->SetPropertyAsUint64(NS_LITERAL_STRING("position"), number);
447  NS_ENSURE_SUCCESS(rv, rv);
448  }
449  break;
452  {
453  PRUint64 number;
454  rv = GetPosition(&number);
455  if (NS_SUCCEEDED(rv)) {
456  rv = bag->SetPropertyAsUint64(NS_LITERAL_STRING("position"), number);
457  NS_ENSURE_SUCCESS(rv, rv);
458  }
459 
460  rv = GetDuration(&number);
461  if (NS_SUCCEEDED(rv)) {
462  rv = bag->SetPropertyAsUint64(NS_LITERAL_STRING("duration"), number);
463  NS_ENSURE_SUCCESS(rv, rv);
464  }
465 
466  rv = bag->SetPropertyAsInterface(NS_LITERAL_STRING("uri"), mUri);
467  NS_ENSURE_SUCCESS(rv, rv);
468  }
469  }
470 
471  nsCOMPtr<nsIVariant> data = do_QueryInterface(sbNewVariant(bag), &rv);
472  NS_ENSURE_SUCCESS(rv, rv);
473 
474  nsCOMPtr<sbIMediacoreEvent> event;
475  rv = sbMediacoreEvent::CreateEvent(aType, // type
476  nsnull, // error
477  data, // data
478  core, // origin
479  getter_AddRefs(event));
480  NS_ENSURE_SUCCESS(rv, rv);
481 
482  rv = DispatchEvent(event, PR_TRUE, nsnull);
483  /* ignore the result */
484 
485  return NS_OK;
486 }
virtual nsresult OnGetPosition(PRUint64 *aPosition)
return NS_OK
Songbird Mediacore Event Definition.
NS_IMPL_THREADSAFE_ISUPPORTS1(sbBaseMediacorePlaybackControl, sbIMediacorePlaybackControl) sbBaseMediacorePlaybackControl
virtual nsresult OnSetPosition(PRUint64 aPosition)
var event
const unsigned long STREAM_BEFORE_STOP
Stream is about to stop.
Songbird Variant Utility Definitions.
virtual nsresult OnSeek(PRUint64 aPosition, PRUint32 aFlags)
const unsigned long STREAM_BEFORE_START
Stream is about to start.
virtual nsresult OnGetDuration(PRUint64 *aPosition)
#define TRACE(args)
const unsigned long SEEKED
Playback has progressed in an unusual way.
static nsresult CreateEvent(PRUint32 aType, sbIMediacoreError *aError, nsIVariant *aData, sbIMediacore *aOrigin, sbIMediacoreEvent **retval)
const unsigned long STREAM_BEFORE_PAUSE
Stream is about to pause.
virtual nsresult OnGetIsPlayingAudio(PRBool *aIsPlayingAudio)
virtual nsresult OnGetIsPlayingVideo(PRBool *aIsPlayingVideo)
Songbird Base Mediacore Playback Control Definition.
observe data
Definition: FeedWriter.js:1329