From d87fb24e4a283117bd8ddc5a0368a310ee328dcd Mon Sep 17 00:00:00 2001 From: Seonae Kim Date: Thu, 4 Apr 2013 13:53:15 +0900 Subject: [PATCH] Attach callback to media element to calculate device rotation [Title] Attach callback to media element to calculate device rotation [Issue] N/A [Problem] There is no place to check the rotation for media. [Cause] N/A [Solution] Add rotation callback using sensor apis. Change-Id: Idbbc97477442cf13009f4c241d9e2ff904240f9b --- Source/WTF/wtf/Platform.h | 2 + Source/WebCore/html/HTMLMediaElement.cpp | 107 +++++++++++++++++++++++++++++++ Source/WebCore/html/HTMLMediaElement.h | 23 +++++++ 3 files changed, 132 insertions(+) diff --git a/Source/WTF/wtf/Platform.h b/Source/WTF/wtf/Platform.h index d7bf256..79ecee9 100755 --- a/Source/WTF/wtf/Platform.h +++ b/Source/WTF/wtf/Platform.h @@ -563,6 +563,8 @@ #define ENABLE_TIZEN_JPEG_IMAGE_SCALE_DECODING 1 /* Keunyong Lee(ky07.lee@samsung.com) : Scaled decoding Feature for Jpeg Image. Becuase this Feature replace Down Sampling, IMAGE_DECODER_DOWN_SAMPLING should be false when you want to use it. */ #define ENABLE_TIZEN_REMOVE_DRAWLINE_ANTIALIAS_NONE 1 /* Keunyong Lee(ky07.lee@samsung.com) : Dash/Dot Box border and Text UnderLine display Bug Fix*/ +#define ENABLE_TIZEN_DEVICE_ROTATION 1 /* Seonae Kim(sunaeluv.kim@samsung.com) : Manage sensor apis to check device's rotation */ + #if ENABLE(TIZEN_WEBKIT2) #define ENABLE_TIZEN_CONTEXT_MENU_WEBKIT_2 1 /* Gyuyoung Kim(gyuyoung.kim@samsung.com), Eunmi Lee(eunmi15.lee@samsung.com) : Support Context Menu for EFL WebKit2 */ diff --git a/Source/WebCore/html/HTMLMediaElement.cpp b/Source/WebCore/html/HTMLMediaElement.cpp index 6a3ace4..c2e2bed 100755 --- a/Source/WebCore/html/HTMLMediaElement.cpp +++ b/Source/WebCore/html/HTMLMediaElement.cpp @@ -119,6 +119,10 @@ #include "TizenExtensibleAPI.h" #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) +#include +#endif + using namespace std; namespace WebCore { @@ -156,6 +160,18 @@ static const char* boolString(bool val) static const char* mediaSourceURLProtocol = "x-media-source"; #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) +#define RADIAN_VALUE (57.2957) + +enum { + ROTATE_0, + ROTATE_90, + ROTATE_180, + ROTATE_270, + ROTATE_ERROR +}; +#endif + using namespace HTMLNames; using namespace std; @@ -273,6 +289,9 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document* docum #if ENABLE(TIZEN_GSTREAMER_VIDEO) , m_suspended(false) #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) + , m_rotation(ROTATE_0) +#endif { LOG(Media, "HTMLMediaElement::HTMLMediaElement"); document->registerForMediaVolumeCallbacks(this); @@ -290,6 +309,9 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document* docum setHasCustomCallbacks(); addElementToDocumentMap(this, document); +#if ENABLE(TIZEN_DEVICE_ROTATION) + registerRotationCallback(); +#endif } HTMLMediaElement::~HTMLMediaElement() @@ -313,7 +335,92 @@ HTMLMediaElement::~HTMLMediaElement() m_mediaController->removeMediaElement(this); removeElementFromDocumentMap(this, document()); +#if ENABLE(TIZEN_DEVICE_ROTATION) + unregisterRotationCallback(); +#endif +} + +#if ENABLE(TIZEN_DEVICE_ROTATION) +bool HTMLMediaElement::isHorizontal() +{ + return (m_rotation == ROTATE_90 || m_rotation == ROTATE_270) ? true : false; +} + +bool HTMLMediaElement::isVertical() +{ + return (m_rotation == ROTATE_0 || m_rotation == ROTATE_180) ? true : false; +} + +void HTMLMediaElement::registerRotationCallback() +{ + sensor_create(&m_handle); + sensor_accelerometer_set_cb(m_handle, 0, onRotationChanged, this); + sensor_start(m_handle, SENSOR_ACCELEROMETER); + + return; +} + +void HTMLMediaElement::unregisterRotationCallback() +{ + sensor_accelerometer_unset_cb(m_handle); + sensor_stop(m_handle, SENSOR_ACCELEROMETER); + sensor_destroy(m_handle); + + return; +} + +int HTMLMediaElement::calcRotation(float x, float y, float z) +{ + double atanV, normZ, rawZ; + int accTheta, accPitch; + int rotation; + + atanV = atan2(y, x); + accTheta = (int)(atanV * (RADIAN_VALUE) + 270) % 360; + rawZ = (double)(z / (0.004 * 9.81)); + + if (rawZ > 250) + normZ = 1.0; + else if (rawZ < -250) + normZ = -1.0; + else + normZ = ((double)rawZ) / 250; + + accPitch = (int)(acos(normZ) * (RADIAN_VALUE)); + + if ((accPitch > 35) && (accPitch < 145)) { + if ((accTheta >= 315 && accTheta <= 359) || (accTheta >= 0 && accTheta < 45)) + rotation = ROTATE_0; + else if (accTheta >= 45 && accTheta < 135) + rotation = ROTATE_90; + else if (accTheta >= 135 && accTheta < 225) + rotation = ROTATE_180; + else if (accTheta >= 225 && accTheta < 315) + rotation = ROTATE_270; + else + rotation = ROTATE_ERROR; + } else + rotation = ROTATE_ERROR; + + return rotation; +} + +void HTMLMediaElement::onRotationChanged(uint64_t timeStamp, sensor_data_accuracy_e accuracy, float x, float y, float z, void* userData) +{ + HTMLMediaElement* that = static_cast(userData); + + int autoRotation = 0; + vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &autoRotation); + if (!autoRotation) + return; + + int rotation = that->calcRotation(x, y, z); + if (rotation == ROTATE_ERROR || rotation == that->m_rotation) + return; + + that->m_rotation = rotation; } +#endif void HTMLMediaElement::didMoveToNewDocument(Document* oldDocument) { diff --git a/Source/WebCore/html/HTMLMediaElement.h b/Source/WebCore/html/HTMLMediaElement.h index 972d9ac..23940ea 100755 --- a/Source/WebCore/html/HTMLMediaElement.h +++ b/Source/WebCore/html/HTMLMediaElement.h @@ -45,6 +45,10 @@ #include "TextTrackCue.h" #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) +#include +#endif + namespace WebCore { #if ENABLE(WEB_AUDIO) @@ -337,6 +341,13 @@ public: bool suspended() { return m_suspended; } #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) + bool isHorizontal(); + bool isVertical(); + + int rotation() { return m_rotation; } +#endif + protected: HTMLMediaElement(const QualifiedName&, Document*, bool); virtual ~HTMLMediaElement(); @@ -573,6 +584,18 @@ private: bool shouldSuspendMedia(); #endif +#if ENABLE(TIZEN_DEVICE_ROTATION) + static void onRotationChanged(uint64_t timeStamp, sensor_data_accuracy_e accuracy, float x, float y, float z, void* userData); + + void registerRotationCallback(); + void unregisterRotationCallback(); + + int calcRotation(float x, float y, float z); + + sensor_h m_handle; + int m_rotation; +#endif + Timer m_loadTimer; Timer m_progressEventTimer; Timer m_playbackProgressTimer; -- 2.7.4