<commit_before>/*
 * gst-droid
 *
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gstdroidcamsrcdev.h"
#include "gstdroidcamsrcdevmemory.h"
#include <stdlib.h>
#include "gstdroidcamsrc.h"

GST_DEBUG_CATEGORY_EXTERN (gst_droidcamsrc_debug);
#define GST_CAT_DEFAULT gst_droidcamsrc_debug

static camera_memory_t *
gst_droidcamsrc_dev_request_memory (int fd, size_t buf_size,
    unsigned int num_bufs, void *user)
{
  GST_DEBUG ("dev request memory fd=%d, buf_size=%d, num_bufs=%d", fd, buf_size,
      num_bufs);

  return gst_droidcamsrc_dev_memory_get (fd, buf_size, num_bufs);
}

static void
gst_droidcamsrc_dev_notify_callback (int32_t msg_type,
    int32_t ext1, int32_t ext2, void *user)
{
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev notify callback");

  // TODO: more messages

  switch (msg_type) {
    case CAMERA_MSG_SHUTTER:
      g_mutex_lock (&dev->lock);
      dev->dev->ops->disable_msg_type (dev->dev, CAMERA_MSG_SHUTTER);
      gst_droidcamsrc_post_message (src,
          gst_structure_new_empty (GST_DROIDCAMSRC_CAPTURE_START));
      g_mutex_unlock (&dev->lock);
      break;

    default:
      GST_WARNING ("unknown message type 0x%x", msg_type);
  }
}

static void
gst_droidcamsrc_dev_data_callback (int32_t msg_type,
    const camera_memory_t * data, unsigned int index,
    camera_frame_metadata_t * metadata, void *user)
{
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev data callback");

  switch (msg_type) {
    case CAMERA_MSG_RAW_IMAGE:
      // TODO:
      break;

    case CAMERA_MSG_COMPRESSED_IMAGE:
    {
      size_t size;
      void *addr = gst_droidcamsrc_dev_memory_get_data (data, index, &size);
      if (!addr) {
        GST_ERROR_OBJECT (src, "invalid memory from camera hal");
      } else {
        GstBuffer *buffer;
        void *d = g_malloc (size);
        memcpy (d, addr, size);
        buffer = gst_buffer_new_wrapped (d, size);
        if (!dev->image_preview_sent) {
          gst_droidcamsrc_post_message (src,
              gst_structure_new_empty (GST_DROIDCAMSRC_CAPTURE_END));
          // TODO: generate and send preview.
          dev->image_preview_sent = TRUE;
        }

        gst_droidcamsrc_timestamp (src, buffer);

        // TODO: extract and post exif
        g_mutex_lock (&dev->imgsrc->lock);
        g_queue_push_tail (dev->imgsrc->queue, buffer);
        g_cond_signal (&dev->imgsrc->cond);
        g_mutex_unlock (&dev->imgsrc->lock);
      }

      gst_droidcamsrc_dev_start (dev);

      g_mutex_lock (&src->capture_lock);
      --src->captures;
      g_mutex_unlock (&src->capture_lock);

      g_object_notify (G_OBJECT (src), "ready-for-capture");
    }
      break;

    default:
      GST_WARNING ("unknown message type 0x%x", msg_type);
  }

  // TODO:
}

static void
gst_droidcamsrc_dev_data_timestamp_callback (int64_t timestamp,
    int32_t msg_type, const camera_memory_t * data,
    unsigned int index, void *user)
{
  void *addr;
  size_t size;
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  //  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev data timestamp callback");

  /* unlikely but just in case */
  if (msg_type != CAMERA_MSG_VIDEO_FRAME) {
    GST_ERROR ("unknown message type 0x%x", msg_type);
  }

  addr = gst_droidcamsrc_dev_memory_get_data (data, index, &size);
  if (!addr) {
    GST_ERROR ("invalid memory from camera HAL");
  } else {
    g_mutex_lock (&dev->lock);
    dev->dev->ops->release_recording_frame (dev->dev, data);
    g_mutex_unlock (&dev->lock);
  }
  // TODO:
}

