Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Poznamky
  2. --------
  3. Python knihovna http://www.johannesbader.ch/2014/06/track-your-heartrate-on-raspberry-pi-with-ant/
  4. ANT protokol http://www.thisisant.com/ a http://www.thisisant.com/developer/resources/downloads#software_tab
  5. PWM http://abyz.co.uk/rpi/pigpio/
  6. video http://w.xuv.be/projects/raspi_video_loop
  7. video/audio soft /opt/vc/src/hello_pi/hello_video/
  8. regulátor /home/pi/programy/kolo/ a tam kolo-test.py
  9. musí běžet pigpiod
  10. musí existovat FIFO /tmp/video_speed
  11. všechno se spouští automaticky přes /etc/rc.local
  12.  
  13. kolo-test.py
  14. ------------
  15.  
  16. import sys
  17. import time
  18. import pigpio
  19. import serial
  20.  
  21. from ant.core import driver
  22. from ant.core import event
  23. from ant.core.constants import *
  24. from ant.core.message import *
  25.  
  26. NETKEY = '\xB9\xA5\x21\xFB\xBD\x72\xC3\x45'
  27.  
  28. # Event callback
  29. class CallbackBlbaTrida(event.EventCallback):
  30.     def __init__(self):
  31.         self.speedFifo = open("/tmp/video_speed", "w", 0)
  32.         self.old_rev_count = 0
  33.         self.old_speed_event = 0
  34.         self.circle = 6
  35.         self.speed = 0.0
  36.         self.otherSpeed = 0.0
  37.         self.fan_min = 5.0
  38.         self.fan_max = 40.0
  39.         self.timeout = 0
  40.         self.timesleep = 0
  41.         self.nocount = 0
  42.         self.pwm = 0
  43.         self.pwm_min = 0.6
  44.     def updateSpeeds(self):
  45.         self.speedFifo.write(str(self.speed) + " " + str(self.otherSpeed) + "\n")
  46.     def process(self, msg):
  47.         if isinstance(msg, ChannelBroadcastDataMessage):
  48.             new_speed_event = 0
  49.             new_rev_count = 0
  50.             speed_event = ord(msg.payload[5]) + ord(msg.payload[6]) * 256
  51.             rev_count = ord(msg.payload[7]) + ord(msg.payload[8]) * 256
  52. #           print "Bike speed event: ", speed_event
  53. #           print "Rev count: ", rev_count
  54.             new_speed_event = speed_event - self.old_speed_event
  55.             if new_speed_event < 0:
  56.                 new_speed_event = 65536 + new_speed_event
  57.             new_rev_count = rev_count - self.old_rev_count
  58.             if new_rev_count < 0:
  59.                 new_rev_count = 65536 + new_rev_count
  60.             self.old_rev_count = rev_count
  61.             self.old_speed_event = speed_event
  62.             if new_speed_event > 0:
  63.                 self.nocount = 0
  64.                 self.speed = (self.circle * float(new_rev_count) * 1024.0) / float(new_speed_event)
  65.                 if self.speed < 1:
  66.                     self.speed = 0
  67.                 # send to FIFO
  68.                 self.updateSpeeds()
  69. #               print "Speed: ", self.speed, " pwm: ", self.pwm * 255.0
  70.             else:
  71.                 # check N/A
  72.                 self.nocount = self.nocount + 1
  73.                 if self.nocount == 4:
  74.                     self.nocount = self.nocount - 1 # N/A report po jednom
  75.                     self.timeout = 1
  76.                     self.speed = 0
  77.                     self.updateSpeeds()
  78. #               print "Speed: N/A"
  79.             self.timesleep = 8
  80.             # change PWM
  81.  
  82. # meh
  83. callbackCosi = CallbackBlbaTrida()
  84.  
  85. # serial
  86. serport = serial.Serial(port="/dev/ttyAMA0", baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
  87. serport.open()
  88. serport.isOpen()
  89. serport.write("0\n")
  90.  
  91. # GPIO PWM driver
  92. pi = pigpio.pi()
  93. pi.set_PWM_frequency(18, 100)
  94. pi.set_PWM_dutycycle(18, 0)
  95.  
  96. # Initialize driver
  97. stick = driver.USB1Driver("/dev/ttyUSB0")
  98. stick.open()
  99.  
  100. # Initialize event machine
  101. evm = event.EventMachine(stick)
  102. evm.registerCallback(callbackCosi)
  103. evm.start()
  104.  
  105. # Reset
  106. msg = SystemResetMessage()
  107. stick.write(msg.encode())
  108. time.sleep(1)
  109.  
  110. # Set network key
  111. msg = NetworkKeyMessage(key=NETKEY)
  112. stick.write(msg.encode())
  113. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  114.     sys.exit()
  115.  
  116. # Initialize it as a receiving channel using our network key
  117. msg = ChannelAssignMessage()
  118. stick.write(msg.encode())
  119. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  120.     sys.exit()
  121.  
  122. # Now set the channel id for pairing with an ANT+ bike cadence/speed sensor
  123. # device: 34946 nebo 56027
  124. msg = ChannelIDMessage(device_type=121, device_number=56027)
  125. stick.write(msg.encode())
  126. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  127.     sys.exit()
  128.  
  129. # Listen forever and ever (not really, but for a long time)
  130. msg = ChannelSearchTimeoutMessage(timeout=255)
  131. stick.write(msg.encode())
  132. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  133.     sys.exit()
  134.  
  135. # We want a ~4.05 Hz transmission period
  136. msg = ChannelPeriodMessage(period=8086)
  137. stick.write(msg.encode())
  138. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  139.     sys.exit()
  140.  
  141. # And ANT frequency 57, of course
  142. msg = ChannelFrequencyMessage(frequency=57)
  143. stick.write(msg.encode())
  144. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  145.     sys.exit()
  146.  
  147. # Time to go live
  148. msg = ChannelOpenMessage()
  149. stick.write(msg.encode())
  150. if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
  151.     sys.exit()
  152.  
  153. # print "Listening for ANT events ..."
  154. sText = ""
  155. while True:
  156.     try:
  157.         # read serial
  158.         while serport.inWaiting() > 0:
  159.             serchar = serport.read(1)
  160.             if serchar == '\n':
  161.                 if sText != "\n":
  162.                     callbackCosi.otherSpeed = float(sText)
  163.                     sText = ""
  164. #                   print "otherSpeed ", callbackCosi.otherSpeed
  165.             else:
  166.                 sText += serchar
  167.         # write serial
  168.         serport.write(str(callbackCosi.speed) + "\n")
  169.         # report timeout check
  170.         if callbackCosi.timesleep > 0:
  171.             callbackCosi.timesleep = callbackCosi.timesleep - 1
  172.         else:
  173.             callbackCosi.updateSpeeds()
  174.         # PWM timeout check
  175.         time.sleep(0.25)
  176.         if callbackCosi.timeout > 0:
  177.             callbackCosi.timeout = callbackCosi.timeout - 1
  178.             if callbackCosi.timeout == 0:
  179.                 callbackCosi.speed = 0
  180.         else:
  181.             callbackCosi.updateSpeeds();
  182.         callbackCosi.pwm = (((callbackCosi.speed + callbackCosi.otherSpeed) - callbackCosi.fan_min) / (callbackCosi.fan_max * 2)) + callbackCosi.pwm_min
  183.         if callbackCosi.pwm > 1.0:
  184.             callbackCosi.pwm = 1.0
  185.         if callbackCosi.pwm < callbackCosi.pwm_min:
  186.             callbackCosi.pwm = 0
  187.         pi.set_PWM_dutycycle(18, callbackCosi.pwm * 255.0)
  188.     except KeyboardInterrupt:
  189.         print "... END"
  190.         break
  191.     except ValueError:
  192.         callbackCosi.otherSpeed = 0
  193. #       print "unknown otherSpeed '", sText, "'"
  194.         sText = ""
  195.  
  196. # Shutdown
  197. msg = SystemResetMessage()
  198. stick.write(msg.encode())
  199. time.sleep(1)
  200.  
  201. evm.stop()
  202. stick.close()
  203.  
  204. pi.set_PWM_dutycycle(18, 0)
  205. pi.set_PWM_frequency(18, 0)
  206.  
  207. pi.stop()
  208.  
  209.  
  210. video.c
  211. -------
  212.  
  213. /*
  214. Copyright (c) 2012, Broadcom Europe Ltd
  215. All rights reserved.
  216.  
  217. Redistribution and use in source and binary forms, with or without
  218. modification, are permitted provided that the following conditions are met:
  219.      * Redistributions of source code must retain the above copyright
  220.         notice, this list of conditions and the following disclaimer.
  221.      * Redistributions in binary form must reproduce the above copyright
  222.         notice, this list of conditions and the following disclaimer in the
  223.         documentation and/or other materials provided with the distribution.
  224.      * Neither the name of the copyright holder nor the
  225.         names of its contributors may be used to endorse or promote products
  226.         derived from this software without specific prior written permission.
  227.  
  228. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  229. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  230. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  231. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  232. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  233. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  234. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  235. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  236. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  237. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  238. */
  239.  
  240. // Video deocode demo using OpenMAX IL though the ilcient helper library
  241.  
  242. #include <stdio.h>
  243. #include <stdlib.h>
  244. #include <string.h>
  245. #include <fcntl.h>
  246. #include <math.h>
  247. #include <pthread.h>
  248. #include <alsa/asoundlib.h>
  249.  
  250. #include "bcm_host.h"
  251. #include "ilclient.h"
  252.  
  253. #include "GLES/gl.h"
  254. #include "EGL/egl.h"
  255. #include "EGL/eglext.h"
  256.  
  257. #define FIFO_NAME "/tmp/video_speed"
  258.  
  259. #define VID_SPEED_MAX   22.0f
  260. #define AUD_SPEED_MAX   12.0f
  261. #define AUD_SPEED_MIN   2.0f
  262.  
  263. // audio
  264. static char *device = "default";
  265. short buf1[8*1024];
  266. short buf2[8*1024];
  267. short bufmix[8*1024];
  268. // vlákna
  269. pthread_t speed_thread;
  270. // aktuální rychlost
  271. float actualSpeed = 0;
  272. float otherSpeed = 0;
  273.  
  274. int speed_func(int pipe)
  275. {
  276.     // toto je další vlákno
  277.     // čte a "průměruje" rychlost kola
  278.     // ukládá do 'actualSpeed'
  279.     char temp;
  280.     int pos;
  281.     float readSpeed, readOther;
  282.     char text[64];
  283.     // čekat na změnu rychlosti
  284.     pos = 0;
  285.     while(1)
  286.     {
  287.         if(read(pipe, &temp, 1) == 1)
  288.         {
  289.             if(temp == '\n')
  290.             {
  291.                 text[pos] = 0;
  292.                 readSpeed = 0;
  293.                 sscanf(text, "%f %f", &readSpeed, &readOther);
  294.                 if(readSpeed < 0)
  295.                     break;
  296.                 actualSpeed = (readSpeed + actualSpeed) / 3.0f;
  297.                 otherSpeed = (readOther + otherSpeed) / 3.0f;
  298.                 if(actualSpeed < 1.0f)
  299.                     actualSpeed = 0;
  300.                 if(otherSpeed < 1.0f)
  301.                     otherSpeed = 0;
  302. //              printf("speed update %f -> %f; other %f\n", readSpeed, actualSpeed, otherSpeed);
  303.                 pos = 0;
  304.             } else {
  305.                 text[pos] = temp;
  306.                 if(pos < sizeof(text) - 1)
  307.                     pos++;
  308.             }
  309.         }
  310.     }
  311.     actualSpeed = -1;
  312.     return 0;
  313. }
  314.  
  315. int sound_func(int nuffin)
  316. {
  317.     // toto je další vlákno
  318.     // stará se o audio
  319.     // - hlasitost obou zvuků
  320.     int i;
  321.     int err;
  322.     snd_pcm_t *handle;
  323.     snd_pcm_sframes_t frames;
  324.     FILE *f[2];
  325.     short *bsrc1, *bsrc2, *btarg;
  326.     float volume[2] = {0.4f, 0.0f};
  327.     float volumeMod = 0.0f;
  328.     float mix;
  329.  
  330.     f[0] = fopen("zvuk1.raw", "rb");
  331.     if(!f[0])
  332.     {
  333.         printf("- failed to open 1st sound\n- audio disabled\n");
  334.         return 1;
  335.     }
  336.     f[1] = fopen("zvuk2.raw", "rb");
  337.     if(!f[1])
  338.     {
  339.         printf("- failed to open 2nd sound\n- audio disabled\n");
  340.         fclose(f[0]);
  341.         return 1;
  342.     }
  343.  
  344.     if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
  345.     {
  346.         printf("Playback open error: %s\n", snd_strerror(err));
  347.         return 1;
  348.     }
  349.     if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 250000)) < 0)
  350.     {
  351.         printf("Playback open error: %s\n", snd_strerror(err));
  352.         return 1;
  353.     }
  354.  
  355.     while(actualSpeed >= 0)
  356.     {
  357.         // úprava hlasitosti
  358. /*      if((actualSpeed + otherSpeed) > AUD_SPEED_MIN)
  359.             volumeMod = -0.00001f; // postupně zeslabit
  360.         else
  361.             volumeMod = 0.00001f; // postupně zesílit
  362. */
  363. /*      // druhý kanál - hlasitost dle rychlostí
  364.         volume[1] = ((actualSpeed + otherSpeed) - 2.0f) / ((AUD_SPEED_MAX - 2.0f) * 2.0f);
  365.         if(volume[1] < 0)
  366.             volume[1] = 0;
  367.         if(volume[1] > 1.0f)
  368.             volume[1] = 1.0f;*/
  369.         volume[0] = 10.0f - (actualSpeed + otherSpeed) * 0.55f;
  370.         if(volume[0] < 0)
  371.             volume[0] = 0;
  372.         if(volume[0] > 1)
  373.             volume[1] = 1;
  374.         // load
  375.         if(volume[0])
  376.         {
  377.             i = fread(buf1, 1, sizeof(buf1), f[0]);
  378.             if(i < sizeof(buf1))
  379.             {
  380.                 // konec souboru, načíst začátek
  381.                 fseek(f[0], 0, SEEK_SET);
  382.                 fread(buf1 + (i / sizeof(short)), 1, sizeof(buf1) - i, f[0]);
  383.             }
  384.         }
  385. /*      if(volume[1])
  386.         {
  387.             i = fread(buf2, 1, sizeof(buf2), f[1]);
  388.             if(i < sizeof(buf2))
  389.             {
  390.                 // konec souboru, načíst začátek
  391.                 fseek(f[1], 0, SEEK_SET);
  392.                 fread(buf2 + (i / sizeof(short)), 1, sizeof(buf2) - i, f[1]);
  393.             }
  394.         }*/
  395.         // MIX
  396.         bsrc1 = buf1;
  397.         bsrc2 = buf2;
  398.         btarg = bufmix;
  399.         for(i = 0; i < sizeof(bufmix)/sizeof(short); i++)
  400.         {
  401.             mix = (float)*bsrc1 * volume[0] + (float)*bsrc2 * volume[1];
  402.             if(mix < -32768.0f)
  403.                 mix = -32768.0f;
  404.             if(mix > 32767.0f)
  405.                 mix = 32767.0f;
  406.             *btarg = (short)mix;
  407.             volume[0] += volumeMod;
  408.             if(volumeMod > 0)
  409.             {
  410.                 if(volume[0] > 0.4f)
  411.                     volume[0] = 0.4f;
  412.             } else {
  413.                 if(volume[0] < 0.0f)
  414.                     volume[0] = 0.0f;
  415.             }
  416.             bsrc1++;
  417.             bsrc2++;
  418.             btarg++;
  419.         }
  420.         // play
  421.         frames = snd_pcm_writei(handle, bufmix, sizeof(bufmix)/sizeof(short));
  422.         if (frames < 0)
  423.             frames = snd_pcm_recover(handle, frames, 0);
  424.         if (frames < 0)
  425.         {
  426.             printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
  427.             break;
  428.         }
  429.     }
  430.  
  431.     fclose(f[0]);
  432.     fclose(f[1]);
  433.     return 0;
  434. }
  435.  
  436. static int video_decode_test(char *filename)
  437. {
  438.     OMX_VIDEO_PARAM_PORTFORMATTYPE format;
  439.     OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
  440.     COMPONENT_T *video_decode = NULL, *video_scheduler = NULL, *video_render = NULL, *clock = NULL;
  441.     COMPONENT_T *list[5];
  442.     TUNNEL_T tunnel[4];
  443.     ILCLIENT_T *client;
  444.     FILE *in;
  445.     int status = 0;
  446.     unsigned int data_len = 0;
  447.     OMX_TIME_CONFIG_SCALETYPE scaleType;
  448.     int speed_pipe;
  449.     float videoSpeed, oldSpeed;
  450.  
  451.     // vlákno rychlosti
  452.     speed_pipe = open(FIFO_NAME, O_RDONLY);
  453.     if(speed_pipe < 0)
  454.     {
  455.         printf("- failed to open named pipe %s\n", FIFO_NAME);
  456.         actualSpeed = VID_SPEED_MAX / 2.0f;
  457.     } else
  458.         pthread_create(&speed_thread, NULL, (void*)speed_func, (void*)speed_pipe);
  459.  
  460.     // vlákno
  461.     pthread_create(&speed_thread, NULL, (void*)sound_func, NULL);
  462.  
  463.     // video stuff
  464.     memset(&scaleType, 0, sizeof(scaleType));
  465.     scaleType.nSize = sizeof(scaleType);
  466.     scaleType.nVersion.nVersion = OMX_VERSION;
  467.  
  468.     memset(list, 0, sizeof(list));
  469.     memset(tunnel, 0, sizeof(tunnel));
  470.  
  471.     if((in = fopen(filename, "rb")) == NULL)
  472.         return -2;
  473.  
  474.     if((client = ilclient_init()) == NULL)
  475.     {
  476.         fclose(in);
  477.         return -3;
  478.     }
  479.  
  480.     if(OMX_Init() != OMX_ErrorNone)
  481.     {
  482.         ilclient_destroy(client);
  483.         fclose(in);
  484.         return -4;
  485.     }
  486.  
  487.     // create video_decode
  488.     if(ilclient_create_component(client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0)
  489.         status = -14;
  490.     list[0] = video_decode;
  491.  
  492.     // create video_render
  493.     if(status == 0 && ilclient_create_component(client, &video_render, "video_render", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  494.         status = -14;
  495.     list[1] = video_render;
  496.  
  497.     // create clock
  498.     if(status == 0 && ilclient_create_component(client, &clock, "clock", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  499.         status = -14;
  500.     list[2] = clock;
  501.  
  502.     memset(&cstate, 0, sizeof(cstate));
  503.     cstate.nSize = sizeof(cstate);
  504.     cstate.nVersion.nVersion = OMX_VERSION;
  505.     cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
  506.     cstate.nWaitMask = 1;
  507.     if(clock != NULL && OMX_SetParameter(ILC_GET_HANDLE(clock), OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
  508.         status = -13;
  509.  
  510.     // create video_scheduler
  511.     if(status == 0 && ilclient_create_component(client, &video_scheduler, "video_scheduler", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  512.         status = -14;
  513.     list[3] = video_scheduler;
  514.  
  515.     set_tunnel(tunnel, video_decode, 131, video_scheduler, 10);
  516.     set_tunnel(tunnel+1, video_scheduler, 11, video_render, 90);
  517.     set_tunnel(tunnel+2, clock, 80, video_scheduler, 12);
  518.  
  519.     // setup clock tunnel first
  520.     if(status == 0 && ilclient_setup_tunnel(tunnel+2, 0, 0) != 0)
  521.         status = -15;
  522.     else
  523.         ilclient_change_component_state(clock, OMX_StateExecuting);
  524.  
  525.     if(status == 0)
  526.         ilclient_change_component_state(video_decode, OMX_StateIdle);
  527.  
  528.     memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
  529.     format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
  530.     format.nVersion.nVersion = OMX_VERSION;
  531.     format.nPortIndex = 130;
  532.     format.eCompressionFormat = OMX_VIDEO_CodingAVC;
  533.  
  534.     if(status == 0 &&
  535.         OMX_SetParameter(ILC_GET_HANDLE(video_decode), OMX_IndexParamVideoPortFormat, &format) == OMX_ErrorNone &&
  536.         ilclient_enable_port_buffers(video_decode, 130, NULL, NULL, NULL) == 0)
  537.     {
  538.         OMX_BUFFERHEADERTYPE *buf;
  539.         int port_settings_changed = 0;
  540.         int first_packet = 1;
  541.  
  542.         ilclient_change_component_state(video_decode, OMX_StateExecuting);
  543.  
  544.         while((buf = ilclient_get_input_buffer(video_decode, 130, 1)) != NULL)
  545.         {
  546.             // feed data and wait until we get port settings changed
  547.             unsigned char *dest = buf->pBuffer;
  548.  
  549.             data_len += fread(dest, 1, buf->nAllocLen-data_len, in);
  550.  
  551.             if(port_settings_changed == 0 &&
  552.                 ((data_len > 0 && ilclient_remove_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1) == 0) ||
  553.                  (data_len == 0 && ilclient_wait_for_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1, ILCLIENT_EVENT_ERROR | ILCLIENT_PARAMETER_CHANGED, 10000) == 0)))
  554.             {
  555.                 port_settings_changed = 1;
  556.  
  557.                 if(ilclient_setup_tunnel(tunnel, 0, 0) != 0)
  558.                 {
  559.                     status = -7;
  560.                     break;
  561.                 }
  562.  
  563.                 ilclient_change_component_state(video_scheduler, OMX_StateExecuting);
  564.  
  565.                 // now setup tunnel to video_render
  566.                 if(ilclient_setup_tunnel(tunnel+1, 0, 1000) != 0)
  567.                 {
  568.                     status = -12;
  569.                     break;
  570.                 }
  571.  
  572.                 ilclient_change_component_state(video_render, OMX_StateExecuting);
  573.             }
  574.             if(!data_len)
  575.                 fseek(in, 0, SEEK_SET);
  576.  
  577.             // speed change
  578.             if(actualSpeed >= 0)
  579.             {
  580.                 videoSpeed =  actualSpeed / (VID_SPEED_MAX/2.0f);
  581.                 if(videoSpeed > 2.0f)
  582.                     videoSpeed = 2.0f;
  583.                 if(videoSpeed < 0.22f)
  584.                 {
  585. //                  printf("- paused\n");
  586.                     // PAUZA
  587.                     scaleType.xScale = 0;
  588.                     OMX_SetParameter(ILC_GET_HANDLE(clock), OMX_IndexConfigTimeScale, &scaleType);
  589.                     while(1)
  590.                     {
  591.                         usleep(100*1000);
  592.                         videoSpeed =  actualSpeed / (VID_SPEED_MAX/2.0f);
  593.                         if(videoSpeed > 0.25f)
  594.                             break;
  595.                     }
  596.                     oldSpeed = 0;
  597.                     if(videoSpeed > 2.0f)
  598.                         videoSpeed = 2.0f;
  599.                     // konec pauzy
  600. //                  printf("- unpaused\n");
  601.                 }
  602.                 videoSpeed = floor(videoSpeed * 100.0f) / 100.0f; // limit desetiných míst
  603.                 if(videoSpeed != oldSpeed)
  604.                 {
  605. //                  printf("- speed change to %f; video speed %f\n", actualSpeed, videoSpeed);
  606.                     scaleType.xScale = floor((videoSpeed * pow(2,16)));
  607.                     OMX_SetParameter(ILC_GET_HANDLE(clock), OMX_IndexConfigTimeScale, &scaleType);
  608.                     oldSpeed = videoSpeed;
  609.                 }
  610.             } else
  611.                 // konec přehrávání videa
  612.                 break;
  613.  
  614.             buf->nFilledLen = data_len;
  615.             data_len = 0;
  616.  
  617.             buf->nOffset = 0;
  618.             if(first_packet)
  619.             {
  620.                 buf->nFlags = OMX_BUFFERFLAG_STARTTIME;
  621.                 first_packet = 0;
  622.             }
  623.             else
  624.                 buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
  625.  
  626.             if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
  627.             {
  628.                 status = -6;
  629.                 break;
  630.             }
  631.         }
  632.  
  633.         buf->nFilledLen = 0;
  634.         buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN | OMX_BUFFERFLAG_EOS;
  635.  
  636.         if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
  637.             status = -20;
  638.  
  639.         // wait for EOS from render
  640.         ilclient_wait_for_event(video_render, OMX_EventBufferFlag, 90, 0, OMX_BUFFERFLAG_EOS, 0,
  641.                                         ILCLIENT_BUFFER_FLAG_EOS, 10000);
  642.  
  643.         // need to flush the renderer to allow video_decode to disable its input port
  644.         ilclient_flush_tunnels(tunnel, 0);
  645.  
  646.         ilclient_disable_port_buffers(video_decode, 130, NULL, NULL, NULL);
  647.     }
  648.  
  649.     fclose(in);
  650.  
  651.     ilclient_disable_tunnel(tunnel);
  652.     ilclient_disable_tunnel(tunnel+1);
  653.     ilclient_disable_tunnel(tunnel+2);
  654.     ilclient_teardown_tunnels(tunnel);
  655.  
  656.     ilclient_state_transition(list, OMX_StateIdle);
  657.     ilclient_state_transition(list, OMX_StateLoaded);
  658.  
  659.     ilclient_cleanup_components(list);
  660.  
  661.     OMX_Deinit();
  662.  
  663.     ilclient_destroy(client);
  664.     close(speed_pipe);
  665.     sleep(1);
  666.     return status;
  667. }
  668.  
  669. int main (int argc, char **argv)
  670. {
  671.     printf("Variable speed looping hello_video mod\n");
  672.     if (argc < 2) {
  673.         printf("Usage: %s <filename>\n", argv[0]);
  674.         exit(1);
  675.     }
  676.     bcm_host_init();
  677.     return video_decode_test(argv[1]);
  678. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement