Merge "seek expection handling bug" into tizen_2.2
[platform/framework/native/media.git] / src / FMediaPlayer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 //
19 // @file                        FMediaPlayer.cpp
20 // @brief               This file contains the implementation of subsystem's Player.
21 //
22
23 #include <stdio.h>
24 #include <FMediaPlayer.h>
25 #include <FMediaMediaStreamInfo.h>
26 #include <FBaseSysLog.h>
27 #include "FMedia_PlayerEvent.h"
28 #include "FMedia_PlayerVideoEvent.h"
29 #include "FMedia_PlayerEventTypes.h"
30 #include "FMedia_PlayerVideoEventTypes.h"
31 #include "FMedia_PlayerImpl.h"
32
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace Media
36 {
37
38 Player::Player(void)
39         : __pPlayerImpl(null)
40 {
41 }
42
43 Player::~Player(void)
44 {
45         if (__pPlayerImpl)
46         {
47                 delete __pPlayerImpl;
48                 __pPlayerImpl = null;
49         }
50
51 }
52
53 result
54 Player::Construct(IPlayerEventListener& listener, const Tizen::Graphics::BufferInfo* pBufferInfo)
55 {
56         result r = E_SUCCESS;
57         ClearLastResult();
58
59
60         SysAssertf(__pPlayerImpl == null,
61                                                                                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
62
63         // Create PlayerImpl instance
64         _PlayerImpl* pPlayerImpl = new (std::nothrow) _PlayerImpl();
65         SysTryCatch(NID_MEDIA, pPlayerImpl, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
66         r = pPlayerImpl->Construct(listener,pBufferInfo);
67         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct pPlayerImple.", GetErrorMessage(r));
68
69         __pPlayerImpl = pPlayerImpl;
70
71         return r;
72
73 CATCH:
74         // Delete PlayerImpl instance
75         if (pPlayerImpl)
76         {
77                 delete pPlayerImpl;
78                 pPlayerImpl = null;
79         }
80
81         return r;
82
83 }
84
85 result
86 Player::Construct(IPlayerEventListener& listener,IPlayerVideoEventListener& videoListener)
87 {
88         result r = E_SUCCESS;
89
90         ClearLastResult();
91
92         SysAssertf(__pPlayerImpl == null,
93                                                                                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
94
95         // Create PlayerImpl instance
96         _PlayerImpl* pPlayerImpl = new (std::nothrow) _PlayerImpl();
97         SysTryCatch(NID_MEDIA, pPlayerImpl, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
98         r = pPlayerImpl->Construct(listener, videoListener, (void*) this);
99         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct pPlayerImpl.", GetErrorMessage(r));
100
101         __pPlayerImpl = pPlayerImpl;
102
103         return r;
104
105 CATCH:
106         // Delete PlayerImpl instance
107         if (pPlayerImpl)
108         {
109                 delete pPlayerImpl;
110                 pPlayerImpl = null;
111         }
112
113         return r;
114
115 }
116
117 result
118 Player::Construct(IPlayerEventListener& listener, Tizen::Graphics::Opengl::VideoTexture& videoTexture)
119 {
120         result r = E_SUCCESS;
121         ClearLastResult();
122         SysAssertf(__pPlayerImpl == null,
123                                                                                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
124         // Create PlayerImpl instance
125         _PlayerImpl* pPlayerImpl = new (std::nothrow) _PlayerImpl();
126         SysTryCatch(NID_MEDIA, pPlayerImpl, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
127         r = pPlayerImpl->Construct(listener, videoTexture);
128         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct pPlayerImpl.", GetErrorMessage(r));
129
130         __pPlayerImpl = pPlayerImpl;
131         return r;
132
133 CATCH:
134         // Delete PlayerImpl instance
135         if (pPlayerImpl)
136         {
137                 delete pPlayerImpl;
138                 pPlayerImpl = null;
139         }
140
141         return r;
142
143 }
144
145 result
146 Player::OpenFile(const Tizen::Base::String& mediaLocalPath, bool isAync)
147 {
148         result r = E_SUCCESS;
149
150         ClearLastResult();
151
152         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
153
154         r = __pPlayerImpl->OpenFile(mediaLocalPath, isAync);
155         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform OpenFile operation.", GetErrorMessage(r));
156
157         return r;
158 }
159
160 result
161 Player::OpenUrl(const Tizen::Base::Utility::Uri& mediaUri, bool isAync)
162 {
163         result r = E_SUCCESS;
164
165         ClearLastResult();
166
167         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
168
169         r = __pPlayerImpl->OpenUrl(mediaUri, isAync);
170         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform OpenUrl operation.", GetErrorMessage(r));
171
172         return r;
173 }
174
175 result
176 Player::OpenBuffer(const Tizen::Base::ByteBuffer& mediaBuffer, bool isAsync)
177 {
178         result r = E_SUCCESS;
179         ClearLastResult();
180
181         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
182
183         r = __pPlayerImpl->OpenBuffer(mediaBuffer, isAsync);
184         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform OpenBuffer operation.", GetErrorMessage(r));
185
186         return r;
187 }
188
189 result
190 Player::Close(void)
191 {
192         result r = E_SUCCESS;
193         ClearLastResult();
194
195         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
196
197         r = __pPlayerImpl->Close();
198         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform Close operation.", GetErrorMessage(r));
199         return r;
200 }
201
202 result
203 Player::Play(void)
204 {
205         result r = E_SUCCESS;
206         ClearLastResult();
207
208         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
209
210         r = __pPlayerImpl->Play();
211         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform Play operation.", GetErrorMessage(r));
212         return r;
213 }
214
215 result
216 Player::Stop(void)
217 {
218         result r = E_SUCCESS;
219         ClearLastResult();
220
221         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
222
223         r = __pPlayerImpl->Stop();
224         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform Stop operation.", GetErrorMessage(r));
225         return r;
226 }
227
228 result
229 Player::Pause(void)
230 {
231         result r = E_SUCCESS;
232         ClearLastResult();
233
234         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
235
236         r = __pPlayerImpl->Pause();
237         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform Pause operation.", GetErrorMessage(r));
238         return r;
239 }
240
241 PlayerState
242 Player::GetState(void) const
243 {
244         PlayerState state = PLAYER_STATE_INITIALIZED;
245         ClearLastResult();
246
247         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
248
249         state = __pPlayerImpl->GetState();
250         return state;
251 }
252
253 long
254 Player::GetPosition(void) const
255 {
256         long position = -1; //MEDIA_INVALID_VALUE;
257         ClearLastResult();
258
259         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
260
261         position = __pPlayerImpl->GetPosition();
262         return position;
263 }
264
265 result
266 Player::SeekTo(long msTime)
267 {
268         result r = E_SUCCESS;
269         ClearLastResult();
270
271         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
272         r = __pPlayerImpl->SeekTo(msTime);
273         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SeekTo operation.", GetErrorMessage(r));
274
275         return r;
276 }
277
278 long
279 Player::GetDuration(void) const
280 {
281         long duration = 0;
282         ClearLastResult();
283
284         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
285
286         duration = __pPlayerImpl->GetDuration();
287         return duration;
288 }
289
290 result
291 Player::SetVolume(int volume)
292 {
293         result r = E_SUCCESS;
294         ClearLastResult();
295
296         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
297
298         r = __pPlayerImpl->SetVolume(volume);
299         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SetVolume operation.", GetErrorMessage(r));
300         return r;
301 }
302
303 int
304 Player::GetVolume(void) const
305 {
306         int volume = -1; //MEDIA_INVALID_VALUE;
307         ClearLastResult();
308
309         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
310
311         volume = __pPlayerImpl->GetVolume();
312         return volume;
313 }
314
315 result
316 Player::SetMute(bool mute)
317 {
318         result r = E_SUCCESS;
319         ClearLastResult();
320
321         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
322
323         r = __pPlayerImpl->SetMute(mute);
324         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SetMute operation.", GetErrorMessage(r));
325
326         return r;
327 }
328
329 bool
330 Player::IsMuted(void) const
331 {
332         bool muted = false;
333
334         ClearLastResult();
335
336         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
337
338         muted = __pPlayerImpl->IsMute();
339         return muted;
340 }
341
342 result
343 Player::SetLooping(bool looping)
344 {
345         result r = E_SUCCESS;
346
347         ClearLastResult();
348
349         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
350
351         r = __pPlayerImpl->SetLooping(looping);
352         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SetLooping operation.", GetErrorMessage(r));
353
354         return r;
355 }
356
357 bool
358 Player::IsLooping(void) const
359 {
360         bool isLoop = false;
361
362         ClearLastResult();
363
364         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
365
366         isLoop = __pPlayerImpl->IsLooping();
367         return isLoop;
368 }
369
370 MediaStreamInfo*
371 Player::GetCurrentMediaStreamInfoN(void) const
372 {
373         MediaStreamInfo* pStreamInfo = null;
374
375         ClearLastResult();
376
377         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
378
379         pStreamInfo = __pPlayerImpl->GetCurrentMediaStreamInfoN();
380         return pStreamInfo;
381 }
382
383
384 result
385 Player::SetRenderingBuffer(const Tizen::Graphics::BufferInfo& bufferInfo)
386 {
387         result r = E_SUCCESS;
388
389         ClearLastResult();
390
391         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
392
393         // Set the canvas to subsystem's player
394         r = __pPlayerImpl->SetRenderingBuffer(bufferInfo);
395         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SetRenderingBuffer operation.", GetErrorMessage(r));
396
397         return r;
398 }
399
400 result
401 Player::CaptureVideo(void)
402 {
403         result r = E_SUCCESS;
404
405         ClearLastResult();
406
407         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
408
409         r = __pPlayerImpl->CaptureVideo();
410         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform CaptureVideo operation.", GetErrorMessage(r));
411
412         return r;
413 }
414
415 result
416 Player::SetAudioStreamType(AudioStreamType type)
417 {
418         result r = E_SUCCESS;
419
420         ClearLastResult();
421
422         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
423
424         r = __pPlayerImpl->SetAudioStreamType(type);
425         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform SetAudioStreamType operation.", GetErrorMessage(r));
426
427         return r;
428 }
429
430 result
431 Player::OpenUrlAsync(const Tizen::Base::String& url, const Tizen::Base::Collection::IMap* pHeader)
432 {
433         result r = E_SUCCESS;
434
435         ClearLastResult();
436
437         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
438         r = __pPlayerImpl->OpenUrlAsync(url, pHeader);
439         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform OpenUrlAsync operation.", GetErrorMessage(r));
440         return r;
441 }
442
443 result
444 Player::OpenUrlAsync(const Tizen::Base::String& url, const Tizen::Base::String& filePath, IPlayerProgressiveDownloadListener& listener, const Tizen::Base::Collection::IMap* pHeader)
445 {
446         result r = E_SUCCESS;
447
448         ClearLastResult();
449
450         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
451         r = __pPlayerImpl->OpenUrlAsync(this, url, filePath, listener, pHeader);
452         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform OpenUrlAsync operation.", GetErrorMessage(r));
453         return r;
454 }
455
456 void
457 Player::SetProgressiveDownloadIntervalByPercent(int percent)
458 {
459         ClearLastResult();
460
461         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
462         __pPlayerImpl->SetProgressiveDownloadIntervalByPercent(percent);
463 }
464
465 int
466 Player::GetHttpStreamingDownloadProgress(void) const
467 {
468         int curPosition = -1; //MEDIA_INVALID_VALUE;
469         ClearLastResult();
470
471         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
472
473         curPosition = __pPlayerImpl->GetHttpStreamingDownloadProgress();
474         return curPosition;
475 }
476
477 result
478 Player::SeekVideoKeyFrameCloseTo(long msTime)
479 {
480         result r = E_SUCCESS;
481
482         ClearLastResult();
483
484         SysAssertf(__pPlayerImpl !=  null, "Not yet constructed! Construct() should be called before use");
485
486         r = __pPlayerImpl->SeekTo(msTime, false);
487         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] SeekTo failed", GetErrorMessage(r));
488         return r;
489 }
490
491 }}  // Tizen::Media