GstDroidCamSrcDev *
gst_droidcamsrc_dev_new (camera_module_t * hw, GstDroidCamSrcPad * vfsrc,
    GstDroidCamSrcPad * imgsrc, GstDroidCamSrcPad * vidsrc)
{
  GstDroidCamSrcDev *dev;

  GST_DEBUG ("dev new");

  dev = g_slice_new0 (GstDroidCamSrcDev);

  dev->hw = hw;
  dev->vfsrc = vfsrc;
  dev->imgsrc = imgsrc;
  dev->vidsrc = vidsrc;

  g_mutex_init (&dev->lock);<commit_msg>pass the data pointer not the memory pointer to release_recording_frame<commit_after>/*
 * gst-droid
 *
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gstdroidcamsrcdev.h"
#include "gstdroidcamsrcdevmemory.h"
#include <stdlib.h>
#include "gstdroidcamsrc.h"

GST_DEBUG_CATEGORY_EXTERN (gst_droidcamsrc_debug);
#define GST_CAT_DEFAULT gst_droidcamsrc_debug

static camera_memory_t *
gst_droidcamsrc_dev_request_memory (int fd, size_t buf_size,
    unsigned int num_bufs, void *user)
{
  GST_DEBUG ("dev request memory fd=%d, buf_size=%d, num_bufs=%d", fd, buf_size,
      num_bufs);

  return gst_droidcamsrc_dev_memory_get (fd, buf_size, num_bufs);
}

static void
gst_droidcamsrc_dev_notify_callback (int32_t msg_type,
    int32_t ext1, int32_t ext2, void *user)
{
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev notify callback");

  // TODO: more messages

  switch (msg_type) {
    case CAMERA_MSG_SHUTTER:
      g_mutex_lock (&dev->lock);
      dev->dev->ops->disable_msg_type (dev->dev, CAMERA_MSG_SHUTTER);
      gst_droidcamsrc_post_message (src,
          gst_structure_new_empty (GST_DROIDCAMSRC_CAPTURE_START));
      g_mutex_unlock (&dev->lock);
      break;

    default:
      GST_WARNING ("unknown message type 0x%x", msg_type);
  }
}

static void
gst_droidcamsrc_dev_data_callback (int32_t msg_type,
    const camera_memory_t * data, unsigned int index,
    camera_frame_metadata_t * metadata, void *user)
{
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev data callback");

  switch (msg_type) {
    case CAMERA_MSG_RAW_IMAGE:
      // TODO:
      break;

    case CAMERA_MSG_COMPRESSED_IMAGE:
    {
      size_t size;
      void *addr = gst_droidcamsrc_dev_memory_get_data (data, index, &size);
      if (!addr) {
        GST_ERROR_OBJECT (src, "invalid memory from camera hal");
      } else {
        GstBuffer *buffer;
        void *d = g_malloc (size);
        memcpy (d, addr, size);
        buffer = gst_buffer_new_wrapped (d, size);
        if (!dev->image_preview_sent) {
          gst_droidcamsrc_post_message (src,
              gst_structure_new_empty (GST_DROIDCAMSRC_CAPTURE_END));
          // TODO: generate and send preview.
          dev->image_preview_sent = TRUE;
        }

        gst_droidcamsrc_timestamp (src, buffer);

        // TODO: extract and post exif
        g_mutex_lock (&dev->imgsrc->lock);
        g_queue_push_tail (dev->imgsrc->queue, buffer);
        g_cond_signal (&dev->imgsrc->cond);
        g_mutex_unlock (&dev->imgsrc->lock);
      }

      gst_droidcamsrc_dev_start (dev);

      g_mutex_lock (&src->capture_lock);
      --src->captures;
      g_mutex_unlock (&src->capture_lock);

      g_object_notify (G_OBJECT (src), "ready-for-capture");
    }
      break;

    default:
      GST_WARNING ("unknown message type 0x%x", msg_type);
  }

  // TODO:
}

static void
gst_droidcamsrc_dev_data_timestamp_callback (int64_t timestamp,
    int32_t msg_type, const camera_memory_t * data,
    unsigned int index, void *user)
{
  void *addr;
  size_t size;
  GstDroidCamSrcDev *dev = (GstDroidCamSrcDev *) user;
  //  GstDroidCamSrc *src = GST_DROIDCAMSRC (GST_PAD_PARENT (dev->imgsrc->pad));

  GST_DEBUG ("dev data timestamp callback");

  /* unlikely but just in case */
  if (msg_type != CAMERA_MSG_VIDEO_FRAME) {
    GST_ERROR ("unknown message type 0x%x", msg_type);
  }

  addr = gst_droidcamsrc_dev_memory_get_data (data, index, &size);
  if (!addr) {
    GST_ERROR ("invalid memory from camera HAL");
  } else {
    g_mutex_lock (&dev->lock);
    dev->dev->ops->release_recording_frame (dev->dev, addr);
    g_mutex_unlock (&dev->lock);
  }
  // TODO:
}

GstDroidCamSrcDev *
gst_droidcamsrc_dev_new (camera_module_t * hw, GstDroidCamSrcPad * vfsrc,
    GstDroidCamSrcPad * imgsrc, GstDroidCamSrcPad * vidsrc)
{
  GstDroidCamSrcDev *dev;

  GST_DEBUG ("dev new");

  dev = g_slice_new0 (GstDroidCamSrcDev);

  dev->hw = hw;
  dev->vfsrc = vfsrc;
  dev->imgsrc = imgsrc;
  dev->vidsrc = vidsrc;

  g_mutex_init (&dev->lock);
{'fraction_non_alphanumeric': 0.10461600483773432, 'fraction_numerical': 0.006853456964321709, 'mean_word_length': 3.6196461824953445, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086274', 'n_tokens_mistral': 3627, 'n_tokens_neox': 3554, 'n_words': 874}