82b1b554c9f5435fa71e143715810f8a2f11aeba
[platform/framework/web/webkit-efl.git] / Source / WebCore / platform / audio / gstreamer / tizen / AudioSessionManagerGStreamerTizen.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "AudioSessionManagerGStreamerTizen.h"
33 #include "GStreamerUtilities.h"
34 #include "GStreamerVersioning.h"
35
36 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
37
38 #include <unistd.h>
39 #include <mm_sound.h>
40 #if ENABLE(TIZEN_EXTENSIBLE_API)
41 #include "TizenExtensibleAPI.h"
42 #endif
43
44 namespace WebCore {
45
46 void AudioSessionManagerGStreamerTizen::setVolumeSessionToMediaType()
47 {
48     volume_type_t volumeType;
49 #if ENABLE(TIZEN_EXTENSIBLE_API)
50     if (TizenExtensibleAPI::extensibleAPI().mediaStreamRecord()) {
51         mm_sound_volume_get_current_playing_type(&volumeType);
52         if (volumeType != VOLUME_TYPE_MEDIA)
53             mm_sound_volume_primary_type_set(VOLUME_TYPE_MEDIA);
54     }
55 #endif
56 }
57
58 void AudioSessionManagerGStreamerTizen::clearVolumeSessionFromMediaType()
59 {
60 #if ENABLE(TIZEN_EXTENSIBLE_API)
61     if (TizenExtensibleAPI::extensibleAPI().mediaStreamRecord())
62         mm_sound_volume_primary_type_clear();
63 #endif
64 }
65 \r
66 AudioSessionManagerGStreamerTizen::AudioSessionManagerGStreamerTizen()
67     : m_eventType(ASM_EVENT_NONE)
68     , m_handle(-1)
69     , m_notifyCallback(0)
70     , m_processIdentifier(getpid())
71     , m_sessionType(MM_SESSION_TYPE_NUM)
72     , m_stateType(ASM_STATE_NONE)
73 {
74 }
75
76 AudioSessionManagerGStreamerTizen::~AudioSessionManagerGStreamerTizen()
77 {
78     unregisterAudioSessionManager();
79 }
80
81 bool AudioSessionManagerGStreamerTizen::registerAudioSessionManager(MMSessionType sessiontype, ASM_sound_cb_t notifyCallback, void* callbackData)
82 {
83     int error = _mm_session_util_read_type(m_processIdentifier, reinterpret_cast<int*>(&m_sessionType));
84     if (error) {
85         m_sessionType = sessiontype;
86         error = mm_session_init((int)m_sessionType);
87         if (error) {
88             LOG_MEDIA_MESSAGE("mm_session_init() failed.");
89             return false;
90         }
91     }
92
93     m_eventType = interpretSessionToEvent(m_sessionType);
94     m_notifyCallback = notifyCallback;
95
96     if (!ASM_register_sound(m_processIdentifier, &m_handle, m_eventType, m_stateType, m_notifyCallback, callbackData, ASM_RESOURCE_NONE, &error)) {
97         LOG_MEDIA_MESSAGE("register is failed. errcode = 0x%X", error);
98         return false;
99     }
100
101     return true;
102 }
103
104 bool AudioSessionManagerGStreamerTizen::unregisterAudioSessionManager()
105 {
106     int error = 0;
107     if (!ASM_unregister_sound(m_handle, m_eventType, &error)) {
108         LOG_MEDIA_MESSAGE("unregister() is failed 0x%X\n", error);
109         return false;
110     }
111
112     return true;
113 }
114
115 int AudioSessionManagerGStreamerTizen::getSoundState(ASM_sound_states_t* state)
116 {
117     int error = 0;
118     if (!ASM_get_sound_state( m_handle, m_eventType, state, &error))
119         LOG_MEDIA_MESSAGE("getSoundState state is failed 0x%X", error);
120
121     return error;
122 }
123
124 bool AudioSessionManagerGStreamerTizen::setSoundState(ASM_sound_states_t state)
125 {
126     int error = 0;
127     if (!ASM_set_sound_state(m_handle, m_eventType, state, ASM_RESOURCE_NONE, &error)) {
128         LOG_MEDIA_MESSAGE("setSoundState state = [%d] failed 0x%X", state, error);
129         return false;
130     }
131
132     return true;
133 }
134
135 ASM_sound_events_t AudioSessionManagerGStreamerTizen::interpretSessionToEvent(MMSessionType sessionType)
136 {
137     switch (sessionType) {
138     case MM_SESSION_TYPE_SHARE:
139         return ASM_EVENT_SHARE_MMPLAYER;
140     case MM_SESSION_TYPE_EXCLUSIVE:
141         return ASM_EVENT_EXCLUSIVE_MMPLAYER;
142     default:
143         return ASM_EVENT_NONE;
144     }
145 }
146 }
147 #endif